2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
28 * Line buffer self-tests
32 /* Forcibly enable assertions */
37 #include <ipxe/linebuf.h>
38 #include <ipxe/test.h>
40 /** Define inline raw data */
41 #define DATA(...) { __VA_ARGS__ }
43 /** Define inline lines */
44 #define LINES(...) { __VA_ARGS__ }
46 /** A line buffer test */
50 /** Length of raw data */
52 /** Expected sequence of lines */
54 /** Number of expected lines */
58 /** Line buffer test expected failure indicator */
59 static const char linebuf_failure
[1];
62 * Define a line buffer test
66 * @v LINES Expected sequence of lines
67 * @ret test Line buffer test
69 #define LINEBUF_TEST( name, DATA, LINES ) \
70 static const char name ## _data[] = DATA; \
71 static const char * name ## _lines[] = LINES; \
72 static struct linebuf_test name = { \
73 .data = name ## _data, \
74 .len = ( sizeof ( name ## _data ) - 1 /* NUL */ ), \
75 .lines = name ## _lines, \
76 .count = ( sizeof ( name ## _lines ) / \
77 sizeof ( name ## _lines[0] ) ), \
80 /** Simple line buffer test */
81 LINEBUF_TEST ( simple
,
82 ( "HTTP/1.1 200 OK\r\n"
83 "Content-Length: 123\r\n"
84 "Content-Type: text/plain\r\n"
86 LINES ( "HTTP/1.1 200 OK",
87 "Content-Length: 123",
88 "Content-Type: text/plain",
91 /** Mixed line terminators */
93 ( "LF only\n" "CRLF\r\n" "\n" "\n" "\r\n" "\r\n" "CR only\r" ),
94 LINES ( "LF only", "CRLF", "", "", "", "",
95 NULL
/* \r should not be treated as a terminator */ ) );
97 /** Split consumption: part 1 */
98 LINEBUF_TEST ( split_1
,
102 /** Split consumption: part 2 */
103 LINEBUF_TEST ( split_2
,
107 /** Split consumption: part 3 */
108 LINEBUF_TEST ( split_3
,
109 ( " multiple calls\r\nand so was this one\r" ),
110 LINES ( "This line was split across multiple calls", NULL
) );
112 /** Split consumption: part 4 */
113 LINEBUF_TEST ( split_4
,
114 ( "\nbut not this one\r\n" ),
115 LINES ( "and so was this one", "but not this one" ) );
117 /** Split consumption: part 5 */
118 LINEBUF_TEST ( split_5
,
122 /** Split consumption: part 6 */
123 LINEBUF_TEST ( split_6
,
124 ( "This line came after a zero-length call\r\n" ),
125 LINES ( "This line came after a zero-length call" ) );
128 LINEBUF_TEST ( embedded_nuls
,
129 ( "This\r\ntest\r\nincludes\r\n\r\nsome\0binary\0data\r\n" ),
130 LINES ( "This", "test", "includes", "", linebuf_failure
) );
133 * Report line buffer initialisation test result
135 * @v linebuf Line buffer
136 * @v file Test code file
137 * @v line Test code line
139 static void linebuf_init_okx ( struct line_buffer
*linebuf
,
140 const char *file
, unsigned int line
) {
142 /* Initialise line buffer */
143 memset ( linebuf
, 0, sizeof ( *linebuf
) );
144 okx ( buffered_line ( linebuf
) == NULL
, file
, line
);
146 #define linebuf_init_ok( linebuf ) \
147 linebuf_init_okx ( linebuf, __FILE__, __LINE__ )
150 * Report line buffer consumption test result
152 * @v test Line buffer test
153 * @v linebuf Line buffer
154 * @v file Test code file
155 * @v line Test code line
157 static void linebuf_consume_okx ( struct linebuf_test
*test
,
158 struct line_buffer
*linebuf
,
159 const char *file
, unsigned int line
) {
160 const char *data
= test
->data
;
161 size_t remaining
= test
->len
;
164 const char *expected
;
168 DBGC ( test
, "LINEBUF %p:\n", test
);
169 DBGC_HDA ( test
, 0, data
, remaining
);
171 /* Consume data one line at a time */
172 for ( i
= 0 ; i
< test
->count
; i
++ ) {
174 /* Add data to line buffer */
175 len
= line_buffer ( linebuf
, data
, remaining
);
177 /* Get buffered line, if any */
178 actual
= buffered_line ( linebuf
);
181 DBGC ( test
, "LINEBUF %p %s\n", test
, strerror ( rc
) );
182 } else if ( actual
!= NULL
) {
183 DBGC ( test
, "LINEBUF %p \"%s\" (consumed %d)\n",
186 DBGC ( test
, "LINEBUF %p unterminated (consumed %d)\n",
190 /* Check for success/failure */
191 expected
= test
->lines
[i
];
192 if ( expected
== linebuf_failure
) {
194 okx ( rc
< 0, file
, line
);
195 okx ( remaining
> 0, file
, line
);
198 okx ( len
>= 0, file
, line
);
199 okx ( ( ( size_t ) len
) <= remaining
, file
, line
);
201 /* Check expected result */
202 if ( expected
== NULL
) {
203 okx ( actual
== NULL
, file
, line
);
205 okx ( actual
!= NULL
, file
, line
);
206 okx ( strcmp ( actual
, expected
) == 0, file
, line
);
214 /* Check that all data was consumed */
215 okx ( remaining
== 0, file
, line
);
217 #define linebuf_consume_ok( test, linebuf ) \
218 linebuf_consume_okx ( test, linebuf, __FILE__, __LINE__ )
221 * Report line buffer accumulation test result
223 * @v test Line buffer test
224 * @v linebuf Line buffer
225 * @v file Test code file
226 * @v line Test code line
228 static void linebuf_accumulated_okx ( struct linebuf_test
*test
,
229 struct line_buffer
*linebuf
,
230 const char *file
, unsigned int line
) {
232 const char *expected
;
235 /* Check each accumulated line */
236 actual
= linebuf
->data
;
237 for ( i
= 0 ; i
< test
->count
; i
++ ) {
239 /* Check accumulated line */
240 okx ( actual
!= NULL
, file
, line
);
241 okx ( actual
>= linebuf
->data
, file
, line
);
242 expected
= test
->lines
[i
];
243 if ( ( expected
== NULL
) || ( expected
== linebuf_failure
) )
245 okx ( strcmp ( actual
, expected
) == 0, file
, line
);
247 /* Move to next line */
248 actual
+= ( strlen ( actual
) + 1 /* NUL */ );
249 okx ( actual
<= ( linebuf
->data
+ linebuf
->len
), file
, line
);
252 #define linebuf_accumulated_ok( test, linebuf ) \
253 linebuf_accumulated_okx ( test, linebuf, __FILE__, __LINE__ )
256 * Report line buffer emptying test result
258 * @v linebuf Line buffer
259 * @v file Test code file
260 * @v line Test code line
262 static void linebuf_empty_okx ( struct line_buffer
*linebuf
,
263 const char *file
, unsigned int line
) {
265 /* Empty line buffer */
266 empty_line_buffer ( linebuf
);
267 okx ( buffered_line ( linebuf
) == NULL
, file
, line
);
269 #define linebuf_empty_ok( linebuf ) \
270 linebuf_empty_okx ( linebuf, __FILE__, __LINE__ )
273 * Report line buffer combined test result
275 * @v test Line buffer test
276 * @v file Test code file
277 * @v line Test code line
279 static void linebuf_okx ( struct linebuf_test
*test
, const char *file
,
280 unsigned int line
) {
281 struct line_buffer linebuf
;
283 linebuf_init_okx ( &linebuf
, file
, line
);
284 linebuf_consume_okx ( test
, &linebuf
, file
, line
);
285 linebuf_accumulated_okx ( test
, &linebuf
, file
, line
);
286 linebuf_empty_okx ( &linebuf
, file
, line
);
288 #define linebuf_ok( test ) \
289 linebuf_okx ( test, __FILE__, __LINE__ )
292 * Perform line buffer self-tests
295 static void linebuf_test_exec ( void ) {
296 struct line_buffer linebuf
;
299 linebuf_ok ( &simple
);
300 linebuf_ok ( &mixed
);
302 /* Split consumption test */
303 linebuf_init_ok ( &linebuf
);
304 linebuf_consume_ok ( &split_1
, &linebuf
);
305 linebuf_consume_ok ( &split_2
, &linebuf
);
306 linebuf_consume_ok ( &split_3
, &linebuf
);
307 linebuf_consume_ok ( &split_4
, &linebuf
);
308 linebuf_consume_ok ( &split_5
, &linebuf
);
309 linebuf_consume_ok ( &split_6
, &linebuf
);
310 linebuf_empty_ok ( &linebuf
);
313 linebuf_ok ( &embedded_nuls
);
316 /** Line buffer self-test */
317 struct self_test linebuf_test __self_test
= {
319 .exec
= linebuf_test_exec
,