iPXE
iobuf_test.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26/** @file
27 *
28 * I/O buffer tests
29 *
30 */
31
32/* Forcibly enable assertions */
33#undef NDEBUG
34
35#include <stdint.h>
36#include <string.h>
37#include <assert.h>
38#include <ipxe/iobuf.h>
39#include <ipxe/io.h>
40#include <ipxe/test.h>
41
42/* Forward declaration */
43struct self_test iobuf_test __self_test;
44
45/**
46 * Report I/O buffer allocation test result
47 *
48 * @v len Required length of buffer
49 * @v align Physical alignment
50 * @v offset Offset from physical alignment
51 * @v file Test code file
52 * @v line Test code line
53 */
54static inline void alloc_iob_okx ( size_t len, size_t align, size_t offset,
55 const char *file, unsigned int line ) {
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}
85#define alloc_iob_ok( len, align, offset ) \
86 alloc_iob_okx ( len, align, offset, __FILE__, __LINE__ )
87
88/**
89 * Report I/O buffer allocation failure test result
90 *
91 * @v len Required length of buffer
92 * @v align Physical alignment
93 * @v offset Offset from physical alignment
94 * @v file Test code file
95 * @v line Test code line
96 */
97static inline void alloc_iob_fail_okx ( size_t len, size_t align, size_t offset,
98 const char *file, unsigned int line ) {
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}
105#define alloc_iob_fail_ok( len, align, offset ) \
106 alloc_iob_fail_okx ( len, align, offset, __FILE__, __LINE__ )
107
108/**
109 * Perform I/O buffer self-tests
110 *
111 */
112static void iobuf_test_exec ( void ) {
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}
137
138/** I/O buffer self-test */
139struct self_test iobuf_test __self_test = {
140 .name = "iobuf",
141 .exec = iobuf_test_exec,
142};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
unsigned long intptr_t
Definition stdint.h:21
Assertions.
uint16_t offset
Offset to command line.
Definition bzimage.h:3
ring len
Length.
Definition dwmac.h:226
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
iPXE I/O API
String functions.
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
I/O buffers.
#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
#define alloc_iob_ok(len, align, offset)
Definition iobuf_test.c:85
static void iobuf_test_exec(void)
Perform I/O buffer self-tests.
Definition iobuf_test.c:112
#define alloc_iob_fail_ok(len, align, offset)
Definition iobuf_test.c:105
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
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
A persistent I/O buffer.
Definition iobuf.h:38
void * data
Start of data.
Definition iobuf.h:53
void * end
End of the buffer.
Definition iobuf.h:57
void * head
Start of the buffer.
Definition iobuf.h:51
A self-test set.
Definition test.h:15
Self-test infrastructure.
#define okx(success, file, line)
Report test result.
Definition test.h:44
#define __self_test
Declare a self-test.
Definition test.h:32