52aa919317f153bdd21ae60894fff361c86540ae
[platform/upstream/connman.git] / unit / test-iptables.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2013  BWM CarIT GmbH. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <glib.h>
27
28 #include "../src/connman.h"
29
30 static void test_iptables_chain0(void)
31 {
32         int err;
33
34         err = __connman_iptables_new_chain("filter", "foo");
35         g_assert(err == 0);
36
37         err = __connman_iptables_commit("filter");
38         g_assert(err == 0);
39
40         err = __connman_iptables_delete_chain("filter", "foo");
41         g_assert(err == 0);
42
43         err = __connman_iptables_commit("filter");
44         g_assert(err == 0);
45 }
46
47 static void test_iptables_chain1(void)
48 {
49         int err;
50
51         err = __connman_iptables_new_chain("filter", "foo");
52         g_assert(err == 0);
53
54         err = __connman_iptables_commit("filter");
55         g_assert(err == 0);
56
57         err = __connman_iptables_flush_chain("filter", "foo");
58         g_assert(err == 0);
59
60         err = __connman_iptables_commit("filter");
61         g_assert(err == 0);
62
63         err = __connman_iptables_delete_chain("filter", "foo");
64         g_assert(err == 0);
65
66         err = __connman_iptables_commit("filter");
67         g_assert(err == 0);
68 }
69
70 static void test_iptables_chain2(void)
71 {
72         int err;
73
74         err = __connman_iptables_change_policy("filter", "INPUT", "DROP");
75         g_assert(err == 0);
76
77         err = __connman_iptables_commit("filter");
78         g_assert(err == 0);
79
80         err = __connman_iptables_change_policy("filter", "INPUT", "ACCEPT");
81         g_assert(err == 0);
82
83         err = __connman_iptables_commit("filter");
84         g_assert(err == 0);
85 }
86
87 static void test_iptables_chain3(void)
88 {
89         int err;
90
91         err = __connman_iptables_new_chain("filter", "user-chain-0");
92         g_assert(err == 0);
93
94         err = __connman_iptables_commit("filter");
95         g_assert(err == 0);
96
97         err = __connman_iptables_new_chain("filter", "user-chain-1");
98         g_assert(err == 0);
99
100         err = __connman_iptables_commit("filter");
101         g_assert(err == 0);
102
103         err = __connman_iptables_delete_chain("filter", "user-chain-1");
104         g_assert(err == 0);
105
106         err = __connman_iptables_commit("filter");
107         g_assert(err == 0);
108
109         err = __connman_iptables_delete_chain("filter", "user-chain-0");
110         g_assert(err == 0);
111
112         err = __connman_iptables_commit("filter");
113         g_assert(err == 0);
114 }
115
116 static void test_iptables_rule0(void)
117 {
118         int err;
119
120         /* Test simple appending and removing a rule */
121
122         err = __connman_iptables_append("filter", "INPUT",
123                                         "-m mark --mark 1 -j LOG");
124         g_assert(err == 0);
125
126         err = __connman_iptables_commit("filter");
127         g_assert(err == 0);
128
129         err = __connman_iptables_delete("filter", "INPUT",
130                                         "-m mark --mark 1 -j LOG");
131         g_assert(err == 0);
132
133         err = __connman_iptables_commit("filter");
134         g_assert(err == 0);
135 }
136
137
138 static void test_iptables_rule1(void)
139 {
140         int err;
141
142         /* Test if we can do NAT stuff */
143
144         err = __connman_iptables_append("nat", "POSTROUTING",
145                                 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
146
147         err = __connman_iptables_commit("nat");
148         g_assert(err == 0);
149
150         err = __connman_iptables_delete("nat", "POSTROUTING",
151                                 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
152
153         err = __connman_iptables_commit("nat");
154         g_assert(err == 0);
155 }
156
157 static void test_iptables_rule2(void)
158 {
159         int err;
160
161         /* Test if the right rule is removed */
162
163         err = __connman_iptables_append("filter", "INPUT",
164                                         "-m mark --mark 1 -j LOG");
165         g_assert(err == 0);
166
167         err = __connman_iptables_commit("filter");
168         g_assert(err == 0);
169
170         err = __connman_iptables_append("filter", "INPUT",
171                                         "-m mark --mark 2 -j LOG");
172         g_assert(err == 0);
173
174         err = __connman_iptables_commit("filter");
175         g_assert(err == 0);
176
177         err = __connman_iptables_delete("filter", "INPUT",
178                                         "-m mark --mark 2 -j LOG");
179         g_assert(err == 0);
180
181         err = __connman_iptables_commit("filter");
182         g_assert(err == 0);
183
184         err = __connman_iptables_delete("filter", "INPUT",
185                                         "-m mark --mark 1 -j LOG");
186         g_assert(err == 0);
187
188         err = __connman_iptables_commit("filter");
189         g_assert(err == 0);
190 }
191
192 static void test_iptables_target0(void)
193 {
194         int err;
195
196         /* Test if 'fallthrough' targets work */
197
198         err = __connman_iptables_append("filter", "INPUT",
199                                         "-m mark --mark 1");
200         g_assert(err == 0);
201
202         err = __connman_iptables_append("filter", "INPUT",
203                                         "-m mark --mark 2");
204         g_assert(err == 0);
205
206         err = __connman_iptables_commit("filter");
207         g_assert(err == 0);
208
209         err = __connman_iptables_delete("filter", "INPUT",
210                                         "-m mark --mark 1");
211         g_assert(err == 0);
212
213         err = __connman_iptables_commit("filter");
214         g_assert(err == 0);
215
216         err = __connman_iptables_delete("filter", "INPUT",
217                                         "-m mark --mark 2");
218         g_assert(err == 0);
219
220         err = __connman_iptables_commit("filter");
221         g_assert(err == 0);
222 }
223
224 struct connman_notifier *nat_notifier;
225
226 struct connman_service {
227         char *dummy;
228 };
229
230 char *connman_service_get_interface(struct connman_service *service)
231 {
232         return "eth0";
233 }
234
235 int connman_notifier_register(struct connman_notifier *notifier)
236 {
237         nat_notifier = notifier;
238
239         return 0;
240 }
241
242 void connman_notifier_unregister(struct connman_notifier *notifier)
243 {
244         nat_notifier = NULL;
245 }
246
247 static void test_nat_basic0(void)
248 {
249         int err;
250
251         err = __connman_nat_enable("bridge", "192.168.2.1", 24);
252         g_assert(err == 0);
253
254         /* test that table is empty */
255         err = __connman_iptables_append("nat", "POSTROUTING",
256                                         "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
257         g_assert(err == 0);
258
259         err = __connman_iptables_commit("nat");
260         g_assert(err == 0);
261
262         __connman_nat_disable("bridge");
263 }
264
265 static void test_nat_basic1(void)
266 {
267         struct connman_service *service;
268         int err;
269
270         service = g_try_new0(struct connman_service, 1);
271         g_assert(service);
272
273         nat_notifier->default_changed(service);
274
275         err = __connman_nat_enable("bridge", "192.168.2.1", 24);
276         g_assert(err == 0);
277
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");
281         g_assert(err == 0);
282
283         err = __connman_iptables_commit("nat");
284         g_assert(err == 0);
285
286         __connman_nat_disable("bridge");
287
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");
291         g_assert(err == 0);
292
293         err = __connman_iptables_commit("nat");
294         g_assert(err == 0);
295
296         g_free(service);
297 }
298
299 int main(int argc, char *argv[])
300 {
301         int err;
302
303         g_test_init(&argc, &argv, NULL);
304
305         __connman_log_init(argv[0], "*", FALSE, FALSE,
306                         "Unit Tests Connection Manager", VERSION);
307         __connman_iptables_init();
308         __connman_nat_init();
309
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);
320
321         err = g_test_run();
322
323         __connman_nat_cleanup();
324         __connman_iptables_cleanup();
325
326         return err;
327 }