dnsproxy: Only one copy of the relevant buffers will be made to a TCP request
[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         int i;
46
47         __connman_ippool_init();
48
49         pool = __connman_ippool_create(23, 1, 500, NULL, NULL);
50         g_assert(pool == NULL);
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_ippool_basic1(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_ippool_exhaust0(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         /* Allocate all possible pools */
115
116         /*
117          *                                             Number of addresses
118          * 24-bit block         10.0.0.0    – 10.255.255.255    16,777,216
119          * 20-bit block         172.16.0.0  – 172.31.255.255     1,048,576
120          * 16-bit block         192.168.0.0 – 192.168.255.255       65,536
121          *
122          * Total                                                17,891,328
123          *
124          * Total numbers of 256 blocks:                             69,888
125          */
126
127         while (TRUE) {
128                 pool = __connman_ippool_create(23, 1, 100, NULL, NULL);
129                 if (pool == NULL)
130                         break;
131                 i += 1;
132                 g_assert(i < 69888);
133
134                 list = g_slist_prepend(list, pool);
135
136                 gateway = __connman_ippool_get_gateway(pool);
137                 broadcast = __connman_ippool_get_broadcast(pool);
138                 subnet_mask = __connman_ippool_get_subnet_mask(pool);
139                 start_ip = __connman_ippool_get_start_ip(pool);
140                 end_ip = __connman_ippool_get_end_ip(pool);
141
142                 g_assert(gateway);
143                 g_assert(broadcast);
144                 g_assert(subnet_mask);
145                 g_assert(start_ip);
146                 g_assert(end_ip);
147         }
148
149         LOG("Number of blocks %d", i);
150
151         for (it = list; it != NULL; it = it->next) {
152                 pool = it->data;
153
154                 __connman_ippool_unref(pool);
155         }
156
157         g_slist_free(list);
158
159         __connman_ippool_cleanup();
160 }
161
162 static void collision_cb(struct connman_ippool *pool, void *user_data)
163 {
164         int *flag = user_data;
165
166         LOG("collision detected");
167
168         g_assert(*flag == 0);
169         g_assert(pool);
170
171         *flag = 1;
172 }
173
174 static void test_ippool_collision0(void)
175 {
176         struct connman_ippool *pool;
177         const char *gateway;
178         const char *broadcast;
179         const char *subnet_mask;
180         const char *start_ip;
181         const char *end_ip;
182         int flag;
183
184         __connman_ippool_init();
185
186         /* Test the IP range collision */
187
188         flag = 0;
189         pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
190         g_assert(pool);
191
192         gateway = __connman_ippool_get_gateway(pool);
193         broadcast = __connman_ippool_get_broadcast(pool);
194         subnet_mask = __connman_ippool_get_subnet_mask(pool);
195         start_ip = __connman_ippool_get_start_ip(pool);
196         end_ip = __connman_ippool_get_end_ip(pool);
197
198         g_assert(gateway);
199         g_assert(broadcast);
200         g_assert(subnet_mask);
201         g_assert(start_ip);
202         g_assert(end_ip);
203
204         LOG("\n\tIP range %s --> %s\n"
205                 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
206                 gateway, broadcast, subnet_mask);
207
208         __connman_ippool_newaddr(23, start_ip, 24);
209
210         g_assert(flag == 0);
211
212         __connman_ippool_newaddr(42, start_ip, 16);
213
214         g_assert(flag == 1);
215
216         __connman_ippool_unref(pool);
217
218         flag = 0;
219
220         pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
221         g_assert(pool);
222
223         gateway = __connman_ippool_get_gateway(pool);
224         broadcast = __connman_ippool_get_broadcast(pool);
225         subnet_mask = __connman_ippool_get_subnet_mask(pool);
226         start_ip = __connman_ippool_get_start_ip(pool);
227         end_ip = __connman_ippool_get_end_ip(pool);
228
229         g_assert(gateway);
230         g_assert(broadcast);
231         g_assert(subnet_mask);
232         g_assert(start_ip);
233         g_assert(end_ip);
234
235         LOG("\n\tIP range %s --> %s\n"
236                 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
237                 gateway, broadcast, subnet_mask);
238
239         __connman_ippool_newaddr(45, start_ip, 22);
240
241         g_assert(flag == 1);
242
243         __connman_ippool_unref(pool);
244
245         __connman_ippool_cleanup();
246 }
247
248 int main(int argc, char *argv[])
249 {
250         g_test_init(&argc, &argv, NULL);
251
252         g_test_add_func("/basic0", test_ippool_basic0);
253         g_test_add_func("/basic1", test_ippool_basic1);
254         g_test_add_func("/exhaust0", test_ippool_exhaust0);
255         g_test_add_func("/collision0", test_ippool_collision0);
256
257         return g_test_run();
258 }