iPXE
base16_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 (at your option) 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  * Base16 tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <stdint.h>
00036 #include <string.h>
00037 #include <ipxe/base16.h>
00038 #include <ipxe/test.h>
00039 
00040 /** A Base16 test */
00041 struct base16_test {
00042         /** Raw data */
00043         const void *data;
00044         /** Length of raw data */
00045         size_t len;
00046         /** Base16-encoded data */
00047         const char *encoded;
00048 };
00049 
00050 /** Define inline data */
00051 #define DATA(...) { __VA_ARGS__ }
00052 
00053 /** Define a base16 test */
00054 #define BASE16( name, DATA, ENCODED )                                   \
00055         static const uint8_t name ## _data[] = DATA;                    \
00056         static struct base16_test name = {                              \
00057                 .data = name ## _data,                                  \
00058                 .len = sizeof ( name ## _data ),                        \
00059                 .encoded = ENCODED,                                     \
00060         }
00061 
00062 /** Empty data test */
00063 BASE16 ( empty_test, DATA(), "" );
00064 
00065 /** "Hello world" test */
00066 BASE16 ( hw_test,
00067          DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
00068          "48656c6c6f20776f726c64" );
00069 
00070 /** Random data test */
00071 BASE16 ( random_test,
00072          DATA ( 0x8b, 0x1a, 0xa2, 0x6c, 0xa9, 0x38, 0x43, 0xb8, 0x81, 0xf8,
00073                 0x30, 0x44, 0xb2, 0x32, 0x6e, 0x82, 0xfe, 0x0f, 0x84, 0x91 ),
00074          "8b1aa26ca93843b881f83044b2326e82fe0f8491" );
00075 
00076 /**
00077  * Report a base16 encoding test result
00078  *
00079  * @v test              Base16 test
00080  * @v file              Test code file
00081  * @v line              Test code line
00082  */
00083 static void base16_encode_okx ( struct base16_test *test, const char *file,
00084                                 unsigned int line ) {
00085         size_t len = base16_encoded_len ( test->len );
00086         char buf[ len + 1 /* NUL */ ];
00087         size_t check_len;
00088 
00089         okx ( len == strlen ( test->encoded ), file, line );
00090         check_len = base16_encode ( test->data, test->len, buf, sizeof ( buf ));
00091         okx ( check_len == len, file, line );
00092         okx ( strcmp ( test->encoded, buf ) == 0, file, line );
00093 }
00094 #define base16_encode_ok( test ) base16_encode_okx ( test, __FILE__, __LINE__ )
00095 
00096 /**
00097  * Report a base16 decoding test result
00098  *
00099  * @v test              Base16 test
00100  * @v file              Test code file
00101  * @v line              Test code line
00102  */
00103 static void base16_decode_okx ( struct base16_test *test, const char *file,
00104                                 unsigned int line ) {
00105         size_t max_len = base16_decoded_max_len ( test->encoded );
00106         uint8_t buf[max_len];
00107         int len;
00108 
00109         len = base16_decode ( test->encoded, buf, sizeof ( buf ) );
00110         okx ( len >= 0, file, line );
00111         okx ( ( size_t ) len <= max_len, file, line );
00112         okx ( ( size_t ) len == test->len, file, line );
00113         okx ( memcmp ( test->data, buf, len ) == 0, file, line );
00114 }
00115 #define base16_decode_ok( test ) base16_decode_okx ( test, __FILE__, __LINE__ )
00116 
00117 /**
00118  * Perform Base16 self-tests
00119  *
00120  */
00121 static void base16_test_exec ( void ) {
00122 
00123         base16_encode_ok ( &empty_test );
00124         base16_decode_ok ( &empty_test );
00125 
00126         base16_encode_ok ( &hw_test );
00127         base16_decode_ok ( &hw_test );
00128 
00129         base16_encode_ok ( &random_test );
00130         base16_decode_ok ( &random_test );
00131 }
00132 
00133 /** Base16 self-test */
00134 struct self_test base16_test __self_test = {
00135         .name = "base16",
00136         .exec = base16_test_exec,
00137 };