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