unit-tests: Don't fail host_create_from_dns() test if IPv6 not supported
[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_dns
241 */
242
243 static void test_create_from_dns(int family, chunk_t addr)
244 {
245 host_t *host;
246
247 host = host_create_from_dns("localhost", family, 500);
248 if (family != AF_INET6)
249 {
250 ck_assert(host != NULL);
251 }
252 if (host)
253 {
254 if (family != AF_UNSPEC)
255 {
256 verify_address(host, addr, family, 500);
257 }
258 host->destroy(host);
259 }
260 }
261
262 START_TEST(test_create_from_dns_any)
263 {
264 test_create_from_dns(AF_UNSPEC, chunk_empty);
265 }
266 END_TEST
267
268 START_TEST(test_create_from_dns_v4)
269 {
270 test_create_from_dns(AF_INET, chunk_from_chars(127,0,0,1));
271 }
272 END_TEST
273
274 START_TEST(test_create_from_dns_v6)
275 {
276 test_create_from_dns(AF_INET6,
277 chunk_from_chars(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1));
278 }
279 END_TEST
280
281 /*******************************************************************************
282 * host_create_from_sockaddr
283 */
284
285 START_TEST(test_create_from_sockaddr_v4)
286 {
287 struct sockaddr_in addr = {
288 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
289 .sin_len = sizeof(struct sockaddr_in),
290 #endif
291 .sin_family = AF_INET,
292 .sin_port = htons(500),
293 }, *val;
294 socklen_t *socklen;
295 host_t *host;
296
297 host = host_create_from_sockaddr((sockaddr_t*)&addr);
298 verify_any(host, AF_INET, 500);
299 val = (struct sockaddr_in*)host->get_sockaddr(host);
300 ck_assert(memeq(&addr, val, sizeof(addr)));
301 socklen = host->get_sockaddr_len(host);
302 ck_assert(*socklen == sizeof(addr));
303 host->destroy(host);
304 }
305 END_TEST
306
307 START_TEST(test_create_from_sockaddr_v6)
308 {
309 struct sockaddr_in6 addr = {
310 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
311 .sin6_len = sizeof(struct sockaddr_in6),
312 #endif
313 .sin6_family = AF_INET6,
314 .sin6_port = htons(500),
315 }, *val;
316 socklen_t *socklen;
317 host_t *host;
318
319 host = host_create_from_sockaddr((sockaddr_t*)&addr);
320 verify_any(host, AF_INET6, 500);
321 val = (struct sockaddr_in6*)host->get_sockaddr(host);
322 ck_assert(memeq(&addr, val, sizeof(addr)));
323 socklen = host->get_sockaddr_len(host);
324 ck_assert(*socklen == sizeof(addr));
325 host->destroy(host);
326 }
327 END_TEST
328
329 START_TEST(test_create_from_sockaddr_other)
330 {
331 struct sockaddr addr = {
332 .sa_family = AF_UNIX,
333 };
334 host_t *host;
335
336 host = host_create_from_sockaddr((sockaddr_t*)&addr);
337 ck_assert(host == NULL);
338 }
339 END_TEST
340
341 /*******************************************************************************
342 * host_create_from_chunk
343 */
344
345 START_TEST(test_create_from_chunk_v4)
346 {
347 host_t *host;
348
349 host = host_create_from_chunk(AF_INET, addr_v4, 500);
350 verify_address(host, addr_v4, AF_INET, 500);
351 host->destroy(host);
352
353 host = host_create_from_chunk(AF_UNSPEC, addr_v4, 500);
354 verify_address(host, addr_v4, AF_INET, 500);
355 host->destroy(host);
356
357 host = host_create_from_chunk(AF_INET, chunk_empty, 500);
358 ck_assert(host == NULL);
359 host = host_create_from_chunk(AF_UNSPEC, chunk_empty, 500);
360 ck_assert(host == NULL);
361 }
362 END_TEST
363
364 START_TEST(test_create_from_chunk_v6)
365 {
366 host_t *host;
367
368 host = host_create_from_chunk(AF_INET6, addr_v6, 500);
369 verify_address(host, addr_v6, AF_INET6, 500);
370 host->destroy(host);
371
372 host = host_create_from_chunk(AF_UNSPEC, addr_v6, 500);
373 verify_address(host, addr_v6, AF_INET6, 500);
374 host->destroy(host);
375
376 host = host_create_from_chunk(AF_INET6, chunk_empty, 500);
377 ck_assert(host == NULL);
378 }
379 END_TEST
380
381 START_TEST(test_create_from_chunk_other)
382 {
383 host_t *host;
384
385 host = host_create_from_chunk(AF_UNIX, addr_v6, 500);
386 ck_assert(host == NULL);
387 }
388 END_TEST
389
390 /*******************************************************************************
391 * host_create_from_subnet
392 */
393
394 START_TEST(test_create_from_subnet_v4)
395 {
396 host_t *host;
397 int bits = -1;
398
399 host = host_create_from_subnet("0.0.0.0/0", &bits);
400 verify_any(host, AF_INET, 0);
401 ck_assert_int_eq(bits, 0);
402 host->destroy(host);
403
404 host = host_create_from_subnet("192.168.0.1", &bits);
405 verify_address(host, addr_v4, AF_INET, 0);
406 ck_assert_int_eq(bits, 32);
407 host->destroy(host);
408
409 host = host_create_from_subnet("192.168.0.1/24", &bits);
410 verify_address(host, addr_v4, AF_INET, 0);
411 ck_assert_int_eq(bits, 24);
412 host->destroy(host);
413
414 host = host_create_from_subnet("foo.b.a.r", &bits);
415 ck_assert(host == NULL);
416 }
417 END_TEST
418
419 START_TEST(test_create_from_subnet_v6)
420 {
421 host_t *host;
422 int bits = -1;
423
424 host = host_create_from_subnet("::/0", &bits);
425 verify_any(host, AF_INET6, 0);
426 ck_assert_int_eq(bits, 0);
427 host->destroy(host);
428
429 host = host_create_from_subnet("fec1::1", &bits);
430 verify_address(host, addr_v6, AF_INET6, 0);
431 ck_assert_int_eq(bits, 128);
432 host->destroy(host);
433
434 host = host_create_from_subnet("fec1::1/64", &bits);
435 verify_address(host, addr_v6, AF_INET6, 0);
436 ck_assert_int_eq(bits, 64);
437 host->destroy(host);
438
439 host = host_create_from_subnet("foo::bar", &bits);
440 ck_assert(host == NULL);
441 }
442 END_TEST
443
444 /*******************************************************************************
445 * host_create_from_range
446 */
447
448 static const chunk_t addr_v4_to = chunk_from_chars(0xc0, 0xa8, 0x00, 0x05);
449 static const chunk_t addr_v6_to = chunk_from_chars(0xfe, 0xc1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
450 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05);
451
452 static void verify_range(char *str, int family, chunk_t from_addr,
453 chunk_t to_addr)
454 {
455 host_t *from, *to;
456
457 if (!family)
458 {
459 ck_assert(!host_create_from_range(str, &from, &to));
460 }
461 else
462 {
463 ck_assert(host_create_from_range(str, &from, &to));
464 verify_address(from, from_addr, family, 0);
465 verify_address(to, to_addr, family, 0);
466 from->destroy(from);
467 to->destroy(to);
468 }
469 }
470
471 START_TEST(test_create_from_range_v4)
472 {
473 host_t *from, *to;
474
475 ck_assert(host_create_from_range("0.0.0.0-0.0.0.0", &from, &to));
476 verify_any(from, AF_INET, 0);
477 verify_any(to, AF_INET, 0);
478 from->destroy(from);
479 to->destroy(to);
480
481 verify_range("192.168.0.1-192.168.0.1", AF_INET, addr_v4, addr_v4);
482 verify_range("192.168.0.1-192.168.0.5", AF_INET, addr_v4, addr_v4_to);
483 verify_range("192.168.0.1- 192.168.0.5", AF_INET, addr_v4, addr_v4_to);
484 verify_range("192.168.0.1 -192.168.0.5", AF_INET, addr_v4, addr_v4_to);
485 verify_range("192.168.0.1 - 192.168.0.5", AF_INET, addr_v4, addr_v4_to);
486 verify_range("192.168.0.5-192.168.0.1", AF_INET, addr_v4_to, addr_v4);
487
488 verify_range("192.168.0.1", 0, chunk_empty, chunk_empty);
489 verify_range("192.168.0.1-", 0, chunk_empty, chunk_empty);
490 verify_range("-192.168.0.1", 0, chunk_empty, chunk_empty);
491 verify_range("192.168.0.1-192", 0, chunk_empty, chunk_empty);
492 verify_range("192.168.0.1-192.168", 0, chunk_empty, chunk_empty);
493 verify_range("192.168.0.1-192.168.0", 0, chunk_empty, chunk_empty);
494 verify_range("foo.b.a.r", 0, chunk_empty, chunk_empty);
495 verify_range("foo.b.a.r-b.a.r.f", 0, chunk_empty, chunk_empty);
496 }
497 END_TEST
498
499 START_TEST(test_create_from_range_v6)
500 {
501 host_t *from, *to;
502
503 ck_assert(host_create_from_range("::-::", &from, &to));
504 verify_any(from, AF_INET6, 0);
505 verify_any(to, AF_INET6, 0);
506 from->destroy(from);
507 to->destroy(to);
508
509 verify_range("fec1::1-fec1::1", AF_INET6, addr_v6, addr_v6);
510 verify_range("fec1::1-fec1::5", AF_INET6, addr_v6, addr_v6_to);
511 verify_range("fec1::1- fec1::5", AF_INET6, addr_v6, addr_v6_to);
512 verify_range("fec1::1 -fec1::5", AF_INET6, addr_v6, addr_v6_to);
513 verify_range("fec1::1 - fec1::5", AF_INET6, addr_v6, addr_v6_to);
514 verify_range("fec1::5-fec1::1", AF_INET6, addr_v6_to, addr_v6);
515
516 verify_range("fec1::1", 0, chunk_empty, chunk_empty);
517 verify_range("fec1::1-", 0, chunk_empty, chunk_empty);
518 verify_range("-fec1::1", 0, chunk_empty, chunk_empty);
519 verify_range("fec1::1-fec1", 0, chunk_empty, chunk_empty);
520 verify_range("foo::bar", 0, chunk_empty, chunk_empty);
521 verify_range("foo::bar-bar::foo", 0, chunk_empty, chunk_empty);
522
523 verify_range("fec1::1-192.168.0.1", 0, chunk_empty, chunk_empty);
524 verify_range("192.168.0.1-fec1::1", 0, chunk_empty, chunk_empty);
525 }
526 END_TEST
527
528 /*******************************************************************************
529 * host_create_netmask
530 */
531
532 static void test_create_netmask(int family)
533 {
534 host_t *netmask;
535 int i, len = (family == AF_INET) ? 32 : 128;
536
537 netmask = host_create_netmask(family, -1);
538 ck_assert(netmask == NULL);
539 for (i = 0; i <= len; i++)
540 {
541 netmask = host_create_netmask(family, i);
542 verify_netmask(netmask->get_address(netmask), i);
543 netmask->destroy(netmask);
544 }
545 netmask = host_create_netmask(family, len + 1);
546 ck_assert(netmask == NULL);
547 }
548
549 START_TEST(test_create_netmask_v4)
550 {
551 test_create_netmask(AF_INET);
552 }
553 END_TEST
554
555 START_TEST(test_create_netmask_v6)
556 {
557 test_create_netmask(AF_INET6);
558 }
559 END_TEST
560
561 START_TEST(test_create_netmask_other)
562 {
563 host_t *netmask;
564
565 netmask = host_create_netmask(AF_UNSPEC, 0);
566 ck_assert(netmask == NULL);
567 }
568 END_TEST
569
570 /*******************************************************************************
571 * equals, ip_equals
572 */
573
574 START_TEST(test_equals)
575 {
576 host_t *a, *b;
577
578 a = host_create_from_string("192.168.0.1", 500);
579 b = host_create_from_string("192.168.0.1", 0);
580 ck_assert(!a->equals(a, b));
581 ck_assert(!b->equals(b, a));
582 ck_assert(a->ip_equals(a, b));
583 ck_assert(b->ip_equals(b, a));
584 b->set_port(b, 500);
585 ck_assert(a->equals(a, b));
586 ck_assert(b->equals(b, a));
587 ck_assert(a->ip_equals(a, b));
588 ck_assert(b->ip_equals(b, a));
589 b->destroy(b);
590 b = host_create_from_string("192.168.0.2", 500);
591 ck_assert(!a->ip_equals(a, b));
592 ck_assert(!a->equals(a, b));
593 b->destroy(b);
594
595 b = host_create_from_string("fec1::1", 500);
596 ck_assert(!a->ip_equals(a, b));
597 ck_assert(!a->equals(a, b));
598 a->destroy(a);
599 a = host_create_from_string("fec1::1", 500);
600 ck_assert(a->equals(a, b));
601 ck_assert(a->ip_equals(a, b));
602 a->destroy(a);
603 b->destroy(b);
604 }
605 END_TEST
606
607 START_TEST(test_equals_any)
608 {
609 host_t *a, *b;
610
611 a = host_create_from_string("%any", 500);
612 b = host_create_from_string("%any", 0);
613 ck_assert(!a->equals(a, b));
614 ck_assert(a->ip_equals(a, b));
615 b->set_port(b, 500);
616 ck_assert(a->equals(a, b));
617 ck_assert(a->ip_equals(a, b));
618 b->destroy(b);
619 b = host_create_from_string("%any6", 0);
620 ck_assert(a->ip_equals(a, b));
621 ck_assert(!a->equals(a, b));
622 b->set_port(b, 500);
623 ck_assert(a->ip_equals(a, b));
624 ck_assert(a->equals(a, b));
625 a->destroy(a);
626 b->destroy(b);
627 }
628 END_TEST
629
630 /*******************************************************************************
631 * clone
632 */
633
634 START_TEST(test_clone)
635 {
636 host_t *a, *b;
637
638 a = host_create_from_string("192.168.0.1", 500);
639 b = a->clone(a);
640 ck_assert(a != b);
641 ck_assert(a->equals(a, b));
642 a->destroy(a);
643 b->destroy(b);
644 }
645 END_TEST
646
647 /*******************************************************************************
648 * printf hook
649 */
650
651 static struct {
652 char *addr;
653 u_int16_t port;
654 /* results for %H, %+H, %#H (falls back to [0]) */
655 char *result[3];
656 } printf_data[] = {
657 {NULL, 0, { "(null)" }},
658 {NULL, 500, { "(null)" }},
659 {"%any", 0, { "%any", "0.0.0.0", "0.0.0.0[0]" }},
660 {"%any", 500, { "%any", "0.0.0.0", "0.0.0.0[500]" }},
661 {"%any6", 0, { "%any6", "::", "::[0]" }},
662 {"%any6", 500, { "%any6", "::", "::[500]" }},
663 {"192.168.0.1", 0, { "192.168.0.1", "192.168.0.1", "192.168.0.1[0]" }},
664 {"192.168.0.1", 500, { "192.168.0.1", "192.168.0.1", "192.168.0.1[500]" }},
665 {"fec1::1", 0, { "fec1::1", "fec1::1", "fec1::1[0]" }},
666 {"fec1::1", 500, { "fec1::1", "fec1::1", "fec1::1[500]" }},
667 };
668
669 static void verify_printf(host_t *host, const char *format, char *expected)
670 {
671 char buf[64];
672
673 snprintf(buf, sizeof(buf), format, host);
674 ck_assert_str_eq(expected, buf);
675 }
676
677 START_TEST(test_printf_hook)
678 {
679 static const char *formats[] = { "%H", "%+H", "%#H" };
680 host_t *host = NULL;
681 char *expected;
682 int i;
683
684 if (printf_data[_i].addr)
685 {
686 host = host_create_from_string(printf_data[_i].addr,
687 printf_data[_i].port);
688 }
689 for (i = 0; i < countof(formats); i++)
690 {
691 expected = printf_data[_i].result[i];
692 expected = expected ?: printf_data[_i].result[0];
693 verify_printf(host, formats[i], expected);
694 }
695 DESTROY_IF(host);
696 }
697 END_TEST
698
699 START_TEST(test_printf_hook_align)
700 {
701 host_t *host;
702
703 verify_printf(NULL, "%14H", " (null)");
704 verify_printf(NULL, "%-14H", "(null) ");
705
706 host = host_create_from_string("192.168.0.1", 0);
707 verify_printf(host, "%14H", " 192.168.0.1");
708 verify_printf(host, "%-14H", "192.168.0.1 ");
709 verify_printf(host, "%4H", "192.168.0.1");
710 verify_printf(host, "%-4H", "192.168.0.1");
711 host->destroy(host);
712 }
713 END_TEST
714
715 Suite *host_suite_create()
716 {
717 Suite *s;
718 TCase *tc;
719
720 s = suite_create("host");
721
722 tc = tcase_create("host_create_any");
723 tcase_add_test(tc, test_create_any_v4);
724 tcase_add_test(tc, test_create_any_v6);
725 tcase_add_test(tc, test_create_any_other);
726 suite_add_tcase(s, tc);
727
728 tc = tcase_create("host_create_from_string");
729 tcase_add_test(tc, test_create_from_string_v4);
730 tcase_add_test(tc, test_create_from_string_any_v6);
731 suite_add_tcase(s, tc);
732
733 tc = tcase_create("host_create_from_string_and_family");
734 tcase_add_test(tc, test_create_from_string_and_family_v4);
735 tcase_add_test(tc, test_create_from_string_and_family_v6);
736 tcase_add_test(tc, test_create_from_string_and_family_other);
737 suite_add_tcase(s, tc);
738
739 tc = tcase_create("host_create_from_dns");
740 tcase_add_test(tc, test_create_from_dns_any);
741 tcase_add_test(tc, test_create_from_dns_v4);
742 tcase_add_test(tc, test_create_from_dns_v6);
743 suite_add_tcase(s, tc);
744
745 tc = tcase_create("host_create_from_sockaddr");
746 tcase_add_test(tc, test_create_from_sockaddr_v4);
747 tcase_add_test(tc, test_create_from_sockaddr_v6);
748 tcase_add_test(tc, test_create_from_sockaddr_other);
749 suite_add_tcase(s, tc);
750
751 tc = tcase_create("host_create_from_chunk");
752 tcase_add_test(tc, test_create_from_chunk_v4);
753 tcase_add_test(tc, test_create_from_chunk_v6);
754 tcase_add_test(tc, test_create_from_chunk_other);
755 suite_add_tcase(s, tc);
756
757 tc = tcase_create("host_create_from_subnet");
758 tcase_add_test(tc, test_create_from_subnet_v4);
759 tcase_add_test(tc, test_create_from_subnet_v6);
760 suite_add_tcase(s, tc);
761
762 tc = tcase_create("host_create_from_range");
763 tcase_add_test(tc, test_create_from_range_v4);
764 tcase_add_test(tc, test_create_from_range_v6);
765 suite_add_tcase(s, tc);
766
767 tc = tcase_create("host_create_netmask");
768 tcase_add_test(tc, test_create_netmask_v4);
769 tcase_add_test(tc, test_create_netmask_v6);
770 tcase_add_test(tc, test_create_netmask_other);
771 suite_add_tcase(s, tc);
772
773 tc = tcase_create("equals, ip_equals");
774 tcase_add_test(tc, test_equals);
775 tcase_add_test(tc, test_equals_any);
776 suite_add_tcase(s, tc);
777
778 tc = tcase_create("clone");
779 tcase_add_test(tc, test_clone);
780 suite_add_tcase(s, tc);
781
782 tc = tcase_create("printf hook");
783 tcase_add_loop_test(tc, test_printf_hook, 0, countof(printf_data));
784 tcase_add_test(tc, test_printf_hook_align);
785 suite_add_tcase(s, tc);
786
787 return s;
788 }