f11ed5519bf2f825199ddc1a7ce0c0644fce8818
[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 int main(int argc, char *argv[])
104 {
105         int err;
106
107         g_test_init(&argc, &argv, NULL);
108
109         __connman_log_init(argv[0], "*", FALSE, FALSE,
110                         "Unit Tests Connection Manager", VERSION);
111         __connman_iptables_init();
112
113         g_test_add_func("/iptables/basic0", test_iptables_basic0);
114         g_test_add_func("/iptables/basic1", test_iptables_basic1);
115         g_test_add_func("/iptables/basic2", test_iptables_basic2);
116
117         err = g_test_run();
118
119         __connman_iptables_cleanup();
120
121         return err;
122 }