test-ippool: Split basic0 test
[platform/upstream/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         for (i = 0; i < 100000; i++) {
48                 pool = __connman_ippool_create(23, 1, 20, NULL, NULL);
49                 g_assert(pool);
50
51                 __connman_ippool_unref(pool);
52         }
53 }
54
55 static void test_ippool_basic1(void)
56 {
57         struct connman_ippool *pool;
58         const char *gateway;
59         const char *broadcast;
60         const char *subnet_mask;
61         const char *start_ip;
62         const char *end_ip;
63         int i;
64
65         /* Test the IP range */
66         for (i = 1; i < 254; i++) {
67                 pool = __connman_ippool_create(23, 1, i, NULL, NULL);
68                 g_assert(pool);
69
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);
75
76                 g_assert(gateway);
77                 g_assert(broadcast);
78                 g_assert(subnet_mask);
79                 g_assert(start_ip);
80                 g_assert(end_ip);
81
82                 LOG("\n\tIP range %s --> %s\n"
83                         "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
84                         gateway, broadcast, subnet_mask);
85
86                 __connman_ippool_unref(pool);
87         }
88 }
89
90 static void test_ippool_basic2(void)
91 {
92         struct connman_ippool *pool;
93         const char *gateway;
94         const char *broadcast;
95         const char *subnet_mask;
96         const char *start_ip;
97         const char *end_ip;
98         GSList *list = NULL, *it;
99         int i = 0;
100
101         /* Allocate all possible pools */
102
103         /*
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
108          *
109          * Total                                                17,891,328
110          *
111          * Total numbers of 256 blocks:                             69,888
112          */
113
114         while (TRUE) {
115                 pool = __connman_ippool_create(23, 1, 100, NULL, NULL);
116                 if (pool == NULL)
117                         break;
118                 i += 1;
119                 g_assert(i < 69888);
120
121                 list = g_slist_prepend(list, pool);
122
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);
128
129                 g_assert(gateway);
130                 g_assert(broadcast);
131                 g_assert(subnet_mask);
132                 g_assert(start_ip);
133                 g_assert(end_ip);
134         }
135
136         LOG("Number of blocks %d", i);
137
138         for (it = list; it != NULL; it = it->next) {
139                 pool = it->data;
140
141                 __connman_ippool_unref(pool);
142         }
143
144         g_slist_free(list);
145 }
146
147 static void collision_cb(struct connman_ippool *pool, void *user_data)
148 {
149         int *flag = user_data;
150
151         LOG("collision detected");
152
153         g_assert(*flag == 0);
154         g_assert(pool);
155
156         *flag = 1;
157 }
158
159 static void test_ippool_collision0(void)
160 {
161         struct connman_ippool *pool;
162         const char *gateway;
163         const char *broadcast;
164         const char *subnet_mask;
165         const char *start_ip;
166         const char *end_ip;
167         int flag;
168
169         /* Test the IP range collision */
170
171         flag = 0;
172         pool = __connman_ippool_create(23, 1, 100, collision_cb, &flag);
173         g_assert(pool);
174
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);
180
181         g_assert(gateway);
182         g_assert(broadcast);
183         g_assert(subnet_mask);
184         g_assert(start_ip);
185         g_assert(end_ip);
186
187         LOG("\n\tIP range %s --> %s\n"
188                 "\tgateway %s broadcast %s mask %s", start_ip, end_ip,
189                 gateway, broadcast, subnet_mask);
190
191         __connman_ippool_newaddr(23, start_ip, 24);
192
193         g_assert(flag == 0);
194
195         __connman_ippool_newaddr(42, start_ip, 24);
196
197         g_assert(flag == 1);
198
199         __connman_ippool_unref(pool);
200 }
201
202 int main(int argc, char *argv[])
203 {
204         int err;
205
206         g_test_init(&argc, &argv, NULL);
207
208         __connman_ippool_init();
209
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);
214
215         err = g_test_run();
216
217         __connman_ippool_cleanup();
218
219         return err;
220 }