unit-tests: Move test suites to its own subfolder
[strongswan.git] / src / libstrongswan / tests / suites / test_host.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "test_suite.h"
17
18 #include <networking/host.h>
19
20 /**
21 * Verify a netmask (a number of set bits starting at byte 0)
22 * Can also be used to check for %any (mask == 0)
23 */
24 static void verify_netmask(chunk_t addr, int mask)
25 {
26 int byte, bit;
27
28 for (byte = 0; byte < addr.len; byte++)
29 {
30 for (bit = 7; bit >= 0; bit--)
31 {
32 int val = (addr.ptr[byte] >> bit) & 0x01;
33 if (mask-- > 0)
34 {
35 ck_assert_int_eq(val, 1);
36 }
37 else
38 {
39 ck_assert_int_eq(val, 0);
40 }
41 }
42 }
43 }
44
45 /*******************************************************************************
46 * host_create_any
47 */
48
49 static void verify_any(host_t *host, int family, u_int16_t port)
50 {
51 verify_netmask(host->get_address(host), 0);
52 ck_assert(host->is_anyaddr(host));
53 ck_assert_int_eq(host->get_port(host), port);
54 ck_assert_int_eq(host->get_family(host), family);
55 }
56
57 static void test_create_any(int family)
58 {
59 host_t *host;
60
61 host = host_create_any(family);
62 verify_any(host, family, 0);
63 host->destroy(host);
64 }
65
66 START_TEST(test_create_any_v4)
67 {
68 test_create_any(AF_INET);
69 }
70 END_TEST
71
72 START_TEST(test_create_any_v6)
73 {
74 test_create_any(AF_INET6);
75 }
76 END_TEST
77
78 START_TEST(test_create_any_other)
79 {
80 host_t *host;
81
82 host = host_create_any(AF_UNSPEC);
83 ck_assert(host == NULL);
84 }
85 END_TEST
86
87 /*******************************************************************************
88 * host_create_from_string
89 */
90
91 static void verify_address(host_t *host, chunk_t addr, int family, u_int16_t port)
92 {
93 ck_assert(chunk_equals(host->get_address(host), addr));
94 ck_assert(!host->is_anyaddr(host));
95 ck_assert_int_eq(host->get_port(host), port);
96 ck_assert_int_eq(host->get_family(host), family);
97 }
98
99 static const chunk_t addr_v4 = chunk_from_chars(0xc0, 0xa8, 0x00, 0x01);
100 static const chunk_t addr_v6 = chunk_from_chars(0xfe, 0xc1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01);
102
103 START_TEST(test_create_from_string_v4)
104 {
105 host_t *host;
106
107 host = host_create_from_string("%any", 500);
108 verify_any(host, AF_INET, 500);
109 host->destroy(host);
110
111 host = host_create_from_string("%any4", 500);
112 verify_any(host, AF_INET, 500);
113 host->destroy(host);
114
115 host = host_create_from_string("0.0.0.0", 500);
116 verify_any(host, AF_INET, 500);
117 host->destroy(host);
118
119 host = host_create_from_string("192.168.0.1", 500);
120 verify_address(host, addr_v4, AF_INET, 500);
121 host->destroy(host);
122
123 host = host_create_from_string("192.168.0.1::500", 500);
124 ck_assert(host == NULL);
125 host = host_create_from_string("123.456.789.012", 500);
126 ck_assert(host == NULL);
127 host = host_create_from_string("1.1.1.1.1.1.1.1", 500);
128 ck_assert(host == NULL);
129 host = host_create_from_string("foo.b.a.r", 500);
130 ck_assert(host == NULL);
131 }
132 END_TEST
133
134 START_TEST(test_create_from_string_any_v6)
135 {
136 host_t *host;
137
138 host = host_create_from_string("%any6", 500);
139 verify_any(host, AF_INET6, 500);
140 host->destroy(host);
141
142 host = host_create_from_string("::", 500);
143 verify_any(host, AF_INET6, 500);
144 host->destroy(host);
145
146 host = host_create_from_string("fec1::1", 500);
147 verify_address(host, addr_v6, AF_INET6, 500);
148 host->destroy(host);
149
150 host = host_create_from_string("fec1::1.500", 500);
151 ck_assert(host == NULL);
152 host = host_create_from_string("f::e::c::1::1", 500);
153 ck_assert(host == NULL);
154 host = host_create_from_string("foo::bar", 500);
155 ck_assert(host == NULL);
156 }
157 END_TEST
158
159 /*******************************************************************************
160 * host_create_from_string_and_family
161 */
162
163 static void test_create_from_string_and_family_any(char *string, int family,
164 int expected)
165 {
166 host_t *host;
167
168 host = host_create_from_string_and_family(string, family, 500);
169 if (expected == AF_UNSPEC)
170 {
171 ck_assert(host == NULL);
172 }
173 else
174 {
175 verify_any(host, expected, 500);
176 host->destroy(host);
177 }
178 }
179
180 static void test_create_from_string_and_family_addr(char *string, chunk_t addr,
181 int family, int expected)
182 {
183 host_t *host;
184
185 host = host_create_from_string_and_family(string, family, 500);
186 if (expected == AF_UNSPEC)
187 {
188 ck_assert(host == NULL);
189 }
190 else
191 {
192 verify_address(host, addr, expected, 500);
193 host->destroy(host);
194 }
195 }
196
197 START_TEST(test_create_from_string_and_family_v4)
198 {
199 test_create_from_string_and_family_any("%any", AF_INET, AF_INET);
200 test_create_from_string_and_family_any("%any4", AF_INET, AF_INET);
201 test_create_from_string_and_family_any("0.0.0.0", AF_INET, AF_INET);
202
203 test_create_from_string_and_family_any("%any4", AF_INET6, AF_UNSPEC);
204 test_create_from_string_and_family_any("0.0.0.0", AF_INET6, AF_UNSPEC);
205
206 test_create_from_string_and_family_addr("192.168.0.1", addr_v4, AF_INET, AF_INET);
207 test_create_from_string_and_family_addr("192.168.0.1", addr_v4, AF_INET6, AF_UNSPEC);
208 }
209 END_TEST
210
211 START_TEST(test_create_from_string_and_family_v6)
212 {
213 test_create_from_string_and_family_any("%any", AF_INET6, AF_INET6);
214 test_create_from_string_and_family_any("%any6", AF_INET6, AF_INET6);
215 test_create_from_string_and_family_any("::", AF_INET6, AF_INET6);
216
217 test_create_from_string_and_family_any("%any6", AF_INET, AF_UNSPEC);
218 test_create_from_string_and_family_any("::", AF_INET, AF_UNSPEC);
219
220 test_create_from_string_and_family_addr("fec1::1", addr_v6, AF_INET6, AF_INET6);
221 test_create_from_string_and_family_addr("fec1::1", addr_v6, AF_INET, AF_UNSPEC);
222 }
223 END_TEST
224
225 START_TEST(test_create_from_string_and_family_other)
226 {
227 test_create_from_string_and_family_any("%any", AF_UNSPEC, AF_INET);
228 test_create_from_string_and_family_any("%any4", AF_UNSPEC, AF_INET);
229 test_create_from_string_and_family_any("0.0.0.0", AF_UNSPEC, AF_INET);
230
231 test_create_from_string_and_family_any("%any6", AF_UNSPEC, AF_INET6);
232 test_create_from_string_and_family_any("::", AF_UNSPEC, AF_INET6);
233
234 test_create_from_string_and_family_addr("192.168.0.1", addr_v4, AF_UNSPEC, AF_INET);
235 test_create_from_string_and_family_addr("fec1::1", addr_v6, AF_UNSPEC, AF_INET6);
236 }
237 END_TEST
238
239 /*******************************************************************************
240 * host_create_from_sockaddr
241 */
242
243 START_TEST(test_create_from_sockaddr_v4)
244 {
245 struct sockaddr_in addr = {
246 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
247 .sin_len = sizeof(struct sockaddr_in),
248 #endif
249 .sin_family = AF_INET,
250 .sin_port = htons(500),
251 }, *val;
252 socklen_t *socklen;
253 host_t *host;
254
255 host = host_create_from_sockaddr((sockaddr_t*)&addr);
256 verify_any(host, AF_INET, 500);
257 val = (struct sockaddr_in*)host->get_sockaddr(host);
258 ck_assert(memeq(&addr, val, sizeof(addr)));
259 socklen = host->get_sockaddr_len(host);
260 ck_assert(*socklen == sizeof(addr));
261 host->destroy(host);
262 }
263 END_TEST
264
265 START_TEST(test_create_from_sockaddr_v6)
266 {
267 struct sockaddr_in6 addr = {
268 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
269 .sin6_len = sizeof(struct sockaddr_in6),
270 #endif
271 .sin6_family = AF_INET6,
272 .sin6_port = htons(500),
273 }, *val;
274 socklen_t *socklen;
275 host_t *host;
276
277 host = host_create_from_sockaddr((sockaddr_t*)&addr);
278 verify_any(host, AF_INET6, 500);
279 val = (struct sockaddr_in6*)host->get_sockaddr(host);
280 ck_assert(memeq(&addr, val, sizeof(addr)));
281 socklen = host->get_sockaddr_len(host);
282 ck_assert(*socklen == sizeof(addr));
283 host->destroy(host);
284 }
285 END_TEST
286
287 START_TEST(test_create_from_sockaddr_other)
288 {
289 struct sockaddr_un addr = {
290 .sun_family = AF_UNIX,
291 };
292 host_t *host;
293
294 host = host_create_from_sockaddr((sockaddr_t*)&addr);
295 ck_assert(host == NULL);
296 }
297 END_TEST
298
299 /*******************************************************************************
300 * host_create_from_chunk
301 */
302
303 START_TEST(test_create_from_chunk_v4)
304 {
305 host_t *host;
306
307 host = host_create_from_chunk(AF_INET, addr_v4, 500);
308 verify_address(host, addr_v4, AF_INET, 500);
309 host->destroy(host);
310
311 host = host_create_from_chunk(AF_UNSPEC, addr_v4, 500);
312 verify_address(host, addr_v4, AF_INET, 500);
313 host->destroy(host);
314
315 host = host_create_from_chunk(AF_INET, chunk_empty, 500);
316 ck_assert(host == NULL);
317 host = host_create_from_chunk(AF_UNSPEC, chunk_empty, 500);
318 ck_assert(host == NULL);
319 }
320 END_TEST
321
322 START_TEST(test_create_from_chunk_v6)
323 {
324 host_t *host;
325
326 host = host_create_from_chunk(AF_INET6, addr_v6, 500);
327 verify_address(host, addr_v6, AF_INET6, 500);
328 host->destroy(host);
329
330 host = host_create_from_chunk(AF_UNSPEC, addr_v6, 500);
331 verify_address(host, addr_v6, AF_INET6, 500);
332 host->destroy(host);
333
334 host = host_create_from_chunk(AF_INET6, chunk_empty, 500);
335 ck_assert(host == NULL);
336 }
337 END_TEST
338
339 START_TEST(test_create_from_chunk_other)
340 {
341 host_t *host;
342
343 host = host_create_from_chunk(AF_UNIX, addr_v6, 500);
344 ck_assert(host == NULL);
345 }
346 END_TEST
347
348 /*******************************************************************************
349 * host_create_from_subnet
350 */
351
352 START_TEST(test_create_from_subnet_v4)
353 {
354 host_t *host;
355 int bits = -1;
356
357 host = host_create_from_subnet("0.0.0.0/0", &bits);
358 verify_any(host, AF_INET, 0);
359 ck_assert_int_eq(bits, 0);
360 host->destroy(host);
361
362 host = host_create_from_subnet("192.168.0.1", &bits);
363 verify_address(host, addr_v4, AF_INET, 0);
364 ck_assert_int_eq(bits, 32);
365 host->destroy(host);
366
367 host = host_create_from_subnet("192.168.0.1/24", &bits);
368 verify_address(host, addr_v4, AF_INET, 0);
369 ck_assert_int_eq(bits, 24);
370 host->destroy(host);
371
372 host = host_create_from_subnet("foo.b.a.r", &bits);
373 ck_assert(host == NULL);
374 }
375 END_TEST
376
377 START_TEST(test_create_from_subnet_v6)
378 {
379 host_t *host;
380 int bits = -1;
381
382 host = host_create_from_subnet("::/0", &bits);
383 verify_any(host, AF_INET6, 0);
384 ck_assert_int_eq(bits, 0);
385 host->destroy(host);
386
387 host = host_create_from_subnet("fec1::1", &bits);
388 verify_address(host, addr_v6, AF_INET6, 0);
389 ck_assert_int_eq(bits, 128);
390 host->destroy(host);
391
392 host = host_create_from_subnet("fec1::1/64", &bits);
393 verify_address(host, addr_v6, AF_INET6, 0);
394 ck_assert_int_eq(bits, 64);
395 host->destroy(host);
396
397 host = host_create_from_subnet("foo::bar", &bits);
398 ck_assert(host == NULL);
399 }
400 END_TEST
401
402 /*******************************************************************************
403 * host_create_netmask
404 */
405
406 static void test_create_netmask(int family)
407 {
408 host_t *netmask;
409 int i, len = (family == AF_INET) ? 32 : 128;
410
411 netmask = host_create_netmask(family, -1);
412 ck_assert(netmask == NULL);
413 for (i = 0; i <= len; i++)
414 {
415 netmask = host_create_netmask(family, i);
416 verify_netmask(netmask->get_address(netmask), i);
417 netmask->destroy(netmask);
418 }
419 netmask = host_create_netmask(family, len + 1);
420 ck_assert(netmask == NULL);
421 }
422
423 START_TEST(test_create_netmask_v4)
424 {
425 test_create_netmask(AF_INET);
426 }
427 END_TEST
428
429 START_TEST(test_create_netmask_v6)
430 {
431 test_create_netmask(AF_INET6);
432 }
433 END_TEST
434
435 START_TEST(test_create_netmask_other)
436 {
437 host_t *netmask;
438
439 netmask = host_create_netmask(AF_UNSPEC, 0);
440 ck_assert(netmask == NULL);
441 }
442 END_TEST
443
444 /*******************************************************************************
445 * equals, ip_equals
446 */
447
448 START_TEST(test_equals)
449 {
450 host_t *a, *b;
451
452 a = host_create_from_string("192.168.0.1", 500);
453 b = host_create_from_string("192.168.0.1", 0);
454 ck_assert(!a->equals(a, b));
455 ck_assert(!b->equals(b, a));
456 ck_assert(a->ip_equals(a, b));
457 ck_assert(b->ip_equals(b, a));
458 b->set_port(b, 500);
459 ck_assert(a->equals(a, b));
460 ck_assert(b->equals(b, a));
461 ck_assert(a->ip_equals(a, b));
462 ck_assert(b->ip_equals(b, a));
463 b->destroy(b);
464 b = host_create_from_string("192.168.0.2", 500);
465 ck_assert(!a->ip_equals(a, b));
466 ck_assert(!a->equals(a, b));
467 b->destroy(b);
468
469 b = host_create_from_string("fec1::1", 500);
470 ck_assert(!a->ip_equals(a, b));
471 ck_assert(!a->equals(a, b));
472 a->destroy(a);
473 a = host_create_from_string("fec1::1", 500);
474 ck_assert(a->equals(a, b));
475 ck_assert(a->ip_equals(a, b));
476 a->destroy(a);
477 b->destroy(b);
478 }
479 END_TEST
480
481 START_TEST(test_equals_any)
482 {
483 host_t *a, *b;
484
485 a = host_create_from_string("%any", 500);
486 b = host_create_from_string("%any", 0);
487 ck_assert(!a->equals(a, b));
488 ck_assert(a->ip_equals(a, b));
489 b->set_port(b, 500);
490 ck_assert(a->equals(a, b));
491 ck_assert(a->ip_equals(a, b));
492 b->destroy(b);
493 b = host_create_from_string("%any6", 0);
494 ck_assert(a->ip_equals(a, b));
495 ck_assert(!a->equals(a, b));
496 b->set_port(b, 500);
497 ck_assert(a->ip_equals(a, b));
498 ck_assert(a->equals(a, b));
499 a->destroy(a);
500 b->destroy(b);
501 }
502 END_TEST
503
504 /*******************************************************************************
505 * clone
506 */
507
508 START_TEST(test_clone)
509 {
510 host_t *a, *b;
511
512 a = host_create_from_string("192.168.0.1", 500);
513 b = a->clone(a);
514 ck_assert(a != b);
515 ck_assert(a->equals(a, b));
516 a->destroy(a);
517 b->destroy(b);
518 }
519 END_TEST
520
521 /*******************************************************************************
522 * printf hook
523 */
524
525 static struct {
526 char *addr;
527 u_int16_t port;
528 /* results for %H, %+H, %#H (falls back to [0]) */
529 char *result[3];
530 } printf_data[] = {
531 {NULL, 0, { "(null)" }},
532 {NULL, 500, { "(null)" }},
533 {"%any", 0, { "%any", "0.0.0.0", "0.0.0.0[0]" }},
534 {"%any", 500, { "%any", "0.0.0.0", "0.0.0.0[500]" }},
535 {"%any6", 0, { "%any6", "::", "::[0]" }},
536 {"%any6", 500, { "%any6", "::", "::[500]" }},
537 {"192.168.0.1", 0, { "192.168.0.1", "192.168.0.1", "192.168.0.1[0]" }},
538 {"192.168.0.1", 500, { "192.168.0.1", "192.168.0.1", "192.168.0.1[500]" }},
539 {"fec1::1", 0, { "fec1::1", "fec1::1", "fec1::1[0]" }},
540 {"fec1::1", 500, { "fec1::1", "fec1::1", "fec1::1[500]" }},
541 };
542
543 static void verify_printf(host_t *host, const char *format, char *expected)
544 {
545 char buf[64];
546
547 snprintf(buf, sizeof(buf), format, host);
548 ck_assert_str_eq(expected, buf);
549 }
550
551 START_TEST(test_printf_hook)
552 {
553 static const char *formats[] = { "%H", "%+H", "%#H" };
554 host_t *host = NULL;
555 char *expected;
556 int i;
557
558 if (printf_data[_i].addr)
559 {
560 host = host_create_from_string(printf_data[_i].addr,
561 printf_data[_i].port);
562 }
563 for (i = 0; i < countof(formats); i++)
564 {
565 expected = printf_data[_i].result[i];
566 expected = expected ?: printf_data[_i].result[0];
567 verify_printf(host, formats[i], expected);
568 }
569 DESTROY_IF(host);
570 }
571 END_TEST
572
573 START_TEST(test_printf_hook_align)
574 {
575 host_t *host;
576
577 verify_printf(NULL, "%14H", " (null)");
578 verify_printf(NULL, "%-14H", "(null) ");
579
580 host = host_create_from_string("192.168.0.1", 0);
581 verify_printf(host, "%14H", " 192.168.0.1");
582 verify_printf(host, "%-14H", "192.168.0.1 ");
583 verify_printf(host, "%4H", "192.168.0.1");
584 verify_printf(host, "%-4H", "192.168.0.1");
585 host->destroy(host);
586 }
587 END_TEST
588
589 Suite *host_suite_create()
590 {
591 Suite *s;
592 TCase *tc;
593
594 s = suite_create("host");
595
596 tc = tcase_create("host_create_any");
597 tcase_add_test(tc, test_create_any_v4);
598 tcase_add_test(tc, test_create_any_v6);
599 tcase_add_test(tc, test_create_any_other);
600 suite_add_tcase(s, tc);
601
602 tc = tcase_create("host_create_from_string");
603 tcase_add_test(tc, test_create_from_string_v4);
604 tcase_add_test(tc, test_create_from_string_any_v6);
605 suite_add_tcase(s, tc);
606
607 tc = tcase_create("host_create_from_string_and_family");
608 tcase_add_test(tc, test_create_from_string_and_family_v4);
609 tcase_add_test(tc, test_create_from_string_and_family_v6);
610 tcase_add_test(tc, test_create_from_string_and_family_other);
611 suite_add_tcase(s, tc);
612
613 tc = tcase_create("host_create_from_sockaddr");
614 tcase_add_test(tc, test_create_from_sockaddr_v4);
615 tcase_add_test(tc, test_create_from_sockaddr_v6);
616 tcase_add_test(tc, test_create_from_sockaddr_other);
617 suite_add_tcase(s, tc);
618
619 tc = tcase_create("host_create_from_chunk");
620 tcase_add_test(tc, test_create_from_chunk_v4);
621 tcase_add_test(tc, test_create_from_chunk_v6);
622 tcase_add_test(tc, test_create_from_chunk_other);
623 suite_add_tcase(s, tc);
624
625 tc = tcase_create("host_create_from_subnet");
626 tcase_add_test(tc, test_create_from_subnet_v4);
627 tcase_add_test(tc, test_create_from_subnet_v6);
628 suite_add_tcase(s, tc);
629
630 tc = tcase_create("host_create_netmask");
631 tcase_add_test(tc, test_create_netmask_v4);
632 tcase_add_test(tc, test_create_netmask_v6);
633 tcase_add_test(tc, test_create_netmask_other);
634 suite_add_tcase(s, tc);
635
636 tc = tcase_create("equals, ip_equals");
637 tcase_add_test(tc, test_equals);
638 tcase_add_test(tc, test_equals_any);
639 suite_add_tcase(s, tc);
640
641 tc = tcase_create("clone");
642 tcase_add_test(tc, test_clone);
643 suite_add_tcase(s, tc);
644
645 tc = tcase_create("printf hook");
646 tcase_add_loop_test(tc, test_printf_hook, 0, countof(printf_data));
647 tcase_add_test(tc, test_printf_hook_align);
648 suite_add_tcase(s, tc);
649
650 return s;
651 }