Added additional test cases for stc-manager firewall dbus methods
[platform/core/connectivity/stc-manager.git] / unittest / unittest.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 #include <unistd.h>
23
24 #include "unittest.h"
25 #include "restriction.h"
26 #include "statistics.h"
27 #include "manager.h"
28 #include "stc-manager.h"
29 #include "firewall.h"
30
31 #define GTEST_MAC "1:c:e:b:00:da"
32
33 using ::testing::InitGoogleTest;
34 using ::testing::Test;
35 using ::testing::TestCase;
36
37 typedef enum {
38         FIREWALL_UNKONWN,
39         FIREWALL_UNLOCKED,
40         FIREWALL_LOCKED
41 } firewall_lock_e;
42
43 TEST(StcManager_Statistics, Init_p)
44 {
45         error_e ret = ERROR_NONE;
46         Statistics stat;
47
48         ret = stat.InitStatistics();
49         EXPECT_EQ(ERROR_NONE, ret);
50 }
51
52 TEST(StcManager_Statistics, Get_p)
53 {
54         error_e ret = ERROR_NONE;
55         Statistics stat;
56         time_t from = 0;
57         time_t to = 0;
58
59         from = stat.MakeTime(2000, 1, 1, 1, 1);
60         time(&to);
61
62         ret = stat.SetRule("TOTAL_DATACALL",
63                 NULL,
64                 IFACE_DATACALL,
65                 from,
66                 to,
67                 TIME_PERIOD_DAY);
68         EXPECT_EQ(ERROR_NONE, ret);
69
70         ret = stat.GetStatistics();
71         EXPECT_EQ(ERROR_NONE, ret);
72
73         ret = stat.SetRule("TOTAL_WIFI",
74                 NULL,
75                 IFACE_WIFI,
76                 from,
77                 to,
78                 TIME_PERIOD_UNKNOWN);
79         EXPECT_EQ(ERROR_NONE, ret);
80
81         ret = stat.GetStatistics();
82         EXPECT_EQ(ERROR_NONE, ret);
83 }
84
85 TEST(StcManager_Statistics, GetAll_p)
86 {
87         error_e ret = ERROR_NONE;
88         Statistics stat;
89         time_t from = 0;
90         time_t to = 0;
91
92         from = stat.MakeTime(2000, 1, 1, 1, 1);
93         time(&to);
94
95         ret = stat.SetRule(NULL,
96                 NULL,
97                 IFACE_UNKNOWN,
98                 from,
99                 to,
100                 TIME_PERIOD_UNKNOWN);
101         EXPECT_EQ(ERROR_NONE, ret);
102
103         ret = stat.GetAllStatistics();
104         EXPECT_EQ(ERROR_NONE, ret);
105 }
106
107 TEST(StcManager_Statistics, GetTotal_p)
108 {
109         error_e ret = ERROR_NONE;
110         Statistics stat;
111         time_t from = 0;
112         time_t to = 0;
113
114         from = stat.MakeTime(2000, 1, 1, 1, 1);
115         time(&to);
116
117         ret = stat.SetRule(NULL,
118                 NULL,
119                 IFACE_DATACALL,
120                 from,
121                 to,
122                 TIME_PERIOD_UNKNOWN);
123         EXPECT_EQ(ERROR_NONE, ret);
124
125         ret = stat.GetTotalStatistics();
126         EXPECT_EQ(ERROR_NONE, ret);
127
128         ret = stat.SetRule(NULL,
129                 NULL,
130                 IFACE_WIFI,
131                 from,
132                 to,
133                 TIME_PERIOD_UNKNOWN);
134         EXPECT_EQ(ERROR_NONE, ret);
135
136         ret = stat.GetTotalStatistics();
137         EXPECT_EQ(ERROR_NONE, ret);
138 }
139
140 TEST(StcManager_Restriction, Set_p)
141 {
142         error_e ret = ERROR_NONE;
143         Restriction rest;
144
145         ret = rest.SetRule("Test_tel",
146                 "seth_w0",
147                 "1234567890abcdefg",
148                 IFACE_DATACALL,
149                 0,
150                 0,
151                 ROAMING_UNKNOWN,
152                 GTEST_MAC);
153         EXPECT_EQ(ERROR_NONE, ret);
154
155         ret = rest.SetRstriction();
156         EXPECT_EQ(ERROR_NONE, ret);
157
158         ret = rest.SetRule("TOTAL_WIFI",
159                 "wlan0",
160                 NULL,
161                 IFACE_WIFI,
162                 2,
163                 1,
164                 ROAMING_UNKNOWN,
165                 GTEST_MAC);
166         EXPECT_EQ(ERROR_NONE, ret);
167
168         ret = rest.SetRstriction();
169         EXPECT_EQ(ERROR_NONE, ret);
170
171         ret = rest.SetRule("Test_eth",
172                 "eth0",
173                 NULL,
174                 IFACE_WIRED,
175                 100000,
176                 80000,
177                 ROAMING_UNKNOWN,
178                 GTEST_MAC);
179         EXPECT_EQ(ERROR_NONE, ret);
180
181         ret = rest.SetRstriction();
182         EXPECT_EQ(ERROR_NONE, ret);
183 }
184
185 TEST(StcManager_Restriction, Get_p)
186 {
187         error_e ret = ERROR_NONE;
188         Restriction rest;
189
190         ret = rest.SetRule("Test_tel",
191                 "seth_w0",
192                 "1234567890abcdefg",
193                 IFACE_DATACALL,
194                 0,
195                 0,
196                 ROAMING_UNKNOWN,
197                 GTEST_MAC);
198         EXPECT_EQ(ERROR_NONE, ret);
199
200         ret = rest.GetRstriction();
201         EXPECT_EQ(ERROR_NONE, ret);
202
203         ret = rest.SetRule("TOTAL_WIFI",
204                 "wlan0",
205                 NULL,
206                 IFACE_WIFI,
207                 0,
208                 0,
209                 ROAMING_UNKNOWN,
210                 GTEST_MAC);
211         EXPECT_EQ(ERROR_NONE, ret);
212
213         ret = rest.GetRstriction();
214         EXPECT_EQ(ERROR_NONE, ret);
215
216         ret = rest.SetRule("TOTAL_IPV4",
217                 "seth_w0",
218                 NULL,
219                 IFACE_DATACALL,
220                 0,
221                 0,
222                 ROAMING_UNKNOWN,
223                 GTEST_MAC);
224         EXPECT_EQ(ERROR_NONE, ret);
225
226         ret = rest.GetRstriction();
227         EXPECT_EQ(ERROR_NONE, ret);
228 }
229
230 TEST(StcManager_Restriction, GetAll_p)
231 {
232         error_e ret = ERROR_NONE;
233         Restriction rest;
234
235         ret = rest.GetAllRstriction();
236         EXPECT_EQ(ERROR_NONE, ret);
237 }
238
239
240 TEST(StcManager_Restriction, Unset_p)
241 {
242         error_e ret = ERROR_NONE;
243         Restriction rest;
244
245         ret = rest.SetRule("Test_tel",
246                 "seth_w0",
247                 "1234567890abcdefg",
248                 IFACE_DATACALL,
249                 0,
250                 0,
251                 ROAMING_UNKNOWN,
252                 GTEST_MAC);
253         EXPECT_EQ(ERROR_NONE, ret);
254
255         ret = rest.UnsetRstriction();
256         EXPECT_EQ(ERROR_NONE, ret);
257
258         ret = rest.SetRule("TOTAL_WIFI",
259                 "wlan0",
260                 NULL,
261                 IFACE_WIFI,
262                 2,
263                 1,
264                 ROAMING_UNKNOWN,
265                 GTEST_MAC);
266         EXPECT_EQ(ERROR_NONE, ret);
267
268         ret = rest.UnsetRstriction();
269         EXPECT_EQ(ERROR_NONE, ret);
270
271         ret = rest.SetRule("Test_eth",
272                 "eth0",
273                 NULL,
274                 IFACE_WIRED,
275                 100000,
276                 80000,
277                 ROAMING_UNKNOWN,
278                 GTEST_MAC);
279         EXPECT_EQ(ERROR_NONE, ret);
280
281         ret = rest.UnsetRstriction();
282         EXPECT_EQ(ERROR_NONE, ret);
283
284         ret = rest.SetRule("TOTAL_DATACALL",
285                 "seth_w0",
286                 NULL,
287                 IFACE_DATACALL,
288                 0,
289                 0,
290                 ROAMING_UNKNOWN,
291                 GTEST_MAC);
292         EXPECT_EQ(ERROR_NONE, ret);
293
294         ret = rest.UnsetRstriction();
295         EXPECT_EQ(ERROR_NONE, ret);
296 }
297
298 TEST(StcManager_Statistics, Reset_p)
299 {
300         error_e ret = ERROR_NONE;
301         Statistics stat;
302         time_t from = 0;
303         time_t to = 0;
304
305         from = stat.MakeTime(2000, 1, 1, 1, 1);
306         time(&to);
307
308         ret = stat.SetRule("TOTAL_DATACALL",
309                 "1234567890abcdefg",
310                 IFACE_DATACALL,
311                 from,
312                 to,
313                 TIME_PERIOD_UNKNOWN);
314         EXPECT_EQ(ERROR_NONE, ret);
315
316         ret = stat.ResetStatistics();
317         EXPECT_EQ(ERROR_NONE, ret);
318
319         ret = stat.SetRule(NULL,
320                 NULL,
321                 IFACE_UNKNOWN,
322                 from,
323                 to,
324                 TIME_PERIOD_UNKNOWN);
325         EXPECT_EQ(ERROR_NONE, ret);
326
327         ret = stat.ResetStatistics();
328         EXPECT_EQ(ERROR_NONE, ret);
329 }
330
331 TEST(StcManager_Firewall, Lock_p)
332 {
333         error_e ret = ERROR_NONE;
334         Firewall f;
335
336         int state;
337         f.GetLock(&state);
338         if (state == FIREWALL_LOCKED)
339                 f.Unlock();
340
341         ret = f.Lock();
342         EXPECT_EQ(ERROR_NONE, ret);
343         f.Unlock();
344 }
345
346 TEST(StcManager_Firewall, Unlock_p)
347 {
348         error_e ret = ERROR_NONE;
349         Firewall f;
350
351         int state;
352         f.GetLock(&state);
353         if (state != FIREWALL_LOCKED)
354                 f.Lock();
355
356         ret = f.Unlock();
357         EXPECT_EQ(ERROR_NONE, ret);
358 }
359
360 TEST(StcManager_Firewall, GetLock_p)
361 {
362         error_e ret = ERROR_NONE;
363         Firewall f;
364
365         int state;
366         f.GetLock(&state);
367         EXPECT_EQ(ERROR_NONE, ret);
368 }
369
370 TEST(StcManager_Firewall, AddChain_p)
371 {
372         error_e ret = ERROR_NONE;
373         Firewall f;
374
375         ret = f.AddChain("gtest");
376         EXPECT_EQ(ERROR_NONE, ret);
377 }
378
379 TEST(StcManager_Firewall, AddRule_p)
380 {
381         error_e ret = ERROR_NONE;
382         Firewall f;
383
384         ret = f.SetRule("gtest",
385                         STC_FW_DIRECTION_IN,
386                         STC_FW_IP_SINGLE,
387                         STC_FW_IP_SINGLE,
388                         STC_FW_PORT_SINGLE,
389                         STC_FW_PORT_SINGLE,
390                         STC_FW_PROTOCOL_TCP,
391                         STC_FW_FAMILY_V4,
392                         "1.1.1.1",
393                         "1.1.1.1",
394                         "2.2.2.2",
395                         "2.2.2.2",
396                         9000,
397                         9000,
398                         9000,
399                         9000,
400                         "wlan0",
401                         STC_FW_RULE_TARGET_ACCEPT);
402
403         EXPECT_EQ(ERROR_NONE, ret);
404
405         ret = f.AddRule();
406         EXPECT_EQ(ERROR_NONE, ret);
407 }
408
409 TEST(StcManager_Firewall, AddRule_n)
410 {
411         error_e ret = ERROR_NONE;
412         Firewall f;
413
414         ret = f.SetRule("gtest",
415                         STC_FW_DIRECTION_IN,
416                         STC_FW_IP_SINGLE,
417                         STC_FW_IP_SINGLE,
418                         STC_FW_PORT_SINGLE,
419                         STC_FW_PORT_SINGLE,
420                         STC_FW_PROTOCOL_TCP,
421                         STC_FW_FAMILY_V4,
422                         "1.1.1.1",
423                         "1.1.1.1",
424                         "2.2.2.2",
425                         "2.2.2.2",
426                         9000,
427                         9000,
428                         9000,
429                         9000,
430                         "wlan0",
431                         STC_FW_RULE_TARGET_MAX);
432         EXPECT_EQ(ERROR_NONE, ret);
433
434         ret = f.AddRule();
435         EXPECT_NE(ERROR_NONE, ret);
436 }
437
438 TEST(StcManager_Firewall, UpdateRule_n)
439 {
440         error_e ret = ERROR_NONE;
441         Firewall f;
442
443         ret = f.SetRule("gtest",
444                         STC_FW_DIRECTION_IN,
445                         STC_FW_IP_SINGLE,
446                         STC_FW_IP_SINGLE,
447                         STC_FW_PORT_SINGLE,
448                         STC_FW_PORT_SINGLE,
449                         STC_FW_PROTOCOL_TCP,
450                         STC_FW_FAMILY_V4,
451                         "1.1.1.1",
452                         "1.1.1.1",
453                         "2.2.2.2",
454                         "2.2.2.2",
455                         9000,
456                         9000,
457                         9000,
458                         9000,
459                         "wlan0",
460                         STC_FW_RULE_TARGET_MAX);
461         EXPECT_EQ(ERROR_NONE, ret);
462
463         ret = f.UpdateRule();
464         EXPECT_NE(ERROR_NONE, ret);
465 }
466
467 TEST(StcManager_Firewall, RemoveRule_n)
468 {
469         error_e ret = ERROR_NONE;
470         Firewall f;
471
472         ret = f.SetRule("gtest",
473                         STC_FW_DIRECTION_IN,
474                         STC_FW_IP_SINGLE,
475                         STC_FW_IP_SINGLE,
476                         STC_FW_PORT_SINGLE,
477                         STC_FW_PORT_SINGLE,
478                         STC_FW_PROTOCOL_TCP,
479                         STC_FW_FAMILY_V4,
480                         "1.1.1.1",
481                         "1.1.1.1",
482                         "2.2.2.2",
483                         "2.2.2.2",
484                         9000,
485                         9000,
486                         9000,
487                         9000,
488                         "wlan0",
489                         STC_FW_RULE_TARGET_MAX);
490         EXPECT_EQ(ERROR_NONE, ret);
491
492         ret = f.RemoveRule();
493         EXPECT_NE(ERROR_NONE, ret);
494 }
495
496
497 TEST(StcManager_Firewall, SetChain_p)
498 {
499         error_e ret = ERROR_NONE;
500         Firewall f;
501
502         ret = f.SetChain("gtest", 1);
503         EXPECT_EQ(ERROR_NONE, ret);
504 }
505
506 TEST(StcManager_Firewall, SetChain_n)
507 {
508         error_e ret = ERROR_NONE;
509         Firewall f;
510
511         ret = f.SetChain(NULL, 0);
512         EXPECT_NE(ERROR_NONE, ret);
513 }
514
515 TEST(StcManager_Firewall, UnsetChain_p)
516 {
517         error_e ret = ERROR_NONE;
518         Firewall f;
519
520         ret = f.UnsetChain("gtest");
521         EXPECT_EQ(ERROR_NONE, ret);
522 }
523
524 TEST(StcManager_Firewall, UnsetChain_n)
525 {
526         error_e ret = ERROR_NONE;
527         Firewall f;
528
529         ret = f.UnsetChain(NULL);
530         EXPECT_NE(ERROR_NONE, ret);
531 }
532
533
534 TEST(StcManager_Firewall, RemoveChain_p)
535 {
536         error_e ret = ERROR_NONE;
537         Firewall f;
538
539         ret = f.RemoveChain("gtest");
540         EXPECT_EQ(ERROR_NONE, ret);
541 }
542
543 TEST(StcManager_Firewall, RemoveChain_n)
544 {
545         error_e ret = ERROR_NONE;
546         Firewall f;
547
548         ret = f.RemoveChain("not_a_chain");
549         EXPECT_NE(ERROR_NONE, ret);
550 }
551
552 TEST(StcManager_Firewall, FlushChain_p)
553 {
554         error_e ret = ERROR_NONE;
555         Firewall f;
556
557         ret = f.AddChain("gtest_chains");
558         EXPECT_EQ(ERROR_NONE, ret);
559
560         ret = f.FlushChain("gtest_chains");
561         EXPECT_EQ(ERROR_NONE, ret);
562
563         f.RemoveChain("gtest_chains");
564 }
565
566
567 TEST(StcManager_Firewall, FlushChain_n)
568 {
569         error_e ret = ERROR_NONE;
570         Firewall f;
571
572         ret = f.FlushChain(NULL);
573         EXPECT_NE(ERROR_NONE, ret);
574 }
575
576 TEST(StcManager_Firewall, GetAllChain_p)
577 {
578         error_e ret = ERROR_NONE;
579         Firewall f;
580
581         ret = f.GetAllChain();
582         EXPECT_EQ(ERROR_NONE, ret);
583 }
584
585 TEST(StcManager_Firewall, GetAllRule_p)
586 {
587         error_e ret = ERROR_NONE;
588         Firewall f;
589
590         ret = f.GetAllRule();
591         EXPECT_EQ(ERROR_NONE, ret);
592 }
593
594 TEST(StcManager_Manager, Stop_p)
595 {
596         error_e ret = ERROR_NONE;
597         Manager mgr;
598
599         ret = mgr.StopManager();
600         EXPECT_EQ(ERROR_NONE, ret);
601 }
602
603 int main(int argc, char **argv)
604 {
605         InitGoogleTest(&argc, argv);
606         return RUN_ALL_TESTS();
607 }