test-nat: Commit table changes
[platform/upstream/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 static void test_nat_basic0(void)
66 {
67         int err;
68
69         err = __connman_nat_enable("bridge", "192.168.2.1", 24);
70         g_assert(err == 0);
71
72         /* test that table is empty */
73         err = __connman_iptables_command("-t nat -C POSTROUTING "
74                                         "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
75         g_assert(err != 0);
76         err = __connman_iptables_commit("nat");
77         g_assert(err == 0);
78
79
80         __connman_nat_disable("bridge");
81 }
82
83 static void test_nat_basic1(void)
84 {
85         struct connman_service *service;
86         int err;
87
88         service = g_try_new0(struct connman_service, 1);
89         g_assert(service);
90
91         nat_notifier->default_changed(service);
92
93         err = __connman_nat_enable("bridge", "192.168.2.1", 24);
94         g_assert(err == 0);
95
96         /* test that table is not empty */
97         err = __connman_iptables_command("-t nat -C POSTROUTING "
98                                         "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
99         g_assert(err == 0);
100         err = __connman_iptables_commit("nat");
101         g_assert(err == 0);
102
103         __connman_nat_disable("bridge");
104
105         /* test that table is empty again */
106         err = __connman_iptables_command("-t nat -C POSTROUTING "
107                                         "-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
108         g_assert(err != 0);
109         err = __connman_iptables_commit("nat");
110         g_assert(err == 0);
111 }
112
113 int main(int argc, char *argv[])
114 {
115         int err;
116
117         g_test_init(&argc, &argv, NULL);
118
119         __connman_log_init(argv[0], "*", FALSE);
120         __connman_iptables_init();
121         __connman_nat_init();
122
123         g_test_add_func("/basic0", test_nat_basic0);
124         g_test_add_func("/basic1", test_nat_basic1);
125
126         err = g_test_run();
127
128         __connman_nat_cleanup();
129         __connman_iptables_cleanup();
130         __connman_log_cleanup();
131
132         return err;
133 }