Updated connman to version 1.35
[platform/upstream/connman.git] / unit / test-ippool.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012-2014  BMW Car IT GmbH.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <glib.h>
27
28 #include "../src/connman.h"
29
30 /* #define DEBUG */
31 #ifdef DEBUG
32 #include <stdio.h>
33
34 #define LOG(fmt, arg...) do { \
35         fprintf(stdout, "%s:%s() " fmt "\n", \
36                         __FILE__, __func__ , ## arg); \
37 } while (0)
38 #else
39 #define LOG(fmt, arg...)
40 #endif
41
42 static void test_case_1(void)
43 {
44         struct connman_ippool *pool;
45         int i;
46
47         __connman_ippool_init();
48
49         pool = __connman_ippool_create(23, 1, 500, NULL, NULL);
50         g_assert(!pool);
51
52         for (i = 0; i < 100000; i++) {
53                 pool = __connman_ippool_create(23, 1, 20, NULL, NULL);
54                 g_assert(pool);
55
56                 __connman_ippool_unref(pool);
57         }
58
59         __connman_ippool_cleanup();
60 }
61
62 static void test_case_2(void)
63 {
64         struct connman_ippool *pool;
65         const char *gateway;
66         const char *broadcast;
67         const char *subnet_mask;
68         const char *start_ip;
69         const char *end_ip;
70         int i;
71
72         __connman_ippool_init();
73
74         /* Test the IP range */
75         for (i = 1; i < 254; i++) {
76                 pool = __connman_ippool_create(23, 1, i, NULL, NULL);
77                 g_assert(pool);
78
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);
84
85                 g_assert(gateway);
86                 g_assert(broadcast);
87                 g_assert(subnet_mask);
88                 g_assert(start_ip);
89                 g_assert(end_ip);
90
91                 LOG("\n\tIP range %s --> %s\n"
92                         "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
93                         gateway, broadcast, subnet_mask);
94
95                 __connman_ippool_unref(pool);
96         }
97
98         __connman_ippool_cleanup();
99 }
100
101 static void test_case_3(void)
102 {
103         struct connman_ippool *pool;
104         const char *gateway;
105         const char *broadcast;
106         const char *subnet_mask;
107         const char *start_ip;
108         const char *end_ip;
109         GSList *list = NULL, *it;
110         int i = 0;
111
112         __connman_ippool_init();
113
114         /*
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
119          *
120          * Total                                                17,891,328
121          *
122          * Total numbers of 256 blocks:                             69,888
123          */
124
125         /*
126          * Completely exhaust the first two pools because they are a bit
127          * too large.
128          */
129         __connman_ippool_newaddr(45, "10.0.0.1", 8);
130         __connman_ippool_newaddr(46, "172.16.0.1", 11);
131
132         while (TRUE) {
133                 pool = __connman_ippool_create(23, 1, 100, NULL, NULL);
134                 if (!pool)
135                         break;
136                 i += 1;
137
138                 list = g_slist_prepend(list, pool);
139
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);
145
146                 g_assert(gateway);
147                 g_assert(broadcast);
148                 g_assert(subnet_mask);
149                 g_assert(start_ip);
150                 g_assert(end_ip);
151         }
152
153         g_assert(i == 255);
154
155         LOG("Number of blocks %d", i);
156
157         for (it = list; it; it = it->next) {
158                 pool = it->data;
159
160                 __connman_ippool_unref(pool);
161         }
162
163         g_slist_free(list);
164
165         __connman_ippool_cleanup();
166 }
167
168 static void collision_cb(struct connman_ippool *pool, void *user_data)
169 {
170         int *flag = user_data;
171
172         LOG("collision detected");
173
174         g_assert(*flag == 0);
175         g_assert(pool);
176
177         *flag = 1;
178 }
179
180 static void test_case_4(void)
181 {
182         struct connman_ippool *pool;
183         const char *gateway;
184         const char *broadcast;
185         const char *subnet_mask;
186         const char *start_ip;
187         const char *end_ip;
188         int flag;
189
190         __connman_ippool_init();
191
192         /* Test the IP range collision */
193
194         flag = 0;
195         pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
196         g_assert(pool);
197
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);
203
204         g_assert(gateway);
205         g_assert(broadcast);
206         g_assert(subnet_mask);
207         g_assert(start_ip);
208         g_assert(end_ip);
209
210         LOG("\n\tIP range %s --> %s\n"
211                 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
212                 gateway, broadcast, subnet_mask);
213
214         __connman_ippool_newaddr(23, start_ip, 24);
215
216         g_assert(flag == 0);
217
218         __connman_ippool_newaddr(42, start_ip, 16);
219
220         g_assert(flag == 1);
221
222         __connman_ippool_unref(pool);
223
224         flag = 0;
225
226         pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
227         g_assert(pool);
228
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);
234
235         g_assert(gateway);
236         g_assert(broadcast);
237         g_assert(subnet_mask);
238         g_assert(start_ip);
239         g_assert(end_ip);
240
241         LOG("\n\tIP range %s --> %s\n"
242                 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
243                 gateway, broadcast, subnet_mask);
244
245         __connman_ippool_newaddr(45, start_ip, 22);
246
247         g_assert(flag == 1);
248
249         __connman_ippool_unref(pool);
250
251         __connman_ippool_cleanup();
252 }
253
254 static void test_case_5(void)
255 {
256         struct connman_ippool *pool1, *pool2;
257         const char *gateway;
258         const char *broadcast;
259         const char *subnet_mask;
260         const char *start_ip;
261         const char *end_ip;
262         int flag;
263
264         __connman_ippool_init();
265
266         /* Test the IP range collision */
267
268         flag = 0;
269         start_ip = "192.168.1.2";
270         __connman_ippool_newaddr(25, start_ip, 24);
271         g_assert(flag == 0);
272
273         /* pool should return 192.168.0.1 now */
274         pool1 = __connman_ippool_create(26, 1, 100, collision_cb, &flag);
275         g_assert(pool1);
276
277         gateway = __connman_ippool_get_gateway(pool1);
278         broadcast = __connman_ippool_get_broadcast(pool1);
279         subnet_mask = __connman_ippool_get_subnet_mask(pool1);
280         start_ip = __connman_ippool_get_start_ip(pool1);
281         end_ip = __connman_ippool_get_end_ip(pool1);
282
283         g_assert(gateway);
284         g_assert(broadcast);
285         g_assert(subnet_mask);
286         g_assert(start_ip);
287         g_assert(end_ip);
288
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");
294
295         LOG("\n\tIP range %s --> %s\n"
296                 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
297                 gateway, broadcast, subnet_mask);
298
299         /*
300          * Now create the pool again, we should not get collision
301          * with existing allocated address.
302          */
303
304         /* pool should return 192.168.2.1 now */
305         flag = 0;
306         pool2 = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
307         g_assert(pool2);
308
309         gateway = __connman_ippool_get_gateway(pool2);
310         broadcast = __connman_ippool_get_broadcast(pool2);
311         subnet_mask = __connman_ippool_get_subnet_mask(pool2);
312         start_ip = __connman_ippool_get_start_ip(pool2);
313         end_ip = __connman_ippool_get_end_ip(pool2);
314
315         g_assert(gateway);
316         g_assert(broadcast);
317         g_assert(subnet_mask);
318         g_assert(start_ip);
319         g_assert(end_ip);
320
321         g_assert_cmpstr(gateway, ==, "192.168.2.1");
322         g_assert_cmpstr(broadcast, ==, "192.168.2.255");
323         g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
324         g_assert_cmpstr(start_ip, ==, "192.168.2.1");
325         g_assert_cmpstr(end_ip, ==, "192.168.2.101");
326
327         LOG("\n\tIP range %s --> %s\n"
328                 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
329                 gateway, broadcast, subnet_mask);
330
331         g_assert(flag == 0);
332
333         __connman_ippool_unref(pool1);
334         __connman_ippool_unref(pool2);
335
336         __connman_ippool_cleanup();
337 }
338
339 static void test_case_6(void)
340 {
341         struct connman_ippool *pool1, *pool2;
342         const char *gateway;
343         const char *broadcast;
344         const char *subnet_mask;
345         const char *start_ip;
346         const char *end_ip;
347         int flag;
348
349         __connman_ippool_init();
350
351         /* Test the IP range collision */
352
353         flag = 0;
354         start_ip = "192.168.1.2";
355         __connman_ippool_newaddr(25, start_ip, 24);
356         g_assert(flag == 0);
357
358         flag = 0;
359         start_ip = "192.168.0.2";
360         __connman_ippool_newaddr(25, start_ip, 24);
361         g_assert(flag == 0);
362
363         /* pool should return 192.168.2.1 now */
364         pool1 = __connman_ippool_create(26, 1, 100, collision_cb, &flag);
365         g_assert(pool1);
366
367         gateway = __connman_ippool_get_gateway(pool1);
368         broadcast = __connman_ippool_get_broadcast(pool1);
369         subnet_mask = __connman_ippool_get_subnet_mask(pool1);
370         start_ip = __connman_ippool_get_start_ip(pool1);
371         end_ip = __connman_ippool_get_end_ip(pool1);
372
373         g_assert(gateway);
374         g_assert(broadcast);
375         g_assert(subnet_mask);
376         g_assert(start_ip);
377         g_assert(end_ip);
378
379         g_assert_cmpstr(gateway, ==, "192.168.2.1");
380         g_assert_cmpstr(broadcast, ==, "192.168.2.255");
381         g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
382         g_assert_cmpstr(start_ip, ==, "192.168.2.1");
383         g_assert_cmpstr(end_ip, ==, "192.168.2.101");
384
385         LOG("\n\tIP range %s --> %s\n"
386                 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
387                 gateway, broadcast, subnet_mask);
388
389         /*
390          * Now create the pool again, we should not get collision
391          * with existing allocated address.
392          */
393
394         /* pool should return 192.168.3.1 now */
395         flag = 0;
396         pool2 = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
397         g_assert(pool2);
398
399         gateway = __connman_ippool_get_gateway(pool2);
400         broadcast = __connman_ippool_get_broadcast(pool2);
401         subnet_mask = __connman_ippool_get_subnet_mask(pool2);
402         start_ip = __connman_ippool_get_start_ip(pool2);
403         end_ip = __connman_ippool_get_end_ip(pool2);
404
405         g_assert(gateway);
406         g_assert(broadcast);
407         g_assert(subnet_mask);
408         g_assert(start_ip);
409         g_assert(end_ip);
410
411         g_assert_cmpstr(gateway, ==, "192.168.3.1");
412         g_assert_cmpstr(broadcast, ==, "192.168.3.255");
413         g_assert_cmpstr(subnet_mask, ==, "255.255.255.0");
414         g_assert_cmpstr(start_ip, ==, "192.168.3.1");
415         g_assert_cmpstr(end_ip, ==, "192.168.3.101");
416
417         LOG("\n\tIP range %s --> %s\n"
418                 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
419                 gateway, broadcast, subnet_mask);
420
421         g_assert(flag == 0);
422
423         flag = 0;
424         start_ip = "192.168.3.2";
425         __connman_ippool_newaddr(25, start_ip, 24);
426         g_assert(flag == 1);
427
428         __connman_ippool_unref(pool1);
429         __connman_ippool_unref(pool2);
430
431         __connman_ippool_cleanup();
432 }
433
434 int main(int argc, char *argv[])
435 {
436         g_test_init(&argc, &argv, NULL);
437
438         g_test_add_func("/ippool/Test case 1", test_case_1);
439         g_test_add_func("/ippool/Test case 2", test_case_2);
440         g_test_add_func("/ippool/Test case 3", test_case_3);
441         g_test_add_func("/ippool/Test case 4", test_case_4);
442         g_test_add_func("/ippool/Test case 5", test_case_5);
443         g_test_add_func("/ippool/Test case 6", test_case_6);
444
445         return g_test_run();
446 }