iPXE
Data Structures | Defines | Functions | Variables
linebuf_test.c File Reference

Line buffer self-tests. More...

#include <string.h>
#include <assert.h>
#include <ipxe/linebuf.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  linebuf_test
 A line buffer test. More...

Defines

#define DATA(...)   { __VA_ARGS__ }
 Define inline raw data.
#define LINES(...)   { __VA_ARGS__ }
 Define inline lines.
#define LINEBUF_TEST(name, DATA, LINES)
 Define a line buffer test.
#define linebuf_init_ok(linebuf)   linebuf_init_okx ( linebuf, __FILE__, __LINE__ )
#define linebuf_consume_ok(test, linebuf)   linebuf_consume_okx ( test, linebuf, __FILE__, __LINE__ )
#define linebuf_accumulated_ok(test, linebuf)   linebuf_accumulated_okx ( test, linebuf, __FILE__, __LINE__ )
#define linebuf_empty_ok(linebuf)   linebuf_empty_okx ( linebuf, __FILE__, __LINE__ )
#define linebuf_ok(test)   linebuf_okx ( test, __FILE__, __LINE__ )

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 LINEBUF_TEST (simple,("HTTP/1.1 200 OK\r\n""Content-Length: 123\r\n""Content-Type: text/plain\r\n""\r\n"), LINES("HTTP/1.1 200 OK","Content-Length: 123","Content-Type: text/plain",""))
 Simple line buffer test.
 LINEBUF_TEST (mixed,("LF only\n""CRLF\r\n""\n""\n""\r\n""\r\n""CR only\r"), LINES("LF only","CRLF","","","","", NULL))
 Mixed line terminators.
 LINEBUF_TEST (split_1,("This line was"), LINES(NULL))
 Split consumption: part 1.
 LINEBUF_TEST (split_2,(" split across"), LINES(NULL))
 Split consumption: part 2.
 LINEBUF_TEST (split_3,(" multiple calls\r\nand so was this one\r"), LINES("This line was split across multiple calls", NULL))
 Split consumption: part 3.
 LINEBUF_TEST (split_4,("\nbut not this one\r\n"), LINES("and so was this one","but not this one"))
 Split consumption: part 4.
 LINEBUF_TEST (split_5,(""), LINES(NULL))
 Split consumption: part 5.
 LINEBUF_TEST (split_6,("This line came after a zero-length call\r\n"), LINES("This line came after a zero-length call"))
 Split consumption: part 6.
 LINEBUF_TEST (embedded_nuls,("This\r\ntest\r\nincludes\r\n\r\nsome\0binary\0data\r\n"), LINES("This","test","includes","", linebuf_failure))
 Embedded NULs.
static void linebuf_init_okx (struct line_buffer *linebuf, const char *file, unsigned int line)
 Report line buffer initialisation test result.
static void linebuf_consume_okx (struct linebuf_test *test, struct line_buffer *linebuf, const char *file, unsigned int line)
 Report line buffer consumption test result.
static void linebuf_accumulated_okx (struct linebuf_test *test, struct line_buffer *linebuf, const char *file, unsigned int line)
 Report line buffer accumulation test result.
static void linebuf_empty_okx (struct line_buffer *linebuf, const char *file, unsigned int line)
 Report line buffer emptying test result.
static void linebuf_okx (struct linebuf_test *test, const char *file, unsigned int line)
 Report line buffer combined test result.
static void linebuf_test_exec (void)
 Perform line buffer self-tests.

Variables

static const char linebuf_failure [1]
 Line buffer test expected failure indicator.
struct self_test linebuf_test __self_test
 Line buffer self-test.

Detailed Description

Line buffer self-tests.

Definition in file linebuf_test.c.


Define Documentation

#define DATA (   ...)    { __VA_ARGS__ }

Define inline raw data.

Definition at line 41 of file linebuf_test.c.

#define LINES (   ...)    { __VA_ARGS__ }

Define inline lines.

Definition at line 44 of file linebuf_test.c.

Referenced by _enter_slk(), _leave_slk(), endwin(), initscr(), mvwin(), pxe_menu_draw_item(), slk_init(), vga_putc(), and video_scroll().

