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
20 FILE_LICENCE ( GPL2_OR_LATER );
28 /* Forcibly enable assertions */
33 #include <ipxe/base16.h>
34 #include <ipxe/test.h>
40 /** Length of raw data */
42 /** Base16-encoded data */
46 /** Define inline data */
47 #define DATA(...) { __VA_ARGS__ }
49 /** Define a base16 test */
50 #define BASE16( name, DATA, ENCODED ) \
51 static const uint8_t name ## _data[] = DATA; \
52 static struct base16_test name = { \
53 .data = name ## _data, \
54 .len = sizeof ( name ## _data ), \
58 /** Empty data test */
59 BASE16 ( empty_test, DATA(), "" );
61 /** "Hello world" test */
63 DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
64 "48656c6c6f20776f726c64" );
66 /** Random data test */
68 DATA ( 0x8b, 0x1a, 0xa2, 0x6c, 0xa9, 0x38, 0x43, 0xb8, 0x81, 0xf8,
69 0x30, 0x44, 0xb2, 0x32, 0x6e, 0x82, 0xfe, 0x0f, 0x84, 0x91 ),
70 "8b1aa26ca93843b881f83044b2326e82fe0f8491" );
73 * Report a base16 encoding test result
77 #define base16_encode_ok( test ) do { \
78 size_t len = base16_encoded_len ( (test)->len ); \
79 char buf[ len + 1 /* NUL */ ]; \
80 ok ( len == strlen ( (test)->encoded ) ); \
81 base16_encode ( (test)->data, (test)->len, buf ); \
82 ok ( strcmp ( (test)->encoded, buf ) == 0 ); \
86 * Report a base16 decoding test result
90 #define base16_decode_ok( test ) do { \
91 size_t max_len = base16_decoded_max_len ( (test)->encoded ); \
92 uint8_t buf[max_len]; \
94 len = base16_decode ( (test)->encoded, buf ); \
96 ok ( ( size_t ) len <= max_len ); \
97 ok ( ( size_t ) len == (test)->len ); \
98 ok ( memcmp ( (test)->data, buf, len ) == 0 ); \
102 * Perform Base16 self-tests
105 static void base16_test_exec ( void ) {
107 base16_encode_ok ( &empty_test );
108 base16_decode_ok ( &empty_test );
110 base16_encode_ok ( &hw_test );
111 base16_decode_ok ( &hw_test );
113 base16_encode_ok ( &random_test );
114 base16_decode_ok ( &random_test );
117 /** Base16 self-test */
118 struct self_test base16_test __self_test = {
120 .exec = base16_test_exec,