summaryrefslogtreecommitdiff
path: root/unit/test-ippool.c
blob: 3afee9aac55e4fb8989f9cde91d8f63caea48a39 (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
/*
 *
 *  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

static void test_case_1(void)
{
	struct connman_ippool *pool;
	int i;

	__connman_ippool_init();

	pool = __connman_ippool_create(23, 1, 500, NULL, NULL);
	g_assert(pool == NULL);

	for (i = 0; i < 100000; i++) {
		pool = __connman_ippool_create(23, 1, 20, NULL, NULL);
		g_assert(pool);

		__connman_ippool_unref(pool);
	}

	__connman_ippool_cleanup();
}

static void test_case_2(void)
{
	struct connman_ippool *pool;
	const char *gateway;
	const char *broadcast;
	const char *subnet_mask;
	const char *start_ip;
	const char *end_ip;
	int i;

	__connman_ippool_init();

	/* Test the IP range */
	for (i = 1; i < 254; i++) {
		pool = __connman_ippool_create(23, 1, i, NULL, NULL);
		g_assert(pool);

		gateway = __connman_ippool_get_gateway(pool);
		broadcast = __connman_ippool_get_broadcast(pool);
		subnet_mask = __connman_ippool_get_subnet_mask(pool);
		start_ip = __connman_ippool_get_start_ip(pool);
		end_ip = __connman_ippool_get_end_ip(pool);

		g_assert(gateway);
		g_assert(broadcast);
		g_assert(subnet_mask);
		g_assert(start_ip);
		g_assert(end_ip);

		LOG("\n\tIP range %s --> %s\n"
			"\tgateway %s broadcast %s mask %s", start_ip, end_ip,
			gateway, broadcast, subnet_mask);

		__connman_ippool_unref(pool);
	}

	__connman_ippool_cleanup();
}

static void test_case_3(void)
{
	struct connman_ippool *pool;
	const char *gateway;
	const char *broadcast;
	const char *subnet_mask;
	const char *start_ip;
	const char *end_ip;
	GSList *list = NULL, *it;
	int i = 0;

	__connman_ippool_init();

	/*
	 *                                             Number of addresses
	 * 24-bit block         10.0.0.0    – 10.255.255.255    16,777,216
	 * 20-bit block         172.16.0.0  – 172.31.255.255     1,048,576
	 * 16-bit block         192.168.0.0 – 192.168.255.255       65,536
	 *
	 * Total                                                17,891,328
	 *
	 * Total numbers of 256 blocks:                             69,888
	 */

	/*
	 * Completely exhaust the first two pools because they are a bit
	 * too large.
	 */
	__connman_ippool_newaddr(45, "10.0.0.1", 8);
	__connman_ippool_newaddr(46, "172.16.0.1", 11);

	while (TRUE) {
		pool = __connman_ippool_create(23, 1, 100, NULL, NULL);
		if (pool == NULL)
			break;
		i += 1;

		list = g_slist_prepend(list, pool);

		gateway = __connman_ippool_get_gateway(pool);
		broadcast = __connman_ippool_get_broadcast(pool);
		subnet_mask = __connman_ippool_get_subnet_mask(pool);
		start_ip = __connman_ippool_get_start_ip(pool);
		end_ip = __connman_ippool_get_end_ip(pool);

		g_assert(gateway);
		g_assert(broadcast);
		g_assert(subnet_mask);
		g_assert(start_ip);
		g_assert(end_ip);
	}

	g_assert(i == 255);

	LOG("Number of blocks %d", i);

	for (it = list; it != NULL; it = it->next) {
		pool = it->data;

		__connman_ippool_unref(pool);
	}

	g_slist_free(list);

	__connman_ippool_cleanup();
}

static void collision_cb(struct connman_ippool *pool, void *user_data)
{
	int *flag = user_data;

	LOG("collision detected");

	g_assert(*flag == 0);
	g_assert(pool);

	*flag = 1;
}

