e378a90b9dba2aa312e157cd8300a22f00572050
[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_rule0(void)
88 {
89         int err;
90
91         /* Test simple appending and removing a rule */
92
93         err = __connman_iptables_append("filter", "INPUT",
94                                         "-m mark --mark 1 -j LOG");
95         g_assert(err == 0);
96
97         err = __connman_iptables_commit("filter");
98         g_assert(err == 0);
99
100         err = __connman_iptables_delete("filter", "INPUT",
101                                         "-m mark --mark 1 -j LOG");
102         g_assert(err == 0);
103
104         err = __connman_iptables_commit("filter");
105         g_assert(err == 0);
106 }
107
108
109 static void test_iptables_rule1(void)
110 {
111         int err;
112
113         /* Test if we can do NAT stuff */
114
115         err = __connman_iptables_append("nat", "POSTROUTING",
116                                 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
117
118         err = __connman_iptables_commit("nat");
119         g_assert(err == 0);
120
121         err = __connman_iptables_delete("nat", "POSTROUTING",
122                                 "-s 10.10.1.0/24 -o eth0 -j MASQUERADE");
123
124         err = __connman_iptables_commit("nat");
125         g_assert(err == 0);
126 }
127
128 static void test_iptables_rule2(void)
129 {
130         int err;
131
132         /* Test if the right rule is removed */
133
134         err = __connman_iptables_append("filter", "INPUT",
135                                         "-m mark --mark 1 -j LOG");
136         g_assert(err == 0);
137
138         err = __connman_iptables_commit("filter");
139         g_assert(err == 0);
140
141         err = __connman_iptables_append("filter", "INPUT",
142                                         "-m mark --mark 2 -j LOG");
143         g_assert(err == 0);
144
145         err = __connman_iptables_commit("filter");
146         g_assert(err == 0);
147
148         err = __connman_iptables_delete("filter", "INPUT",
149                                         "-m mark --mark 2 -j LOG");
150         g_assert(err == 0);
151
152         err = __connman_iptables_commit("filter");
153         g_assert(err == 0);
154
155         err = __connman_iptables_delete("filter", "INPUT",
156                                         "-m mark --mark 1 -j LOG");
157         g_assert(err == 0);
158
159         err = __connman_iptables_commit("filter");
160         g_assert(err == 0);
161 }
162
163 struct connman_notifier *nat_notifier;
164
165 struct connman_service {
166         char *dummy;
167 };
168
169 char *connman_service_get_interface(struct connman_service *service)
170 {
171         return "eth0";
172 }
173
174 int connman_notifier_register(struct connman_notifier *notifier)
175 {
176         nat_notifier = notifier;
177
178         return 0;
179 }
180
181 void connman_notifier_unregister(struct connman_notifier *notifier)
182 {
183         nat_notifier = NULL;
184 }
185
186 static void test_nat_basic0(void)
187 {
188         int err;
189
190         err = __connman_nat_enable("bridge", "192.168.2.1", 24);
191         g_assert(err == 0);
192
193         /* test that table is empty */
194         err = __connman_iptables_append("nat", "POSTROUTING",
195                                         "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
196         g_assert(err == 0);
197
198         err = __connman_iptables_commit("nat");
199         g_assert(err == 0);
200
201         __connman_nat_disable("bridge");
202 }
203
204 static void test_nat_basic1(void)
205 {
206         struct connman_service *service;
207         int err;
208
209         service = g_try_new0(struct connman_service, 1);
210         g_assert(service);
211
212         nat_notifier->default_changed(service);
213
214         err = __connman_nat_enable("bridge", "192.168.2.1", 24);
215         g_assert(err == 0);
216
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");
220         g_assert(err == 0);
221
222         err = __connman_iptables_commit("nat");
223         g_assert(err == 0);
224
225         __connman_nat_disable("bridge");
226
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");
230         g_assert(err == 0);
231
232         err = __connman_iptables_commit("nat");
233         g_assert(err == 0);
234 }
235
236 int main(int argc, char *argv[])
237 {
238         int err;
239
240         g_test_init(&argc, &argv, NULL);
241
242         __connman_log_init(argv[0], "*", FALSE, FALSE,
243                         "Unit Tests Connection Manager", VERSION);
244         __connman_iptables_init();
245         __connman_nat_init();
246
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);
255
256         err = g_test_run();
257
258         __connman_nat_cleanup();
259         __connman_iptables_cleanup();
260
261         return err;
262 }