iPXE
settings_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Settings self-tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <string.h>
00036 #include <ipxe/settings.h>
00037 #include <ipxe/test.h>
00038 
00039 /** Define inline raw data */
00040 #define RAW(...) { __VA_ARGS__ }
00041 
00042 /**
00043  * Report a formatted-store test result
00044  *
00045  * @v _settings         Settings block
00046  * @v _setting          Setting
00047  * @v _formatted        Formatted value
00048  * @v _raw_array        Expected raw value
00049  */
00050 #define storef_ok( _settings, _setting, _formatted, _raw_array ) do {   \
00051         const uint8_t expected[] = _raw_array;                          \
00052         uint8_t actual[ sizeof ( expected ) ];                          \
00053         int len;                                                        \
00054                                                                         \
00055         ok ( storef_setting ( _settings, _setting, _formatted ) == 0 ); \
00056         len = fetch_setting ( _settings, _setting, NULL, NULL, actual,  \
00057                               sizeof ( actual ) );                      \
00058         if ( len >= 0 ) {                                               \
00059                 DBGC ( _settings, "Stored %s \"%s\", got:\n",           \
00060                        (_setting)->type->name, _formatted );            \
00061                 DBGC_HDA ( _settings, 0, actual, len );                 \
00062         } else {                                                        \
00063                 DBGC ( _settings, "Stored %s \"%s\", got error %s\n",   \
00064                        (_setting)->type->name, _formatted,              \
00065                        strerror ( len ) );                              \
00066         }                                                               \
00067         ok ( len == ( int ) sizeof ( actual ) );                        \
00068         ok ( memcmp ( actual, expected, sizeof ( actual ) ) == 0 );     \
00069         } while ( 0 )
00070 
00071 /**
00072  * Report a formatted-fetch test result
00073  *
00074  * @v _settings         Settings block
00075  * @v _setting          Setting
00076  * @v _raw_array        Raw value
00077  * @v _formatted        Expected formatted value
00078  */
00079 #define fetchf_ok( _settings, _setting, _raw_array, _formatted ) do {   \
00080         const uint8_t raw[] = _raw_array;                               \
00081         char actual[ strlen ( _formatted ) + 1 ];                       \
00082         int len;                                                        \
00083                                                                         \
00084         ok ( store_setting ( _settings, _setting, raw,                  \
00085                              sizeof ( raw ) ) == 0 );                   \
00086         len = fetchf_setting ( _settings, _setting, NULL, NULL, actual, \
00087                                sizeof ( actual ) );                     \
00088         DBGC ( _settings, "Fetched %s \"%s\" from:\n",                  \
00089                (_setting)->type->name, actual );                        \
00090         DBGC_HDA ( _settings, 0, raw, sizeof ( raw ) );                 \
00091         ok ( len == ( int ) ( sizeof ( actual ) - 1 ) );                \
00092         ok ( strcmp ( actual, _formatted ) == 0 );                      \
00093         } while ( 0 )
00094 
00095 /**
00096  * Report a numeric-store test result
00097  *
00098  * @v _settings         Settings block
00099  * @v _setting          Setting
00100  * @v _numeric          Numeric value
00101  * @v _raw_array        Expected raw value
00102  */
00103 #define storen_ok( _settings, _setting, _numeric, _raw_array ) do {     \
00104         const uint8_t expected[] = _raw_array;                          \
00105         uint8_t actual[ sizeof ( expected ) ];                          \
00106         int len;                                                        \
00107                                                                         \
00108         ok ( storen_setting ( _settings, _setting, _numeric ) == 0 );   \
00109         len = fetch_setting ( _settings, _setting, NULL, NULL, actual,  \
00110                               sizeof ( actual ) );                      \
00111         if ( len >= 0 ) {                                               \
00112                 DBGC ( _settings, "Stored %s %#lx, got:\n",             \
00113                        (_setting)->type->name,                          \
00114                        ( unsigned long ) _numeric );                    \
00115                 DBGC_HDA ( _settings, 0, actual, len );                 \
00116         } else {                                                        \
00117                 DBGC ( _settings, "Stored %s %#lx, got error %s\n",     \
00118                        (_setting)->type->name,                          \
00119                        ( unsigned long ) _numeric, strerror ( len ) );  \
00120         }                                                               \
00121         ok ( len == ( int ) sizeof ( actual ) );                        \
00122         ok ( memcmp ( actual, expected, sizeof ( actual ) ) == 0 );     \
00123         } while ( 0 )
00124 
00125 /**
00126  * Report a numeric-fetch test result
00127  *
00128  * @v _settings         Settings block
00129  * @v _setting          Setting
00130  * @v _raw_array        Raw array
00131  * @v _numeric          Expected numeric value
00132  */
00133 #define fetchn_ok( _settings, _setting, _raw_array, _numeric ) do {     \
00134         const uint8_t raw[] = _raw_array;                               \
00135         unsigned long actual;                                           \
00136                                                                         \
00137         ok ( store_setting ( _settings, _setting, raw,                  \
00138                              sizeof ( raw ) ) == 0 );                   \
00139         ok ( fetchn_setting ( _settings, _setting, NULL, NULL,          \
00140                               &actual ) == 0 );                         \
00141         DBGC ( _settings, "Fetched %s %#lx from:\n",                    \
00142                (_setting)->type->name, actual );                        \
00143         DBGC_HDA ( _settings, 0, raw, sizeof ( raw ) );                 \
00144         ok ( actual == ( unsigned long ) _numeric );                    \
00145         } while ( 0 )
00146 
00147 /** Test generic settings block */
00148 struct generic_settings test_generic_settings = {
00149         .settings = {
00150                 .refcnt = NULL,
00151                 .siblings =
00152                     LIST_HEAD_INIT ( test_generic_settings.settings.siblings ),
00153                 .children =
00154                     LIST_HEAD_INIT ( test_generic_settings.settings.children ),
00155                 .op = &generic_settings_operations,
00156         },
00157         .list = LIST_HEAD_INIT ( test_generic_settings.list ),
00158 };
00159 
00160 /** Test settings block */
00161 #define test_settings test_generic_settings.settings
00162 
00163 /** Test string setting */
00164 static struct setting test_string_setting = {
00165         .name = "test_string",
00166         .type = &setting_type_string,
00167 };
00168 
00169 /** Test URI-encoded string setting */
00170 static struct setting test_uristring_setting = {
00171         .name = "test_uristring",
00172         .type = &setting_type_uristring,
00173 };
00174 
00175 /** Test IPv4 address setting type */
00176 static struct setting test_ipv4_setting = {
00177         .name = "test_ipv4",
00178         .type = &setting_type_ipv4,
00179 };
00180 
00181 /** Test IPv6 address setting type */
00182 static struct setting test_ipv6_setting = {
00183         .name = "test_ipv6",
00184         .type = &setting_type_ipv6,
00185 };
00186 
00187 /** Test signed 8-bit integer setting type */
00188 static struct setting test_int8_setting = {
00189         .name = "test_int8",
00190         .type = &setting_type_int8,
00191 };
00192 
00193 /** Test signed 16-bit integer setting type */
00194 static struct setting test_int16_setting = {
00195         .name = "test_int16",
00196         .type = &setting_type_int16,
00197 };
00198 
00199 /** Test signed 32-bit integer setting type */
00200 static struct setting test_int32_setting = {
00201         .name = "test_int32",
00202         .type = &setting_type_int32,
00203 };
00204 
00205 /** Test unsigned 8-bit integer setting type */
00206 static struct setting test_uint8_setting = {
00207         .name = "test_uint8",
00208         .type = &setting_type_uint8,
00209 };
00210 
00211 /** Test unsigned 16-bit integer setting type */
00212 static struct setting test_uint16_setting = {
00213         .name = "test_uint16",
00214         .type = &setting_type_uint16,
00215 };
00216 
00217 /** Test unsigned 32-bit integer setting type */
00218 static struct setting test_uint32_setting = {
00219         .name = "test_uint32",
00220         .type = &setting_type_uint32,
00221 };
00222 
00223 /** Test colon-separated hex string setting type */
00224 static struct setting test_hex_setting = {
00225         .name = "test_hex",
00226         .type = &setting_type_hex,
00227 };
00228 
00229 /** Test hyphen-separated hex string setting type */
00230 static struct setting test_hexhyp_setting = {
00231         .name = "test_hexhyp",
00232         .type = &setting_type_hexhyp,
00233 };
00234 
00235 /** Test raw hex string setting type */
00236 static struct setting test_hexraw_setting = {
00237         .name = "test_hexraw",
00238         .type = &setting_type_hexraw,
00239 };
00240 
00241 /** Test Base64 setting type */
00242 static struct setting test_base64_setting = {
00243         .name = "test_base64",
00244         .type = &setting_type_base64,
00245 };
00246 
00247 /** Test UUID setting type */
00248 static struct setting test_uuid_setting = {
00249         .name = "test_uuid",
00250         .type = &setting_type_uuid,
00251 };
00252 
00253 /** Test PCI bus:dev.fn setting type */
00254 static struct setting test_busdevfn_setting = {
00255         .name = "test_busdevfn",
00256         .type = &setting_type_busdevfn,
00257 };
00258 
00259 /**
00260  * Perform settings self-tests
00261  *
00262  */
00263 static void settings_test_exec ( void ) {
00264 
00265         /* Register test settings block */
00266         ok ( register_settings ( &test_settings, NULL, "test" ) == 0 );
00267 
00268         /* "string" setting type */
00269         storef_ok ( &test_settings, &test_string_setting, "hello",
00270                     RAW ( 'h', 'e', 'l', 'l', 'o' ) );
00271         fetchf_ok ( &test_settings, &test_string_setting,
00272                     RAW ( 'w', 'o', 'r', 'l', 'd' ), "world" );
00273 
00274         /* "uristring" setting type */
00275         storef_ok ( &test_settings, &test_uristring_setting, "hello%20world",
00276                     RAW ( 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l',
00277                           'd' ) );
00278         fetchf_ok ( &test_settings, &test_uristring_setting,
00279                     RAW ( 1, 2, 3, 4, 5 ), "%01%02%03%04%05" );
00280         fetchf_ok ( &test_settings, &test_uristring_setting,
00281                     RAW ( 0, ' ', '%', '/', '#', ':', '@', '?', '=', '&' ),
00282                     "%00%20%25%2F%23%3A%40%3F%3D%26" );
00283 
00284         /* "ipv4" setting type */
00285         storef_ok ( &test_settings, &test_ipv4_setting, "192.168.0.1",
00286                     RAW ( 192, 168, 0, 1 ) );
00287         fetchf_ok ( &test_settings, &test_ipv4_setting,
00288                     RAW ( 212, 13, 204, 60 ), "212.13.204.60" );
00289 
00290         /* "ipv6" setting type */
00291         storef_ok ( &test_settings, &test_ipv6_setting,
00292                     "2001:ba8:0:1d4::6950:5845",
00293                     RAW ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
00294                           0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ) );
00295         fetchf_ok ( &test_settings, &test_ipv6_setting,
00296                     RAW ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00297                           0x02, 0x0c, 0x29, 0xff, 0xfe, 0xc5, 0x39, 0xa1 ),
00298                     "fe80::20c:29ff:fec5:39a1" );
00299 
00300         /* Integer setting types (as formatted strings) */
00301         storef_ok ( &test_settings, &test_int8_setting,
00302                     "54", RAW ( 54 ) );
00303         storef_ok ( &test_settings, &test_int8_setting,
00304                     "0x7f", RAW ( 0x7f ) );
00305         storef_ok ( &test_settings, &test_int8_setting,
00306                     "0x1234", RAW ( 0x34 ) );
00307         storef_ok ( &test_settings, &test_int8_setting,
00308                     "-32", RAW ( -32 ) );
00309         fetchf_ok ( &test_settings, &test_int8_setting,
00310                     RAW ( -9 ), "-9" );
00311         fetchf_ok ( &test_settings, &test_int8_setting,
00312                     RAW ( 106 ), "106" );
00313         storef_ok ( &test_settings, &test_uint8_setting,
00314                     "129", RAW ( 129 ) );
00315         storef_ok ( &test_settings, &test_uint8_setting,
00316                     "0x3421", RAW ( 0x21 ) );
00317         fetchf_ok ( &test_settings, &test_uint8_setting,
00318                     RAW ( 0x54 ), "0x54" );
00319         storef_ok ( &test_settings, &test_int16_setting,
00320                     "29483", RAW ( 0x73, 0x2b ) );
00321         fetchf_ok ( &test_settings, &test_int16_setting,
00322                     RAW ( 0x82, 0x14 ), "-32236" );
00323         fetchf_ok ( &test_settings, &test_int16_setting,
00324                     RAW ( 0x12, 0x78 ), "4728" );
00325         storef_ok ( &test_settings, &test_uint16_setting,
00326                     "48727", RAW ( 0xbe, 0x57 ) );
00327         fetchf_ok ( &test_settings, &test_uint16_setting,
00328                     RAW ( 0x9a, 0x24 ), "0x9a24" );
00329         storef_ok ( &test_settings, &test_int32_setting,
00330                     "2901274", RAW ( 0x00, 0x2c, 0x45, 0x1a ) );
00331         fetchf_ok ( &test_settings, &test_int32_setting,
00332                     RAW ( 0xff, 0x34, 0x2d, 0xaf ), "-13357649" );
00333         fetchf_ok ( &test_settings, &test_int32_setting,
00334                     RAW ( 0x01, 0x00, 0x34, 0xab ), "16790699" );
00335         storef_ok ( &test_settings, &test_uint32_setting,
00336                     "0xb598d21", RAW ( 0x0b, 0x59, 0x8d, 0x21 ) );
00337         fetchf_ok ( &test_settings, &test_uint32_setting,
00338                     RAW ( 0xf2, 0x37, 0xb2, 0x18 ), "0xf237b218" );
00339 
00340         /* Integer setting types (as numeric values) */
00341         storen_ok ( &test_settings, &test_int8_setting,
00342                     72, RAW ( 72 ) );
00343         storen_ok ( &test_settings, &test_int8_setting,
00344                     0xabcd, RAW ( 0xcd ) );
00345         fetchn_ok ( &test_settings, &test_int8_setting,
00346                     RAW ( 0xfe ), -2 );
00347         storen_ok ( &test_settings, &test_uint8_setting,
00348                     84, RAW ( 84 ) );
00349         fetchn_ok ( &test_settings, &test_uint8_setting,
00350                     RAW ( 0xfe ), 0xfe );
00351         storen_ok ( &test_settings, &test_int16_setting,
00352                     0x87bd, RAW ( 0x87, 0xbd ) );
00353         fetchn_ok ( &test_settings, &test_int16_setting,
00354                     RAW ( 0x3d, 0x14 ), 0x3d14 );
00355         fetchn_ok ( &test_settings, &test_int16_setting,
00356                     RAW ( 0x80 ), -128 );
00357         storen_ok ( &test_settings, &test_uint16_setting,
00358                     1, RAW ( 0x00, 0x01 ) );
00359         fetchn_ok ( &test_settings, &test_uint16_setting,
00360                     RAW ( 0xbd, 0x87 ), 0xbd87 );
00361         fetchn_ok ( &test_settings, &test_uint16_setting,
00362                     RAW ( 0x80 ), 0x0080 );
00363         storen_ok ( &test_settings, &test_int32_setting,
00364                     0x0812bfd2, RAW ( 0x08, 0x12, 0xbf, 0xd2 ) );
00365         fetchn_ok ( &test_settings, &test_int32_setting,
00366                     RAW ( 0x43, 0x87, 0x91, 0xb4 ), 0x438791b4 );
00367         fetchn_ok ( &test_settings, &test_int32_setting,
00368                     RAW ( 0xff, 0xff, 0xfe ), -2 );
00369         storen_ok ( &test_settings, &test_uint32_setting,
00370                     0xb5927ab8, RAW ( 0xb5, 0x92, 0x7a, 0xb8 ) );
00371         fetchn_ok ( &test_settings, &test_uint32_setting,
00372                     RAW ( 0x98, 0xab, 0x41, 0x81 ), 0x98ab4181 );
00373         fetchn_ok ( &test_settings, &test_uint32_setting,
00374                     RAW ( 0xff, 0xff, 0xfe ), 0x00fffffe );
00375         fetchn_ok ( &test_settings, &test_uint32_setting,
00376                     RAW ( 0, 0, 0, 0x12, 0x34, 0x56, 0x78 ), 0x12345678 );
00377         fetchn_ok ( &test_settings, &test_int32_setting,
00378                     RAW ( 0, 0, 0, 0x12, 0x34, 0x56, 0x78 ), 0x12345678 );
00379         fetchn_ok ( &test_settings, &test_int32_setting,
00380                     RAW ( 0xff, 0xff, 0x87, 0x65, 0x43, 0x21 ), -0x789abcdf );
00381 
00382         /* "hex" setting type */
00383         storef_ok ( &test_settings, &test_hex_setting,
00384                     "08:12:f5:22:90:1b:4b:47:a8:30:cb:4d:67:4c:d6:76",
00385                     RAW ( 0x08, 0x12, 0xf5, 0x22, 0x90, 0x1b, 0x4b, 0x47, 0xa8,
00386                           0x30, 0xcb, 0x4d, 0x67, 0x4c, 0xd6, 0x76 ) );
00387         fetchf_ok ( &test_settings, &test_hex_setting,
00388                     RAW ( 0x62, 0xd9, 0xd4, 0xc4, 0x7e, 0x3b, 0x41, 0x46, 0x91,
00389                           0xc6, 0xfd, 0x0c, 0xbf ),
00390                     "62:d9:d4:c4:7e:3b:41:46:91:c6:fd:0c:bf" );
00391 
00392         /* "hexhyp" setting type */
00393         storef_ok ( &test_settings, &test_hexhyp_setting,
00394                     "11-33-22", RAW ( 0x11, 0x33, 0x22 ) );
00395         fetchf_ok ( &test_settings, &test_hexhyp_setting,
00396                     RAW ( 0x9f, 0xe5, 0x6d, 0xfb, 0x24, 0x3a, 0x4c, 0xbb, 0xa9,
00397                           0x09, 0x6c, 0x66, 0x13, 0xc1, 0xa8, 0xec, 0x27 ),
00398                     "9f-e5-6d-fb-24-3a-4c-bb-a9-09-6c-66-13-c1-a8-ec-27" );
00399 
00400         /* "hexraw" setting type */
00401         storef_ok ( &test_settings, &test_hexraw_setting,
00402                     "012345abcdef", RAW ( 0x01, 0x23, 0x45, 0xab, 0xcd, 0xef ));
00403         fetchf_ok ( &test_settings, &test_hexraw_setting,
00404                     RAW ( 0x9e, 0x4b, 0x6e, 0xef, 0x36, 0xb6, 0x46, 0xfe, 0x8f,
00405                           0x17, 0x06, 0x39, 0x6b, 0xf4, 0x48, 0x4e ),
00406                     "9e4b6eef36b646fe8f1706396bf4484e" );
00407 
00408         /* "base64" setting type */
00409         storef_ok ( &test_settings, &test_base64_setting,
00410                     "cGFzc6\nNwaHJhc2U= ",
00411                     RAW ( 0x70, 0x61, 0x73, 0x73, 0xa3, 0x70, 0x68, 0x72, 0x61,
00412                           0x73, 0x65 ) );
00413         fetchf_ok ( &test_settings, &test_base64_setting,
00414                     RAW ( 0x80, 0x81, 0x82, 0x83, 0x84, 0x00, 0xff ),
00415                     "gIGCg4QA/w==" );
00416 
00417         /* "uuid" setting type (no store capability) */
00418         fetchf_ok ( &test_settings, &test_uuid_setting,
00419                     RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,
00420                           0x7a, 0x7c, 0xfe, 0x4f, 0xca, 0x4a, 0x57 ),
00421                     "1a6a749d-0eda-461a-a87a-7cfe4fca4a57" );
00422 
00423         /* "busdevfn" setting type (no store capability) */
00424         fetchf_ok ( &test_settings, &test_busdevfn_setting,
00425                     RAW ( 0x03, 0x45 ), "0000:03:08.5" );
00426         fetchf_ok ( &test_settings, &test_busdevfn_setting,
00427                     RAW ( 0x00, 0x02, 0x0a, 0x21 ), "0002:0a:04.1" );
00428 
00429         /* Clear and unregister test settings block */
00430         clear_settings ( &test_settings );
00431         unregister_settings ( &test_settings );
00432 }
00433 
00434 /** Settings self-test */
00435 struct self_test settings_test __self_test = {
00436         .name = "settings",
00437         .exec = settings_test_exec,
00438 };
00439 
00440 /* Include real IPv6 setting type */
00441 REQUIRING_SYMBOL ( settings_test );
00442 REQUIRE_OBJECT ( ipv6 );