summaryrefslogtreecommitdiff
path: root/unit/test-nat.c
blob: 4a48461db2b1a69ecc6f57674b91458f26eede29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2012  BWM CarIT GmbH. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>

#include "../src/connman.h"

/* #define DEBUG */
#ifdef DEBUG
#include <stdio.h>

#define LOG(fmt, arg...) do { \
	fprintf(stdout, "%s:%s() " fmt "\n", \
			__FILE__, __func__ , ## arg); \
} while (0)
#else
#define LOG(fmt, arg...)
#endif

struct connman_notifier *nat_notifier;

struct connman_service {
	char *dummy;
};

char *connman_service_get_interface(struct connman_service *service)
{
	return "eth0";
}

int connman_notifier_register(struct connman_notifier *notifier)
{
	nat_notifier = notifier;

	return 0;
}

void connman_notifier_unregister(struct connman_notifier *notifier)
{
	nat_notifier = NULL;
}


static void test_iptables_basic0(void)
{
	int err;

	err = __connman_iptables_command("-C INPUT -i session-bridge -j ACCEPT");
	g_assert(err != 0);
	err = __connman_iptables_commit("filter");
	g_assert(err == 0);

	err = __connman_iptables_command("-I INPUT -i session-bridge -j ACCEPT");
	g_assert(err == 0);
	err = __connman_iptables_commit("filter");
	g_assert(err == 0);

	err = __connman_iptables_command("-C INPUT -i session-bridge -j ACCEPT");
	g_assert(err == 0);
	err = __connman_iptables_commit("filter");
	g_assert(err == 0);

	err = __connman_iptables_command("-D INPUT -i session-bridge -j ACCEPT");
	g_assert(err == 0);
	err = __connman_iptables_commit("filter");
	g_assert(err == 0);

	err = __connman_iptables_command("-C INPUT -i session-bridge -j ACCEPT");
	g_assert(err != 0);
	err = __connman_iptables_commit("filter");
	g_assert(err == 0);
}

static void test_nat_basic0(void)
{
	int err;

	err = __connman_nat_enable("bridge", "192.168.2.1", 24);
	g_assert(err == 0);

	/* test that table is empty */
	err = __connman_iptables_command("-t nat -C POSTROUTING "
					"-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
	g_assert(err != 0);
	err = __connman_iptables_commit("nat");
	g_assert(err == 0);


	__connman_nat_disable("bridge");
}

static void test_nat_basic1(void)
{
	struct connman_service *service;
	int err;

	service = g_try_new0(struct connman_service, 1);
	g_assert(service);

	nat_notifier->default_changed(service);

	err = __connman_nat_enable("bridge", "192.168.2.1", 24);
	g_assert(err == 0);

	/* test that table is not empty */
	err = __connman_iptables_command("-t nat -C POSTROUTING "
					"-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
	g_assert(err == 0);
	err = __connman_iptables_commit("nat");
	g_assert(err == 0);

	__connman_nat_disable("bridge");

	/* test that table is empty again */
	err = __connman_iptables_command("-t nat -C POSTROUTING "
					"-s 192.168.2.1/24 -o eth0 -j MASQUERADE");
	g_assert(err != 0);
	err = __connman_iptables_commit("nat");
	g_assert(err == 0);
}

int main(int argc, char *argv[])
{
	int err;

	g_test_init(&argc, &argv, NULL);

	__connman_log_init(argv[0], "*", FALSE, TRUE);
	__connman_iptables_init();
	__connman_nat_init();

	g_test_add_func("/iptables/basic0", test_iptables_basic0);
	g_test_add_func("/nat/basic0", test_nat_basic0);
	g_test_add_func("/nat/basic1", test_nat_basic1);

	err = g_test_run();

	__connman_nat_cleanup();
	__connman_iptables_cleanup();
	__connman_log_cleanup(TRUE);

	return err;
}