2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
29 using ::testing::InitGoogleTest;
30 using ::testing::Test;
31 using ::testing::TestCase;
33 #define TEST_IN_CHAIN "GTEST_IN"
34 #define TEST_OUT_CHAIN "GTEST_OUT"
35 #define TEST_IFNAME "seth_w0"
37 #define TARGET_ACCEPT "ACCEPT"
38 #define TARGET_DROP "DROP"
39 #define TARGET_LOG "LOG"
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"
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
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"
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"},
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"},
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]);
79 TEST(StcIptables_Chain_Set, Add_p)
81 error_e ret = ERROR_NONE;
84 ret = chain.SetChainName(TEST_IN_CHAIN);
85 EXPECT_EQ(ERROR_NONE, ret);
87 ret = chain.AddChain();
88 EXPECT_EQ(ERROR_NONE, ret);
90 ret = chain.Add6Chain();
91 EXPECT_EQ(ERROR_NONE, ret);
93 ret = chain.SetChainName(TEST_OUT_CHAIN);
94 EXPECT_EQ(ERROR_NONE, ret);
96 ret = chain.AddChain();
97 EXPECT_EQ(ERROR_NONE, ret);
99 ret = chain.Add6Chain();
100 EXPECT_EQ(ERROR_NONE, ret);
103 TEST(StcIptables_Chain_Set, Flush_p)
105 error_e ret = ERROR_NONE;
108 ret = chain.SetChainName(TEST_IN_CHAIN);
109 EXPECT_EQ(ERROR_NONE, ret);
111 ret = chain.FlushChain();
112 EXPECT_EQ(ERROR_NONE, ret);
114 ret = chain.Flush6Chain();
115 EXPECT_EQ(ERROR_NONE, ret);
117 ret = chain.SetChainName(TEST_OUT_CHAIN);
118 EXPECT_EQ(ERROR_NONE, ret);
120 ret = chain.FlushChain();
121 EXPECT_EQ(ERROR_NONE, ret);
123 ret = chain.Flush6Chain();
124 EXPECT_EQ(ERROR_NONE, ret);
127 TEST(StcIptables_Rule_Set, AddIn_p)
129 error_e ret = ERROR_NONE;
133 for (index = 0; index < exams_in_cnt; ++index) {
134 ret = rule.SetRule(TEST_IN_CHAIN,
137 examples_in[index].classid,
138 examples_in[index].nfacct,
139 (index % 2) ? TARGET_ACCEPT : TARGET_DROP);
140 EXPECT_EQ(ERROR_NONE, ret);
142 ret = rule.AddRule();
143 EXPECT_EQ(ERROR_NONE, ret);
145 ret = rule.Add6Rule();
146 EXPECT_EQ(ERROR_NONE, ret);
150 TEST(StcIptables_Rule_Set, AddOut_p)
152 error_e ret = ERROR_NONE;
156 for (index = 0; index < exams_out_cnt; ++index) {
157 ret = rule.SetRule(TEST_OUT_CHAIN,
160 examples_out[index].classid,
161 examples_out[index].nfacct,
162 (index % 2) ? TARGET_ACCEPT : TARGET_DROP);
163 EXPECT_EQ(ERROR_NONE, ret);
165 ret = rule.AddRule();
166 EXPECT_EQ(ERROR_NONE, ret);
168 ret = rule.Add6Rule();
169 EXPECT_EQ(ERROR_NONE, ret);
173 TEST(StcIptables_Rule_Set, AddInIprange_p)
175 error_e ret = ERROR_NONE;
178 ret = rule.SetRule_iprange(TEST_IN_CHAIN,
198 EXPECT_EQ(ERROR_NONE, ret);
200 ret = rule.AddRule();
201 EXPECT_EQ(ERROR_NONE, ret);
204 TEST(StcIptables_Rule_Set, AddOutIprange_p)
206 error_e ret = ERROR_NONE;
209 ret = rule.SetRule_iprange(TEST_OUT_CHAIN,
213 TEST_NFACCT_NAME_OUT,
229 EXPECT_EQ(ERROR_NONE, ret);
231 ret = rule.AddRule();
232 EXPECT_EQ(ERROR_NONE, ret);
235 TEST(StcIptables_Rule_Unset, RemoveIn_p)
237 error_e ret = ERROR_NONE;
241 for (index = 0; index < exams_in_cnt; ++index) {
242 ret = rule.SetRule(TEST_IN_CHAIN,
245 examples_in[index].classid,
246 examples_in[index].nfacct,
247 (index % 2) ? TARGET_ACCEPT : TARGET_DROP);
248 EXPECT_EQ(ERROR_NONE, ret);
250 ret = rule.RemoveRule();
251 EXPECT_EQ(ERROR_NONE, ret);
253 ret = rule.Remove6Rule();
254 EXPECT_EQ(ERROR_NONE, ret);
258 TEST(StcIptables_Rule_Unset, RemoveOut_p)
260 error_e ret = ERROR_NONE;
264 for (index = 0; index < exams_out_cnt; ++index) {
265 ret = rule.SetRule(TEST_OUT_CHAIN,
268 examples_out[index].classid,
269 examples_out[index].nfacct,
270 (index % 2) ? TARGET_ACCEPT : TARGET_DROP);
271 EXPECT_EQ(ERROR_NONE, ret);
273 ret = rule.RemoveRule();
274 EXPECT_EQ(ERROR_NONE, ret);
276 ret = rule.Remove6Rule();
277 EXPECT_EQ(ERROR_NONE, ret);
281 TEST(StcIptables_Rule_Set, RemoveInIprange_p)
283 error_e ret = ERROR_NONE;
286 ret = rule.SetRule_iprange(TEST_IN_CHAIN,
306 EXPECT_EQ(ERROR_NONE, ret);
308 ret = rule.RemoveRule();
309 EXPECT_EQ(ERROR_NONE, ret);
312 TEST(StcIptables_Rule_Set, RemoveOutIprange_p)
314 error_e ret = ERROR_NONE;
317 ret = rule.SetRule_iprange(TEST_OUT_CHAIN,
321 TEST_NFACCT_NAME_OUT,
337 EXPECT_EQ(ERROR_NONE, ret);
339 ret = rule.RemoveRule();
340 EXPECT_EQ(ERROR_NONE, ret);
343 TEST(StcIptables_Chain_Unset, Flush_p)
345 error_e ret = ERROR_NONE;
348 ret = chain.SetChainName(TEST_IN_CHAIN);
349 EXPECT_EQ(ERROR_NONE, ret);
351 ret = chain.FlushChain();
352 EXPECT_EQ(ERROR_NONE, ret);
354 ret = chain.Flush6Chain();
355 EXPECT_EQ(ERROR_NONE, ret);
357 ret = chain.SetChainName(TEST_OUT_CHAIN);
358 EXPECT_EQ(ERROR_NONE, ret);
360 ret = chain.FlushChain();
361 EXPECT_EQ(ERROR_NONE, ret);
363 ret = chain.Flush6Chain();
364 EXPECT_EQ(ERROR_NONE, ret);
367 TEST(StcIptables_Chain_Unset, Remove_p)
369 error_e ret = ERROR_NONE;
372 ret = chain.SetChainName(TEST_IN_CHAIN);
373 EXPECT_EQ(ERROR_NONE, ret);
375 ret = chain.RemoveChain();
376 EXPECT_EQ(ERROR_NONE, ret);
378 ret = chain.Remove6Chain();
379 EXPECT_EQ(ERROR_NONE, ret);
381 ret = chain.SetChainName(TEST_OUT_CHAIN);
382 EXPECT_EQ(ERROR_NONE, ret);
384 ret = chain.RemoveChain();
385 EXPECT_EQ(ERROR_NONE, ret);
387 ret = chain.Remove6Chain();
388 EXPECT_EQ(ERROR_NONE, ret);
392 TEST(StcIptables_Manager, Stop_p)
394 error_e ret = ERROR_NONE;
397 ret = mgr.StopManager();
398 EXPECT_EQ(ERROR_NONE, ret);
401 int main(int argc, char **argv)
403 InitGoogleTest(&argc, argv);
404 return RUN_ALL_TESTS();