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 
24 FILE_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 */
43 struct 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  */
54 static 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 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 }
79 #define alloc_iob_ok( len, align, offset ) \
80  alloc_iob_okx ( len, align, offset, __FILE__, __LINE__ )
81 
82 /**
83  * Report I/O buffer allocation failure test result
84  *
85  * @v len Required length of buffer
86  * @v align Physical alignment
87  * @v offset Offset from physical alignment
88  * @v file Test code file
89  * @v line Test code line
90  */
91 static inline void alloc_iob_fail_okx ( size_t len, size_t align, size_t offset,
92  const char *file, unsigned int line ) {
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 }
99 #define alloc_iob_fail_ok( len, align, offset ) \
100  alloc_iob_fail_okx ( len, align, offset, __FILE__, __LINE__ )
101 
102 /**
103  * Perform I/O buffer self-tests
104  *
105  */
106 static void iobuf_test_exec ( void ) {
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 }
131 
132 /** I/O buffer self-test */
133 struct self_test iobuf_test __self_test = {
134  .name = "iobuf",
135  .exec = iobuf_test_exec,
136 };
static __always_inline void struct dma_mapping size_t size_t align
Definition: dma.h:222
iPXE I/O API
#define iob_put(iobuf, len)
Definition: iobuf.h:120
static void iobuf_test_exec(void)
Perform I/O buffer self-tests.
Definition: iobuf_test.c:106
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
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
Self-test infrastructure.
const char * name
Test set name.
Definition: test.h:17
unsigned long intptr_t
Definition: stdint.h:21
A self-test set.
Definition: test.h:15
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
Assertions.
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
#define alloc_iob_fail_ok(len, align, offset)
Definition: iobuf_test.c:99
static size_t iob_tailroom(struct io_buffer *iobuf)
Calculate available space at end of an I/O buffer.
Definition: iobuf.h:175
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
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:91
uint32_t len
Length.
Definition: ena.h:14
void * data
Start of data.
Definition: iobuf.h:48
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
struct self_test iobuf_test __self_test
I/O buffer self-test.
Definition: iobuf_test.c:43
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
#define alloc_iob_ok(len, align, offset)
Definition: iobuf_test.c:79
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:33