]>
Commit | Line | Data |
---|---|---|
bbe3070a TB |
1 | /* |
2 | * Copyright (C) 2014 Tobias Brunner | |
19ef2aec TB |
3 | * |
4 | * Copyright (C) secunet Security Networks AG | |
bbe3070a TB |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2 of the License, or (at your | |
9 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * for more details. | |
15 | */ | |
16 | ||
17 | #include "test_suite.h" | |
18 | ||
19 | #include <attributes/mem_pool.h> | |
20 | ||
21 | static void assert_host(char *expected, host_t *host) | |
22 | { | |
23 | if (!expected) | |
24 | { | |
b3ab7a48 | 25 | ck_assert_msg(!host, "not expecting IP != %+H", host); |
bbe3070a TB |
26 | } |
27 | else | |
28 | { | |
29 | host_t *verifier; | |
30 | verifier = host_create_from_string(expected, 0); | |
31 | ck_assert_msg(host, "expected IP %+H != NULL", verifier); | |
32 | ck_assert_msg(verifier->ip_equals(verifier, host), "expected IP %+H != " | |
6dee8587 | 33 | "%+H", verifier, host); |
bbe3070a TB |
34 | verifier->destroy(verifier); |
35 | } | |
36 | } | |
37 | ||
38 | static void assert_acquire(mem_pool_t *pool, char *requested, char *expected, | |
39 | mem_pool_op_t operation) | |
40 | { | |
41 | identification_t *id; | |
42 | host_t *req, *acquired; | |
43 | ||
44 | id = identification_create_from_string("tester"); | |
45 | req = host_create_from_string(requested, 0); | |
46 | ||
22e6a06b | 47 | acquired = pool->acquire_address(pool, id, req, operation, NULL); |
bbe3070a TB |
48 | assert_host(expected, acquired); |
49 | DESTROY_IF(acquired); | |
50 | ||
51 | req->destroy(req); | |
52 | id->destroy(id); | |
53 | } | |
54 | ||
55 | static void assert_acquires_new(mem_pool_t *pool, char *pattern, int first) | |
56 | { | |
57 | char expected[16]; | |
58 | int i; | |
59 | ||
60 | for (i = 0; i < pool->get_size(pool); i++) | |
61 | { | |
62 | snprintf(expected, sizeof(expected), pattern, first + i); | |
63 | assert_acquire(pool, "0.0.0.0", expected, MEM_POOL_NEW); | |
64 | ck_assert_int_eq(i + 1, pool->get_online(pool)); | |
65 | } | |
66 | assert_acquire(pool, "0.0.0.0", NULL, MEM_POOL_NEW); | |
67 | } | |
68 | ||
69 | START_TEST(test_config) | |
70 | { | |
71 | mem_pool_t *pool; | |
72 | ||
73 | pool = mem_pool_create("test", NULL, 0); | |
74 | ck_assert_int_eq(0, pool->get_size(pool)); | |
75 | assert_acquire(pool, "192.168.0.1", "192.168.0.1", MEM_POOL_NEW); | |
76 | assert_acquire(pool, "10.0.1.1", "10.0.1.1", MEM_POOL_NEW); | |
77 | assert_acquire(pool, "0.0.0.0", "0.0.0.0", MEM_POOL_NEW); | |
78 | assert_acquire(pool, "255.255.255.255", "255.255.255.255", MEM_POOL_NEW); | |
79 | ck_assert_int_eq(0, pool->get_online(pool)); | |
80 | pool->destroy(pool); | |
81 | } | |
82 | END_TEST | |
83 | ||
84 | START_TEST(test_cidr) | |
85 | { | |
86 | mem_pool_t *pool; | |
87 | host_t *base; | |
88 | ||
89 | base = host_create_from_string("192.168.0.0", 0); | |
90 | ||
91 | pool = mem_pool_create("test", base, 32); | |
92 | ck_assert_int_eq(1, pool->get_size(pool)); | |
93 | assert_acquires_new(pool, "192.168.0.%d", 0); | |
94 | pool->destroy(pool); | |
95 | ||
96 | pool = mem_pool_create("test", base, 31); | |
97 | ck_assert_int_eq(2, pool->get_size(pool)); | |
98 | assert_acquires_new(pool, "192.168.0.%d", 0); | |
99 | pool->destroy(pool); | |
100 | ||
101 | pool = mem_pool_create("test", base, 30); | |
102 | ck_assert_int_eq(2, pool->get_size(pool)); | |
103 | assert_acquires_new(pool, "192.168.0.%d", 1); | |
104 | pool->destroy(pool); | |
105 | ||
106 | pool = mem_pool_create("test", base, 29); | |
107 | ck_assert_int_eq(6, pool->get_size(pool)); | |
108 | assert_acquires_new(pool, "192.168.0.%d", 1); | |
109 | pool->destroy(pool); | |
110 | ||
111 | pool = mem_pool_create("test", base, 24); | |
112 | ck_assert_int_eq(254, pool->get_size(pool)); | |
113 | assert_acquires_new(pool, "192.168.0.%d", 1); | |
114 | pool->destroy(pool); | |
115 | ||
116 | base->destroy(base); | |
117 | } | |
118 | END_TEST | |
119 | ||
120 | START_TEST(test_cidr_offset) | |
121 | { | |
122 | mem_pool_t *pool; | |
123 | host_t *base; | |
124 | ||
125 | base = host_create_from_string("192.168.0.1", 0); | |
126 | pool = mem_pool_create("test", base, 31); | |
127 | ck_assert_int_eq(1, pool->get_size(pool)); | |
128 | assert_acquires_new(pool, "192.168.0.%d", 1); | |
129 | pool->destroy(pool); | |
130 | ||
131 | pool = mem_pool_create("test", base, 30); | |
132 | ck_assert_int_eq(2, pool->get_size(pool)); | |
133 | assert_acquires_new(pool, "192.168.0.%d", 1); | |
134 | pool->destroy(pool); | |
135 | base->destroy(base); | |
136 | ||
137 | base = host_create_from_string("192.168.0.2", 0); | |
138 | pool = mem_pool_create("test", base, 30); | |
139 | ck_assert_int_eq(1, pool->get_size(pool)); | |
140 | assert_acquires_new(pool, "192.168.0.%d", 2); | |
141 | pool->destroy(pool); | |
142 | ||
143 | pool = mem_pool_create("test", base, 24); | |
144 | ck_assert_int_eq(253, pool->get_size(pool)); | |
145 | assert_acquires_new(pool, "192.168.0.%d", 2); | |
146 | pool->destroy(pool); | |
147 | base->destroy(base); | |
148 | ||
149 | base = host_create_from_string("192.168.0.254", 0); | |
150 | pool = mem_pool_create("test", base, 24); | |
151 | ck_assert_int_eq(1, pool->get_size(pool)); | |
152 | assert_acquires_new(pool, "192.168.0.%d", 254); | |
153 | pool->destroy(pool); | |
154 | base->destroy(base); | |
155 | ||
5f99a283 | 156 | /* this results in an empty pool, which is rejected */ |
bbe3070a TB |
157 | base = host_create_from_string("192.168.0.255", 0); |
158 | pool = mem_pool_create("test", base, 24); | |
5f99a283 | 159 | ck_assert(!pool); |
bbe3070a TB |
160 | base->destroy(base); |
161 | } | |
162 | END_TEST | |
163 | ||
164 | START_TEST(test_range) | |
165 | { | |
166 | mem_pool_t *pool; | |
167 | host_t *from, *to; | |
168 | ||
169 | from = host_create_from_string("192.168.0.0", 0); | |
170 | to = host_create_from_string("192.168.0.0", 0); | |
171 | pool = mem_pool_create_range("test", from, to); | |
172 | ck_assert_int_eq(1, pool->get_size(pool)); | |
173 | assert_acquires_new(pool, "192.168.0.%d", 0); | |
174 | pool->destroy(pool); | |
175 | ||
176 | to->destroy(to); | |
177 | to = host_create_from_string("192.168.0.1", 0); | |
178 | pool = mem_pool_create_range("test", from, to); | |
179 | ck_assert_int_eq(2, pool->get_size(pool)); | |
180 | assert_acquires_new(pool, "192.168.0.%d", 0); | |
181 | pool->destroy(pool); | |
182 | ||
183 | from->destroy(from); | |
184 | from = host_create_from_string("192.168.0.10", 0); | |
185 | pool = mem_pool_create_range("test", from, to); | |
186 | ck_assert(!pool); | |
187 | ||
188 | to->destroy(to); | |
189 | to = host_create_from_string("192.168.0.20", 0); | |
190 | pool = mem_pool_create_range("test", from, to); | |
191 | ck_assert_int_eq(11, pool->get_size(pool)); | |
192 | assert_acquires_new(pool, "192.168.0.%d", 10); | |
193 | pool->destroy(pool); | |
194 | ||
195 | from->destroy(from); | |
196 | from = host_create_from_string("fec::1", 0); | |
197 | to->destroy(to); | |
198 | to = host_create_from_string("fed::1", 0); | |
199 | pool = mem_pool_create_range("test", from, to); | |
200 | ck_assert(!pool); | |
201 | ||
202 | from->destroy(from); | |
203 | to->destroy(to); | |
204 | } | |
205 | END_TEST | |
206 | ||
207 | Suite *mem_pool_suite_create() | |
208 | { | |
209 | Suite *s; | |
210 | TCase *tc; | |
211 | ||
212 | s = suite_create("mem_pool"); | |
213 | ||
214 | tc = tcase_create("%config-like pool"); | |
215 | tcase_add_test(tc, test_config); | |
216 | suite_add_tcase(s, tc); | |
217 | ||
218 | tc = tcase_create("cidr constructor"); | |
219 | tcase_add_test(tc, test_cidr); | |
220 | tcase_add_test(tc, test_cidr_offset); | |
221 | suite_add_tcase(s, tc); | |
222 | ||
223 | tc = tcase_create("range constructor"); | |
224 | tcase_add_test(tc, test_range); | |
225 | suite_add_tcase(s, tc); | |
226 | ||
227 | return s; | |
228 | } |