iPXE
base64_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  * Base64 tests
00029  *
00030  * Test vectors generated using "base64 -w 0"
00031  *
00032  */
00033 
00034 /* Forcibly enable assertions */
00035 #undef NDEBUG
00036 
00037 #include <stdint.h>
00038 #include <string.h>
00039 #include <ipxe/base64.h>
00040 #include <ipxe/test.h>
00041 
00042 /** A Base64 test */
00043 struct base64_test {
00044         /** Raw data */
00045         const void *data;
00046         /** Length of raw data */
00047         size_t len;
00048         /** Base64-encoded data */
00049         const char *encoded;
00050 };
00051 
00052 /** Define inline data */
00053 #define DATA(...) { __VA_ARGS__ }
00054 
00055 /** Define a base64 test */
00056 #define BASE64( name, DATA, ENCODED )                                   \
00057         static const uint8_t name ## _data[] = DATA;                    \
00058         static struct base64_test name = {                              \
00059                 .data = name ## _data,                                  \
00060                 .len = sizeof ( name ## _data ),                        \
00061                 .encoded = ENCODED,                                     \
00062         }
00063 
00064 /** Empty data test */
00065 BASE64 ( empty_test, DATA(), "" );
00066 
00067 /** "Hello world" test */
00068 BASE64 ( hw_test,
00069          DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
00070          "SGVsbG8gd29ybGQ=" );
00071 
00072 /** Random data test */
00073 BASE64 ( random_test,
00074          DATA ( 0x36, 0x03, 0x84, 0xdc, 0x4e, 0x03, 0x46, 0xa0, 0xb5, 0x2d,
00075                 0x03, 0x6e, 0xd0, 0x56, 0xed, 0xa0, 0x37, 0x02, 0xac, 0xc6,
00076                 0x65, 0xd1 ),
00077          "NgOE3E4DRqC1LQNu0FbtoDcCrMZl0Q==" );
00078 
00079 /**
00080  * Report a base64 encoding test result
00081  *
00082  * @v test              Base64 test
00083  * @v file              Test code file
00084  * @v line              Test code line
00085  */
00086 static void base64_encode_okx ( struct base64_test *test, const char *file,
00087                                 unsigned int line ) {
00088         size_t len = base64_encoded_len ( test->len );
00089         char buf[ len + 1 /* NUL */ ];
00090         size_t check_len;
00091 
00092         okx ( len == strlen ( test->encoded ), file, line );
00093         check_len = base64_encode ( test->data, test->len, buf, sizeof ( buf ));
00094         okx ( check_len == len, file, line );
00095         okx ( strcmp ( test->encoded, buf ) == 0, file, line );
00096 }
00097 #define base64_encode_ok( test ) base64_encode_okx ( test, __FILE__, __LINE__ )
00098 
00099 /**
00100  * Report a base64 decoding test result
00101  *
00102  * @v test              Base64 test
00103  * @v file              Test code file
00104  * @v line              Test code line
00105  */
00106 static void base64_decode_okx ( struct base64_test *test, const char *file,
00107                                 unsigned int line ) {
00108         size_t max_len = base64_decoded_max_len ( test->encoded );
00109         uint8_t buf[max_len];
00110         int len;
00111 
00112         len = base64_decode ( test->encoded, buf, sizeof ( buf ) );
00113         okx ( len >= 0, file, line );
00114         okx ( ( size_t ) len <= max_len, file, line );
00115         okx ( ( size_t ) len == test->len, file, line );
00116         okx ( memcmp ( test->data, buf, len ) == 0, file, line );
00117 }
00118 #define base64_decode_ok( test ) base64_decode_okx ( test, __FILE__, __LINE__ )
00119 
00120 /**
00121  * Perform Base64 self-tests
00122  *
00123  */
00124 static void base64_test_exec ( void ) {
00125 
00126         base64_encode_ok ( &empty_test );
00127         base64_decode_ok ( &empty_test );
00128 
00129         base64_encode_ok ( &hw_test );
00130         base64_decode_ok ( &hw_test );
00131 
00132         base64_encode_ok ( &random_test );
00133         base64_decode_ok ( &random_test );
00134 }
00135 
00136 /** Base64 self-test */
00137 struct self_test base64_test __self_test = {
00138         .name = "base64",
00139         .exec = base64_test_exec,
00140 };