iPXE
linebuf_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Line buffer self-tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <string.h>
00036 #include <assert.h>
00037 #include <ipxe/linebuf.h>
00038 #include <ipxe/test.h>
00039 
00040 /** Define inline raw data */
00041 #define DATA(...) { __VA_ARGS__ }
00042 
00043 /** Define inline lines */
00044 #define LINES(...) { __VA_ARGS__ }
00045 
00046 /** A line buffer test */
00047 struct linebuf_test {
00048         /** Raw data */
00049         const void *data;
00050         /** Length of raw data */
00051         size_t len;
00052         /** Expected sequence of lines */
00053         const char **lines;
00054         /** Number of expected lines */
00055         unsigned int count;
00056 };
00057 
00058 /** Line buffer test expected failure indicator */
00059 static const char linebuf_failure[1];
00060 
00061 /**
00062  * Define a line buffer test
00063  *
00064  * @v name              Test name
00065  * @v DATA              Raw data
00066  * @v LINES             Expected sequence of lines
00067  * @ret test            Line buffer test
00068  */
00069 #define LINEBUF_TEST( name, DATA, LINES )                               \
00070         static const char name ## _data[] = DATA;                       \
00071         static const char * name ## _lines[] = LINES;                   \
00072         static struct linebuf_test name = {                             \
00073                 .data = name ## _data,                                  \
00074                 .len = ( sizeof ( name ## _data ) - 1 /* NUL */ ),      \
00075                 .lines = name ## _lines,                                \
00076                 .count = ( sizeof ( name ## _lines ) /                  \
00077                            sizeof ( name ## _lines[0] ) ),              \
00078         }
00079 
00080 /** Simple line buffer test */
00081 LINEBUF_TEST ( simple,
00082                ( "HTTP/1.1 200 OK\r\n"
00083                  "Content-Length: 123\r\n"
00084                  "Content-Type: text/plain\r\n"
00085                  "\r\n" ),
00086                LINES ( "HTTP/1.1 200 OK",
00087                        "Content-Length: 123",
00088                        "Content-Type: text/plain",
00089                        "" ) );
00090 
00091 /** Mixed line terminators */
00092 LINEBUF_TEST ( mixed,
00093                ( "LF only\n" "CRLF\r\n" "\n" "\n" "\r\n" "\r\n" "CR only\r" ),
00094                LINES ( "LF only", "CRLF", "", "", "", "",
00095                        NULL /* \r should not be treated as a terminator */ ) );
00096 
00097 /** Split consumption: part 1 */
00098 LINEBUF_TEST ( split_1,
00099                ( "This line was" ),
00100                LINES ( NULL ) );
00101 
00102 /** Split consumption: part 2 */
00103 LINEBUF_TEST ( split_2,
00104                ( " split across" ),
00105                LINES ( NULL ) );
00106 
00107 /** Split consumption: part 3 */
00108 LINEBUF_TEST ( split_3,
00109                ( " multiple calls\r\nand so was this one\r" ),
00110                LINES ( "This line was split across multiple calls", NULL ) );
00111 
00112 /** Split consumption: part 4 */
00113 LINEBUF_TEST ( split_4,
00114                ( "\nbut not this one\r\n" ),
00115                LINES ( "and so was this one", "but not this one" ) );
00116 
00117 /** Split consumption: part 5 */
00118 LINEBUF_TEST ( split_5,
00119                ( "" ),
00120                LINES ( NULL ) );
00121 
00122 /** Split consumption: part 6 */
00123 LINEBUF_TEST ( split_6,
00124                ( "This line came after a zero-length call\r\n" ),
00125                LINES ( "This line came after a zero-length call" ) );
00126 
00127 /** Embedded NULs */
00128 LINEBUF_TEST ( embedded_nuls,
00129                ( "This\r\ntest\r\nincludes\r\n\r\nsome\0binary\0data\r\n" ),
00130                LINES ( "This", "test", "includes", "", linebuf_failure ) );
00131 
00132 /**
00133  * Report line buffer initialisation test result
00134  *
00135  * @v linebuf           Line buffer
00136  * @v file              Test code file
00137  * @v line              Test code line
00138  */
00139 static void linebuf_init_okx ( struct line_buffer *linebuf,
00140                                 const char *file, unsigned int line ) {
00141 
00142         /* Initialise line buffer */
00143         memset ( linebuf, 0, sizeof ( *linebuf ) );
00144         okx ( buffered_line ( linebuf ) == NULL, file, line );
00145 }
00146 #define linebuf_init_ok( linebuf ) \
00147         linebuf_init_okx ( linebuf, __FILE__, __LINE__ )
00148 
00149 /**
00150  * Report line buffer consumption test result
00151  *
00152  * @v test              Line buffer test
00153  * @v linebuf           Line buffer
00154  * @v file              Test code file
00155  * @v line              Test code line
00156  */
00157 static void linebuf_consume_okx ( struct linebuf_test *test,
00158                                   struct line_buffer *linebuf,
00159                                   const char *file, unsigned int line ) {
00160         const char *data = test->data;
00161         size_t remaining = test->len;
00162         int len;
00163         unsigned int i;
00164         const char *expected;
00165         char *actual;
00166         int rc;
00167 
00168         DBGC ( test, "LINEBUF %p:\n", test );
00169         DBGC_HDA ( test, 0, data, remaining );
00170 
00171         /* Consume data one line at a time */
00172         for ( i = 0 ; i < test->count ; i++ ) {
00173 
00174                 /* Add data to line buffer */
00175                 len = line_buffer ( linebuf, data, remaining );
00176 
00177                 /* Get buffered line, if any */
00178                 actual = buffered_line ( linebuf );
00179                 if ( len < 0 ) {
00180                         rc = len;
00181                         DBGC ( test, "LINEBUF %p %s\n", test, strerror ( rc ) );
00182                 } else if ( actual != NULL ) {
00183                         DBGC ( test, "LINEBUF %p \"%s\" (consumed %d)\n",
00184                                test, actual, len );
00185                 } else {
00186                         DBGC ( test, "LINEBUF %p unterminated (consumed %d)\n",
00187                                test, len );
00188                 }
00189 
00190                 /* Check for success/failure */
00191                 expected = test->lines[i];
00192                 if ( expected == linebuf_failure ) {
00193                         rc = len;
00194                         okx ( rc < 0, file, line );
00195                         okx ( remaining > 0, file, line );
00196                         return;
00197                 }
00198                 okx ( len >= 0, file, line );
00199                 okx ( ( ( size_t ) len ) <= remaining, file, line );
00200 
00201                 /* Check expected result */
00202                 if ( expected == NULL ) {
00203                         okx ( actual == NULL, file, line );
00204                 } else {
00205                         okx ( actual != NULL, file, line );
00206                         okx ( strcmp ( actual, expected ) == 0, file, line );
00207                 }
00208 
00209                 /* Consume data */
00210                 data += len;
00211                 remaining -= len;
00212         }
00213 
00214         /* Check that all data was consumed */
00215         okx ( remaining == 0, file, line );
00216 }
00217 #define linebuf_consume_ok( test, linebuf ) \
00218         linebuf_consume_okx ( test, linebuf, __FILE__, __LINE__ )
00219 
00220 /**
00221  * Report line buffer accumulation test result
00222  *
00223  * @v test              Line buffer test
00224  * @v linebuf           Line buffer
00225  * @v file              Test code file
00226  * @v line              Test code line
00227  */
00228 static void linebuf_accumulated_okx ( struct linebuf_test *test,
00229                                       struct line_buffer *linebuf,
00230                                       const char *file, unsigned int line ) {
00231         const char *actual;
00232         const char *expected;
00233         unsigned int i;
00234 
00235         /* Check each accumulated line */
00236         actual = linebuf->data;
00237         for ( i = 0 ; i < test->count ; i++ ) {
00238 
00239                 /* Check accumulated line */
00240                 okx ( actual != NULL, file, line );
00241                 okx ( actual >= linebuf->data, file, line );
00242                 expected = test->lines[i];
00243                 if ( ( expected == NULL ) || ( expected == linebuf_failure ) )
00244                         return;
00245                 okx ( strcmp ( actual, expected ) == 0, file, line );
00246 
00247                 /* Move to next line */
00248                 actual += ( strlen ( actual ) + 1 /* NUL */ );
00249                 okx ( actual <= ( linebuf->data + linebuf->len ), file, line );
00250         }
00251 }
00252 #define linebuf_accumulated_ok( test, linebuf ) \
00253         linebuf_accumulated_okx ( test, linebuf, __FILE__, __LINE__ )
00254 
00255 /**
00256  * Report line buffer emptying test result
00257  *
00258  * @v linebuf           Line buffer
00259  * @v file              Test code file
00260  * @v line              Test code line
00261  */
00262 static void linebuf_empty_okx ( struct line_buffer *linebuf,
00263                                 const char *file, unsigned int line ) {
00264 
00265         /* Empty line buffer */
00266         empty_line_buffer ( linebuf );
00267         okx ( buffered_line ( linebuf ) == NULL, file, line );
00268 }
00269 #define linebuf_empty_ok( linebuf ) \
00270         linebuf_empty_okx ( linebuf, __FILE__, __LINE__ )
00271 
00272 /**
00273  * Report line buffer combined test result
00274  *
00275  * @v test              Line buffer test
00276  * @v file              Test code file
00277  * @v line              Test code line
00278  */
00279 static void linebuf_okx ( struct linebuf_test *test, const char *file,
00280                           unsigned int line ) {
00281         struct line_buffer linebuf;
00282 
00283         linebuf_init_okx ( &linebuf, file, line );
00284         linebuf_consume_okx ( test, &linebuf, file, line );
00285         linebuf_accumulated_okx ( test, &linebuf, file, line );
00286         linebuf_empty_okx ( &linebuf, file, line );
00287 }
00288 #define linebuf_ok( test ) \
00289         linebuf_okx ( test, __FILE__, __LINE__ )
00290 
00291 /**
00292  * Perform line buffer self-tests
00293  *
00294  */
00295 static void linebuf_test_exec ( void ) {
00296         struct line_buffer linebuf;
00297 
00298         /* Basic tests */
00299         linebuf_ok ( &simple );
00300         linebuf_ok ( &mixed );
00301 
00302         /* Split consumption test */
00303         linebuf_init_ok ( &linebuf );
00304         linebuf_consume_ok ( &split_1, &linebuf );
00305         linebuf_consume_ok ( &split_2, &linebuf );
00306         linebuf_consume_ok ( &split_3, &linebuf );
00307         linebuf_consume_ok ( &split_4, &linebuf );
00308         linebuf_consume_ok ( &split_5, &linebuf );
00309         linebuf_consume_ok ( &split_6, &linebuf );
00310         linebuf_empty_ok ( &linebuf );
00311 
00312         /* Embedded NULs */
00313         linebuf_ok ( &embedded_nuls );
00314 }
00315 
00316 /** Line buffer self-test */
00317 struct self_test linebuf_test __self_test = {
00318         .name = "linebuf",
00319         .exec = linebuf_test_exec,
00320 };