iPXE
iobuf_test.c File Reference

I/O buffer tests. More...

#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <ipxe/iobuf.h>
#include <ipxe/io.h>
#include <ipxe/test.h>

Go to the source code of this file.

Macros

#define alloc_iob_ok(len, align, offset)
#define alloc_iob_fail_ok(len, align, offset)

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void alloc_iob_okx (size_t len, size_t align, size_t offset, const char *file, unsigned int line)
 Report I/O buffer allocation test result.
static void alloc_iob_fail_okx (size_t len, size_t align, size_t offset, const char *file, unsigned int line)
 Report I/O buffer allocation failure test result.
static void iobuf_test_exec (void)
 Perform I/O buffer self-tests.

Variables

struct self_test iobuf_test __self_test
 I/O buffer self-test.

Detailed Description

I/O buffer tests.

Definition in file iobuf_test.c.

Macro Definition Documentation

◆ alloc_iob_ok

#define alloc_iob_ok ( len,
align,
offset )
Value:
alloc_iob_okx ( len, align, offset, __FILE__, __LINE__ )
uint16_t offset
Offset to command line.
Definition bzimage.h:3
ring len
Length.
Definition dwmac.h:226
static void alloc_iob_okx(size_t len, size_t align, size_t offset, const char *file, unsigned int line)
Report I/O buffer allocation test result.
Definition iobuf_test.c:54

Definition at line 85 of file iobuf_test.c.

85#define alloc_iob_ok( len, align, offset ) \
86 alloc_iob_okx ( len, align, offset, __FILE__, __LINE__ )

Referenced by iobuf_test_exec().

◆ alloc_iob_fail_ok

#define alloc_iob_fail_ok ( len,
align,
offset )
Value:
alloc_iob_fail_okx ( len, align, offset, __FILE__, __LINE__ )
static void alloc_iob_fail_okx(size_t len, size_t align, size_t offset, const char *file, unsigned int line)
Report I/O buffer allocation failure test result.
Definition iobuf_test.c:97

Definition at line 105 of file iobuf_test.c.

105#define alloc_iob_fail_ok( len, align, offset ) \
106 alloc_iob_fail_okx ( len, align, offset, __FILE__, __LINE__ )

Referenced by iobuf_test_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ alloc_iob_okx()

void alloc_iob_okx ( size_t len,
size_t align,
size_t offset,
const char * file,
unsigned int line )
inlinestatic

Report I/O buffer allocation test result.

Parameters
lenRequired length of buffer
alignPhysical alignment
offsetOffset from physical alignment
fileTest code file
lineTest code line

Definition at line 54 of file iobuf_test.c.

55 {
56 struct io_buffer *iobuf;
57
58 /* Allocate I/O buffer */
59 iobuf = alloc_iob_raw ( len, align, offset );
60 okx ( iobuf != NULL, file, line );
61 DBGC ( &iobuf_test, "IOBUF %p (%#08lx+%#zx) for %#zx align %#zx "
62 "offset %#zx\n", iobuf, virt_to_phys ( iobuf->data ),
63 iob_tailroom ( iobuf ), len, align, offset );
64
65 /* Validate requested length and data alignment */
66 okx ( ( ( ( intptr_t ) iobuf ) & ( __alignof__ ( *iobuf ) - 1 ) ) == 0,
67 file, line );
68 okx ( iob_tailroom ( iobuf ) >= len, file, line );
69 okx ( ( ( align == 0 ) ||
70 ( ( virt_to_phys ( iobuf->data ) & ( align - 1 ) ) ==
71 ( offset & ( align - 1 ) ) ) ), file, line );
72
73 /* Validate overall buffer alignment */
74 okx ( ( ( ( intptr_t ) iobuf->head ) & ( IOB_ZLEN - 1 ) ) == 0,
75 file, line );
76 okx ( ( ( ( intptr_t ) iobuf->end ) & ( IOB_ZLEN - 1 ) ) == 0,
77 file, line );
78
79 /* Overwrite entire content of I/O buffer (for Valgrind) */
80 memset ( iob_put ( iobuf, len ), 0x55, len );
81
82 /* Free I/O buffer */
83 free_iob ( iobuf );
84}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
unsigned long intptr_t
Definition stdint.h:21
#define DBGC(...)
Definition compiler.h:505
void * memset(void *dest, int character, size_t len) __nonnull
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition iobuf.c:153
struct io_buffer * alloc_iob_raw(size_t len, size_t align, size_t offset)
Allocate I/O buffer with specified alignment and offset.
Definition iobuf.c:49
#define IOB_ZLEN
Minimum I/O buffer length and alignment.
Definition iobuf.h:29
#define iob_put(iobuf, len)
Definition iobuf.h:125
static size_t iob_tailroom(struct io_buffer *iobuf)
Calculate available space at end of an I/O buffer.
Definition iobuf.h:180
A persistent I/O buffer.
Definition iobuf.h:38
void * data
Start of data.
Definition iobuf.h:53
#define okx(success, file, line)
Report test result.
Definition test.h:44

