check-addr.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * tests/check-addr.c nl_addr unit tests
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2013 Thomas Graf <tgraf@suug.ch>
  10. */
  11. #include <check.h>
  12. #include <netlink/addr.h>
  13. START_TEST(addr_alloc)
  14. {
  15. struct nl_addr *addr;
  16. addr = nl_addr_alloc(16);
  17. fail_if(addr == NULL,
  18. "Allocation should not return NULL");
  19. fail_if(nl_addr_iszero(addr) == 0,
  20. "New empty address should be all zeros");
  21. fail_if(nl_addr_get_family(addr) != AF_UNSPEC,
  22. "New empty address should have family AF_UNSPEC");
  23. fail_if(nl_addr_get_prefixlen(addr) != 0,
  24. "New empty address should have prefix length 0");
  25. fail_if(nl_addr_shared(addr),
  26. "New empty address should not be shared");
  27. fail_if(nl_addr_get(addr) != addr,
  28. "nl_addr_get() should return pointer to address");
  29. fail_if(nl_addr_shared(addr) == 0,
  30. "Address should be shared after call to nl_addr_get()");
  31. nl_addr_put(addr);
  32. fail_if(nl_addr_shared(addr),
  33. "Address should not be shared after call to nl_addr_put()");
  34. fail_if(nl_addr_fill_sockaddr(addr, NULL, 0) == 0,
  35. "Socket address filling should fail for empty address");
  36. nl_addr_put(addr);
  37. }
  38. END_TEST
  39. START_TEST(addr_binary_addr)
  40. {
  41. struct nl_addr *addr, *addr2;
  42. char baddr[4] = { 0x1, 0x2, 0x3, 0x4 };
  43. char baddr2[6] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 };
  44. addr = nl_addr_alloc(4);
  45. fail_if(addr == NULL,
  46. "Allocation should not return NULL");
  47. fail_if(nl_addr_set_binary_addr(addr, baddr, 4) < 0,
  48. "Valid binary address should be settable");
  49. fail_if(nl_addr_get_prefixlen(addr) != 0,
  50. "Prefix length should be unchanged after nl_addr_set_binary_addr()");
  51. fail_if(nl_addr_get_len(addr) != 4,
  52. "Address length should be 4");
  53. fail_if(nl_addr_set_binary_addr(addr, baddr2, 6) == 0,
  54. "Should not be able to set binary address exceeding maximum length");
  55. fail_if(nl_addr_get_len(addr) != 4,
  56. "Address length should still be 4");
  57. fail_if(nl_addr_guess_family(addr) != AF_INET,
  58. "Binary address of length 4 should be guessed as AF_INET");
  59. fail_if(memcmp(baddr, nl_addr_get_binary_addr(addr), 4) != 0,
  60. "Binary address mismatches");
  61. addr2 = nl_addr_build(AF_UNSPEC, baddr, 4);
  62. fail_if(addr2 == NULL,
  63. "Building of address should not fail");
  64. nl_addr_set_prefixlen(addr, 32);
  65. fail_if(nl_addr_get_prefixlen(addr) != 32,
  66. "Prefix length should be successful changed after nl_addr_set_prefixlen()");
  67. fail_if(nl_addr_cmp(addr, addr2),
  68. "Addresses built from same binary address should match");
  69. nl_addr_put(addr);
  70. nl_addr_put(addr2);
  71. }
  72. END_TEST
  73. START_TEST(addr_parse4)
  74. {
  75. struct nl_addr *addr4, *clone;
  76. struct sockaddr_in sin;
  77. socklen_t len = sizeof(sin);
  78. char *addr_str = "10.0.0.1/16";
  79. char buf[128];
  80. fail_if(nl_addr_parse(addr_str, AF_INET6, &addr4) == 0,
  81. "Should not be able to parse IPv4 address in IPv6 mode");
  82. fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr4) != 0,
  83. "Should be able to parse \"%s\"", addr_str);
  84. fail_if(nl_addr_get_family(addr4) != AF_INET,
  85. "Address family should be AF_INET");
  86. fail_if(nl_addr_get_prefixlen(addr4) != 16,
  87. "Prefix length should be 16");
  88. fail_if(nl_addr_iszero(addr4),
  89. "Address should not be all zeroes");
  90. clone = nl_addr_clone(addr4);
  91. fail_if(clone == NULL,
  92. "Cloned address should not be NULL");
  93. fail_if(nl_addr_cmp(addr4, clone) != 0,
  94. "Cloned address should not mismatch original");
  95. fail_if(nl_addr_fill_sockaddr(addr4, (struct sockaddr *) &sin, &len) != 0,
  96. "Should be able to fill socketaddr");
  97. fail_if(strcmp(nl_addr2str(addr4, buf, sizeof(buf)), addr_str),
  98. "Address translated back to string does not match original");
  99. nl_addr_put(addr4);
  100. nl_addr_put(clone);
  101. }
  102. END_TEST
  103. START_TEST(addr_parse6)
  104. {
  105. struct nl_addr *addr6, *clone;
  106. struct sockaddr_in6 sin;
  107. socklen_t len = sizeof(sin);
  108. char *addr_str = "2001:1:2::3/64";
  109. char buf[128];
  110. fail_if(nl_addr_parse(addr_str, AF_INET, &addr6) == 0,
  111. "Should not be able to parse IPv6 address in IPv4 mode");
  112. fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr6) != 0,
  113. "Should be able to parse \"%s\"", addr_str);
  114. fail_if(nl_addr_get_family(addr6) != AF_INET6,
  115. "Address family should be AF_INET6");
  116. fail_if(nl_addr_get_prefixlen(addr6) != 64,
  117. "Prefix length should be 64");
  118. fail_if(nl_addr_iszero(addr6),
  119. "Address should not be all zeroes");
  120. clone = nl_addr_clone(addr6);
  121. fail_if(clone == NULL,
  122. "Cloned address should not be NULL");
  123. fail_if(nl_addr_cmp(addr6, clone) != 0,
  124. "Cloned address should not mismatch original");
  125. fail_if(nl_addr_fill_sockaddr(addr6, (struct sockaddr *) &sin, &len) != 0,
  126. "Should be able to fill socketaddr");
  127. fail_if(strcmp(nl_addr2str(addr6, buf, sizeof(buf)), addr_str),
  128. "Address translated back to string does not match original");
  129. nl_addr_put(addr6);
  130. nl_addr_put(clone);
  131. }
  132. END_TEST
  133. START_TEST(addr_info)
  134. {
  135. struct nl_addr *addr;
  136. char *addr_str = "127.0.0.1";
  137. struct addrinfo *result;
  138. fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr) != 0,
  139. "Parsing of valid address should not fail");
  140. fail_if(nl_addr_info(addr, &result) != 0,
  141. "getaddrinfo() on loopback address should work");
  142. freeaddrinfo(result);
  143. nl_addr_put(addr);
  144. }
  145. END_TEST
  146. Suite *make_nl_addr_suite(void)
  147. {
  148. Suite *suite = suite_create("Abstract addresses");
  149. TCase *tc_addr = tcase_create("Core");
  150. tcase_add_test(tc_addr, addr_alloc);
  151. tcase_add_test(tc_addr, addr_binary_addr);
  152. tcase_add_test(tc_addr, addr_parse4);
  153. tcase_add_test(tc_addr, addr_parse6);
  154. tcase_add_test(tc_addr, addr_info);
  155. suite_add_tcase(suite, tc_addr);
  156. return suite;
  157. }