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;
47 for (i = 0; i < 100000; i++) {
48 pool = __connman_ippool_create(23, 1, 20, NULL, NULL);
51 __connman_ippool_unref(pool);
55 static void test_ippool_basic1(void)
57 struct connman_ippool *pool;
59 const char *broadcast;
60 const char *subnet_mask;
65 /* Test the IP range */
66 for (i = 1; i < 254; i++) {
67 pool = __connman_ippool_create(23, 1, i, NULL, NULL);
70 gateway = __connman_ippool_get_gateway(pool);
71 broadcast = __connman_ippool_get_broadcast(pool);
72 subnet_mask = __connman_ippool_get_subnet_mask(pool);
73 start_ip = __connman_ippool_get_start_ip(pool);
74 end_ip = __connman_ippool_get_end_ip(pool);
78 g_assert(subnet_mask);
82 LOG("\n\tIP range %s --> %s\n"
83 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
84 gateway, broadcast, subnet_mask);
86 __connman_ippool_unref(pool);
90 static void test_ippool_basic2(void)
92 struct connman_ippool *pool;
94 const char *broadcast;
95 const char *subnet_mask;
98 GSList *list = NULL, *it;
101 /* Allocate all possible pools */
104 * Number of addresses
105 * 24-bit block 10.0.0.0 – 10.255.255.255 16,777,216
106 * 20-bit block 172.16.0.0 – 172.31.255.255 1,048,576
107 * 16-bit block 192.168.0.0 – 192.168.255.255 65,536
111 * Total numbers of 256 blocks: 69,888
115 pool = __connman_ippool_create(23, 1, 100, NULL, NULL);
121 list = g_slist_prepend(list, pool);
123 gateway = __connman_ippool_get_gateway(pool);
124 broadcast = __connman_ippool_get_broadcast(pool);
125 subnet_mask = __connman_ippool_get_subnet_mask(pool);
126 start_ip = __connman_ippool_get_start_ip(pool);
127 end_ip = __connman_ippool_get_end_ip(pool);
131 g_assert(subnet_mask);
136 LOG("Number of blocks %d", i);
138 for (it = list; it != NULL; it = it->next) {
141 __connman_ippool_unref(pool);
147 static void collision_cb(struct connman_ippool *pool, void *user_data)
149 int *flag = user_data;
151 LOG("collision detected");
153 g_assert(*flag == 0);
159 static void test_ippool_collision0(void)
161 struct connman_ippool *pool;
163 const char *broadcast;
164 const char *subnet_mask;
165 const char *start_ip;
169 /* Test the IP range collision */
172 pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
175 gateway = __connman_ippool_get_gateway(pool);
176 broadcast = __connman_ippool_get_broadcast(pool);
177 subnet_mask = __connman_ippool_get_subnet_mask(pool);
178 start_ip = __connman_ippool_get_start_ip(pool);
179 end_ip = __connman_ippool_get_end_ip(pool);
183 g_assert(subnet_mask);
187 LOG("\n\tIP range %s --> %s\n"
188 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
189 gateway, broadcast, subnet_mask);
191 __connman_ippool_newaddr(23, start_ip, 24);
195 __connman_ippool_newaddr(42, start_ip, 24);
199 __connman_ippool_unref(pool);
202 int main(int argc, char *argv[])
206 g_test_init(&argc, &argv, NULL);
208 __connman_ippool_init();
210 g_test_add_func("/basic0", test_ippool_basic0);
211 g_test_add_func("/basic1", test_ippool_basic1);
212 g_test_add_func("/basic2", test_ippool_basic2);
213 g_test_add_func("/collision0", test_ippool_collision0);
217 __connman_ippool_cleanup();