References alloc_iob_raw(), io_buffer::data, DBGC, io_buffer::end, free_iob(), io_buffer::head, iob_put, iob_tailroom(), IOB_ZLEN, len, memset(), NULL, offset, and okx.

◆ alloc_iob_fail_okx()

void alloc_iob_fail_okx ( size_t len,
size_t align,
size_t offset,
const char * file,
unsigned int line )
inlinestatic

Report I/O buffer allocation failure test result.

Parameters
lenRequired length of buffer
alignPhysical alignment
offsetOffset from physical alignment
fileTest code file
lineTest code line

Definition at line 97 of file iobuf_test.c.

98 {
99 struct io_buffer *iobuf;
100
101 /* Allocate I/O buffer */
102 iobuf = alloc_iob_raw ( len, align, offset );
103 okx ( iobuf == NULL, file, line );
104}

References alloc_iob_raw(), len, NULL, offset, and okx.

◆ iobuf_test_exec()

void iobuf_test_exec ( void )
static

Perform I/O buffer self-tests.

Definition at line 112 of file iobuf_test.c.

112 {
113
114 /* Check zero-length allocations */
115 alloc_iob_ok ( 0, 0, 0 );
116 alloc_iob_ok ( 0, 0, 1 );
117 alloc_iob_ok ( 0, 1, 0 );
118 alloc_iob_ok ( 0, 1024, 0 );
119 alloc_iob_ok ( 0, 139, -17 );
120
121 /* Check various sensible allocations */
122 alloc_iob_ok ( 1, 0, 0 );
123 alloc_iob_ok ( 16, 16, 0 );
124 alloc_iob_ok ( 64, 0, 0 );
125 alloc_iob_ok ( 65, 0, 0 );
126 alloc_iob_ok ( 65, 1024, 19 );
127 alloc_iob_ok ( 1536, 1536, 0 );
128 alloc_iob_ok ( 2048, 2048, 0 );
129 alloc_iob_ok ( 2048, 2048, -10 );
130
131 /* Excessively large or excessively aligned allocations should fail */
132 alloc_iob_fail_ok ( -1UL, 0, 0 );
133 alloc_iob_fail_ok ( -1UL, 1024, 0 );
134 alloc_iob_fail_ok ( 0, -1UL, 0 );
135 alloc_iob_fail_ok ( 1024, -1UL, 0 );
136}
#define alloc_iob_ok(len, align, offset)
Definition iobuf_test.c:85
#define alloc_iob_fail_ok(len, align, offset)
Definition iobuf_test.c:105

References alloc_iob_fail_ok, and alloc_iob_ok.

Variable Documentation

◆ __self_test

struct self_test iobuf_test __self_test
Initial value:
= {
.name = "iobuf",
.exec = iobuf_test_exec,
}
static void iobuf_test_exec(void)
Perform I/O buffer self-tests.
Definition iobuf_test.c:112

I/O buffer self-test.

Definition at line 43 of file iobuf_test.c.