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