iPXE
Data Structures | Macros | 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...
 

Macros

#define DATA(...)   { __VA_ARGS__ }
 Define inline raw data. More...
 
#define LINES(...)   { __VA_ARGS__ }
 Define inline lines. More...
 
#define LINEBUF_TEST(name, DATA, LINES)
 Define a line buffer test. More...
 
#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. More...
 
 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. More...
 
 LINEBUF_TEST (split_1,("This line was"), LINES(NULL))
 Split consumption: part 1. More...
 
 LINEBUF_TEST (split_2,(" split across"), LINES(NULL))
 Split consumption: part 2. More...
 
 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. More...
 
 LINEBUF_TEST (split_4,("\nbut not this one\r\n"), LINES("and so was this one", "but not this one"))
 Split consumption: part 4. More...
 
 LINEBUF_TEST (split_5,(""), LINES(NULL))
 Split consumption: part 5. More...
 
 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. More...
 
 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. More...
 
static void linebuf_init_okx (struct line_buffer *linebuf, const char *file, unsigned int line)
 Report line buffer initialisation test result. More...
 
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. More...
 
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. More...
 
static void linebuf_empty_okx (struct line_buffer *linebuf, const char *file, unsigned int line)
 Report line buffer emptying test result. More...
 
static void linebuf_okx (struct linebuf_test *test, const char *file, unsigned int line)
 Report line buffer combined test result. More...
 
static void linebuf_test_exec (void)
 Perform line buffer self-tests. More...
 

Variables

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

Detailed Description

Line buffer self-tests.

Definition in file linebuf_test.c.

Macro Definition Documentation

◆ DATA

#define DATA (   ...)    { __VA_ARGS__ }

Define inline raw data.

Definition at line 41 of file linebuf_test.c.

◆ LINES

#define LINES (   ...)    { __VA_ARGS__ }

Define inline lines.

Definition at line 44 of file linebuf_test.c.

◆ LINEBUF_TEST

#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] ) ), \
}
const char * name
Definition: ath9k_hw.c:1984
#define DATA(...)
Define inline raw data.
Definition: linebuf_test.c:41
#define LINES(...)
Define inline lines.
Definition: linebuf_test.c:44
A line buffer test.
Definition: linebuf_test.c:47

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.

◆ linebuf_init_ok

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

Definition at line 146 of file linebuf_test.c.

◆ linebuf_consume_ok

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

Definition at line 217 of file linebuf_test.c.

◆ linebuf_accumulated_ok

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

Definition at line 252 of file linebuf_test.c.

◆ linebuf_empty_ok

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

Definition at line 269 of file linebuf_test.c.

◆ linebuf_ok

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

Definition at line 288 of file linebuf_test.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ LINEBUF_TEST() [1/9]

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() [2/9]

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() [3/9]

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

Split consumption: part 1.

◆ LINEBUF_TEST() [4/9]

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

Split consumption: part 2.

◆ LINEBUF_TEST() [5/9]

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() [6/9]

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() [7/9]

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

Split consumption: part 5.

◆ LINEBUF_TEST() [8/9]

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() [9/9]

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.

◆ linebuf_init_okx()

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.

140  {
141 
142  /* Initialise line buffer */
143  memset ( linebuf, 0, sizeof ( *linebuf ) );
144  okx ( buffered_line ( linebuf ) == NULL, file, line );
145 }
#define okx(success, file, line)
Report test result.
Definition: test.h:44
char * buffered_line(struct line_buffer *linebuf)
Retrieve buffered-up line.
Definition: linebuf.c:45
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
void * memset(void *dest, int character, size_t len) __nonnull

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

Referenced by linebuf_okx().

◆ linebuf_consume_okx()

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.

