summaryrefslogtreecommitdiff
path: root/src/tests/test-ip4-config.c
blob: 3eee5f1366bf2c886b2685c3c13f45d14b9e6b22 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2013 - 2014 Red Hat, Inc.
 *
 */

#include <glib.h>
#include <string.h>
#include <arpa/inet.h>

#include "nm-ip4-config.h"

static void
addr_init (NMPlatformIP4Address *a, const char *addr, const char *peer, guint plen)
{
	memset (a, 0, sizeof (*a));
	g_assert (inet_pton (AF_INET, addr, (void *) &a->address) == 1);
	if (peer)
		g_assert (inet_pton (AF_INET, peer, (void *) &a->peer_address) == 1);
	a->plen = plen;
}

static void
route_new (NMPlatformIP4Route *route, const char *network, guint plen, const char *gw)
{
	guint n;

	g_assert (route);
	memset (route, 0, sizeof (*route));
	g_assert (inet_pton (AF_INET, network, (void *) &n) == 1);
	route->network = n;
	route->plen = plen;
	if (gw) {
		n = 0;
		g_assert (inet_pton (AF_INET, gw, (void *) &n) == 1);
		route->gateway = n;
	}
}

static guint32
addr_to_num (const char *addr)
{
	guint n;

	g_assert (inet_pton (AF_INET, addr, (void *) &n) == 1);
	return n;
}

static NMIP4Config *
build_test_config (void)
{
	NMIP4Config *config;
	NMPlatformIP4Address addr;
	NMPlatformIP4Route route;

	/* Build up the config to subtract */
	config = nm_ip4_config_new ();

	addr_init (&addr, "192.168.1.10", "1.2.3.4", 24);
	nm_ip4_config_add_address (config, &addr);
	
	route_new (&route, "10.0.0.0", 8, "192.168.1.1");
	nm_ip4_config_add_route (config, &route);

	route_new (&route, "172.16.0.0", 16, "192.168.1.1");
	nm_ip4_config_add_route (config, &route);

	nm_ip4_config_set_gateway (config, addr_to_num ("192.168.1.1"));

	nm_ip4_config_add_nameserver (config, addr_to_num ("4.2.2.1"));
	nm_ip4_config_add_nameserver (config, addr_to_num ("4.2.2.2"));
	nm_ip4_config_add_domain (config, "foobar.com");
	nm_ip4_config_add_domain (config, "baz.com");
	nm_ip4_config_add_search (config, "blahblah.com");
	nm_ip4_config_add_search (config, "beatbox.com");

	nm_ip4_config_add_nis_server (config, addr_to_num ("1.2.3.9"));
	nm_ip4_config_add_nis_server (config, addr_to_num ("1.2.3.10"));

	nm_ip4_config_add_wins (config, addr_to_num ("4.2.3.9"));
	nm_ip4_config_add_wins (config, addr_to_num ("4.2.3.10"));

	return config;
}

