iPXE
base64_test.c
Go to the documentation of this file.
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
24FILE_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 */
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 */
65BASE64 ( empty_test, DATA(), "" );
66
67/** "Hello world" test */
68BASE64 ( hw_test,
69 DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
70 "SGVsbG8gd29ybGQ=" );
71
72/** Random data test */
73BASE64 ( 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 */
86static 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 */
106static 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 */
124static 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 */
138 .name = "base64",
139 .exec = base64_test_exec,
140};
#define DATA(...)
Define inline data.
Definition acpi_test.c:74
unsigned char uint8_t
Definition stdint.h:10
size_t base64_encode(const void *raw, size_t raw_len, char *data, size_t len)
Base64-encode data.
Definition base64.c:52
int base64_decode(const char *encoded, void *data, size_t len)
Base64-decode string.
Definition base64.c:92
Base64 encoding.
static size_t base64_decoded_max_len(const char *encoded)
Calculate maximum length of base64-decoded string.
Definition base64.h:35
static size_t base64_encoded_len(size_t raw_len)
Calculate length of base64-encoded data.
Definition base64.h:22
#define base64_decode_ok(test)
static void base64_decode_okx(struct base64_test *test, const char *file, unsigned int line)
Report a base64 decoding test result.
#define base64_encode_ok(test)
Definition base64_test.c:97
static void base64_encode_okx(struct base64_test *test, const char *file, unsigned int line)
Report a base64 encoding test result.
Definition base64_test.c:86
static void base64_test_exec(void)
Perform Base64 self-tests.
#define BASE64(name, DATA, ENCODED)
Define a base64 test.
Definition base64_test.c:56
#define DATA(...)
Define inline data.
Definition base64_test.c:53
ring len
Length.
Definition dwmac.h:226
static int test
Definition epic100.c:73
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
String functions.
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
A Base64 test.
Definition base64_test.c:43
const char * encoded
Base64-encoded data.
Definition base64_test.c:49
const void * data
Raw data.
Definition base64_test.c:45
size_t len
Length of raw data.
Definition base64_test.c:47
A self-test set.
Definition test.h:15
Self-test infrastructure.
#define okx(success, file, line)
Report test result.
Definition test.h:44
#define __self_test
Declare a self-test.
Definition test.h:32