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 );
30 * Test vectors generated using "base64 -w 0"
34 /* Forcibly enable assertions */
39 #include <ipxe/base64.h>
40 #include <ipxe/test.h>
46 /** Length of raw data */
48 /** Base64-encoded data */
52 /** Define inline data */
53 #define DATA(...) { __VA_ARGS__ }
55 /** Define a base64 test */
56 #define BASE64( name, DATA, ENCODED ) \
57 static const uint8_t name ## _data[] = DATA; \
58 static struct base64_test name = { \
59 .data = name ## _data, \
60 .len = sizeof ( name ## _data ), \
64 /** Empty data test */
65 BASE64 ( empty_test, DATA(), "" );
67 /** "Hello world" test */
69 DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
72 /** Random data test */
74 DATA ( 0x36, 0x03, 0x84, 0xdc, 0x4e, 0x03, 0x46, 0xa0, 0xb5, 0x2d,
75 0x03, 0x6e, 0xd0, 0x56, 0xed, 0xa0, 0x37, 0x02, 0xac, 0xc6,
77 "NgOE3E4DRqC1LQNu0FbtoDcCrMZl0Q==" );
80 * Report a base64 encoding test result
83 * @v file Test code file
84 * @v line Test code line
86 static void base64_encode_okx ( struct base64_test *test, const char *file,
88 size_t len = base64_encoded_len ( test->len );
89 char buf[ len + 1 /* NUL */ ];
92 okx ( len == strlen ( test->encoded ), file, line );
93 check_len = base64_encode ( test->data, test->len, buf, sizeof ( buf ));
94 okx ( check_len == len, file, line );
95 okx ( strcmp ( test->encoded, buf ) == 0, file, line );
97 #define base64_encode_ok( test ) base64_encode_okx ( test, __FILE__, __LINE__ )
100 * Report a base64 decoding test result
102 * @v test Base64 test
103 * @v file Test code file
104 * @v line Test code line
106 static void base64_decode_okx ( struct base64_test *test, const char *file,
107 unsigned int line ) {
108 size_t max_len = base64_decoded_max_len ( test->encoded );
109 uint8_t buf[max_len];
112 len = base64_decode ( test->encoded, buf, sizeof ( buf ) );
113 okx ( len >= 0, file, line );
114 okx ( ( size_t ) len <= max_len, file, line );
115 okx ( ( size_t ) len == test->len, file, line );
116 okx ( memcmp ( test->data, buf, len ) == 0, file, line );
118 #define base64_decode_ok( test ) base64_decode_okx ( test, __FILE__, __LINE__ )
121 * Perform Base64 self-tests
124 static void base64_test_exec ( void ) {
126 base64_encode_ok ( &empty_test );
127 base64_decode_ok ( &empty_test );
129 base64_encode_ok ( &hw_test );
130 base64_decode_ok ( &hw_test );
132 base64_encode_ok ( &random_test );
133 base64_decode_ok ( &random_test );
136 /** Base64 self-test */
137 struct self_test base64_test __self_test = {
139 .exec = base64_test_exec,