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_rule0(void)
91 /* Test simple appending and removing a rule */
93 err = __connman_iptables_append("filter", "INPUT",
94 "-m mark --mark 1 -j LOG");
97 err = __connman_iptables_commit("filter");
100 err = __connman_iptables_delete("filter", "INPUT",
101 "-m mark --mark 1 -j LOG");
104 err = __connman_iptables_commit("filter");
109 static void test_iptables_rule1(void)
113 /* Test if we can do NAT stuff */
115 err = __connman_iptables_append("nat", "POSTROUTING",
116 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
118 err = __connman_iptables_commit("nat");
121 err = __connman_iptables_delete("nat", "POSTROUTING",
122 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
124 err = __connman_iptables_commit("nat");
128 static void test_iptables_rule2(void)
132 /* Test if the right rule is removed */
134 err = __connman_iptables_append("filter", "INPUT",
135 "-m mark --mark 1 -j LOG");
138 err = __connman_iptables_commit("filter");
141 err = __connman_iptables_append("filter", "INPUT",
142 "-m mark --mark 2 -j LOG");
145 err = __connman_iptables_commit("filter");
148 err = __connman_iptables_delete("filter", "INPUT",
149 "-m mark --mark 2 -j LOG");
152 err = __connman_iptables_commit("filter");
155 err = __connman_iptables_delete("filter", "INPUT",
156 "-m mark --mark 1 -j LOG");
159 err = __connman_iptables_commit("filter");
163 struct connman_notifier *nat_notifier;
165 struct connman_service {
169 char *connman_service_get_interface(struct connman_service *service)
174 int connman_notifier_register(struct connman_notifier *notifier)
176 nat_notifier = notifier;
181 void connman_notifier_unregister(struct connman_notifier *notifier)
186 static void test_nat_basic0(void)
190 err = __connman_nat_enable("bridge", "192.168.2.1", 24);
193 /* test that table is empty */
194 err = __connman_iptables_append("nat", "POSTROUTING",
195 "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
198 err = __connman_iptables_commit("nat");
201 __connman_nat_disable("bridge");
204 static void test_nat_basic1(void)
206 struct connman_service *service;
209 service = g_try_new0(struct connman_service, 1);
212 nat_notifier->default_changed(service);
214 err = __connman_nat_enable("bridge", "192.168.2.1", 24);
217 /* test that table is not empty */
218 err = __connman_iptables_append("nat", "POSTROUTING",
219 "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
222 err = __connman_iptables_commit("nat");
225 __connman_nat_disable("bridge");
227 /* test that table is empty again */
228 err = __connman_iptables_delete("nat", "POSTROUTING",
229 "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
232 err = __connman_iptables_commit("nat");
236 int main(int argc, char *argv[])
240 g_test_init(&argc, &argv, NULL);
242 __connman_log_init(argv[0], "*", FALSE, FALSE,
243 "Unit Tests Connection Manager", VERSION);
244 __connman_iptables_init();
245 __connman_nat_init();
247 g_test_add_func("/iptables/chain0", test_iptables_chain0);
248 g_test_add_func("/iptables/chain1", test_iptables_chain1);
249 g_test_add_func("/iptables/chain2", test_iptables_chain2);
250 g_test_add_func("/iptables/rule0", test_iptables_rule0);
251 g_test_add_func("/iptables/rule1", test_iptables_rule1);
252 g_test_add_func("/iptables/rule2", test_iptables_rule2);
253 g_test_add_func("/nat/basic0", test_nat_basic0);
254 g_test_add_func("/nat/basic1", test_nat_basic1);
258 __connman_nat_cleanup();
259 __connman_iptables_cleanup();