static void
test_subtract (void)
{
	NMIP4Config *src, *dst;
	NMPlatformIP4Address addr;
	NMPlatformIP4Route route;
	const NMPlatformIP4Address *test_addr;
	const NMPlatformIP4Route *test_route;
	const char *expected_addr = "192.168.1.12";
	guint32 expected_addr_plen = 24;
	const char *expected_route_dest = "8.7.6.5";
	guint32 expected_route_plen = 8;
	const char *expected_route_next_hop = "192.168.1.1";
	guint32 expected_ns1 = addr_to_num ("8.8.8.8");
	guint32 expected_ns2 = addr_to_num ("8.8.8.9");
	const char *expected_domain = "wonderfalls.com";
	const char *expected_search = "somewhere.com";
	guint32 expected_nis = addr_to_num ("1.2.3.13");
	guint32 expected_wins = addr_to_num ("2.3.4.5");
	guint32 expected_mss = 1400;
	guint32 expected_mtu = 1492;

	src = build_test_config ();

	/* add a couple more things to the test config */
	dst = build_test_config ();
	addr_init (&addr, expected_addr, NULL, expected_addr_plen);
	nm_ip4_config_add_address (dst, &addr);
	
	route_new (&route, expected_route_dest, expected_route_plen, expected_route_next_hop);
	nm_ip4_config_add_route (dst, &route);

	nm_ip4_config_add_nameserver (dst, expected_ns1);
	nm_ip4_config_add_nameserver (dst, expected_ns2);
	nm_ip4_config_add_domain (dst, expected_domain);
	nm_ip4_config_add_search (dst, expected_search);

	nm_ip4_config_add_nis_server (dst, expected_nis);
	nm_ip4_config_add_wins (dst, expected_wins);

	nm_ip4_config_set_mss (dst, expected_mss);
	nm_ip4_config_set_mtu (dst, expected_mtu);

	nm_ip4_config_subtract (dst, src);

	/* ensure what's left is what we expect */
	g_assert_cmpuint (nm_ip4_config_get_num_addresses (dst), ==, 1);
	test_addr = nm_ip4_config_get_address (dst, 0);
	g_assert (test_addr != NULL);
	g_assert_cmpuint (test_addr->address, ==, addr_to_num (expected_addr));
	g_assert_cmpuint (test_addr->peer_address, ==, 0);
	g_assert_cmpuint (test_addr->plen, ==, expected_addr_plen);

	g_assert_cmpuint (nm_ip4_config_get_gateway (dst), ==, 0);

	g_assert_cmpuint (nm_ip4_config_get_num_routes (dst), ==, 1);
	test_route = nm_ip4_config_get_route (dst, 0);
	g_assert (test_route != NULL);
	g_assert_cmpuint (test_route->network, ==, addr_to_num (expected_route_dest));
	g_assert_cmpuint (test_route->plen, ==, expected_route_plen);
	g_assert_cmpuint (test_route->gateway, ==, addr_to_num (expected_route_next_hop));

	g_assert_cmpuint (nm_ip4_config_get_num_nameservers (dst), ==, 2);
	g_assert_cmpuint (nm_ip4_config_get_nameserver (dst, 0), ==, expected_ns1);
	g_assert_cmpuint (nm_ip4_config_get_nameserver (dst, 1), ==, expected_ns2);

	g_assert_cmpuint (nm_ip4_config_get_num_domains (dst), ==, 1);
	g_assert_cmpstr (nm_ip4_config_get_domain (dst, 0), ==, expected_domain);
	g_assert_cmpuint (nm_ip4_config_get_num_searches (dst), ==, 1);
	g_assert_cmpstr (nm_ip4_config_get_search (dst, 0), ==, expected_search);

	g_assert_cmpuint (nm_ip4_config_get_num_nis_servers (dst), ==, 1);
	g_assert_cmpuint (nm_ip4_config_get_nis_server (dst, 0), ==, expected_nis);

	g_assert_cmpuint (nm_ip4_config_get_num_wins (dst), ==, 1);
	g_assert_cmpuint (nm_ip4_config_get_wins (dst, 0), ==, expected_wins);

	g_assert_cmpuint (nm_ip4_config_get_mss (dst), ==, expected_mss);
	g_assert_cmpuint (nm_ip4_config_get_mtu (dst), ==, expected_mtu);

	g_object_unref (src);
	g_object_unref (dst);
}

static void
test_compare_with_source (void)
{
	NMIP4Config *a, *b;
	NMPlatformIP4Address addr;
	NMPlatformIP4Route route;

	a = nm_ip4_config_new ();
	b = nm_ip4_config_new ();

	/* Address */
	addr_init (&addr, "1.2.3.4", NULL, 24);
	addr.source = NM_PLATFORM_SOURCE_USER;
	nm_ip4_config_add_address (a, &addr);

	addr.source = NM_PLATFORM_SOURCE_VPN;
	nm_ip4_config_add_address (b, &addr);

	/* Route */
	route_new (&route, "10.0.0.0", 8, "192.168.1.1");
	route.source = NM_PLATFORM_SOURCE_USER;
	nm_ip4_config_add_route (a, &route);

	route.source = NM_PLATFORM_SOURCE_VPN;
	nm_ip4_config_add_route (b, &route);

	/* Assert that the configs are basically the same, eg that the source is ignored */
	g_assert (nm_ip4_config_equal (a, b));

	g_object_unref (a);
	g_object_unref (b);
}

static void
test_add_address_with_source (void)
{
	NMIP4Config *a;
	NMPlatformIP4Address addr;
	const NMPlatformIP4Address *test_addr;

	a = nm_ip4_config_new ();

	/* Test that a higher priority source is not overwritten */
	addr_init (&addr, "1.2.3.4", NULL, 24);
	addr.source = NM_PLATFORM_SOURCE_USER;
	nm_ip4_config_add_address (a, &addr);

	test_addr = nm_ip4_config_get_address (a, 0);
	g_assert_cmpint (test_addr->source, ==, NM_PLATFORM_SOURCE_USER);

	addr.source = NM_PLATFORM_SOURCE_VPN;
	nm_ip4_config_add_address (a, &addr);

	test_addr = nm_ip4_config_get_address (a, 0);
	g_assert_cmpint (test_addr->source, ==, NM_PLATFORM_SOURCE_USER);

	/* Test that a lower priority address source is overwritten */
	nm_ip4_config_del_address (a, 0);
	addr.source = NM_PLATFORM_SOURCE_KERNEL;
	nm_ip4_config_add_address (a, &addr);

	test_addr = nm_ip4_config_get_address (a, 0);
	g_assert_cmpint (test_addr->source, ==, NM_PLATFORM_SOURCE_KERNEL);

	addr.source = NM_PLATFORM_SOURCE_USER;
	nm_ip4_config_add_address (a, &addr);

	test_addr = nm_ip4_config_get_address (a, 0);
	g_assert_cmpint (test_addr->source, ==, NM_PLATFORM_SOURCE_USER);

	g_object_unref (a);
}

