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.

ipv4_test.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * IPv4 tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <byteswap.h>
  34. #include <ipxe/in.h>
  35. #include <ipxe/test.h>
  36. /** Define inline IPv4 address */
  37. #define IPV4(a,b,c,d) \
  38. htonl ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
  39. /**
  40. * Report an inet_ntoa() test result
  41. *
  42. * @v addr IPv4 address
  43. * @v text Expected textual representation
  44. * @v file Test code file
  45. * @v line Test code line
  46. */
  47. static void inet_ntoa_okx ( uint32_t addr, const char *text, const char *file,
  48. unsigned int line ) {
  49. struct in_addr in = { .s_addr = addr };
  50. char *actual;
  51. /* Format address */
  52. actual = inet_ntoa ( in );
  53. DBG ( "inet_ntoa ( %d.%d.%d.%d ) = %s\n",
  54. ( ( ntohl ( addr ) >> 24 ) & 0xff ),
  55. ( ( ntohl ( addr ) >> 16 ) & 0xff ),
  56. ( ( ntohl ( addr ) >> 8 ) & 0xff ),
  57. ( ( ntohl ( addr ) >> 0 ) & 0xff ), actual );
  58. okx ( strcmp ( actual, text ) == 0, file, line );
  59. }
  60. #define inet_ntoa_ok( addr, text ) \
  61. inet_ntoa_okx ( addr, text, __FILE__, __LINE__ )
  62. /**
  63. * Report an inet_aton() test result
  64. *
  65. * @v text Textual representation
  66. * @v addr Expected IPv4 address
  67. * @v file Test code file
  68. * @v line Test code line
  69. */
  70. static void inet_aton_okx ( const char *text, uint32_t addr, const char *file,
  71. unsigned int line ) {
  72. struct in_addr actual;
  73. /* Parse address */
  74. okx ( inet_aton ( text, &actual ) != 0, file, line );
  75. DBG ( "inet_aton ( \"%s\" ) = %s\n", text, inet_ntoa ( actual ) );
  76. okx ( actual.s_addr == addr, file, line );
  77. };
  78. #define inet_aton_ok( text, addr ) \
  79. inet_aton_okx ( text, addr, __FILE__, __LINE__ )
  80. /**
  81. * Report an inet_aton() failure test result
  82. *
  83. * @v text Textual representation
  84. * @v file Test code file
  85. * @v line Test code line
  86. */
  87. static void inet_aton_fail_okx ( const char *text, const char *file,
  88. unsigned int line ) {
  89. struct in_addr actual;
  90. /* Attempt to parse address */
  91. okx ( inet_aton ( text, &actual ) == 0, file, line );
  92. }
  93. #define inet_aton_fail_ok( text ) \
  94. inet_aton_fail_okx ( text, __FILE__, __LINE__ )
  95. /**
  96. * Perform IPv4 self-tests
  97. *
  98. */
  99. static void ipv4_test_exec ( void ) {
  100. /* Address testing macros */
  101. ok ( IN_IS_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
  102. ok ( ! IN_IS_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
  103. ok ( ! IN_IS_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
  104. ok ( ! IN_IS_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
  105. ok ( IN_IS_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
  106. ok ( ! IN_IS_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
  107. ok ( ! IN_IS_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
  108. ok ( ! IN_IS_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
  109. ok ( IN_IS_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
  110. ok ( ! IN_IS_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
  111. ok ( ! IN_IS_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
  112. ok ( ! IN_IS_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
  113. ok ( ! IN_IS_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
  114. ok ( ! IN_IS_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
  115. ok ( IN_IS_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
  116. ok ( IN_IS_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
  117. ok ( IN_IS_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
  118. /* inet_ntoa() tests */
  119. inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );
  120. inet_ntoa_ok ( IPV4 ( 0, 0, 0, 0 ), "0.0.0.0" );
  121. inet_ntoa_ok ( IPV4 ( 255, 255, 255, 255 ), "255.255.255.255" );
  122. inet_ntoa_ok ( IPV4 ( 212, 13, 204, 60 ), "212.13.204.60" );
  123. /* inet_aton() tests */
  124. inet_aton_ok ( "212.13.204.60", IPV4 ( 212, 13, 204, 60 ) );
  125. inet_aton_ok ( "127.0.0.1", IPV4 ( 127, 0, 0, 1 ) );
  126. /* inet_aton() failure tests */
  127. inet_aton_fail_ok ( "256.0.0.1" ); /* Byte out of range */
  128. inet_aton_fail_ok ( "212.13.204.60.1" ); /* Too long */
  129. inet_aton_fail_ok ( "127.0.0" ); /* Too short */
  130. inet_aton_fail_ok ( "1.2.3.a" ); /* Invalid characters */
  131. inet_aton_fail_ok ( "127.0..1" ); /* Missing bytes */
  132. }
  133. /** IPv4 self-test */
  134. struct self_test ipv4_test __self_test = {
  135. .name = "ipv4",
  136. .exec = ipv4_test_exec,
  137. };