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_ippool_basic0(void)
44 struct connman_ippool *pool;
46 const char *broadcast;
47 const char *subnet_mask;
52 /* Test the IP range */
54 pool = __connman_ippool_create(23, 1, 500, NULL, NULL);
55 g_assert(pool == NULL);
57 for (i = 1; i < 254; i++) {
58 pool = __connman_ippool_create(23, 1, i, NULL, NULL);
61 gateway = __connman_ippool_get_gateway(pool);
62 broadcast = __connman_ippool_get_broadcast(pool);
63 subnet_mask = __connman_ippool_get_subnet_mask(pool);
64 start_ip = __connman_ippool_get_start_ip(pool);
65 end_ip = __connman_ippool_get_end_ip(pool);
69 g_assert(subnet_mask);
73 LOG("\n\tIP range %s --> %s\n"
74 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
75 gateway, broadcast, subnet_mask);
77 __connman_ippool_unref(pool);
81 static void test_ippool_basic1(void)
83 struct connman_ippool *pool;
85 const char *broadcast;
86 const char *subnet_mask;
89 GSList *list = NULL, *it;
92 /* Allocate all possible pools */
96 * 24-bit block 10.0.0.0 – 10.255.255.255 16,777,216
97 * 20-bit block 172.16.0.0 – 172.31.255.255 1,048,576
98 * 16-bit block 192.168.0.0 – 192.168.255.255 65,536
102 * Total numbers of 256 blocks: 69,888
106 pool = __connman_ippool_create(23, 1, 100, NULL, NULL);
112 list = g_slist_prepend(list, pool);
114 gateway = __connman_ippool_get_gateway(pool);
115 broadcast = __connman_ippool_get_broadcast(pool);
116 subnet_mask = __connman_ippool_get_subnet_mask(pool);
117 start_ip = __connman_ippool_get_start_ip(pool);
118 end_ip = __connman_ippool_get_end_ip(pool);
122 g_assert(subnet_mask);
127 LOG("Number of blocks %d", i);
129 for (it = list; it != NULL; it = it->next) {
132 __connman_ippool_unref(pool);
138 static void collision_cb(struct connman_ippool *pool, void *user_data)
140 int *flag = user_data;
142 LOG("collision detected");
144 g_assert(*flag == 0);
150 static void test_ippool_collision0(void)
152 struct connman_ippool *pool;
154 const char *broadcast;
155 const char *subnet_mask;
156 const char *start_ip;
160 /* Test the IP range collision */
163 pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
166 gateway = __connman_ippool_get_gateway(pool);
167 broadcast = __connman_ippool_get_broadcast(pool);
168 subnet_mask = __connman_ippool_get_subnet_mask(pool);
169 start_ip = __connman_ippool_get_start_ip(pool);
170 end_ip = __connman_ippool_get_end_ip(pool);
174 g_assert(subnet_mask);
178 LOG("\n\tIP range %s --> %s\n"
179 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
180 gateway, broadcast, subnet_mask);
182 __connman_ippool_newaddr(23, start_ip);
186 __connman_ippool_newaddr(42, end_ip);
190 __connman_ippool_unref(pool);
193 int main(int argc, char *argv[])
197 g_test_init(&argc, &argv, NULL);
199 __connman_ippool_init();
201 g_test_add_func("/basic0", test_ippool_basic0);
202 g_test_add_func("/basic1", test_ippool_basic1);
203 g_test_add_func("/collision0", test_ippool_collision0);
207 __connman_ippool_cleanup();