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 );
26 * Test vectors generated using "base64 -w 0"
30 /* Forcibly enable assertions */
35 #include <ipxe/base64.h>
36 #include <ipxe/test.h>
42 /** Length of raw data */
44 /** Base64-encoded data */
48 /** Define inline data */
49 #define DATA(...) { __VA_ARGS__ }
51 /** Define a base64 test */
52 #define BASE64( name, DATA, ENCODED ) \
53 static const uint8_t name ## _data[] = DATA; \
54 static struct base64_test name = { \
55 .data = name ## _data, \
56 .len = sizeof ( name ## _data ), \
60 /** Empty data test */
61 BASE64 ( empty_test, DATA(), "" );
63 /** "Hello world" test */
65 DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
68 /** Random data test */
70 DATA ( 0x36, 0x03, 0x84, 0xdc, 0x4e, 0x03, 0x46, 0xa0, 0xb5, 0x2d,
71 0x03, 0x6e, 0xd0, 0x56, 0xed, 0xa0, 0x37, 0x02, 0xac, 0xc6,
73 "NgOE3E4DRqC1LQNu0FbtoDcCrMZl0Q==" );
76 * Report a base64 encoding test result
80 #define base64_encode_ok( test ) do { \
81 size_t len = base64_encoded_len ( (test)->len ); \
82 char buf[ len + 1 /* NUL */ ]; \
83 ok ( len == strlen ( (test)->encoded ) ); \
84 base64_encode ( (test)->data, (test)->len, buf ); \
85 ok ( strcmp ( (test)->encoded, buf ) == 0 ); \
89 * Report a base64 decoding test result
93 #define base64_decode_ok( test ) do { \
94 size_t max_len = base64_decoded_max_len ( (test)->encoded ); \
95 uint8_t buf[max_len]; \
97 len = base64_decode ( (test)->encoded, buf ); \
99 ok ( ( size_t ) len <= max_len ); \
100 ok ( ( size_t ) len == (test)->len ); \
101 ok ( memcmp ( (test)->data, buf, len ) == 0 ); \
105 * Perform Base64 self-tests
108 static void base64_test_exec ( void ) {
110 base64_encode_ok ( &empty_test );
111 base64_decode_ok ( &empty_test );
113 base64_encode_ok ( &hw_test );
114 base64_decode_ok ( &hw_test );
116 base64_encode_ok ( &random_test );
117 base64_decode_ok ( &random_test );
120 /** Base64 self-test */
121 struct self_test base64_test __self_test = {
123 .exec = base64_test_exec,