Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ipv6_test.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * IPv6 tests
  23. *
  24. */
  25. /* Forcibly enable assertions */
  26. #undef NDEBUG
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include <byteswap.h>
  30. #include <ipxe/ipv6.h>
  31. #include <ipxe/test.h>
  32. /** Define inline IPv6 address */
  33. #define IPV6(...) { __VA_ARGS__ }
  34. /** The unspecified IPv6 address */
  35. static const struct in6_addr sample_unspecified = {
  36. .s6_addr = IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
  38. };
  39. /** A sample link-local IPv6 address */
  40. static const struct in6_addr sample_link_local = {
  41. .s6_addr = IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x69, 0xff, 0xfe, 0x50, 0x58, 0x45 ),
  43. };
  44. /** A sample global IPv6 address */
  45. static const struct in6_addr sample_global = {
  46. .s6_addr = IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
  47. 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ),
  48. };
  49. /** A sample multicast IPv6 address */
  50. static const struct in6_addr sample_multicast = {
  51. .s6_addr = IPV6 ( 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
  53. };
  54. /**
  55. * Report an inet6_ntoa() test result
  56. *
  57. * @v addr IPv6 address
  58. * @v text Expected textual representation
  59. */
  60. #define inet6_ntoa_ok( addr, text ) do { \
  61. static const struct in6_addr in = { \
  62. .s6_addr = addr, \
  63. }; \
  64. static const char expected[] = text; \
  65. char *actual; \
  66. \
  67. actual = inet6_ntoa ( &in ); \
  68. DBG ( "inet6_ntoa ( %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ) " \
  69. "= %s\n", ntohs ( in.s6_addr16[0] ), \
  70. ntohs ( in.s6_addr16[1] ), ntohs ( in.s6_addr16[2] ), \
  71. ntohs ( in.s6_addr16[3] ), ntohs ( in.s6_addr16[4] ), \
  72. ntohs ( in.s6_addr16[5] ), ntohs ( in.s6_addr16[6] ), \
  73. ntohs ( in.s6_addr16[7] ), actual ); \
  74. ok ( strcmp ( actual, expected ) == 0 ); \
  75. } while ( 0 )
  76. /**
  77. * Report an inet6_aton() test result
  78. *
  79. * @v text Textual representation
  80. * @v addr Expected IPv6 address
  81. */
  82. #define inet6_aton_ok( text, addr ) do { \
  83. static const char string[] = text; \
  84. static const struct in6_addr expected = { \
  85. .s6_addr = addr, \
  86. }; \
  87. struct in6_addr actual; \
  88. \
  89. ok ( inet6_aton ( string, &actual ) == 0 ); \
  90. DBG ( "inet6_aton ( \"%s\" ) = %s\n", string, \
  91. inet6_ntoa ( &actual ) ); \
  92. ok ( memcmp ( &actual, &expected, sizeof ( actual ) ) == 0 ); \
  93. } while ( 0 )
  94. /**
  95. * Report an inet6_aton() failure test result
  96. *
  97. * @v text Textual representation
  98. */
  99. #define inet6_aton_fail_ok( text ) do { \
  100. static const char string[] = text; \
  101. struct in6_addr dummy; \
  102. \
  103. ok ( inet6_aton ( string, &dummy ) != 0 ); \
  104. } while ( 0 )
  105. /**
  106. * Perform IPv6 self-tests
  107. *
  108. */
  109. static void ipv6_test_exec ( void ) {
  110. /* Address testing macros */
  111. ok ( IN6_IS_ADDR_UNSPECIFIED ( &sample_unspecified ) );
  112. ok ( ! IN6_IS_ADDR_UNSPECIFIED ( &sample_link_local ) );
  113. ok ( ! IN6_IS_ADDR_UNSPECIFIED ( &sample_global ) );
  114. ok ( ! IN6_IS_ADDR_UNSPECIFIED ( &sample_multicast ) );
  115. ok ( ! IN6_IS_ADDR_MULTICAST ( &sample_unspecified ) );
  116. ok ( ! IN6_IS_ADDR_MULTICAST ( &sample_link_local ) );
  117. ok ( ! IN6_IS_ADDR_MULTICAST ( &sample_global ) );
  118. ok ( IN6_IS_ADDR_MULTICAST ( &sample_multicast ) );
  119. ok ( ! IN6_IS_ADDR_LINKLOCAL ( &sample_unspecified ) );
  120. ok ( IN6_IS_ADDR_LINKLOCAL ( &sample_link_local ) );
  121. ok ( ! IN6_IS_ADDR_LINKLOCAL ( &sample_global ) );
  122. ok ( ! IN6_IS_ADDR_LINKLOCAL ( &sample_multicast ) );
  123. /* inet6_ntoa() tests */
  124. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
  125. 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ),
  126. "2001:ba8:0:1d4::6950:5845" );
  127. /* No zeros */
  128. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x01,
  129. 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01 ),
  130. "2001:db8:1:1:1:1:1:1" );
  131. /* Run of zeros */
  132. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  133. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
  134. "2001:db8::1" );
  135. /* No "::" for single zero */
  136. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x01,
  137. 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01 ),
  138. "2001:db8:0:1:1:1:1:1" );
  139. /* Use "::" for longest run of zeros */
  140. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
  142. "2001:0:0:1::1" );
  143. /* Use "::" for leftmost equal-length run of zeros */
  144. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  145. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
  146. "2001:db8::1:0:0:1" );
  147. /* Trailing run of zeros */
  148. inet6_ntoa_ok ( IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  149. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
  150. "fe80::" );
  151. /* Leading run of zeros */
  152. inet6_ntoa_ok ( IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
  154. "::1" );
  155. /* All zeros */
  156. inet6_ntoa_ok ( IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  157. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
  158. "::" );
  159. /* Maximum length */
  160. inet6_ntoa_ok ( IPV6 ( 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  161. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ),
  162. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" );
  163. /* inet6_aton() tests */
  164. inet6_aton_ok ( "2001:ba8:0:1d4::6950:5845",
  165. IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
  166. 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45));
  167. /* No zeros */
  168. inet6_aton_ok ( "2001:db8:1:1:1:1:1:1",
  169. IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x01,
  170. 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01));
  171. /* All intervening zeros */
  172. inet6_aton_ok ( "fe80::1",
  173. IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01));
  175. /* Trailing run of zeros */
  176. inet6_aton_ok ( "fe80::",
  177. IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  179. /* Leading run of zeros */
  180. inet6_aton_ok ( "::1",
  181. IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  182. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01));
  183. /* All zeros */
  184. inet6_aton_ok ( "::",
  185. IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  186. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  187. /* inet6_aton() failure tests */
  188. inet6_aton_fail_ok ( "20012:ba8:0:1d4::6950:5845" );
  189. inet6_aton_fail_ok ( "200z:ba8:0:1d4::6950:5845" );
  190. inet6_aton_fail_ok ( "2001.ba8:0:1d4::6950:5845" );
  191. inet6_aton_fail_ok ( "2001:db8:1:1:1:1:1" );
  192. inet6_aton_fail_ok ( "2001:db8:1:1:1:1:1:1:2" );
  193. inet6_aton_fail_ok ( "2001:db8::1::2" );
  194. inet6_aton_fail_ok ( "2001:ba8:0:1d4:::6950:5845" );
  195. inet6_aton_fail_ok ( ":::" );
  196. }
  197. /** IPv6 self-test */
  198. struct self_test ipv6_test __self_test = {
  199. .name = "ipv6",
  200. .exec = ipv6_test_exec,
  201. };