iPXE
Macros | Functions | Variables
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)   alloc_iob_okx ( len, align, offset, __FILE__, __LINE__ )
 
#define alloc_iob_fail_ok(len, align, offset)   alloc_iob_fail_okx ( len, align, offset, __FILE__, __LINE__ )
 

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. More...
 
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. More...
 
static void iobuf_test_exec (void)
 Perform I/O buffer self-tests. More...
 

Variables

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

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 
)    alloc_iob_okx ( len, align, offset, __FILE__, __LINE__ )

Definition at line 79 of file iobuf_test.c.

◆ alloc_iob_fail_ok

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

Definition at line 99 of file iobuf_test.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ alloc_iob_okx()

static 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 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  /* Overwrite entire content of I/O buffer (for Valgrind) */
74  memset ( iob_put ( iobuf, len ), 0x55, len );
75 
76  /* Free I/O buffer */
77  free_iob ( iobuf );
78 }
#define iob_put(iobuf, len)
Definition: iobuf.h:116
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
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:48
#define DBGC(...)
Definition: compiler.h:505
unsigned long intptr_t
Definition: stdint.h:21
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
#define okx(success, file, line)
Report test result.
Definition: test.h:44
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static size_t iob_tailroom(struct io_buffer *iobuf)
Calculate available space at end of an I/O buffer.
Definition: iobuf.h:171
uint32_t len
Length.
Definition: ena.h:14
void * data
Start of data.
Definition: iobuf.h:44
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:32

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

◆ alloc_iob_fail_okx()

static 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 91 of file iobuf_test.c.

92  {
93  struct io_buffer *iobuf;
94 
95  /* Allocate I/O buffer */
96  iobuf = alloc_iob_raw ( len, align, offset );
97  okx ( iobuf == NULL, file, line );
98 }
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:48
#define okx(success, file, line)
Report test result.
Definition: test.h:44
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
A persistent I/O buffer.
Definition: iobuf.h:32

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

◆ iobuf_test_exec()

static void iobuf_test_exec ( void  )
static

Perform I/O buffer self-tests.

Definition at line 106 of file iobuf_test.c.

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

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:106

I/O buffer self-test.

Definition at line 43 of file iobuf_test.c.