Merge "Added additional test cases for stc-manager firewall dbus methods" into tizen
[platform/core/connectivity/stc-manager.git] / unittest / firewall.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <iostream>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "firewall.h"
24 #include "common.h"
25
26 static void AsyncReadyCallback(GObject *source_object,
27         GAsyncResult *res, gpointer user_data)
28 {
29         MainLoop *M = (MainLoop *)user_data;
30         GLOGD("Succeeded to response async callback");
31         M->quit();
32 }
33
34 FRule::FRule()
35 {
36         this->chain[0] = '\0';
37         this->direction = 0;
38         this->siptype = 0;
39         this->diptype = 0;
40         this->sporttype = 0;
41         this->dporttype = 0;
42         this->protocol = 0;
43         this->family = 0;
44         this->sip1[0] = '\0';
45         this->dip1[0] = '\0';
46         this->sip2[0] = '\0';
47         this->dip2[0] = '\0';
48         this->dport1 = 0;
49         this->dport2 = 0;
50         this->sport1 = 0;
51         this->sport2 = 0;
52         this->iface[0] = '\0';
53         this->target = 0;
54 }
55
56 FRule::~FRule()
57 {
58 }
59
60 Firewall::Firewall()
61 {
62         Create();
63 }
64
65 Firewall::~Firewall()
66 {
67         Destroy();
68 }
69
70 error_e Firewall::SetRule(const char *chain, guint16 direction,
71                 guint16 siptype, guint16 diptype, guint16 sporttype,
72                 guint16 dporttype, guint16 protocol, guint16 family,
73                 const char *sip1, const char *dip1, const char *sip2,
74                 const char *dip2, guint32 dport1, guint32 dport2,
75                 guint32 sport1, guint32 sport2, const char *iface,
76                 guint16 target)
77 {
78         if (chain == NULL || strlen(chain) == 0)
79                 this->m_Rule.chain[0] = '\0';
80         else
81                 g_strlcpy(this->m_Rule.chain, chain, CHAIN_LEN);
82
83         if (sip1 == NULL || strlen(sip1) == 0)
84                 this->m_Rule.sip1[0] = '\0';
85         else
86                 g_strlcpy(this->m_Rule.sip1, sip1, IP_LEN);
87
88         if (sip2 == NULL || strlen(sip2) == 0)
89                 this->m_Rule.sip2[0] = '\0';
90         else
91                 g_strlcpy(this->m_Rule.sip2, sip2, IP_LEN);
92
93         if (dip1 == NULL || strlen(dip1) == 0)
94                 this->m_Rule.dip1[0] = '\0';
95         else
96                 g_strlcpy(this->m_Rule.dip1, dip1, IP_LEN);
97
98         if (dip2 == NULL || strlen(dip2) == 0)
99                 this->m_Rule.dip2[0] = '\0';
100         else
101                 g_strlcpy(this->m_Rule.dip2, dip2, IP_LEN);
102
103         if (iface == NULL || strlen(iface) == 0)
104                 this->m_Rule.iface[0] = '\0';
105         else
106                 g_strlcpy(this->m_Rule.iface, iface, IFACE_LEN);
107
108
109         this->m_Rule.direction = direction;
110         this->m_Rule.siptype = siptype;
111         this->m_Rule.diptype = diptype;
112         this->m_Rule.sporttype = sporttype;
113         this->m_Rule.dporttype = dporttype;
114         this->m_Rule.protocol = protocol;
115         this->m_Rule.family = family;
116         this->m_Rule.dport1 = dport1;
117         this->m_Rule.dport2 = dport2;
118         this->m_Rule.sport1 = sport1;
119         this->m_Rule.sport2 = sport2;
120         this->m_Rule.target = target;
121
122         return ERROR_NONE;
123 }
124
125 void Firewall::MakeRuleParams(GVariant **params)
126 {
127         GVariantBuilder *builder;
128
129         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
130
131         g_variant_builder_add(builder, "{sv}",
132                         FIREWALL_RULE_CHAIN,
133                         g_variant_new_string(this->m_Rule.chain));
134
135         g_variant_builder_add(builder, "{sv}",
136                         FIREWALL_RULE_DIRECTION,
137                         g_variant_new_uint16(this->m_Rule.direction));
138
139         g_variant_builder_add(builder, "{sv}",
140                         FIREWALL_RULE_IFNAME,
141                         g_variant_new_string(this->m_Rule.iface));
142
143         g_variant_builder_add(builder, "{sv}",
144                         FIREWALL_RULE_PROTOCOL,
145                         g_variant_new_uint16(this->m_Rule.protocol));
146
147         g_variant_builder_add(builder, "{sv}",
148                         FIREWALL_RULE_TARGET,
149                         g_variant_new_uint16(this->m_Rule.target));
150
151         g_variant_builder_add(builder, "{sv}",
152                         FIREWALL_RULE_FAMILY,
153                         g_variant_new_uint16(this->m_Rule.family));
154
155         g_variant_builder_add(builder, "{sv}",
156                         FIREWALL_RULE_SIPTYPE,
157                         g_variant_new_uint16(this->m_Rule.siptype));
158
159         g_variant_builder_add(builder, "{sv}",
160                         FIREWALL_RULE_SIP1,
161                         g_variant_new_string(this->m_Rule.sip1));
162
163         g_variant_builder_add(builder, "{sv}",
164                         FIREWALL_RULE_SIP2,
165                         g_variant_new_string(this->m_Rule.sip2));
166
167         g_variant_builder_add(builder, "{sv}",
168                         FIREWALL_RULE_DIPTYPE,
169                         g_variant_new_uint16(this->m_Rule.diptype));
170
171         g_variant_builder_add(builder, "{sv}",
172                         FIREWALL_RULE_DIP1,
173                         g_variant_new_string(this->m_Rule.dip1));
174
175         g_variant_builder_add(builder, "{sv}",
176                         FIREWALL_RULE_DIP2,
177                         g_variant_new_string(this->m_Rule.dip2));
178
179         g_variant_builder_add(builder, "{sv}",
180                         FIREWALL_RULE_SPORTTYPE,
181                         g_variant_new_uint16(this->m_Rule.sporttype));
182
183         g_variant_builder_add(builder, "{sv}",
184                         FIREWALL_RULE_SPORT1,
185                         g_variant_new_uint32(this->m_Rule.sport1));
186
187         g_variant_builder_add(builder, "{sv}",
188                         FIREWALL_RULE_SPORT2,
189                         g_variant_new_uint32(this->m_Rule.sport2));
190
191         g_variant_builder_add(builder, "{sv}",
192                         FIREWALL_RULE_DPORTTYPE,
193                         g_variant_new_uint16(this->m_Rule.dporttype));
194
195         g_variant_builder_add(builder, "{sv}",
196                         FIREWALL_RULE_DPORT1,
197                         g_variant_new_uint32(this->m_Rule.dport1));
198
199         g_variant_builder_add(builder, "{sv}",
200                         FIREWALL_RULE_DPORT2,
201                         g_variant_new_uint32(this->m_Rule.dport2));
202
203         *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
204         g_variant_builder_unref(builder);
205 }
206
207 error_e Firewall::GetAllRule(void)
208 {
209         MainLoop M;
210         error_e error = ERROR_NONE;
211
212         error = InvokeMethodNonblock(STC_MGR_SERVICE,
213                 STC_MGR_FIREWALL_PATH,
214                 STC_MGR_FIREWALL_INTERFACE,
215                 STC_MGR_METHOD_FIREWALL_GET_ALL,
216                 NULL,
217                 DBUS_REPLY_TIMEOUT,
218                 AsyncReadyCallback,
219                 &M);
220
221         if (error != ERROR_NONE) {
222                 GLOGD("Failed to invoke dbus method nonblock");
223                 return error;
224         }
225
226         GLOGD("Succeeded to get all restriction");
227
228         M.run(GMAINTIMEOUT);
229         return ERROR_NONE;
230 }
231
232 error_e Firewall::UpdateRule(void)
233 {
234         GVariant *message = NULL;
235         GVariant *params = NULL;
236         error_e error = ERROR_NONE;
237         int result = 0;
238
239         MakeRuleParams(&params);
240
241         message = InvokeMethod(STC_MGR_SERVICE,
242                 STC_MGR_FIREWALL_PATH,
243                 STC_MGR_FIREWALL_INTERFACE,
244                 STC_MGR_METHOD_FIREWALL_UPDATE,
245                 params,
246                 &error);
247
248         if (message == NULL) {
249                 GLOGD("Failed to invoke dbus method");
250                 return error;
251         }
252
253         g_variant_get(message, "(i)", &result);
254         GLOGD("Succeeded to set restriction [%d]", result);
255         g_variant_unref(message);
256
257         return ERROR_NONE;
258 }
259
260 error_e Firewall::RemoveRule(void)
261 {
262         GVariant *message = NULL;
263         GVariant *params = NULL;
264         error_e error = ERROR_NONE;
265         int result = 0;
266
267         MakeRuleParams(&params);
268
269         message = InvokeMethod(STC_MGR_SERVICE,
270                 STC_MGR_FIREWALL_PATH,
271                 STC_MGR_FIREWALL_INTERFACE,
272                 STC_MGR_METHOD_FIREWALL_REMOVE,
273                 params,
274                 &error);
275
276         if (message == NULL) {
277                 GLOGD("Failed to invoke dbus method");
278                 return error;
279         }
280
281         g_variant_get(message, "(i)", &result);
282         GLOGD("Succeeded to set restriction [%d]", result);
283         g_variant_unref(message);
284
285         return ERROR_NONE;
286 }
287
288 error_e Firewall::AddRule(void)
289 {
290         GVariant *message = NULL;
291         GVariant *params = NULL;
292         error_e error = ERROR_NONE;
293         int result = 0;
294
295         MakeRuleParams(&params);
296
297         message = InvokeMethod(STC_MGR_SERVICE,
298                 STC_MGR_FIREWALL_PATH,
299                 STC_MGR_FIREWALL_INTERFACE,
300                 STC_MGR_METHOD_FIREWALL_ADD,
301                 params,
302                 &error);
303
304         if (message == NULL) {
305                 GLOGD("Failed to invoke dbus method");
306                 return error;
307         }
308
309         g_variant_get(message, "(i)", &result);
310         GLOGD("Succeeded to set restriction [%d]", result);
311         g_variant_unref(message);
312
313         return ERROR_NONE;
314 }
315
316 error_e Firewall::SetChain(const char *chain, unsigned int target)
317 {
318         GVariant *message = NULL;
319         error_e error = ERROR_NONE;
320         int result = 0;
321
322         message = InvokeMethod(STC_MGR_SERVICE,
323                 STC_MGR_FIREWALL_PATH,
324                 STC_MGR_FIREWALL_INTERFACE,
325                 STC_MGR_METHOD_FIREWALL_SET,
326                 g_variant_new("(su)", chain, target),
327                 &error);
328
329         if (message == NULL) {
330                 GLOGD("Failed to invoke dbus method");
331                 return error;
332         }
333
334         g_variant_get(message, "(i)", &result);
335         GLOGD("Succeeded to set restriction [%d]", result);
336         g_variant_unref(message);
337
338         return ERROR_NONE;
339 }
340
341 error_e Firewall::UnsetChain(const char *chain)
342 {
343         GVariant *message = NULL;
344         error_e error = ERROR_NONE;
345         int result = 0;
346
347         message = InvokeMethod(STC_MGR_SERVICE,
348                 STC_MGR_FIREWALL_PATH,
349                 STC_MGR_FIREWALL_INTERFACE,
350                 STC_MGR_METHOD_FIREWALL_UNSET,
351                 g_variant_new("(s)", chain),
352                 &error);
353
354         if (message == NULL) {
355                 GLOGD("Failed to invoke dbus method");
356                 return error;
357         }
358
359         g_variant_get(message, "(i)", &result);
360         GLOGD("Succeeded to set restriction [%d]", result);
361         g_variant_unref(message);
362
363         return ERROR_NONE;
364 }
365
366 error_e Firewall::FlushChain(const char *chain)
367 {
368         GVariant *message = NULL;
369         error_e error = ERROR_NONE;
370         int result = 0;
371
372         message = InvokeMethod(STC_MGR_SERVICE,
373                 STC_MGR_FIREWALL_PATH,
374                 STC_MGR_FIREWALL_INTERFACE,
375                 STC_MGR_METHOD_FIREWALL_FLUSH,
376                 g_variant_new("(s)", chain),
377                 &error);
378
379         if (message == NULL) {
380                 GLOGD("Failed to invoke dbus method");
381                 return error;
382         }
383
384         g_variant_get(message, "(i)", &result);
385         GLOGD("Succeeded to set restriction [%d]", result);
386         g_variant_unref(message);
387
388         return ERROR_NONE;
389 }
390
391 error_e Firewall::GetAllChain(void)
392 {
393         MainLoop M;
394         error_e error = ERROR_NONE;
395
396         error = InvokeMethodNonblock(STC_MGR_SERVICE,
397                 STC_MGR_FIREWALL_PATH,
398                 STC_MGR_FIREWALL_INTERFACE,
399                 STC_MGR_METHOD_FIREWALL_GET_CHAIN,
400                 NULL,
401                 DBUS_REPLY_TIMEOUT,
402                 AsyncReadyCallback,
403                 &M);
404
405         if (error != ERROR_NONE) {
406                 GLOGD("Failed to invoke dbus method nonblock");
407                 return error;
408         }
409
410         GLOGD("Succeeded to get all restriction");
411
412         M.run(GMAINTIMEOUT);
413         return ERROR_NONE;
414 }
415
416 error_e Firewall::RemoveChain(const char *chain)
417 {
418         GVariant *message = NULL;
419         error_e error = ERROR_NONE;
420         int result = 0;
421
422         message = InvokeMethod(STC_MGR_SERVICE,
423                 STC_MGR_FIREWALL_PATH,
424                 STC_MGR_FIREWALL_INTERFACE,
425                 STC_MGR_METHOD_FIREWALL_REMOVE_CHAIN,
426                 g_variant_new("(s)", chain),
427                 &error);
428
429         if (message == NULL) {
430                 GLOGD("Failed to invoke dbus method");
431                 return error;
432         }
433
434         g_variant_get(message, "(i)", &result);
435         GLOGD("Succeeded to set restriction [%d]", result);
436         g_variant_unref(message);
437
438         return ERROR_NONE;
439 }
440
441 error_e Firewall::AddChain(const char *chain)
442 {
443         GVariant *message = NULL;
444         error_e error = ERROR_NONE;
445         int result = 0;
446
447         message = InvokeMethod(STC_MGR_SERVICE,
448                 STC_MGR_FIREWALL_PATH,
449                 STC_MGR_FIREWALL_INTERFACE,
450                 STC_MGR_METHOD_FIREWALL_ADD_CHAIN,
451                 g_variant_new("(s)", chain),
452                 &error);
453
454         if (message == NULL) {
455                 GLOGD("Failed to invoke dbus method");
456                 return error;
457         }
458
459         g_variant_get(message, "(i)", &result);
460         GLOGD("Succeeded to set restriction [%d]", result);
461         g_variant_unref(message);
462
463         return ERROR_NONE;
464 }
465
466 error_e Firewall::GetLock(int *state)
467 {
468         GVariant *message = NULL;
469         error_e error = ERROR_NONE;
470
471         message = InvokeMethod(STC_MGR_SERVICE,
472                 STC_MGR_FIREWALL_PATH,
473                 STC_MGR_FIREWALL_INTERFACE,
474                 STC_MGR_METHOD_FIREWALL_GET_LOCK,
475                 NULL,
476                 &error);
477
478         if (message == NULL) {
479                 GLOGD("Failed to invoke dbus method");
480                 return error;
481         }
482
483         g_variant_get(message, "(i)", &state);
484         GLOGD("Succeeded to get lock state[%d]", state);
485         g_variant_unref(message);
486
487         return ERROR_NONE;
488 }
489
490 error_e Firewall::Unlock()
491 {
492         GVariant *message = NULL;
493         error_e error = ERROR_NONE;
494         int result = 0;
495
496         message = InvokeMethod(STC_MGR_SERVICE,
497                 STC_MGR_FIREWALL_PATH,
498                 STC_MGR_FIREWALL_INTERFACE,
499                 STC_MGR_METHOD_FIREWALL_UNLOCK,
500                 NULL,
501                 &error);
502
503         if (message == NULL) {
504                 GLOGD("Failed to invoke dbus method");
505                 return error;
506         }
507
508         g_variant_get(message, "(i)", &result);
509         GLOGD("Succeeded to unlock result[%d]", result);
510         g_variant_unref(message);
511
512         return ERROR_NONE;
513 }
514
515 error_e Firewall::Lock()
516 {
517         GVariant *message = NULL;
518         error_e error = ERROR_NONE;
519         int result = 0;
520
521         message = InvokeMethod(STC_MGR_SERVICE,
522                 STC_MGR_FIREWALL_PATH,
523                 STC_MGR_FIREWALL_INTERFACE,
524                 STC_MGR_METHOD_FIREWALL_LOCK,
525                 NULL,
526                 &error);
527
528         if (message == NULL) {
529                 GLOGD("Failed to invoke dbus method");
530                 return error;
531         }
532
533         g_variant_get(message, "(i)", &result);
534         GLOGD("Succeeded to unlock result[%d]", result);
535         g_variant_unref(message);
536
537         return ERROR_NONE;
538 }
539