Modify init value for logging mode
[platform/core/connectivity/stc-iptables.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 "chain.h"
26 #include "rule.h"
27 #include "manager.h"
28
29 using ::testing::InitGoogleTest;
30 using ::testing::Test;
31 using ::testing::TestCase;
32
33 #define TEST_IN_CHAIN  "GTEST_IN"
34 #define TEST_OUT_CHAIN "GTEST_OUT"
35 #define TEST_IFNAME    "seth_w0"
36
37 #define TARGET_ACCEPT  "ACCEPT"
38 #define TARGET_DROP    "DROP"
39 #define TARGET_LOG     "LOG"
40
41 #define TEST_SRC_IP_1 "192.168.10.1"
42 #define TEST_SRC_IP_2 "192.168.10.9"
43 #define TEST_DST_IP_1 "192.168.10.10"
44 #define TEST_DST_IP_2 "192.168.10.15"
45
46 #define TEST_SRC_PORT_1 80
47 #define TEST_SRC_PORT_2 90
48 #define TEST_DST_PORT_1 80
49 #define TEST_DST_PORT_2 90
50
51 #define TEST_CLASSID 8
52 #define TEST_NFACCT_NAME_IN "c2_1_8_seth_w0"
53 #define TEST_NFACCT_NAME_OUT "c4_1_8_seth_w0"
54
55 typedef struct {
56         int classid;
57         const char *nfacct;
58 } cgroup_nfacct_s;
59
60 static cgroup_nfacct_s examples_in[] = {
61         {21, "c2_1_21_seth_w0"}, {22, "c2_1_22_seth_w0"},
62         {23, "c2_1_23_seth_w0"}, {24, "c2_1_24_seth_w0"},
63         {25, "c2_1_25_seth_w0"}, {26, "c2_1_26_seth_w0"},
64         {27, "c2_1_27_seth_w0"}, {28, "c2_1_28_seth_w0"},
65         {29, "c2_1_29_seth_w0"},
66 };
67
68 static cgroup_nfacct_s examples_out[] = {
69         {21, "c4_1_21_seth_w0"}, {22, "c4_1_22_seth_w0"},
70         {23, "c4_1_23_seth_w0"}, {24, "c4_1_24_seth_w0"},
71         {25, "c4_1_25_seth_w0"}, {26, "c4_1_26_seth_w0"},
72         {27, "c4_1_27_seth_w0"}, {28, "c4_1_28_seth_w0"},
73         {29, "c4_1_29_seth_w0"},
74 };
75
76 static int exams_in_cnt = sizeof(examples_in) / sizeof(examples_in[0]);
77 static int exams_out_cnt = sizeof(examples_out) / sizeof(examples_out[0]);
78
79 TEST(StcIptables_Chain_Set, Add_p)
80 {
81         error_e ret = ERROR_NONE;
82         Chain chain;
83
84         ret = chain.SetChainName(TEST_IN_CHAIN);
85         EXPECT_EQ(ERROR_NONE, ret);
86
87         ret = chain.AddChain();
88         EXPECT_EQ(ERROR_NONE, ret);
89
90         ret = chain.Add6Chain();
91         EXPECT_EQ(ERROR_NONE, ret);
92
93         ret = chain.SetChainName(TEST_OUT_CHAIN);
94         EXPECT_EQ(ERROR_NONE, ret);
95
96         ret = chain.AddChain();
97         EXPECT_EQ(ERROR_NONE, ret);
98
99         ret = chain.Add6Chain();
100         EXPECT_EQ(ERROR_NONE, ret);
101 }
102
103 TEST(StcIptables_Chain_Set, Flush_p)
104 {
105         error_e ret = ERROR_NONE;
106         Chain chain;
107
108         ret = chain.SetChainName(TEST_IN_CHAIN);
109         EXPECT_EQ(ERROR_NONE, ret);
110
111         ret = chain.FlushChain();
112         EXPECT_EQ(ERROR_NONE, ret);
113
114         ret = chain.Flush6Chain();
115         EXPECT_EQ(ERROR_NONE, ret);
116
117         ret = chain.SetChainName(TEST_OUT_CHAIN);
118         EXPECT_EQ(ERROR_NONE, ret);
119
120         ret = chain.FlushChain();
121         EXPECT_EQ(ERROR_NONE, ret);
122
123         ret = chain.Flush6Chain();
124         EXPECT_EQ(ERROR_NONE, ret);
125 }
126
127 TEST(StcIptables_Rule_Set, AddIn_p)
128 {
129         error_e ret = ERROR_NONE;
130         int index;
131         Rule rule;
132
133         for (index = 0; index < exams_in_cnt; ++index) {
134                 ret = rule.SetRule(TEST_IN_CHAIN,
135                         IPT_RULE_IN,
136                         TEST_IFNAME,
137                         examples_in[index].classid,
138                         examples_in[index].nfacct,
139                         (index % 2) ? TARGET_ACCEPT : TARGET_DROP);
140                 EXPECT_EQ(ERROR_NONE, ret);
141
142                 ret = rule.AddRule();
143                 EXPECT_EQ(ERROR_NONE, ret);
144
145                 ret = rule.Add6Rule();
146                 EXPECT_EQ(ERROR_NONE, ret);
147         }
148 }
149
150 TEST(StcIptables_Rule_Set, AddOut_p)
151 {
152         error_e ret = ERROR_NONE;
153         int index;
154         Rule rule;
155
156         for (index = 0; index < exams_out_cnt; ++index) {
157                 ret = rule.SetRule(TEST_OUT_CHAIN,
158                         IPT_RULE_OUT,
159                         TEST_IFNAME,
160                         examples_out[index].classid,
161                         examples_out[index].nfacct,
162                         (index % 2) ? TARGET_ACCEPT : TARGET_DROP);
163                 EXPECT_EQ(ERROR_NONE, ret);
164
165                 ret = rule.AddRule();
166                 EXPECT_EQ(ERROR_NONE, ret);
167
168                 ret = rule.Add6Rule();
169                 EXPECT_EQ(ERROR_NONE, ret);
170         }
171 }
172
173 TEST(StcIptables_Rule_Set, AddInIprange_p)
174 {
175         error_e ret = ERROR_NONE;
176         Rule rule;
177
178         ret = rule.SetRule_iprange(TEST_IN_CHAIN,
179                         IPT_RULE_IN,
180                         TEST_IFNAME,
181                         TEST_CLASSID,
182                         TEST_NFACCT_NAME_IN,
183                         TARGET_ACCEPT,
184                         IPT_ACTION_ACCEPT,
185                         IPT_PROTOCOL_ALL,
186                         IPT_IP_RANGE,
187                         IPT_IP_RANGE,
188                         TEST_SRC_IP_1,
189                         TEST_SRC_IP_2,
190                         TEST_DST_IP_1,
191                         TEST_DST_IP_2,
192                         IPT_PORT_RANGE,
193                         IPT_PORT_RANGE,
194                         TEST_SRC_PORT_1,
195                         TEST_SRC_PORT_2,
196                         TEST_DST_PORT_1,
197                         TEST_DST_PORT_2);
198         EXPECT_EQ(ERROR_NONE, ret);
199
200         ret = rule.AddRule();
201         EXPECT_EQ(ERROR_NONE, ret);
202 }
203
204 TEST(StcIptables_Rule_Set, AddOutIprange_p)
205 {
206         error_e ret = ERROR_NONE;
207         Rule rule;
208
209         ret = rule.SetRule_iprange(TEST_OUT_CHAIN,
210                         IPT_RULE_OUT,
211                         TEST_IFNAME,
212                         TEST_CLASSID,
213                         TEST_NFACCT_NAME_OUT,
214                         TARGET_ACCEPT,
215                         IPT_ACTION_ACCEPT,
216                         IPT_PROTOCOL_ALL,
217                         IPT_IP_RANGE,
218                         IPT_IP_RANGE,
219                         TEST_SRC_IP_1,
220                         TEST_SRC_IP_2,
221                         TEST_DST_IP_1,
222                         TEST_DST_IP_2,
223                         IPT_PORT_RANGE,
224                         IPT_PORT_RANGE,
225                         TEST_SRC_PORT_1,
226                         TEST_SRC_PORT_2,
227                         TEST_DST_PORT_1,
228                         TEST_DST_PORT_2);
229         EXPECT_EQ(ERROR_NONE, ret);
230
231         ret = rule.AddRule();
232         EXPECT_EQ(ERROR_NONE, ret);
233 }
234
235 TEST(StcIptables_Rule_Unset, RemoveIn_p)
236 {
237         error_e ret = ERROR_NONE;
238         int index;
239         Rule rule;
240
241         for (index = 0; index < exams_in_cnt; ++index) {
242                 ret = rule.SetRule(TEST_IN_CHAIN,
243                         IPT_RULE_IN,
244                         TEST_IFNAME,
245                         examples_in[index].classid,
246                         examples_in[index].nfacct,
247                         (index % 2) ? TARGET_ACCEPT : TARGET_DROP);
248                 EXPECT_EQ(ERROR_NONE, ret);
249
250                 ret = rule.RemoveRule();
251                 EXPECT_EQ(ERROR_NONE, ret);
252
253                 ret = rule.Remove6Rule();
254                 EXPECT_EQ(ERROR_NONE, ret);
255         }
256 }
257
258 TEST(StcIptables_Rule_Unset, RemoveOut_p)
259 {
260         error_e ret = ERROR_NONE;
261         int index;
262         Rule rule;
263
264         for (index = 0; index < exams_out_cnt; ++index) {
265                 ret = rule.SetRule(TEST_OUT_CHAIN,
266                         IPT_RULE_OUT,
267                         TEST_IFNAME,
268                         examples_out[index].classid,
269                         examples_out[index].nfacct,
270                         (index % 2) ? TARGET_ACCEPT : TARGET_DROP);
271                 EXPECT_EQ(ERROR_NONE, ret);
272
273                 ret = rule.RemoveRule();
274                 EXPECT_EQ(ERROR_NONE, ret);
275
276                 ret = rule.Remove6Rule();
277                 EXPECT_EQ(ERROR_NONE, ret);
278         }
279 }
280
281 TEST(StcIptables_Rule_Set, RemoveInIprange_p)
282 {
283         error_e ret = ERROR_NONE;
284         Rule rule;
285
286         ret = rule.SetRule_iprange(TEST_IN_CHAIN,
287                         IPT_RULE_IN,
288                         TEST_IFNAME,
289                         TEST_CLASSID,
290                         TEST_NFACCT_NAME_IN,
291                         TARGET_ACCEPT,
292                         IPT_ACTION_ACCEPT,
293                         IPT_PROTOCOL_ALL,
294                         IPT_IP_RANGE,
295                         IPT_IP_RANGE,
296                         TEST_SRC_IP_1,
297                         TEST_SRC_IP_2,
298                         TEST_DST_IP_1,
299                         TEST_DST_IP_2,
300                         IPT_PORT_RANGE,
301                         IPT_PORT_RANGE,
302                         TEST_SRC_PORT_1,
303                         TEST_SRC_PORT_2,
304                         TEST_DST_PORT_1,
305                         TEST_DST_PORT_2);
306         EXPECT_EQ(ERROR_NONE, ret);
307
308         ret = rule.RemoveRule();
309         EXPECT_EQ(ERROR_NONE, ret);
310 }
311
312 TEST(StcIptables_Rule_Set, RemoveOutIprange_p)
313 {
314         error_e ret = ERROR_NONE;
315         Rule rule;
316
317         ret = rule.SetRule_iprange(TEST_OUT_CHAIN,
318                         IPT_RULE_OUT,
319                         TEST_IFNAME,
320                         TEST_CLASSID,
321                         TEST_NFACCT_NAME_OUT,
322                         TARGET_ACCEPT,
323                         IPT_ACTION_ACCEPT,
324                         IPT_PROTOCOL_ALL,
325                         IPT_IP_RANGE,
326                         IPT_IP_RANGE,
327                         TEST_SRC_IP_1,
328                         TEST_SRC_IP_2,
329                         TEST_DST_IP_1,
330                         TEST_DST_IP_2,
331                         IPT_PORT_RANGE,
332                         IPT_PORT_RANGE,
333                         TEST_SRC_PORT_1,
334                         TEST_SRC_PORT_2,
335                         TEST_DST_PORT_1,
336                         TEST_DST_PORT_2);
337         EXPECT_EQ(ERROR_NONE, ret);
338
339         ret = rule.RemoveRule();
340         EXPECT_EQ(ERROR_NONE, ret);
341 }
342
343 TEST(StcIptables_Chain_Unset, Flush_p)
344 {
345         error_e ret = ERROR_NONE;
346         Chain chain;
347
348         ret = chain.SetChainName(TEST_IN_CHAIN);
349         EXPECT_EQ(ERROR_NONE, ret);
350
351         ret = chain.FlushChain();
352         EXPECT_EQ(ERROR_NONE, ret);
353
354         ret = chain.Flush6Chain();
355         EXPECT_EQ(ERROR_NONE, ret);
356
357         ret = chain.SetChainName(TEST_OUT_CHAIN);
358         EXPECT_EQ(ERROR_NONE, ret);
359
360         ret = chain.FlushChain();
361         EXPECT_EQ(ERROR_NONE, ret);
362
363         ret = chain.Flush6Chain();
364         EXPECT_EQ(ERROR_NONE, ret);
365 }
366
367 TEST(StcIptables_Chain_Unset, Remove_p)
368 {
369         error_e ret = ERROR_NONE;
370         Chain chain;
371
372         ret = chain.SetChainName(TEST_IN_CHAIN);
373         EXPECT_EQ(ERROR_NONE, ret);
374
375         ret = chain.RemoveChain();
376         EXPECT_EQ(ERROR_NONE, ret);
377
378         ret = chain.Remove6Chain();
379         EXPECT_EQ(ERROR_NONE, ret);
380
381         ret = chain.SetChainName(TEST_OUT_CHAIN);
382         EXPECT_EQ(ERROR_NONE, ret);
383
384         ret = chain.RemoveChain();
385         EXPECT_EQ(ERROR_NONE, ret);
386
387         ret = chain.Remove6Chain();
388         EXPECT_EQ(ERROR_NONE, ret);
389 }
390
391
392 TEST(StcIptables_Manager, Stop_p)
393 {
394         error_e ret = ERROR_NONE;
395         Manager mgr;
396
397         ret = mgr.StopManager();
398         EXPECT_EQ(ERROR_NONE, ret);
399 }
400
401 int main(int argc, char **argv)
402 {
403         InitGoogleTest(&argc, argv);
404         return RUN_ALL_TESTS();
405 }