static void test_case_4(void)
{
	struct connman_ippool *pool;
	const char *gateway;
	const char *broadcast;
	const char *subnet_mask;
	const char *start_ip;
	const char *end_ip;
	int flag;

	__connman_ippool_init();

	/* Test the IP range collision */

	flag = 0;
	pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
	g_assert(pool);

	gateway = __connman_ippool_get_gateway(pool);
	broadcast = __connman_ippool_get_broadcast(pool);
	subnet_mask = __connman_ippool_get_subnet_mask(pool);
	start_ip = __connman_ippool_get_start_ip(pool);
	end_ip = __connman_ippool_get_end_ip(pool);

	g_assert(gateway);
	g_assert(broadcast);
	g_assert(subnet_mask);
	g_assert(start_ip);
	g_assert(end_ip);

	LOG("\n\tIP range %s --> %s\n"
		"\tgateway %s broadcast %s mask %s", start_ip, end_ip,
		gateway, broadcast, subnet_mask);

	__connman_ippool_newaddr(23, start_ip, 24);

	g_assert(flag == 0);

	__connman_ippool_newaddr(42, start_ip, 16);

	g_assert(flag == 1);

	__connman_ippool_unref(pool);

	flag = 0;

	pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
	g_assert(pool);

	gateway = __connman_ippool_get_gateway(pool);
	broadcast = __connman_ippool_get_broadcast(pool);
	subnet_mask = __connman_ippool_get_subnet_mask(pool);
	start_ip = __connman_ippool_get_start_ip(pool);
	end_ip = __connman_ippool_get_end_ip(pool);

	g_assert(gateway);
	g_assert(broadcast);
	g_assert(subnet_mask);
	g_assert(start_ip);
	g_assert(end_ip);

	LOG("\n\tIP range %s --> %s\n"
		"\tgateway %s broadcast %s mask %s", start_ip, end_ip,
		gateway, broadcast, subnet_mask);

	__connman_ippool_newaddr(45, start_ip, 22);

	g_assert(flag == 1);

	__connman_ippool_unref(pool);

	__connman_ippool_cleanup();
}

static void test_case_5(void)
{
	struct connman_ippool *pool;
	const char *gateway;
	const char *broadcast;
	const char *subnet_mask;
	const char *start_ip;
	const char *end_ip;
	int flag;

	__connman_ippool_init();

	/* Test the IP range collision */

	flag = 0;
	start_ip = "192.168.1.2";
	__connman_ippool_newaddr(25, start_ip, 24);
	g_assert(flag == 0);

	/* pool should return 192.168.0.1 now */
	pool = __connman_ippool_create(26, 1, 100, collision_cb, &flag);
	g_assert(pool);

	gateway = __connman_ippool_get_gateway(pool);
	broadcast = __connman_ippool_get_broadcast(pool);
	subnet_mask = __connman_ippool_get_subnet_mask(pool);
	start_ip = __connman_ippool_get_start_ip(pool);
	end_ip = __connman_ippool_get_end_ip(pool);

	g_assert(gateway);
	g_assert(broadcast);
	g_assert(subnet_mask);
	g_assert(start_ip);
	g_assert(end_ip);

	g_assert_cmpstr(gateway, ==, "192.168.0.1");
	g_assert_cmpstr(broadcast, ==, "192.168.0.255");
	g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
	g_assert_cmpstr(start_ip, ==, "192.168.0.1");
	g_assert_cmpstr(end_ip, ==, "192.168.0.101");

	LOG("\n\tIP range %s --> %s\n"
		"\tgateway %s broadcast %s mask %s", start_ip, end_ip,
		gateway, broadcast, subnet_mask);

	__connman_ippool_unref(pool);

	/*
	 * Now create the pool again, we should not get collision
	 * with existing allocated address.
	 */

	/* pool should return 192.168.2.1 now */
	flag = 0;
	pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
	g_assert(pool);

	gateway = __connman_ippool_get_gateway(pool);
	broadcast = __connman_ippool_get_broadcast(pool);
	subnet_mask = __connman_ippool_get_subnet_mask(pool);
	start_ip = __connman_ippool_get_start_ip(pool);
	end_ip = __connman_ippool_get_end_ip(pool);

	g_assert(gateway);
	g_assert(broadcast);
	g_assert(subnet_mask);
	g_assert(start_ip);
	g_assert(end_ip);

	g_assert_cmpstr(gateway, ==, "192.168.2.1");
	g_assert_cmpstr(broadcast, ==, "192.168.2.255");
	g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
	g_assert_cmpstr(start_ip, ==, "192.168.2.1");
	g_assert_cmpstr(end_ip, ==, "192.168.2.101");

	LOG("\n\tIP range %s --> %s\n"
		"\tgateway %s broadcast %s mask %s", start_ip, end_ip,
		gateway, broadcast, subnet_mask);

	g_assert(flag == 0);

	__connman_ippool_unref(pool);

	__connman_ippool_cleanup();
}

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

	g_test_add_func("/ippool/Test case 1", test_case_1);
	g_test_add_func("/ippool/Test case 2", test_case_2);
	g_test_add_func("/ippool/Test case 3", test_case_3);
	g_test_add_func("/ippool/Test case 4", test_case_4);
	g_test_add_func("/ippool/Test case 5", test_case_5);

	return g_test_run();
}