static void
test_add_route_with_source (void)
{
	NMIP4Config *a;
	NMPlatformIP4Route route;
	const NMPlatformIP4Route *test_route;

	a = nm_ip4_config_new ();

	/* Test that a higher priority source is not overwritten */
	route_new (&route, "1.2.3.4", 24, "1.2.3.1");
	route.source = NM_PLATFORM_SOURCE_USER;
	nm_ip4_config_add_route (a, &route);

	test_route = nm_ip4_config_get_route (a, 0);
	g_assert_cmpint (test_route->source, ==, NM_PLATFORM_SOURCE_USER);

	route.source = NM_PLATFORM_SOURCE_VPN;
	nm_ip4_config_add_route (a, &route);

	test_route = nm_ip4_config_get_route (a, 0);
	g_assert_cmpint (test_route->source, ==, NM_PLATFORM_SOURCE_USER);

	/* Test that a lower priority address source is overwritten */
	nm_ip4_config_del_route (a, 0);
	route.source = NM_PLATFORM_SOURCE_KERNEL;
	nm_ip4_config_add_route (a, &route);

	test_route = nm_ip4_config_get_route (a, 0);
	g_assert_cmpint (test_route->source, ==, NM_PLATFORM_SOURCE_KERNEL);

	route.source = NM_PLATFORM_SOURCE_USER;
	nm_ip4_config_add_route (a, &route);

	test_route = nm_ip4_config_get_route (a, 0);
	g_assert_cmpint (test_route->source, ==, NM_PLATFORM_SOURCE_USER);

	g_object_unref (a);
}

static void
test_merge_subtract_mss_mtu (void)
{
	NMIP4Config *cfg1, *cfg2, *cfg3;
	guint32 expected_mss2 = 1400;
	guint32 expected_mtu2 = 1492;
	guint32 expected_mss3 = 555;
	guint32 expected_mtu3 = 666;

	cfg1 = build_test_config ();
	cfg2 = build_test_config ();
	cfg3 = build_test_config ();

	/* add MSS, MTU to configs to test them */
	nm_ip4_config_set_mss (cfg2, expected_mss2);
	nm_ip4_config_set_mtu (cfg2, expected_mtu2);
	nm_ip4_config_set_mss (cfg3, expected_mss3);
	nm_ip4_config_set_mtu (cfg3, expected_mtu3);

	nm_ip4_config_merge (cfg1, cfg2);
	/* ensure MSS and MTU are in cfg1 */
	g_assert_cmpuint (nm_ip4_config_get_mss (cfg1), ==, expected_mss2);
	g_assert_cmpuint (nm_ip4_config_get_mtu (cfg1), ==, expected_mtu2);

	nm_ip4_config_merge (cfg1, cfg3);
	/* ensure again the same MSS and MTU are in cfg1 */
	g_assert_cmpuint (nm_ip4_config_get_mss (cfg1), ==, expected_mss2);
	g_assert_cmpuint (nm_ip4_config_get_mtu (cfg1), ==, expected_mtu2);

	nm_ip4_config_subtract (cfg1, cfg2);
	/* ensure MSS and MTU are zero in cfg1 */
	g_assert_cmpuint (nm_ip4_config_get_mss (cfg1), ==, 0);
	g_assert_cmpuint (nm_ip4_config_get_mtu (cfg1), ==, 0);

	g_object_unref (cfg1);
	g_object_unref (cfg2);
	g_object_unref (cfg3);
}

/*******************************************/

int
main (int argc, char **argv)
{
	g_test_init (&argc, &argv, NULL);

#if !GLIB_CHECK_VERSION (2, 35, 0)
	g_type_init ();
#endif

	g_test_add_func ("/ip4-config/subtract", test_subtract);
	g_test_add_func ("/ip4-config/compare-with-source", test_compare_with_source);
	g_test_add_func ("/ip4-config/add-address-with-source", test_add_address_with_source);
	g_test_add_func ("/ip4-config/add-route-with-source", test_add_route_with_source);
	g_test_add_func ("/ip4-config/merge-subtract-mss-mtu", test_merge_subtract_mss_mtu);

	return g_test_run ();
}