5 * Copyright (C) 2012 BWM CarIT GmbH. All rights reserved.
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.
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.
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
28 #include "../src/connman.h"
34 #define LOG(fmt, arg...) do { \
35 fprintf(stdout, "%s:%s() " fmt "\n", \
36 __FILE__, __func__ , ## arg); \
39 #define LOG(fmt, arg...)
42 static void test_case_1(void)
44 struct connman_ippool *pool;
47 __connman_ippool_init();
49 pool = __connman_ippool_create(23, 1, 500, NULL, NULL);
52 for (i = 0; i < 100000; i++) {
53 pool = __connman_ippool_create(23, 1, 20, NULL, NULL);
56 __connman_ippool_unref(pool);
59 __connman_ippool_cleanup();
62 static void test_case_2(void)
64 struct connman_ippool *pool;
66 const char *broadcast;
67 const char *subnet_mask;
72 __connman_ippool_init();
74 /* Test the IP range */
75 for (i = 1; i < 254; i++) {
76 pool = __connman_ippool_create(23, 1, i, NULL, NULL);
79 gateway = __connman_ippool_get_gateway(pool);
80 broadcast = __connman_ippool_get_broadcast(pool);
81 subnet_mask = __connman_ippool_get_subnet_mask(pool);
82 start_ip = __connman_ippool_get_start_ip(pool);
83 end_ip = __connman_ippool_get_end_ip(pool);
87 g_assert(subnet_mask);
91 LOG("\n\tIP range %s --> %s\n"
92 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
93 gateway, broadcast, subnet_mask);
95 __connman_ippool_unref(pool);
98 __connman_ippool_cleanup();
101 static void test_case_3(void)
103 struct connman_ippool *pool;
105 const char *broadcast;
106 const char *subnet_mask;
107 const char *start_ip;
109 GSList *list = NULL, *it;
112 __connman_ippool_init();
115 * Number of addresses
116 * 24-bit block 10.0.0.0 – 10.255.255.255 16,777,216
117 * 20-bit block 172.16.0.0 – 172.31.255.255 1,048,576
118 * 16-bit block 192.168.0.0 – 192.168.255.255 65,536
122 * Total numbers of 256 blocks: 69,888
126 * Completely exhaust the first two pools because they are a bit
129 __connman_ippool_newaddr(45, "10.0.0.1", 8);
130 __connman_ippool_newaddr(46, "172.16.0.1", 11);
133 pool = __connman_ippool_create(23, 1, 100, NULL, NULL);
138 list = g_slist_prepend(list, pool);
140 gateway = __connman_ippool_get_gateway(pool);
141 broadcast = __connman_ippool_get_broadcast(pool);
142 subnet_mask = __connman_ippool_get_subnet_mask(pool);
143 start_ip = __connman_ippool_get_start_ip(pool);
144 end_ip = __connman_ippool_get_end_ip(pool);
148 g_assert(subnet_mask);
155 LOG("Number of blocks %d", i);
157 for (it = list; it; it = it->next) {
160 __connman_ippool_unref(pool);
165 __connman_ippool_cleanup();
168 static void collision_cb(struct connman_ippool *pool, void *user_data)
170 int *flag = user_data;
172 LOG("collision detected");
174 g_assert(*flag == 0);
180 static void test_case_4(void)
182 struct connman_ippool *pool;
184 const char *broadcast;
185 const char *subnet_mask;
186 const char *start_ip;
190 __connman_ippool_init();
192 /* Test the IP range collision */
195 pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
198 gateway = __connman_ippool_get_gateway(pool);
199 broadcast = __connman_ippool_get_broadcast(pool);
200 subnet_mask = __connman_ippool_get_subnet_mask(pool);
201 start_ip = __connman_ippool_get_start_ip(pool);
202 end_ip = __connman_ippool_get_end_ip(pool);
206 g_assert(subnet_mask);
210 LOG("\n\tIP range %s --> %s\n"
211 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
212 gateway, broadcast, subnet_mask);
214 __connman_ippool_newaddr(23, start_ip, 24);
218 __connman_ippool_newaddr(42, start_ip, 16);
222 __connman_ippool_unref(pool);
226 pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
229 gateway = __connman_ippool_get_gateway(pool);
230 broadcast = __connman_ippool_get_broadcast(pool);
231 subnet_mask = __connman_ippool_get_subnet_mask(pool);
232 start_ip = __connman_ippool_get_start_ip(pool);
233 end_ip = __connman_ippool_get_end_ip(pool);
237 g_assert(subnet_mask);
241 LOG("\n\tIP range %s --> %s\n"
242 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
243 gateway, broadcast, subnet_mask);
245 __connman_ippool_newaddr(45, start_ip, 22);
249 __connman_ippool_unref(pool);
251 __connman_ippool_cleanup();
254 static void test_case_5(void)
256 struct connman_ippool *pool;
258 const char *broadcast;
259 const char *subnet_mask;
260 const char *start_ip;
264 __connman_ippool_init();
266 /* Test the IP range collision */
269 start_ip = "192.168.1.2";
270 __connman_ippool_newaddr(25, start_ip, 24);
273 /* pool should return 192.168.0.1 now */
274 pool = __connman_ippool_create(26, 1, 100, collision_cb, &flag);
277 gateway = __connman_ippool_get_gateway(pool);
278 broadcast = __connman_ippool_get_broadcast(pool);
279 subnet_mask = __connman_ippool_get_subnet_mask(pool);
280 start_ip = __connman_ippool_get_start_ip(pool);
281 end_ip = __connman_ippool_get_end_ip(pool);
285 g_assert(subnet_mask);
289 g_assert_cmpstr(gateway, ==, "192.168.0.1");
290 g_assert_cmpstr(broadcast, ==, "192.168.0.255");
291 g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
292 g_assert_cmpstr(start_ip, ==, "192.168.0.1");
293 g_assert_cmpstr(end_ip, ==, "192.168.0.101");
295 LOG("\n\tIP range %s --> %s\n"
296 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
297 gateway, broadcast, subnet_mask);
299 __connman_ippool_unref(pool);
302 * Now create the pool again, we should not get collision
303 * with existing allocated address.
306 /* pool should return 192.168.2.1 now */
308 pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
311 gateway = __connman_ippool_get_gateway(pool);
312 broadcast = __connman_ippool_get_broadcast(pool);
313 subnet_mask = __connman_ippool_get_subnet_mask(pool);
314 start_ip = __connman_ippool_get_start_ip(pool);
315 end_ip = __connman_ippool_get_end_ip(pool);
319 g_assert(subnet_mask);
323 g_assert_cmpstr(gateway, ==, "192.168.2.1");
324 g_assert_cmpstr(broadcast, ==, "192.168.2.255");
325 g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
326 g_assert_cmpstr(start_ip, ==, "192.168.2.1");
327 g_assert_cmpstr(end_ip, ==, "192.168.2.101");
329 LOG("\n\tIP range %s --> %s\n"
330 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
331 gateway, broadcast, subnet_mask);
335 __connman_ippool_unref(pool);
337 __connman_ippool_cleanup();
340 static void test_case_6(void)
342 struct connman_ippool *pool;
344 const char *broadcast;
345 const char *subnet_mask;
346 const char *start_ip;
350 __connman_ippool_init();
352 /* Test the IP range collision */
355 start_ip = "192.168.1.2";
356 __connman_ippool_newaddr(25, start_ip, 24);
360 start_ip = "192.168.0.2";
361 __connman_ippool_newaddr(25, start_ip, 24);
364 /* pool should return 192.168.2.1 now */
365 pool = __connman_ippool_create(26, 1, 100, collision_cb, &flag);
368 gateway = __connman_ippool_get_gateway(pool);
369 broadcast = __connman_ippool_get_broadcast(pool);
370 subnet_mask = __connman_ippool_get_subnet_mask(pool);
371 start_ip = __connman_ippool_get_start_ip(pool);
372 end_ip = __connman_ippool_get_end_ip(pool);
376 g_assert(subnet_mask);
380 g_assert_cmpstr(gateway, ==, "192.168.2.1");
381 g_assert_cmpstr(broadcast, ==, "192.168.2.255");
382 g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
383 g_assert_cmpstr(start_ip, ==, "192.168.2.1");
384 g_assert_cmpstr(end_ip, ==, "192.168.2.101");
386 LOG("\n\tIP range %s --> %s\n"
387 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
388 gateway, broadcast, subnet_mask);
390 __connman_ippool_unref(pool);
393 * Now create the pool again, we should not get collision
394 * with existing allocated address.
397 /* pool should return 192.168.3.1 now */
399 pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
402 gateway = __connman_ippool_get_gateway(pool);
403 broadcast = __connman_ippool_get_broadcast(pool);
404 subnet_mask = __connman_ippool_get_subnet_mask(pool);
405 start_ip = __connman_ippool_get_start_ip(pool);
406 end_ip = __connman_ippool_get_end_ip(pool);
410 g_assert(subnet_mask);
414 g_assert_cmpstr(gateway, ==, "192.168.3.1");
415 g_assert_cmpstr(broadcast, ==, "192.168.3.255");
416 g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
417 g_assert_cmpstr(start_ip, ==, "192.168.3.1");
418 g_assert_cmpstr(end_ip, ==, "192.168.3.101");
420 LOG("\n\tIP range %s --> %s\n"
421 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
422 gateway, broadcast, subnet_mask);
427 start_ip = "192.168.3.2";
428 __connman_ippool_newaddr(25, start_ip, 24);
431 __connman_ippool_unref(pool);
433 __connman_ippool_cleanup();
436 int main(int argc, char *argv[])
438 g_test_init(&argc, &argv, NULL);
440 g_test_add_func("/ippool/Test case 1", test_case_1);
441 g_test_add_func("/ippool/Test case 2", test_case_2);
442 g_test_add_func("/ippool/Test case 3", test_case_3);
443 g_test_add_func("/ippool/Test case 4", test_case_4);
444 g_test_add_func("/ippool/Test case 5", test_case_5);
445 g_test_add_func("/ippool/Test case 6", test_case_6);