These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / tests / base16_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  * Base16 tests
29  *
30  */
31
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34
35 #include <stdint.h>
36 #include <string.h>
37 #include <ipxe/base16.h>
38 #include <ipxe/test.h>
39
40 /** A Base16 test */
41 struct base16_test {
42         /** Raw data */
43         const void *data;
44         /** Length of raw data */
45         size_t len;
46         /** Base16-encoded data */
47         const char *encoded;
48 };
49
50 /** Define inline data */
51 #define DATA(...) { __VA_ARGS__ }
52
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 ),                        \
59                 .encoded = ENCODED,                                     \
60         }
61
62 /** Empty data test */
63 BASE16 ( empty_test, DATA(), "" );
64
65 /** "Hello world" test */
66 BASE16 ( hw_test,
67          DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
68          "48656c6c6f20776f726c64" );
69
70 /** Random data test */
71 BASE16 ( random_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" );
75
76 /**
77  * Report a base16 encoding test result
78  *
79  * @v test              Base16 test
80  * @v file              Test code file
81  * @v line              Test code line
82  */
83 static void base16_encode_okx ( struct base16_test *test, const char *file,
84                                 unsigned int line ) {
85         size_t len = base16_encoded_len ( test->len );
86         char buf[ len + 1 /* NUL */ ];
87         size_t check_len;
88
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 );
93 }
94 #define base16_encode_ok( test ) base16_encode_okx ( test, __FILE__, __LINE__ )
95
96 /**
97  * Report a base16 decoding test result
98  *
99  * @v test              Base16 test
100  * @v file              Test code file
101  * @v line              Test code line
102  */
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];
107         int len;
108
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 );
114 }
115 #define base16_decode_ok( test ) base16_decode_okx ( test, __FILE__, __LINE__ )
116
117 /**
118  * Perform Base16 self-tests
119  *
120  */
121 static void base16_test_exec ( void ) {
122
123         base16_encode_ok ( &empty_test );
124         base16_decode_ok ( &empty_test );
125
126         base16_encode_ok ( &hw_test );
127         base16_decode_ok ( &hw_test );
128
129         base16_encode_ok ( &random_test );
130         base16_decode_ok ( &random_test );
131 }
132
133 /** Base16 self-test */
134 struct self_test base16_test __self_test = {
135         .name = "base16",
136         .exec = base16_test_exec,
137 };