[infiniband] Retrieve GID flag from cached path entries
[ipxe.git] / src / tests / base64_test.c
1 /*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA.
18 *
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.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27 *
28 * Base64 tests
29 *
30 * Test vectors generated using "base64 -w 0"
31 *
32 */
33
34 /* Forcibly enable assertions */
35 #undef NDEBUG
36
37 #include <stdint.h>
38 #include <string.h>
39 #include <ipxe/base64.h>
40 #include <ipxe/test.h>
41
42 /** A Base64 test */
43 struct base64_test {
44 /** Raw data */
45 const void *data;
46 /** Length of raw data */
47 size_t len;
48 /** Base64-encoded data */
49 const char *encoded;
50 };
51
52 /** Define inline data */
53 #define DATA(...) { __VA_ARGS__ }
54
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 ), \
61 .encoded = ENCODED, \
62 }
63
64 /** Empty data test */
65 BASE64 ( empty_test, DATA(), "" );
66
67 /** "Hello world" test */
68 BASE64 ( hw_test,
69 DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
70 "SGVsbG8gd29ybGQ=" );
71
72 /** Random data test */
73 BASE64 ( random_test,
74 DATA ( 0x36, 0x03, 0x84, 0xdc, 0x4e, 0x03, 0x46, 0xa0, 0xb5, 0x2d,
75 0x03, 0x6e, 0xd0, 0x56, 0xed, 0xa0, 0x37, 0x02, 0xac, 0xc6,
76 0x65, 0xd1 ),
77 "NgOE3E4DRqC1LQNu0FbtoDcCrMZl0Q==" );
78
79 /**
80 * Report a base64 encoding test result
81 *
82 * @v test Base64 test
83 * @v file Test code file
84 * @v line Test code line
85 */
86 static void base64_encode_okx ( struct base64_test *test, const char *file,
87 unsigned int line ) {
88 size_t len = base64_encoded_len ( test->len );
89 char buf[ len + 1 /* NUL */ ];
90 size_t check_len;
91
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 );
96 }
97 #define base64_encode_ok( test ) base64_encode_okx ( test, __FILE__, __LINE__ )
98
99 /**
100 * Report a base64 decoding test result
101 *
102 * @v test Base64 test
103 * @v file Test code file
104 * @v line Test code line
105 */
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];
110 int len;
111
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 );
117 }
118 #define base64_decode_ok( test ) base64_decode_okx ( test, __FILE__, __LINE__ )
119
120 /**
121 * Perform Base64 self-tests
122 *
123 */
124 static void base64_test_exec ( void ) {
125
126 base64_encode_ok ( &empty_test );
127 base64_decode_ok ( &empty_test );
128
129 base64_encode_ok ( &hw_test );
130 base64_decode_ok ( &hw_test );
131
132 base64_encode_ok ( &random_test );
133 base64_decode_ok ( &random_test );
134 }
135
136 /** Base64 self-test */
137 struct self_test base64_test __self_test = {
138 .name = "base64",
139 .exec = base64_test_exec,
140 };