dnsproxy: Only one copy of the relevant buffers will be made to a TCP request
[framework/connectivity/connman.git] / unit / test-nat.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  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 /* #define DEBUG */
31 #ifdef DEBUG
32 #include <stdio.h>
33
34 #define LOG(fmt, arg...) do { \
35         fprintf(stdout, "%s:%s() " fmt "\n", \
36                         __FILE__, __func__ , ## arg); \
37 } while (0)
38 #else
39 #define LOG(fmt, arg...)
40 #endif
41
42 struct connman_notifier *nat_notifier;
43
44 struct connman_service {
45         char *dummy;
46 };
47
48 char *connman_service_get_interface(struct connman_service *service)
49 {
50         return "eth0";
51 }
52
53 int connman_notifier_register(struct connman_notifier *notifier)
54 {
55         nat_notifier = notifier;
56
57         return 0;
58 }
59
60 void connman_notifier_unregister(struct connman_notifier *notifier)
61 {
62         nat_notifier = NULL;
63 }
64
65
66 static void test_iptables_basic0(void)
67 {
68         int err;
69
70         err = __connman_iptables_command("-C INPUT -i session-bridge -j ACCEPT");
71         g_assert(err != 0);
72         err = __connman_iptables_commit("filter");
73         g_assert(err == 0);
74
75         err = __connman_iptables_command("-I INPUT -i session-bridge -j ACCEPT");
76         g_assert(err == 0);
77         err = __connman_iptables_commit("filter");
78         g_assert(err == 0);
79
80         err = __connman_iptables_command("-C INPUT -i session-bridge -j ACCEPT");
81         g_assert(err == 0);
82         err = __connman_iptables_commit("filter");
83         g_assert(err == 0);
84
85         err = __connman_iptables_command("-D INPUT -i session-bridge -j ACCEPT");
86         g_assert(err == 0);
87         err = __connman_iptables_commit("filter");
88         g_assert(err == 0);
89
90         err = __connman_iptables_command("-C INPUT -i session-bridge -j ACCEPT");
91         g_assert(err != 0);
92         err = __connman_iptables_commit("filter");
93         g_assert(err == 0);
94 }
95
96 static void test_nat_basic0(void)
97 {
98         int err;
99
100         err = __connman_nat_enable("bridge", "192.168.2.1", 24);
101         g_assert(err == 0);
102
103         /* test that table is empty */
104         err = __connman_iptables_command("-t nat -C POSTROUTING "
105                                         "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
106         g_assert(err != 0);
107         err = __connman_iptables_commit("nat");
108         g_assert(err == 0);
109
110
111         __connman_nat_disable("bridge");
112 }
113
114 static void test_nat_basic1(void)
115 {
116         struct connman_service *service;
117         int err;
118
119         service = g_try_new0(struct connman_service, 1);
120         g_assert(service);
121
122         nat_notifier->default_changed(service);
123
124         err = __connman_nat_enable("bridge", "192.168.2.1", 24);
125         g_assert(err == 0);
126
127         /* test that table is not empty */
128         err = __connman_iptables_command("-t nat -C POSTROUTING "
129                                         "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
130         g_assert(err == 0);
131         err = __connman_iptables_commit("nat");
132         g_assert(err == 0);
133
134         __connman_nat_disable("bridge");
135
136         /* test that table is empty again */
137         err = __connman_iptables_command("-t nat -C POSTROUTING "
138                                         "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
139         g_assert(err != 0);
140         err = __connman_iptables_commit("nat");
141         g_assert(err == 0);
142 }
143
144 int main(int argc, char *argv[])
145 {
146         int err;
147
148         g_test_init(&argc, &argv, NULL);
149
150         __connman_log_init(argv[0], "*", FALSE);
151         __connman_iptables_init();
152         __connman_nat_init();
153
154         g_test_add_func("/iptables/basic0", test_iptables_basic0);
155         g_test_add_func("/nat/basic0", test_nat_basic0);
156         g_test_add_func("/nat/basic1", test_nat_basic1);
157
158         err = g_test_run();
159
160         __connman_nat_cleanup();
161         __connman_iptables_cleanup();
162         __connman_log_cleanup();
163
164         return err;
165 }