Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ipv4_test.c 4.8KB

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