These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / tests / settings_test.c
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., 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  * Settings self-tests
29  *
30  */
31
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34
35 #include <string.h>
36 #include <ipxe/settings.h>
37 #include <ipxe/test.h>
38
39 /** Define inline raw data */
40 #define RAW(...) { __VA_ARGS__ }
41
42 /**
43  * Report a formatted-store test result
44  *
45  * @v _settings         Settings block
46  * @v _setting          Setting
47  * @v _formatted        Formatted value
48  * @v _raw_array        Expected raw value
49  */
50 #define storef_ok( _settings, _setting, _formatted, _raw_array ) do {   \
51         const uint8_t expected[] = _raw_array;                          \
52         uint8_t actual[ sizeof ( expected ) ];                          \
53         int len;                                                        \
54                                                                         \
55         ok ( storef_setting ( _settings, _setting, _formatted ) == 0 ); \
56         len = fetch_setting ( _settings, _setting, NULL, NULL, actual,  \
57                               sizeof ( actual ) );                      \
58         if ( len >= 0 ) {                                               \
59                 DBGC ( _settings, "Stored %s \"%s\", got:\n",           \
60                        (_setting)->type->name, _formatted );            \
61                 DBGC_HDA ( _settings, 0, actual, len );                 \
62         } else {                                                        \
63                 DBGC ( _settings, "Stored %s \"%s\", got error %s\n",   \
64                        (_setting)->type->name, _formatted,              \
65                        strerror ( len ) );                              \
66         }                                                               \
67         ok ( len == ( int ) sizeof ( actual ) );                        \
68         ok ( memcmp ( actual, expected, sizeof ( actual ) ) == 0 );     \
69         } while ( 0 )
70
71 /**
72  * Report a formatted-fetch test result
73  *
74  * @v _settings         Settings block
75  * @v _setting          Setting
76  * @v _raw_array        Raw value
77  * @v _formatted        Expected formatted value
78  */
79 #define fetchf_ok( _settings, _setting, _raw_array, _formatted ) do {   \
80         const uint8_t raw[] = _raw_array;                               \
81         char actual[ strlen ( _formatted ) + 1 ];                       \
82         int len;                                                        \
83                                                                         \
84         ok ( store_setting ( _settings, _setting, raw,                  \
85                              sizeof ( raw ) ) == 0 );                   \
86         len = fetchf_setting ( _settings, _setting, NULL, NULL, actual, \
87                                sizeof ( actual ) );                     \
88         DBGC ( _settings, "Fetched %s \"%s\" from:\n",                  \
89                (_setting)->type->name, actual );                        \
90         DBGC_HDA ( _settings, 0, raw, sizeof ( raw ) );                 \
91         ok ( len == ( int ) ( sizeof ( actual ) - 1 ) );                \
92         ok ( strcmp ( actual, _formatted ) == 0 );                      \
93         } while ( 0 )
94
95 /**
96  * Report a numeric-store test result
97  *
98  * @v _settings         Settings block
99  * @v _setting          Setting
100  * @v _numeric          Numeric value
101  * @v _raw_array        Expected raw value
102  */
103 #define storen_ok( _settings, _setting, _numeric, _raw_array ) do {     \
104         const uint8_t expected[] = _raw_array;                          \
105         uint8_t actual[ sizeof ( expected ) ];                          \
106         int len;                                                        \
107                                                                         \
108         ok ( storen_setting ( _settings, _setting, _numeric ) == 0 );   \
109         len = fetch_setting ( _settings, _setting, NULL, NULL, actual,  \
110                               sizeof ( actual ) );                      \
111         if ( len >= 0 ) {                                               \
112                 DBGC ( _settings, "Stored %s %#lx, got:\n",             \
113                        (_setting)->type->name,                          \
114                        ( unsigned long ) _numeric );                    \
115                 DBGC_HDA ( _settings, 0, actual, len );                 \
116         } else {                                                        \
117                 DBGC ( _settings, "Stored %s %#lx, got error %s\n",     \
118                        (_setting)->type->name,                          \
119                        ( unsigned long ) _numeric, strerror ( len ) );  \
120         }                                                               \
121         ok ( len == ( int ) sizeof ( actual ) );                        \
122         ok ( memcmp ( actual, expected, sizeof ( actual ) ) == 0 );     \
123         } while ( 0 )
124
125 /**
126  * Report a numeric-fetch test result
127  *
128  * @v _settings         Settings block
129  * @v _setting          Setting
130  * @v _raw_array        Raw array
131  * @v _numeric          Expected numeric value
132  */
133 #define fetchn_ok( _settings, _setting, _raw_array, _numeric ) do {     \
134         const uint8_t raw[] = _raw_array;                               \
135         unsigned long actual;                                           \
136                                                                         \
137         ok ( store_setting ( _settings, _setting, raw,                  \
138                              sizeof ( raw ) ) == 0 );                   \
139         ok ( fetchn_setting ( _settings, _setting, NULL, NULL,          \
140                               &actual ) == 0 );                         \
141         DBGC ( _settings, "Fetched %s %#lx from:\n",                    \
142                (_setting)->type->name, actual );                        \
143         DBGC_HDA ( _settings, 0, raw, sizeof ( raw ) );                 \
144         ok ( actual == ( unsigned long ) _numeric );                    \
145         } while ( 0 )
146
147 /** Test generic settings block */
148 struct generic_settings test_generic_settings = {
149         .settings = {
150                 .refcnt = NULL,
151                 .siblings =
152                     LIST_HEAD_INIT ( test_generic_settings.settings.siblings ),
153                 .children =
154                     LIST_HEAD_INIT ( test_generic_settings.settings.children ),
155                 .op = &generic_settings_operations,
156         },
157         .list = LIST_HEAD_INIT ( test_generic_settings.list ),
158 };
159
160 /** Test settings block */
161 #define test_settings test_generic_settings.settings
162
163 /** Test string setting */
164 static struct setting test_string_setting = {
165         .name = "test_string",
166         .type = &setting_type_string,
167 };
168
169 /** Test IPv4 address setting type */
170 static struct setting test_ipv4_setting = {
171         .name = "test_ipv4",
172         .type = &setting_type_ipv4,
173 };
174
175 /** Test IPv6 address setting type */
176 static struct setting test_ipv6_setting = {
177         .name = "test_ipv6",
178         .type = &setting_type_ipv6,
179 };
180
181 /** Test signed 8-bit integer setting type */
182 static struct setting test_int8_setting = {
183         .name = "test_int8",
184         .type = &setting_type_int8,
185 };
186
187 /** Test signed 16-bit integer setting type */
188 static struct setting test_int16_setting = {
189         .name = "test_int16",
190         .type = &setting_type_int16,
191 };
192
193 /** Test signed 32-bit integer setting type */
194 static struct setting test_int32_setting = {
195         .name = "test_int32",
196         .type = &setting_type_int32,
197 };
198
199 /** Test unsigned 8-bit integer setting type */
200 static struct setting test_uint8_setting = {
201         .name = "test_uint8",
202         .type = &setting_type_uint8,
203 };
204
205 /** Test unsigned 16-bit integer setting type */
206 static struct setting test_uint16_setting = {
207         .name = "test_uint16",
208         .type = &setting_type_uint16,
209 };
210
211 /** Test unsigned 32-bit integer setting type */
212 static struct setting test_uint32_setting = {
213         .name = "test_uint32",
214         .type = &setting_type_uint32,
215 };
216
217 /** Test colon-separated hex string setting type */
218 static struct setting test_hex_setting = {
219         .name = "test_hex",
220         .type = &setting_type_hex,
221 };
222
223 /** Test hyphen-separated hex string setting type */
224 static struct setting test_hexhyp_setting = {
225         .name = "test_hexhyp",
226         .type = &setting_type_hexhyp,
227 };
228
229 /** Test raw hex string setting type */
230 static struct setting test_hexraw_setting = {
231         .name = "test_hexraw",
232         .type = &setting_type_hexraw,
233 };
234
235 /** Test Base64 setting type */
236 static struct setting test_base64_setting = {
237         .name = "test_base64",
238         .type = &setting_type_base64,
239 };
240
241 /** Test UUID setting type */
242 static struct setting test_uuid_setting = {
243         .name = "test_uuid",
244         .type = &setting_type_uuid,
245 };
246
247 /** Test PCI bus:dev.fn setting type */
248 static struct setting test_busdevfn_setting = {
249         .name = "test_busdevfn",
250         .type = &setting_type_busdevfn,
251 };
252
253 /**
254  * Perform settings self-tests
255  *
256  */
257 static void settings_test_exec ( void ) {
258
259         /* Register test settings block */
260         ok ( register_settings ( &test_settings, NULL, "test" ) == 0 );
261
262         /* "string" setting type */
263         storef_ok ( &test_settings, &test_string_setting, "hello",
264                     RAW ( 'h', 'e', 'l', 'l', 'o' ) );
265         fetchf_ok ( &test_settings, &test_string_setting,
266                     RAW ( 'w', 'o', 'r', 'l', 'd' ), "world" );
267
268         /* "ipv4" setting type */
269         storef_ok ( &test_settings, &test_ipv4_setting, "192.168.0.1",
270                     RAW ( 192, 168, 0, 1 ) );
271         fetchf_ok ( &test_settings, &test_ipv4_setting,
272                     RAW ( 212, 13, 204, 60 ), "212.13.204.60" );
273
274         /* "ipv6" setting type */
275         storef_ok ( &test_settings, &test_ipv6_setting,
276                     "2001:ba8:0:1d4::6950:5845",
277                     RAW ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
278                           0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ) );
279         fetchf_ok ( &test_settings, &test_ipv6_setting,
280                     RAW ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
281                           0x02, 0x0c, 0x29, 0xff, 0xfe, 0xc5, 0x39, 0xa1 ),
282                     "fe80::20c:29ff:fec5:39a1" );
283
284         /* Integer setting types (as formatted strings) */
285         storef_ok ( &test_settings, &test_int8_setting,
286                     "54", RAW ( 54 ) );
287         storef_ok ( &test_settings, &test_int8_setting,
288                     "0x7f", RAW ( 0x7f ) );
289         storef_ok ( &test_settings, &test_int8_setting,
290                     "0x1234", RAW ( 0x34 ) );
291         storef_ok ( &test_settings, &test_int8_setting,
292                     "-32", RAW ( -32 ) );
293         fetchf_ok ( &test_settings, &test_int8_setting,
294                     RAW ( -9 ), "-9" );
295         fetchf_ok ( &test_settings, &test_int8_setting,
296                     RAW ( 106 ), "106" );
297         storef_ok ( &test_settings, &test_uint8_setting,
298                     "129", RAW ( 129 ) );
299         storef_ok ( &test_settings, &test_uint8_setting,
300                     "0x3421", RAW ( 0x21 ) );
301         fetchf_ok ( &test_settings, &test_uint8_setting,
302                     RAW ( 0x54 ), "0x54" );
303         storef_ok ( &test_settings, &test_int16_setting,
304                     "29483", RAW ( 0x73, 0x2b ) );
305         fetchf_ok ( &test_settings, &test_int16_setting,
306                     RAW ( 0x82, 0x14 ), "-32236" );
307         fetchf_ok ( &test_settings, &test_int16_setting,
308                     RAW ( 0x12, 0x78 ), "4728" );
309         storef_ok ( &test_settings, &test_uint16_setting,
310                     "48727", RAW ( 0xbe, 0x57 ) );
311         fetchf_ok ( &test_settings, &test_uint16_setting,
312                     RAW ( 0x9a, 0x24 ), "0x9a24" );
313         storef_ok ( &test_settings, &test_int32_setting,
314                     "2901274", RAW ( 0x00, 0x2c, 0x45, 0x1a ) );
315         fetchf_ok ( &test_settings, &test_int32_setting,
316                     RAW ( 0xff, 0x34, 0x2d, 0xaf ), "-13357649" );
317         fetchf_ok ( &test_settings, &test_int32_setting,
318                     RAW ( 0x01, 0x00, 0x34, 0xab ), "16790699" );
319         storef_ok ( &test_settings, &test_uint32_setting,
320                     "0xb598d21", RAW ( 0x0b, 0x59, 0x8d, 0x21 ) );
321         fetchf_ok ( &test_settings, &test_uint32_setting,
322                     RAW ( 0xf2, 0x37, 0xb2, 0x18 ), "0xf237b218" );
323
324         /* Integer setting types (as numeric values) */
325         storen_ok ( &test_settings, &test_int8_setting,
326                     72, RAW ( 72 ) );
327         storen_ok ( &test_settings, &test_int8_setting,
328                     0xabcd, RAW ( 0xcd ) );
329         fetchn_ok ( &test_settings, &test_int8_setting,
330                     RAW ( 0xfe ), -2 );
331         storen_ok ( &test_settings, &test_uint8_setting,
332                     84, RAW ( 84 ) );
333         fetchn_ok ( &test_settings, &test_uint8_setting,
334                     RAW ( 0xfe ), 0xfe );
335         storen_ok ( &test_settings, &test_int16_setting,
336                     0x87bd, RAW ( 0x87, 0xbd ) );
337         fetchn_ok ( &test_settings, &test_int16_setting,
338                     RAW ( 0x3d, 0x14 ), 0x3d14 );
339         fetchn_ok ( &test_settings, &test_int16_setting,
340                     RAW ( 0x80 ), -128 );
341         storen_ok ( &test_settings, &test_uint16_setting,
342                     1, RAW ( 0x00, 0x01 ) );
343         fetchn_ok ( &test_settings, &test_uint16_setting,
344                     RAW ( 0xbd, 0x87 ), 0xbd87 );
345         fetchn_ok ( &test_settings, &test_uint16_setting,
346                     RAW ( 0x80 ), 0x0080 );
347         storen_ok ( &test_settings, &test_int32_setting,
348                     0x0812bfd2, RAW ( 0x08, 0x12, 0xbf, 0xd2 ) );
349         fetchn_ok ( &test_settings, &test_int32_setting,
350                     RAW ( 0x43, 0x87, 0x91, 0xb4 ), 0x438791b4 );
351         fetchn_ok ( &test_settings, &test_int32_setting,
352                     RAW ( 0xff, 0xff, 0xfe ), -2 );
353         storen_ok ( &test_settings, &test_uint32_setting,
354                     0xb5927ab8, RAW ( 0xb5, 0x92, 0x7a, 0xb8 ) );
355         fetchn_ok ( &test_settings, &test_uint32_setting,
356                     RAW ( 0x98, 0xab, 0x41, 0x81 ), 0x98ab4181 );
357         fetchn_ok ( &test_settings, &test_uint32_setting,
358                     RAW ( 0xff, 0xff, 0xfe ), 0x00fffffe );
359         fetchn_ok ( &test_settings, &test_uint32_setting,
360                     RAW ( 0, 0, 0, 0x12, 0x34, 0x56, 0x78 ), 0x12345678 );
361         fetchn_ok ( &test_settings, &test_int32_setting,
362                     RAW ( 0, 0, 0, 0x12, 0x34, 0x56, 0x78 ), 0x12345678 );
363         fetchn_ok ( &test_settings, &test_int32_setting,
364                     RAW ( 0xff, 0xff, 0x87, 0x65, 0x43, 0x21 ), -0x789abcdf );
365
366         /* "hex" setting type */
367         storef_ok ( &test_settings, &test_hex_setting,
368                     "08:12:f5:22:90:1b:4b:47:a8:30:cb:4d:67:4c:d6:76",
369                     RAW ( 0x08, 0x12, 0xf5, 0x22, 0x90, 0x1b, 0x4b, 0x47, 0xa8,
370                           0x30, 0xcb, 0x4d, 0x67, 0x4c, 0xd6, 0x76 ) );
371         fetchf_ok ( &test_settings, &test_hex_setting,
372                     RAW ( 0x62, 0xd9, 0xd4, 0xc4, 0x7e, 0x3b, 0x41, 0x46, 0x91,
373                           0xc6, 0xfd, 0x0c, 0xbf ),
374                     "62:d9:d4:c4:7e:3b:41:46:91:c6:fd:0c:bf" );
375
376         /* "hexhyp" setting type */
377         storef_ok ( &test_settings, &test_hexhyp_setting,
378                     "11-33-22", RAW ( 0x11, 0x33, 0x22 ) );
379         fetchf_ok ( &test_settings, &test_hexhyp_setting,
380                     RAW ( 0x9f, 0xe5, 0x6d, 0xfb, 0x24, 0x3a, 0x4c, 0xbb, 0xa9,
381                           0x09, 0x6c, 0x66, 0x13, 0xc1, 0xa8, 0xec, 0x27 ),
382                     "9f-e5-6d-fb-24-3a-4c-bb-a9-09-6c-66-13-c1-a8-ec-27" );
383
384         /* "hexraw" setting type */
385         storef_ok ( &test_settings, &test_hexraw_setting,
386                     "012345abcdef", RAW ( 0x01, 0x23, 0x45, 0xab, 0xcd, 0xef ));
387         fetchf_ok ( &test_settings, &test_hexraw_setting,
388                     RAW ( 0x9e, 0x4b, 0x6e, 0xef, 0x36, 0xb6, 0x46, 0xfe, 0x8f,
389                           0x17, 0x06, 0x39, 0x6b, 0xf4, 0x48, 0x4e ),
390                     "9e4b6eef36b646fe8f1706396bf4484e" );
391
392         /* "base64" setting type */
393         storef_ok ( &test_settings, &test_base64_setting,
394                     "cGFzc6\nNwaHJhc2U= ",
395                     RAW ( 0x70, 0x61, 0x73, 0x73, 0xa3, 0x70, 0x68, 0x72, 0x61,
396                           0x73, 0x65 ) );
397         fetchf_ok ( &test_settings, &test_base64_setting,
398                     RAW ( 0x80, 0x81, 0x82, 0x83, 0x84, 0x00, 0xff ),
399                     "gIGCg4QA/w==" );
400
401         /* "uuid" setting type (no store capability) */
402         fetchf_ok ( &test_settings, &test_uuid_setting,
403                     RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,
404                           0x7a, 0x7c, 0xfe, 0x4f, 0xca, 0x4a, 0x57 ),
405                     "1a6a749d-0eda-461a-a87a-7cfe4fca4a57" );
406
407         /* "busdevfn" setting type (no store capability) */
408         fetchf_ok ( &test_settings, &test_busdevfn_setting,
409                     RAW ( 0x03, 0x45 ), "03:08.5" );
410
411         /* Clear and unregister test settings block */
412         clear_settings ( &test_settings );
413         unregister_settings ( &test_settings );
414 }
415
416 /** Settings self-test */
417 struct self_test settings_test __self_test = {
418         .name = "settings",
419         .exec = settings_test_exec,
420 };
421
422 /* Include real IPv6 setting type */
423 REQUIRING_SYMBOL ( settings_test );
424 REQUIRE_OBJECT ( ipv6 );