[efi] Include installed protocol list in unknown handle names
[ipxe.git] / src / tests / tcpip_test.c
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 * 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
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27 *
28 * TCP/IP self-tests
29 *
30 */
31
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <assert.h>
39 #include <ipxe/test.h>
40 #include <ipxe/profile.h>
41 #include <ipxe/tcpip.h>
42
43 /** Number of sample iterations for profiling */
44 #define PROFILE_COUNT 16
45
46 /** A TCP/IP fixed-data test */
47 struct tcpip_test {
48 /** Data */
49 const void *data;
50 /** Length of data */
51 size_t len;
52 };
53
54 /** A TCP/IP pseudorandom-data test */
55 struct tcpip_random_test {
56 /** Seed */
57 unsigned int seed;
58 /** Length of data */
59 size_t len;
60 /** Alignment offset */
61 size_t offset;
62 };
63
64 /** Define inline data */
65 #define DATA(...) { __VA_ARGS__ }
66
67 /** Define a TCP/IP fixed-data test */
68 #define TCPIP_TEST( name, DATA ) \
69 static const uint8_t __attribute__ (( aligned ( 16 ) )) \
70 name ## _data[] = DATA; \
71 static struct tcpip_test name = { \
72 .data = name ## _data, \
73 .len = sizeof ( name ## _data ), \
74 }
75
76 /** Define a TCP/IP pseudorandom-data test */
77 #define TCPIP_RANDOM_TEST( name, SEED, LEN, OFFSET ) \
78 static struct tcpip_random_test name = { \
79 .seed = SEED, \
80 .len = LEN, \
81 .offset = OFFSET, \
82 }
83
84 /** Buffer for pseudorandom-data tests */
85 static uint8_t __attribute__ (( aligned ( 16 ) ))
86 tcpip_data[ 4096 + 7 /* offset */ ];
87
88 /** Empty data */
89 TCPIP_TEST ( empty, DATA() );
90
91 /** Single byte */
92 TCPIP_TEST ( one_byte, DATA ( 0xeb ) );
93
94 /** Double byte */
95 TCPIP_TEST ( two_bytes, DATA ( 0xba, 0xbe ) );
96
97 /** Final wrap-around carry (big-endian) */
98 TCPIP_TEST ( final_carry_big,
99 DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ) );
100
101 /** Final wrap-around carry (little-endian) */
102 TCPIP_TEST ( final_carry_little,
103 DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 ) );
104
105 /** Random data (aligned) */
106 TCPIP_RANDOM_TEST ( random_aligned, 0x12345678UL, 4096, 0 );
107
108 /** Random data (unaligned, +1) */
109 TCPIP_RANDOM_TEST ( random_unaligned_1, 0x12345678UL, 4096, 1 );
110
111 /** Random data (unaligned, +2) */
112 TCPIP_RANDOM_TEST ( random_unaligned_2, 0x12345678UL, 4096, 2 );
113
114 /** Random data (aligned, truncated) */
115 TCPIP_RANDOM_TEST ( random_aligned_truncated, 0x12345678UL, 4095, 0 );
116
117 /** Random data (unaligned start and finish) */
118 TCPIP_RANDOM_TEST ( partial, 0xcafebabe, 121, 5 );
119
120 /**
121 * Calculate TCP/IP checksum
122 *
123 * @v data Data to sum
124 * @v len Length of data
125 * @ret cksum Checksum
126 *
127 * This is a reference implementation taken from RFC1071 (and modified
128 * to fix compilation without warnings under gcc).
129 */
130 static uint16_t rfc_tcpip_chksum ( const void *data, size_t len ) {
131 unsigned long sum = 0;
132
133 while ( len > 1 ) {
134 sum += *( ( uint16_t * ) data );
135 data += 2;
136 len -= 2;
137 }
138
139 if ( len > 0 )
140 sum += *( ( uint8_t * ) data );
141
142 while ( sum >> 16 )
143 sum = ( ( sum & 0xffff ) + ( sum >> 16 ) );
144
145 return ~sum;
146 }
147
148 /**
149 * Report TCP/IP fixed-data test result
150 *
151 * @v test TCP/IP test
152 * @v file Test code file
153 * @v line Test code line
154 */
155 static void tcpip_okx ( struct tcpip_test *test, const char *file,
156 unsigned int line ) {
157 uint16_t expected;
158 uint16_t generic_sum;
159 uint16_t sum;
160
161 /* Verify generic_tcpip_continue_chksum() result */
162 expected = rfc_tcpip_chksum ( test->data, test->len );
163 generic_sum = generic_tcpip_continue_chksum ( TCPIP_EMPTY_CSUM,
164 test->data, test->len );
165 okx ( generic_sum == expected, file, line );
166
167 /* Verify optimised tcpip_continue_chksum() result */
168 sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, test->data, test->len );
169 okx ( sum == expected, file, line );
170 }
171 #define tcpip_ok( test ) tcpip_okx ( test, __FILE__, __LINE__ )
172
173 /**
174 * Report TCP/IP pseudorandom-data test result
175 *
176 * @v test TCP/IP test
177 * @v file Test code file
178 * @v line Test code line
179 */
180 static void tcpip_random_okx ( struct tcpip_random_test *test,
181 const char *file, unsigned int line ) {
182 uint8_t *data = ( tcpip_data + test->offset );
183 struct profiler profiler;
184 uint16_t expected;
185 uint16_t generic_sum;
186 uint16_t sum;
187 unsigned int i;
188
189 /* Sanity check */
190 assert ( ( test->len + test->offset ) <= sizeof ( tcpip_data ) );
191
192 /* Generate random data */
193 srandom ( test->seed );
194 for ( i = 0 ; i < test->len ; i++ )
195 data[i] = random();
196
197 /* Verify generic_tcpip_continue_chksum() result */
198 expected = rfc_tcpip_chksum ( data, test->len );
199 generic_sum = generic_tcpip_continue_chksum ( TCPIP_EMPTY_CSUM,
200 data, test->len );
201 okx ( generic_sum == expected, file, line );
202
203 /* Verify optimised tcpip_continue_chksum() result */
204 sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, test->len );
205 okx ( sum == expected, file, line );
206
207 /* Profile optimised calculation */
208 memset ( &profiler, 0, sizeof ( profiler ) );
209 for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
210 profile_start ( &profiler );
211 sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data,
212 test->len );
213 profile_stop ( &profiler );
214 }
215 DBG ( "TCPIP checksummed %zd bytes (+%zd) in %ld +/- %ld ticks\n",
216 test->len, test->offset, profile_mean ( &profiler ),
217 profile_stddev ( &profiler ) );
218 }
219 #define tcpip_random_ok( test ) tcpip_random_okx ( test, __FILE__, __LINE__ )
220
221 /**
222 * Perform TCP/IP self-tests
223 *
224 */
225 static void tcpip_test_exec ( void ) {
226
227 tcpip_ok ( &empty );
228 tcpip_ok ( &one_byte );
229 tcpip_ok ( &two_bytes );
230 tcpip_ok ( &final_carry_big );
231 tcpip_ok ( &final_carry_little );
232 tcpip_random_ok ( &random_aligned );
233 tcpip_random_ok ( &random_unaligned_1 );
234 tcpip_random_ok ( &random_unaligned_2 );
235 tcpip_random_ok ( &random_aligned_truncated );
236 tcpip_random_ok ( &partial );
237 }
238
239 /** TCP/IP self-test */
240 struct self_test tcpip_test __self_test = {
241 .name = "tcpip",
242 .exec = tcpip_test_exec,
243 };