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_chain0(void)
34 err = __connman_iptables_new_chain("filter", "foo");
37 err = __connman_iptables_commit("filter");
40 err = __connman_iptables_delete_chain("filter", "foo");
43 err = __connman_iptables_commit("filter");
47 static void test_iptables_chain1(void)
51 err = __connman_iptables_new_chain("filter", "foo");
54 err = __connman_iptables_commit("filter");
57 err = __connman_iptables_flush_chain("filter", "foo");
60 err = __connman_iptables_commit("filter");
63 err = __connman_iptables_delete_chain("filter", "foo");
66 err = __connman_iptables_commit("filter");
70 static void test_iptables_chain2(void)
74 err = __connman_iptables_change_policy("filter", "INPUT", "DROP");
77 err = __connman_iptables_commit("filter");
80 err = __connman_iptables_change_policy("filter", "INPUT", "ACCEPT");
83 err = __connman_iptables_commit("filter");
87 static void test_iptables_chain3(void)
91 err = __connman_iptables_new_chain("filter", "user-chain-0");
94 err = __connman_iptables_commit("filter");
97 err = __connman_iptables_new_chain("filter", "user-chain-1");
100 err = __connman_iptables_commit("filter");
103 err = __connman_iptables_delete_chain("filter", "user-chain-1");
106 err = __connman_iptables_commit("filter");
109 err = __connman_iptables_delete_chain("filter", "user-chain-0");
112 err = __connman_iptables_commit("filter");
116 static void test_iptables_rule0(void)
120 /* Test simple appending and removing a rule */
122 err = __connman_iptables_append("filter", "INPUT",
123 "-m mark --mark 1 -j LOG");
126 err = __connman_iptables_commit("filter");
129 err = __connman_iptables_delete("filter", "INPUT",
130 "-m mark --mark 1 -j LOG");
133 err = __connman_iptables_commit("filter");
138 static void test_iptables_rule1(void)
142 /* Test if we can do NAT stuff */
144 err = __connman_iptables_append("nat", "POSTROUTING",
145 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
147 err = __connman_iptables_commit("nat");
150 err = __connman_iptables_delete("nat", "POSTROUTING",
151 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
153 err = __connman_iptables_commit("nat");
157 static void test_iptables_rule2(void)
161 /* Test if the right rule is removed */
163 err = __connman_iptables_append("filter", "INPUT",
164 "-m mark --mark 1 -j LOG");
167 err = __connman_iptables_commit("filter");
170 err = __connman_iptables_append("filter", "INPUT",
171 "-m mark --mark 2 -j LOG");
174 err = __connman_iptables_commit("filter");
177 err = __connman_iptables_delete("filter", "INPUT",
178 "-m mark --mark 2 -j LOG");
181 err = __connman_iptables_commit("filter");
184 err = __connman_iptables_delete("filter", "INPUT",
185 "-m mark --mark 1 -j LOG");
188 err = __connman_iptables_commit("filter");
192 static void test_iptables_target0(void)
196 /* Test if 'fallthrough' targets work */
198 err = __connman_iptables_append("filter", "INPUT",
202 err = __connman_iptables_append("filter", "INPUT",
206 err = __connman_iptables_commit("filter");
209 err = __connman_iptables_delete("filter", "INPUT",
213 err = __connman_iptables_commit("filter");
216 err = __connman_iptables_delete("filter", "INPUT",
220 err = __connman_iptables_commit("filter");
224 struct connman_notifier *nat_notifier;
226 struct connman_service {
230 char *connman_service_get_interface(struct connman_service *service)
235 int connman_notifier_register(struct connman_notifier *notifier)
237 nat_notifier = notifier;
242 void connman_notifier_unregister(struct connman_notifier *notifier)
247 static void test_nat_basic0(void)
251 err = __connman_nat_enable("bridge", "192.168.2.1", 24);
254 /* test that table is empty */
255 err = __connman_iptables_append("nat", "POSTROUTING",
256 "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
259 err = __connman_iptables_commit("nat");
262 __connman_nat_disable("bridge");
265 static void test_nat_basic1(void)
267 struct connman_service *service;
270 service = g_try_new0(struct connman_service, 1);
273 nat_notifier->default_changed(service);
275 err = __connman_nat_enable("bridge", "192.168.2.1", 24);
278 /* test that table is not empty */
279 err = __connman_iptables_append("nat", "POSTROUTING",
280 "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
283 err = __connman_iptables_commit("nat");
286 __connman_nat_disable("bridge");
288 /* test that table is empty again */
289 err = __connman_iptables_delete("nat", "POSTROUTING",
290 "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
293 err = __connman_iptables_commit("nat");
299 int main(int argc, char *argv[])
303 g_test_init(&argc, &argv, NULL);
305 __connman_log_init(argv[0], "*", FALSE, FALSE,
306 "Unit Tests Connection Manager", VERSION);
307 __connman_iptables_init();
308 __connman_nat_init();
310 g_test_add_func("/iptables/chain0", test_iptables_chain0);
311 g_test_add_func("/iptables/chain1", test_iptables_chain1);
312 g_test_add_func("/iptables/chain2", test_iptables_chain2);
313 g_test_add_func("/iptables/chain3", test_iptables_chain3);
314 g_test_add_func("/iptables/rule0", test_iptables_rule0);
315 g_test_add_func("/iptables/rule1", test_iptables_rule1);
316 g_test_add_func("/iptables/rule2", test_iptables_rule2);
317 g_test_add_func("/iptables/target0", test_iptables_target0);
318 g_test_add_func("/nat/basic0", test_nat_basic0);
319 g_test_add_func("/nat/basic1", test_nat_basic1);
323 __connman_nat_cleanup();
324 __connman_iptables_cleanup();