You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ipv6_test.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /**
  35. * Report an inet6_ntoa() test result
  36. *
  37. * @v addr IPv6 address
  38. * @v text Expected textual representation
  39. */
  40. #define inet6_ntoa_ok( addr, text ) do { \
  41. static const struct in6_addr in = { \
  42. .s6_addr = addr, \
  43. }; \
  44. static const char expected[] = text; \
  45. char *actual; \
  46. \
  47. actual = inet6_ntoa ( &in ); \
  48. DBG ( "inet6_ntoa ( %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ) " \
  49. "= %s\n", ntohs ( in.s6_addr16[0] ), \
  50. ntohs ( in.s6_addr16[1] ), ntohs ( in.s6_addr16[2] ), \
  51. ntohs ( in.s6_addr16[3] ), ntohs ( in.s6_addr16[4] ), \
  52. ntohs ( in.s6_addr16[5] ), ntohs ( in.s6_addr16[6] ), \
  53. ntohs ( in.s6_addr16[7] ), actual ); \
  54. ok ( strcmp ( actual, expected ) == 0 ); \
  55. } while ( 0 )
  56. /**
  57. * Report an inet6_aton() test result
  58. *
  59. * @v text Textual representation
  60. * @v addr Expected IPv6 address
  61. */
  62. #define inet6_aton_ok( text, addr ) do { \
  63. static const char string[] = text; \
  64. static const struct in6_addr expected = { \
  65. .s6_addr = addr, \
  66. }; \
  67. struct in6_addr actual; \
  68. \
  69. ok ( inet6_aton ( string, &actual ) == 0 ); \
  70. DBG ( "inet6_aton ( \"%s\" ) = %s\n", string, \
  71. inet6_ntoa ( &actual ) ); \
  72. ok ( memcmp ( &actual, &expected, sizeof ( actual ) ) == 0 ); \
  73. } while ( 0 )
  74. /**
  75. * Report an inet6_aton() failure test result
  76. *
  77. * @v text Textual representation
  78. */
  79. #define inet6_aton_fail_ok( text ) do { \
  80. static const char string[] = text; \
  81. struct in6_addr dummy; \
  82. \
  83. ok ( inet6_aton ( string, &dummy ) != 0 ); \
  84. } while ( 0 )
  85. /**
  86. * Perform IPv6 self-tests
  87. *
  88. */
  89. static void ipv6_test_exec ( void ) {
  90. /* inet6_ntoa() tests */
  91. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
  92. 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ),
  93. "2001:ba8:0:1d4::6950:5845" );
  94. /* No zeros */
  95. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x01,
  96. 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01 ),
  97. "2001:db8:1:1:1:1:1:1" );
  98. /* Run of zeros */
  99. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  100. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
  101. "2001:db8::1" );
  102. /* No "::" for single zero */
  103. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x01,
  104. 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01 ),
  105. "2001:db8:0:1:1:1:1:1" );
  106. /* Use "::" for longest run of zeros */
  107. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
  109. "2001:0:0:1::1" );
  110. /* Use "::" for leftmost equal-length run of zeros */
  111. inet6_ntoa_ok ( IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  112. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
  113. "2001:db8::1:0:0:1" );
  114. /* Trailing run of zeros */
  115. inet6_ntoa_ok ( IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
  117. "fe80::" );
  118. /* Leading run of zeros */
  119. inet6_ntoa_ok ( IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ),
  121. "::1" );
  122. /* All zeros */
  123. inet6_ntoa_ok ( IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
  125. "::" );
  126. /* Maximum length */
  127. inet6_ntoa_ok ( IPV6 ( 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  128. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ),
  129. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" );
  130. /* inet6_aton() tests */
  131. inet6_aton_ok ( "2001:ba8:0:1d4::6950:5845",
  132. IPV6 ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
  133. 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45));
  134. /* No zeros */
  135. inet6_aton_ok ( "2001:db8:1:1:1:1:1:1",
  136. IPV6 ( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x01,
  137. 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01));
  138. /* All intervening zeros */
  139. inet6_aton_ok ( "fe80::1",
  140. IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01));
  142. /* Trailing run of zeros */
  143. inet6_aton_ok ( "fe80::",
  144. IPV6 ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  146. /* Leading run of zeros */
  147. inet6_aton_ok ( "::1",
  148. IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  149. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01));
  150. /* All zeros */
  151. inet6_aton_ok ( "::",
  152. IPV6 ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  154. /* inet6_aton() failure tests */
  155. inet6_aton_fail_ok ( "20012:ba8:0:1d4::6950:5845" );
  156. inet6_aton_fail_ok ( "200z:ba8:0:1d4::6950:5845" );
  157. inet6_aton_fail_ok ( "2001.ba8:0:1d4::6950:5845" );
  158. inet6_aton_fail_ok ( "2001:db8:1:1:1:1:1" );
  159. inet6_aton_fail_ok ( "2001:db8:1:1:1:1:1:1:2" );
  160. inet6_aton_fail_ok ( "2001:db8::1::2" );
  161. inet6_aton_fail_ok ( "2001:ba8:0:1d4:::6950:5845" );
  162. inet6_aton_fail_ok ( ":::" );
  163. }
  164. /** IPv6 self-test */
  165. struct self_test ipv6_test __self_test = {
  166. .name = "ipv6",
  167. .exec = ipv6_test_exec,
  168. };