summaryrefslogtreecommitdiffstats
path: root/src/tests/iobuf_test.c
blob: a417c2e8c95e66d36e15e4b7d270d958bfc797b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

/** @file
 *
 * I/O buffer tests
 *
 */

/* Forcibly enable assertions */
#undef NDEBUG

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

/* Forward declaration */
struct self_test iobuf_test __self_test;

/**
 * Report I/O buffer allocation test result
 *
 * @v len		Required length of buffer
 * @v align		Physical alignment
 * @v offset		Offset from physical alignment
 * @v file		Test code file
 * @v line		Test code line
 */
static inline void alloc_iob_okx ( size_t len, size_t align, size_t offset,
				   const char *file, unsigned int line ) {
	struct io_buffer *iobuf;

	/* Allocate I/O buffer */
	iobuf = alloc_iob_raw ( len, align, offset );
	okx ( iobuf != NULL, file, line );
	DBGC ( &iobuf_test, "IOBUF %p (%#08lx+%#zx) for %#zx align %#zx "
	       "offset %#zx\n", iobuf, virt_to_phys ( iobuf->data ),
	       iob_tailroom ( iobuf ), len, align, offset );

	/* Validate requested length and alignment */
	okx ( ( ( ( intptr_t ) iobuf ) & ( __alignof__ ( *iobuf ) - 1 ) ) == 0,
		file, line );
	okx ( iob_tailroom ( iobuf ) >= len, file, line );
	okx ( ( ( align == 0 ) ||
		( ( virt_to_phys ( iobuf->data ) & ( align - 1 ) ) ==
		  ( offset & ( align - 1 ) ) ) ), file, line );

	/* Overwrite entire content of I/O buffer (for Valgrind) */
	memset ( iob_put ( iobuf, len ), 0x55, len );

	/* Free I/O buffer */
	free_iob ( iobuf );
}
#define alloc_iob_ok( len, align, offset ) \
	alloc_iob_okx ( len, align, offset, __FILE__, __LINE__ )

/**
 * Report I/O buffer allocation failure test result
 *
 * @v len		Required length of buffer
 * @v align		Physical alignment
 * @v offset		Offset from physical alignment
 * @v file		Test code file
 * @v line		Test code line
 */
static inline void alloc_iob_fail_okx ( size_t len, size_t align, size_t offset,
					const char *file, unsigned int line ) {
	struct io_buffer *iobuf;

	/* Allocate I/O buffer */
	iobuf = alloc_iob_raw ( len, align, offset );
	okx ( iobuf == NULL, file, line );
}
#define alloc_iob_fail_ok( len, align, offset ) \
	alloc_iob_fail_okx ( len, align, offset, __FILE__, __LINE__ )

/**
 * Perform I/O buffer self-tests
 *
 */
static void iobuf_test_exec ( void ) {

	/* Check zero-length allocations */
	alloc_iob_ok ( 0, 0, 0 );
	alloc_iob_ok ( 0, 0, 1 );
	alloc_iob_ok ( 0, 1, 0 );
	alloc_iob_ok ( 0, 1024, 0 );
	alloc_iob_ok ( 0, 139, -17 );

	/* Check various sensible allocations */
	alloc_iob_ok ( 1, 0, 0 );
	alloc_iob_ok ( 16, 16, 0 );
	alloc_iob_ok ( 64, 0, 0 );
	alloc_iob_ok ( 65, 0, 0 );
	alloc_iob_ok ( 65, 1024, 19 );
	alloc_iob_ok ( 1536, 1536, 0 );
	alloc_iob_ok ( 2048, 2048, 0 );
	alloc_iob_ok ( 2048, 2048, -10 );

	/* Excessively large or excessively aligned allocations should fail */
	alloc_iob_fail_ok ( -1UL, 0, 0 );
	alloc_iob_fail_ok ( -1UL, 1024, 0 );
	alloc_iob_fail_ok ( 0, -1UL, 0 );
	alloc_iob_fail_ok ( 1024, -1UL, 0 );
}

/** I/O buffer self-test */
struct self_test iobuf_test __self_test = {
	.name = "iobuf",
	.exec = iobuf_test_exec,
};