aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/settings_test.c
blob: ccc2694bbddf6eb7d17809fed8b7d01cca3ebe77 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * Copyright (C) 2012 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

FILE_LICENCE ( GPL2_OR_LATER );

/** @file
 *
 * Settings self-tests
 *
 */

/* Forcibly enable assertions */
#undef NDEBUG

#include <string.h>
#include <ipxe/settings.h>
#include <ipxe/test.h>

/** Define inline raw data */
#define RAW(...) { __VA_ARGS__ }

/**
 * Report a formatted-store test result
 *
 * @v settings		Settings block
 * @v setting		Setting
 * @v formatted		Formatted value
 * @v raw_array		Expected raw value
 */
#define storef_ok( settings, setting, formatted, raw_array ) do {	\
	const uint8_t expected[] = raw_array;				\
	uint8_t actual[ sizeof ( expected ) ];				\
	int len;							\
									\
	ok ( storef_setting ( settings, setting, formatted ) == 0 );	\
	len = fetch_setting ( settings, setting, actual,		\
			      sizeof ( actual ) );			\
	DBGC ( settings, "Stored %s \"%s\", got:\n",			\
	       (setting)->type->name, formatted );			\
	DBGC_HDA ( settings, 0, actual, len );				\
	ok ( len == ( int ) sizeof ( actual ) );			\
	ok ( memcmp ( actual, expected, sizeof ( actual ) ) == 0 );	\
	} while ( 0 )

/**
 * Report a formatted-fetch test result
 *
 * @v settings		Settings block
 * @v setting		Setting
 * @v raw_array		Raw value
 * @v formatted		Expected formatted value
 */
#define fetchf_ok( settings, setting, raw_array, formatted ) do {	\
	const uint8_t raw[] = raw_array;				\
	char actual[ strlen ( formatted ) + 1 ];			\
	int len;							\
									\
	ok ( store_setting ( settings, setting, raw,			\
			     sizeof ( raw ) ) == 0 );			\
	len = fetchf_setting ( settings, setting, actual,		\
			       sizeof ( actual ) );			\
	DBGC ( settings, "Fetched %s \"%s\" from:\n",			\
	       (setting)->type->name, formatted );			\
	DBGC_HDA ( settings, 0, raw, sizeof ( raw ) );			\
	ok ( len == ( int ) ( sizeof ( actual ) - 1 ) );		\
	ok ( strcmp ( actual, formatted ) == 0 );			\
	} while ( 0 )

/** Test generic settings block */
struct generic_settings test_generic_settings = {
	.settings = {
		.refcnt = NULL,
		.siblings =
		    LIST_HEAD_INIT ( test_generic_settings.settings.siblings ),
		.children =
		    LIST_HEAD_INIT ( test_generic_settings.settings.children ),
		.op = &generic_settings_operations,
	},
	.list = LIST_HEAD_INIT ( test_generic_settings.list ),
};

/** Test settings block */
#define test_settings test_generic_settings.settings

/** Test string setting */
static struct setting test_string_setting = {
	.name = "test_string",
	.type = &setting_type_string,
};

/** Test URI-encoded string setting */
static struct setting test_uristring_setting = {
	.name = "test_uristring",
	.type = &setting_type_uristring,
};

/** Test IPv4 address setting type */
static struct setting test_ipv4_setting = {
	.name = "test_ipv4",
	.type = &setting_type_ipv4,
};

/** Test signed 8-bit integer setting type */
static struct setting test_int8_setting = {
	.name = "test_int8",
	.type = &setting_type_int8,
};

/** Test signed 16-bit integer setting type */
static struct setting test_int16_setting = {
	.name = "test_int16",
	.type = &setting_type_int16,
};

/** Test signed 32-bit integer setting type */
static struct setting test_int32_setting = {
	.name = "test_int32",
	.type = &setting_type_int32,
};

/** Test unsigned 8-bit integer setting type */
static struct setting test_uint8_setting = {
	.name = "test_uint8",
	.type = &setting_type_uint8,
};

/** Test unsigned 16-bit integer setting type */
static struct setting test_uint16_setting = {
	.name = "test_uint16",
	.type = &setting_type_uint16,
};

/** Test unsigned 32-bit integer setting type */
static struct setting test_uint32_setting = {
	.name = "test_uint32",
	.type = &setting_type_uint32,
};

/** Test colon-separated hex string setting type */
static struct setting test_hex_setting = {
	.name = "test_hex",
	.type = &setting_type_hex,
};

/** Test hyphen-separated hex string setting type */
static struct setting test_hexhyp_setting = {
	.name = "test_hexhyp",
	.type = &setting_type_hexhyp,
};

/** Test UUID setting type */
static struct setting test_uuid_setting = {
	.name = "test_uuid",
	.type = &setting_type_uuid,
};

/**
 * Perform settings self-tests
 *
 */
