test-ippool: Add collision unit test
[framework/connectivity/connman.git] / unit / test-ippool.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  BWM CarIT GmbH. All rights reserved.
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_ippool_basic0(void)
43 {
44         struct connman_ippool *pool;
45         const char *gateway;
46         const char *broadcast;
47         const char *subnet_mask;
48         const char *start_ip;
49         const char *end_ip;
50         int i;
51
52         /* Test the IP range */
53
54         pool = __connman_ippool_create(23, 1, 500, NULL, NULL);
55         g_assert(pool == NULL);
56
57         for (i = 1; i < 254; i++) {
58                 pool = __connman_ippool_create(23, 1, i, NULL, NULL);
59                 g_assert(pool);
60
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);
66
67                 g_assert(gateway);
68                 g_assert(broadcast);
69                 g_assert(subnet_mask);
70                 g_assert(start_ip);
71                 g_assert(end_ip);
72
73                 LOG("\n\tIP range %s --> %s\n"
74                         "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
75                         gateway, broadcast, subnet_mask);
76
77                 __connman_ippool_unref(pool);
78         }
79 }
80
81 static void test_ippool_basic1(void)
82 {
83         struct connman_ippool *pool;
84         const char *gateway;
85         const char *broadcast;
86         const char *subnet_mask;
87         const char *start_ip;
88         const char *end_ip;
89         GSList *list = NULL, *it;
90         int i = 0;
91
92         /* Allocate all possible pools */
93
94         /*
95          *                                             Number of addresses
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
99          *
100          * Total                                                17,891,328
101          *
102          * Total numbers of 256 blocks:                             69,888
103          */
104
105         while (TRUE) {
106                 pool = __connman_ippool_create(23, 1, 100, NULL, NULL);
107                 if (pool == NULL)
108                         break;
109                 i += 1;
110                 g_assert(i < 69888);
111
112                 list = g_slist_prepend(list, pool);
113
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);
119
120                 g_assert(gateway);
121                 g_assert(broadcast);
122                 g_assert(subnet_mask);
123                 g_assert(start_ip);
124                 g_assert(end_ip);
125         }
126
127         LOG("Number of blocks %d", i);
128
129         for (it = list; it != NULL; it = it->next) {
130                 pool = it->data;
131
132                 __connman_ippool_unref(pool);
133         }
134
135         g_slist_free(list);
136 }
137
138 static void collision_cb(struct connman_ippool *pool, void *user_data)
139 {
140         int *flag = user_data;
141
142         LOG("collision detected");
143
144         g_assert(*flag == 0);
145         g_assert(pool);
146
147         *flag = 1;
148 }
149
150 static void test_ippool_collision0(void)
151 {
152         struct connman_ippool *pool;
153         const char *gateway;
154         const char *broadcast;
155         const char *subnet_mask;
156         const char *start_ip;
157         const char *end_ip;
158         int flag;
159
160         /* Test the IP range collision */
161
162         flag = 0;
163         pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
164         g_assert(pool);
165
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);
171
172         g_assert(gateway);
173         g_assert(broadcast);
174         g_assert(subnet_mask);
175         g_assert(start_ip);
176         g_assert(end_ip);
177
178         LOG("\n\tIP range %s --> %s\n"
179                 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
180                 gateway, broadcast, subnet_mask);
181
182         __connman_ippool_newaddr(23, start_ip);
183
184         g_assert(flag == 0);
185
186         __connman_ippool_newaddr(42, end_ip);
187
188         g_assert(flag == 1);
189
190         __connman_ippool_unref(pool);
191 }
192
193 int main(int argc, char *argv[])
194 {
195         int err;
196
197         g_test_init(&argc, &argv, NULL);
198
199         __connman_ippool_init();
200
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);
204
205         err = g_test_run();
206
207         __connman_ippool_cleanup();
208
209         return err;
210 }