5 * Copyright (C) 2013 BWM CarIT GmbH. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "../src/connman.h"
30 static void test_iptables_basic0(void)
34 err = __connman_iptables_command("-t filter -A INPUT "
35 "-m mark --mark 1 -j LOG");
38 err = __connman_iptables_commit("filter");
41 err = __connman_iptables_command("-t filter -D INPUT "
42 "-m mark --mark 1 -j LOG");
45 err = __connman_iptables_commit("filter");
49 static void test_iptables_basic1(void)
53 /* Test if we can do NAT stuff */
55 err = __connman_iptables_command("-t nat -A POSTROUTING "
56 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
58 err = __connman_iptables_commit("nat");
61 err = __connman_iptables_command("-t nat -D POSTROUTING "
62 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
64 err = __connman_iptables_commit("nat");
68 static void test_iptables_basic2(void)
72 /* Test if the right rule is removed */
74 err = __connman_iptables_command("-t filter -A INPUT "
75 "-m mark --mark 1 -j LOG");
78 err = __connman_iptables_commit("filter");
81 err = __connman_iptables_command("-t filter -A INPUT "
82 "-m mark --mark 2 -j LOG");
85 err = __connman_iptables_commit("filter");
88 err = __connman_iptables_command("-t filter -D INPUT "
89 "-m mark --mark 2 -j LOG");
92 err = __connman_iptables_commit("filter");
95 err = __connman_iptables_command("-t filter -D INPUT "
96 "-m mark --mark 1 -j LOG");
99 err = __connman_iptables_commit("filter");
103 static void test_iptables_chain0(void)
107 err = __connman_iptables_new_chain("filter", "foo");
110 err = __connman_iptables_commit("filter");
113 err = __connman_iptables_delete_chain("filter", "foo");
116 err = __connman_iptables_commit("filter");
120 static void test_iptables_chain1(void)
124 err = __connman_iptables_new_chain("filter", "foo");
127 err = __connman_iptables_commit("filter");
130 err = __connman_iptables_flush_chain("filter", "foo");
133 err = __connman_iptables_commit("filter");
136 err = __connman_iptables_delete_chain("filter", "foo");
139 err = __connman_iptables_commit("filter");
143 static void test_iptables_chain2(void)
147 err = __connman_iptables_change_policy("filter", "INPUT", "DROP");
150 err = __connman_iptables_commit("filter");
153 err = __connman_iptables_change_policy("filter", "INPUT", "ACCEPT");
156 err = __connman_iptables_commit("filter");
160 static void test_iptables_rule0(void)
164 /* Test simple appending and removing a rule */
166 err = __connman_iptables_append("filter", "INPUT",
167 "-m mark --mark 1 -j LOG");
170 err = __connman_iptables_commit("filter");
173 err = __connman_iptables_delete("filter", "INPUT",
174 "-m mark --mark 1 -j LOG");
177 err = __connman_iptables_commit("filter");
182 static void test_iptables_rule1(void)
186 /* Test if we can do NAT stuff */
188 err = __connman_iptables_append("nat", "POSTROUTING",
189 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
191 err = __connman_iptables_commit("nat");
194 err = __connman_iptables_delete("nat", "POSTROUTING",
195 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
197 err = __connman_iptables_commit("nat");
201 static void test_iptables_rule2(void)
205 /* Test if the right rule is removed */
207 err = __connman_iptables_append("filter", "INPUT",
208 "-m mark --mark 1 -j LOG");
211 err = __connman_iptables_commit("filter");
214 err = __connman_iptables_append("filter", "INPUT",
215 "-m mark --mark 2 -j LOG");
218 err = __connman_iptables_commit("filter");
221 err = __connman_iptables_delete("filter", "INPUT",
222 "-m mark --mark 2 -j LOG");
225 err = __connman_iptables_commit("filter");
228 err = __connman_iptables_delete("filter", "INPUT",
229 "-m mark --mark 1 -j LOG");
232 err = __connman_iptables_commit("filter");
236 struct connman_notifier *nat_notifier;
238 struct connman_service {
242 char *connman_service_get_interface(struct connman_service *service)
247 int connman_notifier_register(struct connman_notifier *notifier)
249 nat_notifier = notifier;
254 void connman_notifier_unregister(struct connman_notifier *notifier)
259 static void test_nat_basic0(void)
263 err = __connman_nat_enable("bridge", "192.168.2.1", 24);
266 /* test that table is empty */
267 err = __connman_iptables_append("nat", "POSTROUTING",
268 "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
271 err = __connman_iptables_commit("nat");
274 __connman_nat_disable("bridge");
277 static void test_nat_basic1(void)
279 struct connman_service *service;
282 service = g_try_new0(struct connman_service, 1);
285 nat_notifier->default_changed(service);
287 err = __connman_nat_enable("bridge", "192.168.2.1", 24);
290 /* test that table is not empty */
291 err = __connman_iptables_append("nat", "POSTROUTING",
292 "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
295 err = __connman_iptables_commit("nat");
298 __connman_nat_disable("bridge");
300 /* test that table is empty again */
301 err = __connman_iptables_delete("nat", "POSTROUTING",
302 "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
305 err = __connman_iptables_commit("nat");
309 int main(int argc, char *argv[])
313 g_test_init(&argc, &argv, NULL);
315 __connman_log_init(argv[0], "*", FALSE, FALSE,
316 "Unit Tests Connection Manager", VERSION);
317 __connman_iptables_init();
318 __connman_nat_init();
320 g_test_add_func("/iptables/basic0", test_iptables_basic0);
321 g_test_add_func("/iptables/basic1", test_iptables_basic1);
322 g_test_add_func("/iptables/basic2", test_iptables_basic2);
323 g_test_add_func("/iptables/chain0", test_iptables_chain0);
324 g_test_add_func("/iptables/chain1", test_iptables_chain1);
325 g_test_add_func("/iptables/chain2", test_iptables_chain2);
326 g_test_add_func("/iptables/rule0", test_iptables_rule0);
327 g_test_add_func("/iptables/rule1", test_iptables_rule1);
328 g_test_add_func("/iptables/rule2", test_iptables_rule2);
329 g_test_add_func("/nat/basic0", test_nat_basic0);
330 g_test_add_func("/nat/basic1", test_nat_basic1);
334 __connman_nat_cleanup();
335 __connman_iptables_cleanup();