static void settings_test_exec ( void ) {

	/* Register test settings block */
	ok ( register_settings ( &test_settings, NULL, "test" ) == 0 );

	/* "string" setting type */
	storef_ok ( &test_settings, &test_string_setting, "hello",
		    RAW ( 'h', 'e', 'l', 'l', 'o' ) );
	fetchf_ok ( &test_settings, &test_string_setting,
		    RAW ( 'w', 'o', 'r', 'l', 'd' ), "world" );

	/* "uristring" setting type */
	storef_ok ( &test_settings, &test_uristring_setting, "hello%20world",
		    RAW ( 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l',
			  'd' ) );
	fetchf_ok ( &test_settings, &test_uristring_setting,
		    RAW ( 1, 2, 3, 4, 5 ), "%01%02%03%04%05" );

	/* "ipv4" setting type */
	storef_ok ( &test_settings, &test_ipv4_setting, "192.168.0.1",
		    RAW ( 192, 168, 0, 1 ) );
	fetchf_ok ( &test_settings, &test_ipv4_setting,
		    RAW ( 212, 13, 204, 60 ), "212.13.204.60" );

	/* Integer setting types */
	storef_ok ( &test_settings, &test_int8_setting,
		    "54", RAW ( 54 ) );
	storef_ok ( &test_settings, &test_int8_setting,
		    "0x7f", RAW ( 0x7f ) );
	storef_ok ( &test_settings, &test_int8_setting,
		    "0x1234", RAW ( 0x34 ) );
	storef_ok ( &test_settings, &test_int8_setting,
		    "-32", RAW ( -32 ) );
	fetchf_ok ( &test_settings, &test_int8_setting,
		    RAW ( -9 ), "-9" );
	fetchf_ok ( &test_settings, &test_int8_setting,
		    RAW ( 106 ), "106" );
	storef_ok ( &test_settings, &test_uint8_setting,
		    "129", RAW ( 129 ) );
	storef_ok ( &test_settings, &test_uint8_setting,
		    "0x3421", RAW ( 0x21 ) );
	fetchf_ok ( &test_settings, &test_uint8_setting,
		    RAW ( 0x54 ), "0x54" );
	storef_ok ( &test_settings, &test_int16_setting,
		    "29483", RAW ( 0x73, 0x2b ) );
	fetchf_ok ( &test_settings, &test_int16_setting,
		    RAW ( 0x82, 0x14 ), "-32236" );
	fetchf_ok ( &test_settings, &test_int16_setting,
		    RAW ( 0x12, 0x78 ), "4728" );
	storef_ok ( &test_settings, &test_uint16_setting,
		    "48727", RAW ( 0xbe, 0x57 ) );
	fetchf_ok ( &test_settings, &test_uint16_setting,
		    RAW ( 0x9a, 0x24 ), "0x9a24" );
	storef_ok ( &test_settings, &test_int32_setting,
		    "2901274", RAW ( 0x00, 0x2c, 0x45, 0x1a ) );
	fetchf_ok ( &test_settings, &test_int32_setting,
		    RAW ( 0xff, 0x34, 0x2d, 0xaf ), "-13357649" );
	fetchf_ok ( &test_settings, &test_int32_setting,
		    RAW ( 0x01, 0x00, 0x34, 0xab ), "16790699" );
	storef_ok ( &test_settings, &test_uint32_setting,
		    "0xb598d21", RAW ( 0x0b, 0x59, 0x8d, 0x21 ) );
	fetchf_ok ( &test_settings, &test_uint32_setting,
		    RAW ( 0xf2, 0x37, 0xb2, 0x18 ), "0xf237b218" );

	/* "hex" setting type */
	storef_ok ( &test_settings, &test_hex_setting,
		    "", RAW ( 0x00 ) );
	storef_ok ( &test_settings, &test_hex_setting,
		    ":", RAW ( 0x00, 0x00 ) );
	storef_ok ( &test_settings, &test_hex_setting,
		    "1:2:", RAW ( 0x01, 0x02, 0x00 ) );
	storef_ok ( &test_settings, &test_hex_setting,
		    "08:12:f5:22:90:1b:4b:47:a8:30:cb:4d:67:4c:d6:76",
		    RAW ( 0x08, 0x12, 0xf5, 0x22, 0x90, 0x1b, 0x4b, 0x47, 0xa8,
			  0x30, 0xcb, 0x4d, 0x67, 0x4c, 0xd6, 0x76 ) );
	fetchf_ok ( &test_settings, &test_hex_setting,
		    RAW ( 0x62, 0xd9, 0xd4, 0xc4, 0x7e, 0x3b, 0x41, 0x46, 0x91,
			  0xc6, 0xfd, 0x0c, 0xbf ),
		    "62:d9:d4:c4:7e:3b:41:46:91:c6:fd:0c:bf" );

	/* "hexhyp" setting type */
	storef_ok ( &test_settings, &test_hexhyp_setting,
		    "11-33-22", RAW ( 0x11, 0x33, 0x22 ) );
	fetchf_ok ( &test_settings, &test_hexhyp_setting,
		    RAW ( 0x9f, 0xe5, 0x6d, 0xfb, 0x24, 0x3a, 0x4c, 0xbb, 0xa9,
			  0x09, 0x6c, 0x66, 0x13, 0xc1, 0xa8, 0xec, 0x27 ),
		    "9f-e5-6d-fb-24-3a-4c-bb-a9-09-6c-66-13-c1-a8-ec-27" );

	/* "uuid" setting type (no store capability) */
	fetchf_ok ( &test_settings, &test_uuid_setting,
		    RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,
			  0x7a, 0x7c, 0xfe, 0x4f, 0xca, 0x4a, 0x57 ),
		    "1a6a749d-0eda-461a-a87a-7cfe4fca4a57" );

	/* Unregister test settings block */
	unregister_settings ( &test_settings );
}

/** Settings self-test */
struct self_test settings_test __self_test = {
	.name = "settings",
	.exec = settings_test_exec,
};