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