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.

tcpip_test.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (C) 2012 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. * TCP/IP self-tests
  23. *
  24. */
  25. /* Forcibly enable assertions */
  26. #undef NDEBUG
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <assert.h>
  31. #include <ipxe/test.h>
  32. #include <ipxe/profile.h>
  33. #include <ipxe/tcpip.h>
  34. /** A TCP/IP fixed-data test */
  35. struct tcpip_test {
  36. /** Data */
  37. const void *data;
  38. /** Length of data */
  39. size_t len;
  40. };
  41. /** A TCP/IP pseudorandom-data test */
  42. struct tcpip_random_test {
  43. /** Seed */
  44. unsigned int seed;
  45. /** Length of data */
  46. size_t len;
  47. /** Alignment offset */
  48. size_t offset;
  49. };
  50. /** Define inline data */
  51. #define DATA(...) { __VA_ARGS__ }
  52. /** Define a TCP/IP fixed-data test */
  53. #define TCPIP_TEST( name, DATA ) \
  54. static const uint8_t __attribute__ (( aligned ( 16 ) )) \
  55. name ## _data[] = DATA; \
  56. static struct tcpip_test name = { \
  57. .data = name ## _data, \
  58. .len = sizeof ( name ## _data ), \
  59. }
  60. /** Define a TCP/IP pseudorandom-data test */
  61. #define TCPIP_RANDOM_TEST( name, SEED, LEN, OFFSET ) \
  62. static struct tcpip_random_test name = { \
  63. .seed = SEED, \
  64. .len = LEN, \
  65. .offset = OFFSET, \
  66. }
  67. /** Buffer for pseudorandom-data tests */
  68. static uint8_t __attribute__ (( aligned ( 16 ) ))
  69. tcpip_data[ 4096 + 7 /* offset */ ];
  70. /** Empty data */
  71. TCPIP_TEST ( empty, DATA() );
  72. /** Single byte */
  73. TCPIP_TEST ( one_byte, DATA ( 0xeb ) );
  74. /** Double byte */
  75. TCPIP_TEST ( two_bytes, DATA ( 0xba, 0xbe ) );
  76. /** Final wrap-around carry (big-endian) */
  77. TCPIP_TEST ( final_carry_big,
  78. DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ) );
  79. /** Final wrap-around carry (little-endian) */
  80. TCPIP_TEST ( final_carry_little,
  81. DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 ) );
  82. /** Random data (aligned) */
  83. TCPIP_RANDOM_TEST ( random_aligned, 0x12345678UL, 4096, 0 );
  84. /** Random data (unaligned, +1) */
  85. TCPIP_RANDOM_TEST ( random_unaligned_1, 0x12345678UL, 4096, 1 );
  86. /** Random data (unaligned, +2) */
  87. TCPIP_RANDOM_TEST ( random_unaligned_2, 0x12345678UL, 4096, 2 );
  88. /** Random data (aligned, truncated) */
  89. TCPIP_RANDOM_TEST ( random_aligned_truncated, 0x12345678UL, 4095, 0 );
  90. /** Random data (unaligned start and finish) */
  91. TCPIP_RANDOM_TEST ( partial, 0xcafebabe, 121, 5 );
  92. /**
  93. * Calculate TCP/IP checksum
  94. *
  95. * @v data Data to sum
  96. * @v len Length of data
  97. * @ret cksum Checksum
  98. *
  99. * This is a reference implementation taken from RFC1071 (and modified
  100. * to fix compilation without warnings under gcc).
  101. */
  102. static uint16_t rfc_tcpip_chksum ( const void *data, size_t len ) {
  103. unsigned long sum = 0;
  104. while ( len > 1 ) {
  105. sum += *( ( uint16_t * ) data );
  106. data += 2;
  107. len -= 2;
  108. }
  109. if ( len > 0 )
  110. sum += *( ( uint8_t * ) data );
  111. while ( sum >> 16 )
  112. sum = ( ( sum & 0xffff ) + ( sum >> 16 ) );
  113. return ~sum;
  114. }
  115. /**
  116. * Report TCP/IP fixed-data test result
  117. *
  118. * @v test TCP/IP test
  119. */
  120. #define tcpip_ok( test ) do { \
  121. uint16_t expected; \
  122. uint16_t generic_sum; \
  123. uint16_t sum; \
  124. expected = rfc_tcpip_chksum ( (test)->data, (test)->len ); \
  125. generic_sum = generic_tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, \
  126. (test)->data, \
  127. (test)->len ); \
  128. ok ( generic_sum == expected ); \
  129. sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, (test)->data, \
  130. (test)->len ); \
  131. ok ( sum == expected ); \
  132. } while ( 0 )
  133. /**
  134. * Report TCP/IP pseudorandom-data test result
  135. *
  136. * @v test TCP/IP test
  137. */
  138. #define tcpip_random_ok( test ) do { \
  139. uint8_t *data = ( tcpip_data + (test)->offset ); \
  140. uint16_t expected; \
  141. uint16_t generic_sum; \
  142. uint16_t sum; \
  143. unsigned long elapsed; \
  144. unsigned int i; \
  145. assert ( ( (test)->len + (test)->offset ) <= \
  146. sizeof ( tcpip_data ) ); \
  147. srandom ( (test)->seed ); \
  148. for ( i = 0 ; i < (test)->len ; i++ ) \
  149. data[i] = random(); \
  150. expected = rfc_tcpip_chksum ( data, (test)->len ); \
  151. generic_sum = generic_tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, \
  152. data, \
  153. (test)->len ); \
  154. ok ( generic_sum == expected ); \
  155. simple_profile(); \
  156. sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, \
  157. (test)->len ); \
  158. elapsed = simple_profile(); \
  159. ok ( sum == expected ); \
  160. DBG ( "TCPIP checksummed %zd bytes (+%zd) in %ld ticks\n", \
  161. (test)->len, (test)->offset, elapsed ); \
  162. } while ( 0 )
  163. /**
  164. * Perform TCP/IP self-tests
  165. *
  166. */
  167. static void tcpip_test_exec ( void ) {
  168. tcpip_ok ( &empty );
  169. tcpip_ok ( &one_byte );
  170. tcpip_ok ( &two_bytes );
  171. tcpip_ok ( &final_carry_big );
  172. tcpip_ok ( &final_carry_little );
  173. tcpip_random_ok ( &random_aligned );
  174. tcpip_random_ok ( &random_unaligned_1 );
  175. tcpip_random_ok ( &random_unaligned_2 );
  176. tcpip_random_ok ( &random_aligned_truncated );
  177. tcpip_random_ok ( &partial );
  178. }
  179. /** TCP/IP self-test */
  180. struct self_test tcpip_test __self_test = {
  181. .name = "tcpip",
  182. .exec = tcpip_test_exec,
  183. };