#define LINEBUF_TEST (   name,
  DATA,
  LINES 
)
Value:
static const char name ## _data[] = DATA;                       \
        static const char * name ## _lines[] = LINES;                   \
        static struct linebuf_test name = {                             \
                .data = name ## _data,                                  \
                .len = ( sizeof ( name ## _data ) - 1 /* NUL */ ),      \
                .lines = name ## _lines,                                \
                .count = ( sizeof ( name ## _lines ) /                  \
                           sizeof ( name ## _lines[0] ) ),              \
        }

Define a line buffer test.

Parameters:
nameTest name
DATARaw data
LINESExpected sequence of lines
Return values:
testLine buffer test

Definition at line 69 of file linebuf_test.c.

#define linebuf_init_ok (   linebuf)    linebuf_init_okx ( linebuf, __FILE__, __LINE__ )

Definition at line 146 of file linebuf_test.c.

Referenced by linebuf_test_exec().

#define linebuf_consume_ok (   test,
  linebuf 
)    linebuf_consume_okx ( test, linebuf, __FILE__, __LINE__ )

Definition at line 217 of file linebuf_test.c.

Referenced by linebuf_test_exec().

#define linebuf_accumulated_ok (   test,
  linebuf 
)    linebuf_accumulated_okx ( test, linebuf, __FILE__, __LINE__ )

Definition at line 252 of file linebuf_test.c.

#define linebuf_empty_ok (   linebuf)    linebuf_empty_okx ( linebuf, __FILE__, __LINE__ )

Definition at line 269 of file linebuf_test.c.

Referenced by linebuf_test_exec().

#define linebuf_ok (   test)    linebuf_okx ( test, __FILE__, __LINE__ )

Definition at line 288 of file linebuf_test.c.

Referenced by linebuf_test_exec().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
LINEBUF_TEST ( simple  ,
("HTTP/1.1 200 OK\r\n""Content-Length: 123\r\n""Content-Type: text/plain\r\n""\r\n")  ,
LINES("HTTP/1.1 200 OK","Content-Length: 123","Content-Type: text/plain","")   
)

Simple line buffer test.

LINEBUF_TEST ( mixed  ,
("LF only\n""CRLF\r\n""\n""\n""\r\n""\r\n""CR only\r")  ,
LINES("LF only","CRLF","","","","", NULL)   
)

Mixed line terminators.

LINEBUF_TEST ( split_1  ,
("This line was")  ,
LINES(NULL  
)

Split consumption: part 1.

LINEBUF_TEST ( split_2  ,
(" split across")  ,
LINES(NULL  
)

Split consumption: part 2.

LINEBUF_TEST ( split_3  ,
(" multiple calls\r\nand so was this one\r")  ,
LINES("This line was split across multiple calls", NULL)   
)

Split consumption: part 3.

LINEBUF_TEST ( split_4  ,
("\nbut not this one\r\n")  ,
LINES("and so was this one","but not this one")   
)

Split consumption: part 4.

LINEBUF_TEST ( split_5  ,
("")  ,
LINES(NULL  
)

Split consumption: part 5.

LINEBUF_TEST ( split_6  ,
("This line came after a zero-length call\r\n")  ,
LINES("This line came after a zero-length call")   
)

Split consumption: part 6.

LINEBUF_TEST ( embedded_nuls  ,
("This\r\ntest\r\nincludes\r\n\r\nsome\0binary\0data\r\n")  ,
LINES("This","test","includes","", linebuf_failure)   
)

Embedded NULs.

static void linebuf_init_okx ( struct line_buffer linebuf,
const char *  file,
unsigned int  line 
) [static]

Report line buffer initialisation test result.

Parameters:
linebufLine buffer
fileTest code file
lineTest code line

Definition at line 139 of file linebuf_test.c.

References buffered_line(), memset(), NULL, and okx.

Referenced by linebuf_okx().

                                                                      {

        /* Initialise line buffer */
        memset ( linebuf, 0, sizeof ( *linebuf ) );
        okx ( buffered_line ( linebuf ) == NULL, file, line );
}
static void linebuf_consume_okx ( struct linebuf_test test,
struct line_buffer linebuf,
const char *  file,
unsigned int  line 
) [static]

Report line buffer consumption test result.

Parameters:
testLine buffer test
linebufLine buffer
fileTest code file
lineTest code line

Definition at line 157 of file linebuf_test.c.

References buffered_line(), linebuf_test::count, linebuf_test::data, data, DBGC, DBGC_HDA, linebuf_test::len, len, line_buffer(), linebuf_failure, linebuf_test::lines, NULL, okx, rc, strcmp(), and strerror().

Referenced by linebuf_okx().

                                                                        {
        const char *data = test->data;
        size_t remaining = test->len;
        int len;
        unsigned int i;
        const char *expected;
        char *actual;
        int rc;

        DBGC ( test, "LINEBUF %p:\n", test );
        DBGC_HDA ( test, 0, data, remaining );

        /* Consume data one line at a time */
        for ( i = 0 ; i < test->count ; i++ ) {

                /* Add data to line buffer */
                len = line_buffer ( linebuf, data, remaining );

                /* Get buffered line, if any */
                actual = buffered_line ( linebuf );
                if ( len < 0 ) {
                        rc = len;
                        DBGC ( test, "LINEBUF %p %s\n", test, strerror ( rc ) );
                } else if ( actual != NULL ) {
                        DBGC ( test, "LINEBUF %p \"%s\" (consumed %d)\n",
                               test, actual, len );
                } else {
                        DBGC ( test, "LINEBUF %p unterminated (consumed %d)\n",
                               test, len );
                }

                /* Check for success/failure */
                expected = test->lines[i];
                if ( expected == linebuf_failure ) {
                        rc = len;
                        okx ( rc < 0, file, line );
                        okx ( remaining > 0, file, line );
                        return;
                }
                okx ( len >= 0, file, line );
                okx ( ( ( size_t ) len ) <= remaining, file, line );

                /* Check expected result */
                if ( expected == NULL ) {
                        okx ( actual == NULL, file, line );
                } else {
                        okx ( actual != NULL, file, line );
                        okx ( strcmp ( actual, expected ) == 0, file, line );
                }

                /* Consume data */
                data += len;
                remaining -= len;
        }

        /* Check that all data was consumed */
        okx ( remaining == 0, file, line );
}
static void linebuf_accumulated_okx ( struct linebuf_test test,
struct line_buffer linebuf,
const char *  file,
unsigned int  line 
) [static]

Report line buffer accumulation test result.

Parameters:
testLine buffer test
linebufLine buffer
fileTest code file
lineTest code line

Definition at line 228 of file linebuf_test.c.

References linebuf_test::count, line_buffer::data, line_buffer::len, linebuf_failure, linebuf_test::lines, NULL, okx, strcmp(), and strlen().

Referenced by linebuf_okx().

                                                                            {
        const char *actual;
        const char *expected;
        unsigned int i;

        /* Check each accumulated line */
        actual = linebuf->data;
        for ( i = 0 ; i < test->count ; i++ ) {

                /* Check accumulated line */
                okx ( actual != NULL, file, line );
                okx ( actual >= linebuf->data, file, line );
                expected = test->lines[i];
                if ( ( expected == NULL ) || ( expected == linebuf_failure ) )
                        return;
                okx ( strcmp ( actual, expected ) == 0, file, line );

                /* Move to next line */
                actual += ( strlen ( actual ) + 1 /* NUL */ );
                okx ( actual <= ( linebuf->data + linebuf->len ), file, line );
        }
}
static void linebuf_empty_okx ( struct line_buffer linebuf,
const char *  file,
unsigned int  line 
) [static]

Report line buffer emptying test result.

Parameters:
linebufLine buffer
fileTest code file
lineTest code line

Definition at line 262 of file linebuf_test.c.

References buffered_line(), empty_line_buffer(), NULL, and okx.

Referenced by linebuf_okx().

                                                                      {

        /* Empty line buffer */
        empty_line_buffer ( linebuf );
        okx ( buffered_line ( linebuf ) == NULL, file, line );
}
static void linebuf_okx ( struct linebuf_test test,
const char *  file,
unsigned int  line 
) [static]

Report line buffer combined test result.

Parameters:
testLine buffer test
fileTest code file
lineTest code line

Definition at line 279 of file linebuf_test.c.

References linebuf_accumulated_okx(), linebuf_consume_okx(), linebuf_empty_okx(), and linebuf_init_okx().

                                              {
        struct line_buffer linebuf;

        linebuf_init_okx ( &linebuf, file, line );
        linebuf_consume_okx ( test, &linebuf, file, line );
        linebuf_accumulated_okx ( test, &linebuf, file, line );
        linebuf_empty_okx ( &linebuf, file, line );
}
static void linebuf_test_exec ( void  ) [static]

Perform line buffer self-tests.

Definition at line 295 of file linebuf_test.c.

References linebuf_consume_ok, linebuf_empty_ok, linebuf_init_ok, and linebuf_ok.

                                       {
        struct line_buffer linebuf;

        /* Basic tests */
        linebuf_ok ( &simple );
        linebuf_ok ( &mixed );

        /* Split consumption test */
        linebuf_init_ok ( &linebuf );
        linebuf_consume_ok ( &split_1, &linebuf );
        linebuf_consume_ok ( &split_2, &linebuf );
        linebuf_consume_ok ( &split_3, &linebuf );
        linebuf_consume_ok ( &split_4, &linebuf );
        linebuf_consume_ok ( &split_5, &linebuf );
        linebuf_consume_ok ( &split_6, &linebuf );
        linebuf_empty_ok ( &linebuf );

        /* Embedded NULs */
        linebuf_ok ( &embedded_nuls );
}

Variable Documentation

const char linebuf_failure[1] [static]

Line buffer test expected failure indicator.

Definition at line 59 of file linebuf_test.c.

Referenced by linebuf_accumulated_okx(), and linebuf_consume_okx().

Initial value:
 {
        .name = "linebuf",
        .exec = linebuf_test_exec,
}

Line buffer self-test.

Definition at line 317 of file linebuf_test.c.