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