Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ipv4_test.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2015 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. * IPv4 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/in.h>
  31. #include <ipxe/test.h>
  32. /** Define inline IPv4 address */
  33. #define IPV4(a,b,c,d) ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
  34. /**
  35. * Report an inet_ntoa() test result
  36. *
  37. * @v addr IPv4 address
  38. * @v text Expected textual representation
  39. * @v file Test code file
  40. * @v line Test code line
  41. */
  42. static void inet_ntoa_okx ( uint32_t addr, const char *text, const char *file,
  43. unsigned int line ) {
  44. struct in_addr in = { .s_addr = htonl ( addr ) };
  45. char *actual;
  46. /* Format address */
  47. actual = inet_ntoa ( in );
  48. DBG ( "inet_ntoa ( %d.%d.%d.%d ) = %s\n",
  49. ( ( ntohl ( addr ) >> 24 ) & 0xff ),
  50. ( ( ntohl ( addr ) >> 16 ) & 0xff ),
  51. ( ( ntohl ( addr ) >> 8 ) & 0xff ),
  52. ( ( ntohl ( addr ) >> 0 ) & 0xff ), actual );
  53. okx ( strcmp ( actual, text ) == 0, file, line );
  54. }
  55. #define inet_ntoa_ok( addr, text ) \
  56. inet_ntoa_okx ( addr, text, __FILE__, __LINE__ )
  57. /**
  58. * Report an inet_aton() test result
  59. *
  60. * @v text Textual representation
  61. * @v addr Expected IPv4 address
  62. * @v file Test code file
  63. * @v line Test code line
  64. */
  65. static void inet_aton_okx ( const char *text, uint32_t addr, const char *file,
  66. unsigned int line ) {
  67. struct in_addr actual;
  68. /* Parse address */
  69. okx ( inet_aton ( text, &actual ) != 0, file, line );
  70. DBG ( "inet_aton ( \"%s\" ) = %s\n", text, inet_ntoa ( actual ) );
  71. okx ( ntohl ( actual.s_addr ) == addr, file, line );
  72. };
  73. #define inet_aton_ok( text, addr ) \
  74. inet_aton_okx ( text, addr, __FILE__, __LINE__ )
  75. /**
  76. * Report an inet_aton() failure test result
  77. *
  78. * @v text Textual representation
  79. * @v file Test code file
  80. * @v line Test code line
  81. */
  82. static void inet_aton_fail_okx ( const char *text, const char *file,
  83. unsigned int line ) {
  84. struct in_addr actual;
  85. /* Attempt to parse address */
  86. okx ( inet_aton ( text, &actual ) == 0, file, line );
  87. }
  88. #define inet_aton_fail_ok( text ) \
  89. inet_aton_fail_okx ( text, __FILE__, __LINE__ )
  90. /**
  91. * Perform IPv4 self-tests
  92. *
  93. */
  94. static void ipv4_test_exec ( void ) {
  95. /* Address testing macros */
  96. ok ( IN_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
  97. ok ( ! IN_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
  98. ok ( ! IN_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
  99. ok ( ! IN_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
  100. ok ( IN_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
  101. ok ( ! IN_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
  102. ok ( ! IN_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
  103. ok ( ! IN_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
  104. ok ( IN_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
  105. ok ( ! IN_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
  106. ok ( ! IN_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
  107. ok ( ! IN_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
  108. ok ( ! IN_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
  109. ok ( ! IN_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
  110. ok ( IN_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
  111. ok ( IN_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
  112. ok ( IN_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
  113. /* inet_ntoa() tests */
  114. inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );
  115. inet_ntoa_ok ( IPV4 ( 0, 0, 0, 0 ), "0.0.0.0" );
  116. inet_ntoa_ok ( IPV4 ( 255, 255, 255, 255 ), "255.255.255.255" );
  117. inet_ntoa_ok ( IPV4 ( 212, 13, 204, 60 ), "212.13.204.60" );
  118. /* inet_aton() tests */
  119. inet_aton_ok ( "212.13.204.60", IPV4 ( 212, 13, 204, 60 ) );
  120. inet_aton_ok ( "127.0.0.1", IPV4 ( 127, 0, 0, 1 ) );
  121. /* inet_aton() failure tests */
  122. inet_aton_fail_ok ( "256.0.0.1" ); /* Byte out of range */
  123. inet_aton_fail_ok ( "212.13.204.60.1" ); /* Too long */
  124. inet_aton_fail_ok ( "127.0.0" ); /* Too short */
  125. inet_aton_fail_ok ( "1.2.3.a" ); /* Invalid characters */
  126. inet_aton_fail_ok ( "127.0..1" ); /* Missing bytes */
  127. }
  128. /** IPv4 self-test */
  129. struct self_test ipv4_test __self_test = {
  130. .name = "ipv4",
  131. .exec = ipv4_test_exec,
  132. };