2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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 (at your option) any later version.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 /* Forcibly enable assertions */
37 #include <ipxe/base16.h>
38 #include <ipxe/test.h>
44 /** Length of raw data */
46 /** Base16-encoded data */
50 /** Define inline data */
51 #define DATA(...) { __VA_ARGS__ }
53 /** Define a base16 test */
54 #define BASE16( name, DATA, ENCODED ) \
55 static const uint8_t name ## _data[] = DATA; \
56 static struct base16_test name = { \
57 .data = name ## _data, \
58 .len = sizeof ( name ## _data ), \
62 /** Empty data test */
63 BASE16 ( empty_test, DATA(), "" );
65 /** "Hello world" test */
67 DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
68 "48656c6c6f20776f726c64" );
70 /** Random data test */
72 DATA ( 0x8b, 0x1a, 0xa2, 0x6c, 0xa9, 0x38, 0x43, 0xb8, 0x81, 0xf8,
73 0x30, 0x44, 0xb2, 0x32, 0x6e, 0x82, 0xfe, 0x0f, 0x84, 0x91 ),
74 "8b1aa26ca93843b881f83044b2326e82fe0f8491" );
77 * Report a base16 encoding test result
80 * @v file Test code file
81 * @v line Test code line
83 static void base16_encode_okx ( struct base16_test *test, const char *file,
85 size_t len = base16_encoded_len ( test->len );
86 char buf[ len + 1 /* NUL */ ];
89 okx ( len == strlen ( test->encoded ), file, line );
90 check_len = base16_encode ( test->data, test->len, buf, sizeof ( buf ));
91 okx ( check_len == len, file, line );
92 okx ( strcmp ( test->encoded, buf ) == 0, file, line );
94 #define base16_encode_ok( test ) base16_encode_okx ( test, __FILE__, __LINE__ )
97 * Report a base16 decoding test result
100 * @v file Test code file
101 * @v line Test code line
103 static void base16_decode_okx ( struct base16_test *test, const char *file,
104 unsigned int line ) {
105 size_t max_len = base16_decoded_max_len ( test->encoded );
106 uint8_t buf[max_len];
109 len = base16_decode ( test->encoded, buf, sizeof ( buf ) );
110 okx ( len >= 0, file, line );
111 okx ( ( size_t ) len <= max_len, file, line );
112 okx ( ( size_t ) len == test->len, file, line );
113 okx ( memcmp ( test->data, buf, len ) == 0, file, line );
115 #define base16_decode_ok( test ) base16_decode_okx ( test, __FILE__, __LINE__ )
118 * Perform Base16 self-tests
121 static void base16_test_exec ( void ) {
123 base16_encode_ok ( &empty_test );
124 base16_decode_ok ( &empty_test );
126 base16_encode_ok ( &hw_test );
127 base16_decode_ok ( &hw_test );
129 base16_encode_ok ( &random_test );
130 base16_decode_ok ( &random_test );
133 /** Base16 self-test */
134 struct self_test base16_test __self_test = {
136 .exec = base16_test_exec,