nat: Use new iptables API
[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_basic0(void)
31 {
32         int err;
33
34         err = __connman_iptables_command("-t filter -A INPUT "
35                                         "-m mark --mark 1 -j LOG");
36         g_assert(err == 0);
37
38         err = __connman_iptables_commit("filter");
39         g_assert(err == 0);
40
41         err = __connman_iptables_command("-t filter -D INPUT "
42                                         "-m mark --mark 1 -j LOG");
43         g_assert(err == 0);
44
45         err = __connman_iptables_commit("filter");
46         g_assert(err == 0);
47 }
48
49 static void test_iptables_basic1(void)
50 {
51         int err;
52
53         /* Test if we can do NAT stuff */
54
55         err = __connman_iptables_command("-t nat -A POSTROUTING "
56                                 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
57
58         err = __connman_iptables_commit("nat");
59         g_assert(err == 0);
60
61         err = __connman_iptables_command("-t nat -D POSTROUTING "
62                                 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
63
64         err = __connman_iptables_commit("nat");
65         g_assert(err == 0);
66 }
67
68 static void test_iptables_basic2(void)
69 {
70         int err;
71
72         /* Test if the right rule is removed */
73
74         err = __connman_iptables_command("-t filter -A INPUT "
75                                         "-m mark --mark 1 -j LOG");
76         g_assert(err == 0);
77
78         err = __connman_iptables_commit("filter");
79         g_assert(err == 0);
80
81         err = __connman_iptables_command("-t filter -A INPUT "
82                                         "-m mark --mark 2 -j LOG");
83         g_assert(err == 0);
84
85         err = __connman_iptables_commit("filter");
86         g_assert(err == 0);
87
88         err = __connman_iptables_command("-t filter -D INPUT "
89                                         "-m mark --mark 2 -j LOG");
90         g_assert(err == 0);
91
92         err = __connman_iptables_commit("filter");
93         g_assert(err == 0);
94
95         err = __connman_iptables_command("-t filter -D INPUT "
96                                         "-m mark --mark 1 -j LOG");
97         g_assert(err == 0);
98
99         err = __connman_iptables_commit("filter");
100         g_assert(err == 0);
101 }
102
103 static void test_iptables_chain0(void)
104 {
105         int err;
106
107         err = __connman_iptables_new_chain("filter", "foo");
108         g_assert(err == 0);
109
110         err = __connman_iptables_commit("filter");
111         g_assert(err == 0);
112
113         err = __connman_iptables_delete_chain("filter", "foo");
114         g_assert(err == 0);
115
116         err = __connman_iptables_commit("filter");
117         g_assert(err == 0);
118 }
119
120 static void test_iptables_chain1(void)
121 {
122         int err;
123
124         err = __connman_iptables_new_chain("filter", "foo");
125         g_assert(err == 0);
126
127         err = __connman_iptables_commit("filter");
128         g_assert(err == 0);
129
130         err = __connman_iptables_flush_chain("filter", "foo");
131         g_assert(err == 0);
132
133         err = __connman_iptables_commit("filter");
134         g_assert(err == 0);
135
136         err = __connman_iptables_delete_chain("filter", "foo");
137         g_assert(err == 0);
138
139         err = __connman_iptables_commit("filter");
140         g_assert(err == 0);
141 }
142
143 static void test_iptables_chain2(void)
144 {
145         int err;
146
147         err = __connman_iptables_change_policy("filter", "INPUT", "DROP");
148         g_assert(err == 0);
149
150         err = __connman_iptables_commit("filter");
151         g_assert(err == 0);
152
153         err = __connman_iptables_change_policy("filter", "INPUT", "ACCEPT");
154         g_assert(err == 0);
155
156         err = __connman_iptables_commit("filter");
157         g_assert(err == 0);
158 }
159
160 static void test_iptables_rule0(void)
161 {
162         int err;
163
164         /* Test simple appending and removing a rule */
165
166         err = __connman_iptables_append("filter", "INPUT",
167                                         "-m mark --mark 1 -j LOG");
168         g_assert(err == 0);
169
170         err = __connman_iptables_commit("filter");
171         g_assert(err == 0);
172
173         err = __connman_iptables_delete("filter", "INPUT",
174                                         "-m mark --mark 1 -j LOG");
175         g_assert(err == 0);
176
177         err = __connman_iptables_commit("filter");
178         g_assert(err == 0);
179 }
180
181
182 static void test_iptables_rule1(void)
183 {
184         int err;
185
186         /* Test if we can do NAT stuff */
187
188         err = __connman_iptables_append("nat", "POSTROUTING",
189                                 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
190
191         err = __connman_iptables_commit("nat");
192         g_assert(err == 0);
193
194         err = __connman_iptables_delete("nat", "POSTROUTING",
195                                 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
196
197         err = __connman_iptables_commit("nat");
198         g_assert(err == 0);
199 }
200
201 static void test_iptables_rule2(void)
202 {
203         int err;
204
205         /* Test if the right rule is removed */
206
207         err = __connman_iptables_append("filter", "INPUT",
208                                         "-m mark --mark 1 -j LOG");
209         g_assert(err == 0);
210
211         err = __connman_iptables_commit("filter");
212         g_assert(err == 0);
213
214         err = __connman_iptables_append("filter", "INPUT",
215                                         "-m mark --mark 2 -j LOG");
216         g_assert(err == 0);
217
218         err = __connman_iptables_commit("filter");
219         g_assert(err == 0);
220
221         err = __connman_iptables_delete("filter", "INPUT",
222                                         "-m mark --mark 2 -j LOG");
223         g_assert(err == 0);
224
225         err = __connman_iptables_commit("filter");
226         g_assert(err == 0);
227
228         err = __connman_iptables_delete("filter", "INPUT",
229                                         "-m mark --mark 1 -j LOG");
230         g_assert(err == 0);
231
232         err = __connman_iptables_commit("filter");
233         g_assert(err == 0);
234 }
235
236 struct connman_notifier *nat_notifier;
237
238 struct connman_service {
239         char *dummy;
240 };
241
242 char *connman_service_get_interface(struct connman_service *service)
243 {
244         return "eth0";
245 }
246
247 int connman_notifier_register(struct connman_notifier *notifier)
248 {
249         nat_notifier = notifier;
250
251         return 0;
252 }
253
254 void connman_notifier_unregister(struct connman_notifier *notifier)
255 {
256         nat_notifier = NULL;
257 }
258
259 static void test_nat_basic0(void)
260 {
261         int err;
262
263         err = __connman_nat_enable("bridge", "192.168.2.1", 24);
264         g_assert(err == 0);
265
266         /* test that table is empty */
267         err = __connman_iptables_append("nat", "POSTROUTING",
268                                         "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
269         g_assert(err == 0);
270
271         err = __connman_iptables_commit("nat");
272         g_assert(err == 0);
273
274         __connman_nat_disable("bridge");
275 }
276
277 static void test_nat_basic1(void)
278 {
279         struct connman_service *service;
280         int err;
281
282         service = g_try_new0(struct connman_service, 1);
283         g_assert(service);
284
285         nat_notifier->default_changed(service);
286
287         err = __connman_nat_enable("bridge", "192.168.2.1", 24);
288         g_assert(err == 0);
289
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");
293         g_assert(err == 0);
294
295         err = __connman_iptables_commit("nat");
296         g_assert(err == 0);
297
298         __connman_nat_disable("bridge");
299
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");
303         g_assert(err == 0);
304
305         err = __connman_iptables_commit("nat");
306         g_assert(err == 0);
307 }
308
309 int main(int argc, char *argv[])
310 {
311         int err;
312
313         g_test_init(&argc, &argv, NULL);
314
315         __connman_log_init(argv[0], "*", FALSE, FALSE,
316                         "Unit Tests Connection Manager", VERSION);
317         __connman_iptables_init();
318         __connman_nat_init();
319
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);
331
332         err = g_test_run();
333
334         __connman_nat_cleanup();
335         __connman_iptables_cleanup();
336
337         return err;
338 }