Browse Source

[test] Add self-tests for setting types

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
46409231ba
3 changed files with 281 additions and 0 deletions
  1. 2
    0
      src/include/ipxe/settings.h
  2. 278
    0
      src/tests/settings_test.c
  3. 1
    0
      src/tests/tests.c

+ 2
- 0
src/include/ipxe/settings.h View File

@@ -283,6 +283,7 @@ extern int fetchf_named_setting ( const char *name, char *name_buf,
283 283
 extern char * expand_settings ( const char *string );
284 284
 
285 285
 extern struct setting_type setting_type_string __setting_type;
286
+extern struct setting_type setting_type_uristring __setting_type;
286 287
 extern struct setting_type setting_type_ipv4 __setting_type;
287 288
 extern struct setting_type setting_type_int8 __setting_type;
288 289
 extern struct setting_type setting_type_int16 __setting_type;
@@ -291,6 +292,7 @@ extern struct setting_type setting_type_uint8 __setting_type;
291 292
 extern struct setting_type setting_type_uint16 __setting_type;
292 293
 extern struct setting_type setting_type_uint32 __setting_type;
293 294
 extern struct setting_type setting_type_hex __setting_type;
295
+extern struct setting_type setting_type_hexhyp __setting_type;
294 296
 extern struct setting_type setting_type_uuid __setting_type;
295 297
 
296 298
 extern struct setting ip_setting __setting ( SETTING_IPv4 );

+ 278
- 0
src/tests/settings_test.c View File

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

+ 1
- 0
src/tests/tests.c View File

@@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
27 27
 /* Drag in all applicable self-tests */
28 28
 REQUIRE_OBJECT ( list_test );
29 29
 REQUIRE_OBJECT ( byteswap_test );
30
+REQUIRE_OBJECT ( settings_test );
30 31
 REQUIRE_OBJECT ( time_test );
31 32
 REQUIRE_OBJECT ( md5_test );
32 33
 REQUIRE_OBJECT ( sha1_test );

Loading…
Cancel
Save