test-ippool: Add unit test for ippool
[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(1, 500);
55         g_assert(pool == NULL);
56
57         for (i = 1; i < 254; i++) {
58                 pool = __connman_ippool_create(1, i);
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(1, 100);
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 int main(int argc, char *argv[])
139 {
140         int err;
141
142         g_test_init(&argc, &argv, NULL);
143
144         __connman_ippool_init();
145
146         g_test_add_func("/basic0", test_ippool_basic0);
147         g_test_add_func("/basic1", test_ippool_basic1);
148
149         err = g_test_run();
150
151         __connman_ippool_cleanup();
152
153         return err;
154 }