159  {
160  const char *data = test->data;
161  size_t remaining = test->len;
162  int len;
163  unsigned int i;
164  const char *expected;
165  char *actual;
166  int rc;
167 
168  DBGC ( test, "LINEBUF %p:\n", test );
169  DBGC_HDA ( test, 0, data, remaining );
170 
171  /* Consume data one line at a time */
172  for ( i = 0 ; i < test->count ; i++ ) {
173 
174  /* Add data to line buffer */
175  len = line_buffer ( linebuf, data, remaining );
176 
177  /* Get buffered line, if any */
178  actual = buffered_line ( linebuf );
179  if ( len < 0 ) {
180  rc = len;
181  DBGC ( test, "LINEBUF %p %s\n", test, strerror ( rc ) );
182  } else if ( actual != NULL ) {
183  DBGC ( test, "LINEBUF %p \"%s\" (consumed %d)\n",
184  test, actual, len );
185  } else {
186  DBGC ( test, "LINEBUF %p unterminated (consumed %d)\n",
187  test, len );
188  }
189 
190  /* Check for success/failure */
191  expected = test->lines[i];
192  if ( expected == linebuf_failure ) {
193  rc = len;
194  okx ( rc < 0, file, line );
195  okx ( remaining > 0, file, line );
196  return;
197  }
198  okx ( len >= 0, file, line );
199  okx ( ( ( size_t ) len ) <= remaining, file, line );
200 
201  /* Check expected result */
202  if ( expected == NULL ) {
203  okx ( actual == NULL, file, line );
204  } else {
205  okx ( actual != NULL, file, line );
206  okx ( strcmp ( actual, expected ) == 0, file, line );
207  }
208 
209  /* Consume data */
210  data += len;
211  remaining -= len;
212  }
213 
214  /* Check that all data was consumed */
215  okx ( remaining == 0, file, line );
216 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static const char linebuf_failure[1]
Line buffer test expected failure indicator.
Definition: linebuf_test.c:59
#define DBGC(...)
Definition: compiler.h:505
#define okx(success, file, line)
Report test result.
Definition: test.h:44
#define DBGC_HDA(...)
Definition: compiler.h:506
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
char * buffered_line(struct line_buffer *linebuf)
Retrieve buffered-up line.
Definition: linebuf.c:45
uint32_t len
Length.
Definition: ena.h:14
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:157
int line_buffer(struct line_buffer *linebuf, const char *data, size_t len)
Buffer up received data by lines.
Definition: linebuf.c:91
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static int test
Definition: epic100.c:73

References buffered_line(), data, DBGC, DBGC_HDA, len, line_buffer(), linebuf_failure, NULL, okx, rc, strcmp(), strerror(), and test.

Referenced by linebuf_okx().

◆ linebuf_accumulated_okx()

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.

230  {
231  const char *actual;
232  const char *expected;
233  unsigned int i;
234 
235  /* Check each accumulated line */
236  actual = linebuf->data;
237  for ( i = 0 ; i < test->count ; i++ ) {
238 
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 ) )
244  return;
245  okx ( strcmp ( actual, expected ) == 0, file, line );
246 
247  /* Move to next line */
248  actual += ( strlen ( actual ) + 1 /* NUL */ );
249  okx ( actual <= ( linebuf->data + linebuf->len ), file, line );
250  }
251 }
static const char linebuf_failure[1]
Line buffer test expected failure indicator.
Definition: linebuf_test.c:59
char * data
Data buffer.
Definition: linebuf.h:18
#define okx(success, file, line)
Report test result.
Definition: test.h:44
size_t len
Length of buffered data.
Definition: linebuf.h:20
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:157
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static int test
Definition: epic100.c:73

References line_buffer::data, line_buffer::len, linebuf_failure, NULL, okx, strcmp(), strlen(), and test.

Referenced by linebuf_okx().

◆ linebuf_empty_okx()

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.

263  {
264 
265  /* Empty line buffer */
266  empty_line_buffer ( linebuf );
267  okx ( buffered_line ( linebuf ) == NULL, file, line );
268 }
#define okx(success, file, line)
Report test result.
Definition: test.h:44
void empty_line_buffer(struct line_buffer *linebuf)
Discard line buffer contents.
Definition: linebuf.c:65
char * buffered_line(struct line_buffer *linebuf)
Retrieve buffered-up line.
Definition: linebuf.c:45
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

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

Referenced by linebuf_okx().

◆ linebuf_okx()

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.

280  {
281  struct line_buffer linebuf;
282 
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 );
287 }
static void linebuf_empty_okx(struct line_buffer *linebuf, const char *file, unsigned int line)
Report line buffer emptying test result.
Definition: linebuf_test.c:262
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.
Definition: linebuf_test.c:157
static void linebuf_init_okx(struct line_buffer *linebuf, const char *file, unsigned int line)
Report line buffer initialisation test result.
Definition: linebuf_test.c:139
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.
Definition: linebuf_test.c:228
A line buffer.
Definition: linebuf.h:16
static int test
Definition: epic100.c:73

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

◆ linebuf_test_exec()

static void linebuf_test_exec ( void  )
static

Perform line buffer self-tests.

Definition at line 295 of file linebuf_test.c.

295  {
296  struct line_buffer linebuf;
297 
298  /* Basic tests */
299  linebuf_ok ( &simple );
300  linebuf_ok ( &mixed );
301 
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 );
311 
312  /* Embedded NULs */
313  linebuf_ok ( &embedded_nuls );
314 }
#define linebuf_init_ok(linebuf)
Definition: linebuf_test.c:146
#define linebuf_empty_ok(linebuf)
Definition: linebuf_test.c:269
#define linebuf_consume_ok(test, linebuf)
Definition: linebuf_test.c:217
#define linebuf_ok(test)
Definition: linebuf_test.c:288
A line buffer.
Definition: linebuf.h:16

References linebuf_consume_ok, linebuf_empty_ok, linebuf_init_ok, and linebuf_ok.

Variable Documentation

◆ linebuf_failure

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().

◆ __self_test

struct self_test linebuf_test __self_test
Initial value:
= {
.name = "linebuf",
}
static void linebuf_test_exec(void)
Perform line buffer self-tests.
Definition: linebuf_test.c:295

Line buffer self-test.

Definition at line 317 of file linebuf_test.c.