2
* Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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 any later version.
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.
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
20
FILE_LICENCE ( GPL2_OR_LATER );
28
/* Forcibly enable assertions */
32
#include <ipxe/settings.h>
33
#include <ipxe/test.h>
35
/** Define inline raw data */
36
#define RAW(...) { __VA_ARGS__ }
39
* Report a formatted-store test result
41
* @v settings Settings block
43
* @v formatted Formatted value
44
* @v raw_array Expected raw value
46
#define storef_ok( settings, setting, formatted, raw_array ) do { \
47
const uint8_t expected[] = raw_array; \
48
uint8_t actual[ sizeof ( expected ) ]; \
51
ok ( storef_setting ( settings, setting, formatted ) == 0 ); \
52
len = fetch_setting ( settings, setting, actual, \
53
sizeof ( actual ) ); \
54
DBGC ( settings, "Stored %s \"%s\", got:\n", \
55
(setting)->type->name, formatted ); \
56
DBGC_HDA ( settings, 0, actual, len ); \
57
ok ( len == ( int ) sizeof ( actual ) ); \
58
ok ( memcmp ( actual, expected, sizeof ( actual ) ) == 0 ); \
62
* Report a formatted-fetch test result
64
* @v settings Settings block
66
* @v raw_array Raw value
67
* @v formatted Expected formatted value
69
#define fetchf_ok( settings, setting, raw_array, formatted ) do { \
70
const uint8_t raw[] = raw_array; \
71
char actual[ strlen ( formatted ) + 1 ]; \
74
ok ( store_setting ( settings, setting, raw, \
75
sizeof ( raw ) ) == 0 ); \
76
len = fetchf_setting ( settings, setting, actual, \
77
sizeof ( actual ) ); \
78
DBGC ( settings, "Fetched %s \"%s\" from:\n", \
79
(setting)->type->name, formatted ); \
80
DBGC_HDA ( settings, 0, raw, sizeof ( raw ) ); \
81
ok ( len == ( int ) ( sizeof ( actual ) - 1 ) ); \
82
ok ( strcmp ( actual, formatted ) == 0 ); \
85
/** Test generic settings block */
86
struct generic_settings test_generic_settings = {
90
LIST_HEAD_INIT ( test_generic_settings.settings.siblings ),
92
LIST_HEAD_INIT ( test_generic_settings.settings.children ),
93
.op = &generic_settings_operations,
95
.list = LIST_HEAD_INIT ( test_generic_settings.list ),
98
/** Test settings block */
99
#define test_settings test_generic_settings.settings
101
/** Test string setting */
102
static struct setting test_string_setting = {
103
.name = "test_string",
104
.type = &setting_type_string,
107
/** Test URI-encoded string setting */
108
static struct setting test_uristring_setting = {
109
.name = "test_uristring",
110
.type = &setting_type_uristring,
113
/** Test IPv4 address setting type */
114
static struct setting test_ipv4_setting = {
116
.type = &setting_type_ipv4,
119
/** Test signed 8-bit integer setting type */
120
static struct setting test_int8_setting = {
122
.type = &setting_type_int8,
125
/** Test signed 16-bit integer setting type */
126
static struct setting test_int16_setting = {
127
.name = "test_int16",
128
.type = &setting_type_int16,
131
/** Test signed 32-bit integer setting type */
132
static struct setting test_int32_setting = {
133
.name = "test_int32",
134
.type = &setting_type_int32,
137
/** Test unsigned 8-bit integer setting type */
138
static struct setting test_uint8_setting = {
139
.name = "test_uint8",
140
.type = &setting_type_uint8,
143
/** Test unsigned 16-bit integer setting type */
144
static struct setting test_uint16_setting = {
145
.name = "test_uint16",
146
.type = &setting_type_uint16,
149
/** Test unsigned 32-bit integer setting type */
150
static struct setting test_uint32_setting = {
151
.name = "test_uint32",
152
.type = &setting_type_uint32,
155
/** Test colon-separated hex string setting type */
156
static struct setting test_hex_setting = {
158
.type = &setting_type_hex,
161
/** Test hyphen-separated hex string setting type */
162
static struct setting test_hexhyp_setting = {
163
.name = "test_hexhyp",
164
.type = &setting_type_hexhyp,
167
/** Test UUID setting type */
168
static struct setting test_uuid_setting = {
170
.type = &setting_type_uuid,
174
* Perform settings self-tests
177
static void settings_test_exec ( void ) {
179
/* Register test settings block */
180
ok ( register_settings ( &test_settings, NULL, "test" ) == 0 );
182
/* "string" setting type */
183
storef_ok ( &test_settings, &test_string_setting, "hello",
184
RAW ( 'h', 'e', 'l', 'l', 'o' ) );
185
fetchf_ok ( &test_settings, &test_string_setting,
186
RAW ( 'w', 'o', 'r', 'l', 'd' ), "world" );
188
/* "uristring" setting type */
189
storef_ok ( &test_settings, &test_uristring_setting, "hello%20world",
190
RAW ( 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l',
192
fetchf_ok ( &test_settings, &test_uristring_setting,
193
RAW ( 1, 2, 3, 4, 5 ), "%01%02%03%04%05" );
195
/* "ipv4" setting type */
196
storef_ok ( &test_settings, &test_ipv4_setting, "192.168.0.1",
197
RAW ( 192, 168, 0, 1 ) );
198
fetchf_ok ( &test_settings, &test_ipv4_setting,
199
RAW ( 212, 13, 204, 60 ), "212.13.204.60" );
201
/* Integer setting types */
202
storef_ok ( &test_settings, &test_int8_setting,
204
storef_ok ( &test_settings, &test_int8_setting,
205
"0x7f", RAW ( 0x7f ) );
206
storef_ok ( &test_settings, &test_int8_setting,
207
"0x1234", RAW ( 0x34 ) );
208
storef_ok ( &test_settings, &test_int8_setting,
209
"-32", RAW ( -32 ) );
210
fetchf_ok ( &test_settings, &test_int8_setting,
212
fetchf_ok ( &test_settings, &test_int8_setting,
213
RAW ( 106 ), "106" );
214
storef_ok ( &test_settings, &test_uint8_setting,
215
"129", RAW ( 129 ) );
216
storef_ok ( &test_settings, &test_uint8_setting,
217
"0x3421", RAW ( 0x21 ) );
218
fetchf_ok ( &test_settings, &test_uint8_setting,
219
RAW ( 0x54 ), "0x54" );
220
storef_ok ( &test_settings, &test_int16_setting,
221
"29483", RAW ( 0x73, 0x2b ) );
222
fetchf_ok ( &test_settings, &test_int16_setting,
223
RAW ( 0x82, 0x14 ), "-32236" );
224
fetchf_ok ( &test_settings, &test_int16_setting,
225
RAW ( 0x12, 0x78 ), "4728" );
226
storef_ok ( &test_settings, &test_uint16_setting,
227
"48727", RAW ( 0xbe, 0x57 ) );
228
fetchf_ok ( &test_settings, &test_uint16_setting,
229
RAW ( 0x9a, 0x24 ), "0x9a24" );
230
storef_ok ( &test_settings, &test_int32_setting,
231
"2901274", RAW ( 0x00, 0x2c, 0x45, 0x1a ) );
232
fetchf_ok ( &test_settings, &test_int32_setting,
233
RAW ( 0xff, 0x34, 0x2d, 0xaf ), "-13357649" );
234
fetchf_ok ( &test_settings, &test_int32_setting,
235
RAW ( 0x01, 0x00, 0x34, 0xab ), "16790699" );
236
storef_ok ( &test_settings, &test_uint32_setting,
237
"0xb598d21", RAW ( 0x0b, 0x59, 0x8d, 0x21 ) );
238
fetchf_ok ( &test_settings, &test_uint32_setting,
239
RAW ( 0xf2, 0x37, 0xb2, 0x18 ), "0xf237b218" );
241
/* "hex" setting type */
242
storef_ok ( &test_settings, &test_hex_setting,
243
":", RAW ( 0x00, 0x00 ) );
244
storef_ok ( &test_settings, &test_hex_setting,
245
"1:2:", RAW ( 0x01, 0x02, 0x00 ) );
246
storef_ok ( &test_settings, &test_hex_setting,
247
"08:12:f5:22:90:1b:4b:47:a8:30:cb:4d:67:4c:d6:76",
248
RAW ( 0x08, 0x12, 0xf5, 0x22, 0x90, 0x1b, 0x4b, 0x47, 0xa8,
249
0x30, 0xcb, 0x4d, 0x67, 0x4c, 0xd6, 0x76 ) );
250
fetchf_ok ( &test_settings, &test_hex_setting,
251
RAW ( 0x62, 0xd9, 0xd4, 0xc4, 0x7e, 0x3b, 0x41, 0x46, 0x91,
252
0xc6, 0xfd, 0x0c, 0xbf ),
253
"62:d9:d4:c4:7e:3b:41:46:91:c6:fd:0c:bf" );
255
/* "hexhyp" setting type */
256
storef_ok ( &test_settings, &test_hexhyp_setting,
257
"11-33-22", RAW ( 0x11, 0x33, 0x22 ) );
258
fetchf_ok ( &test_settings, &test_hexhyp_setting,
259
RAW ( 0x9f, 0xe5, 0x6d, 0xfb, 0x24, 0x3a, 0x4c, 0xbb, 0xa9,
260
0x09, 0x6c, 0x66, 0x13, 0xc1, 0xa8, 0xec, 0x27 ),
261
"9f-e5-6d-fb-24-3a-4c-bb-a9-09-6c-66-13-c1-a8-ec-27" );
263
/* "uuid" setting type (no store capability) */
264
fetchf_ok ( &test_settings, &test_uuid_setting,
265
RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,
266
0x7a, 0x7c, 0xfe, 0x4f, 0xca, 0x4a, 0x57 ),
267
"1a6a749d-0eda-461a-a87a-7cfe4fca4a57" );
269
/* Clear and unregister test settings block */
270
clear_settings ( &test_settings );
271
unregister_settings ( &test_settings );
274
/** Settings self-test */
275
struct self_test settings_test __self_test = {
277
.exec = settings_test_exec,