Explorar el Código

[test] Add self-tests for base64

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown hace 12 años
padre
commit
f4c88d55bd
Se han modificado 2 ficheros con 125 adiciones y 0 borrados
  1. 124
    0
      src/tests/base64_test.c
  2. 1
    0
      src/tests/tests.c

+ 124
- 0
src/tests/base64_test.c Ver fichero

@@ -0,0 +1,124 @@
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
+
20
+FILE_LICENCE ( GPL2_OR_LATER );
21
+
22
+/** @file
23
+ *
24
+ * Base64 tests
25
+ *
26
+ * Test vectors generated using "base64 -w 0"
27
+ *
28
+ */
29
+
30
+/* Forcibly enable assertions */
31
+#undef NDEBUG
32
+
33
+#include <stdint.h>
34
+#include <string.h>
35
+#include <ipxe/base64.h>
36
+#include <ipxe/test.h>
37
+
38
+/** A Base64 test */
39
+struct base64_test {
40
+	/** Raw data */
41
+	const void *data;
42
+	/** Length of raw data */
43
+	size_t len;
44
+	/** Base64-encoded data */
45
+	const char *encoded;
46
+};
47
+
48
+/** Define inline data */
49
+#define DATA(...) { __VA_ARGS__ }
50
+
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 ),			\
57
+		.encoded = ENCODED,					\
58
+	}
59
+
60
+/** Empty data test */
61
+BASE64 ( empty_test, DATA(), "" );
62
+
63
+/** "Hello world" test */
64
+BASE64 ( hw_test,
65
+	 DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
66
+	 "SGVsbG8gd29ybGQ=" );
67
+
68
+/** Random data test */
69
+BASE64 ( random_test,
70
+	 DATA ( 0x36, 0x03, 0x84, 0xdc, 0x4e, 0x03, 0x46, 0xa0, 0xb5, 0x2d,
71
+		0x03, 0x6e, 0xd0, 0x56, 0xed, 0xa0, 0x37, 0x02, 0xac, 0xc6,
72
+		0x65, 0xd1 ),
73
+	 "NgOE3E4DRqC1LQNu0FbtoDcCrMZl0Q==" );
74
+
75
+/**
76
+ * Report a base64 encoding test result
77
+ *
78
+ * @v test		Base64 test
79
+ */
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 );			\
86
+	} while ( 0 )
87
+
88
+/**
89
+ * Report a base64 decoding test result
90
+ *
91
+ * @v test		Base64 test
92
+ */
93
+#define base64_decode_ok( test ) do {					\
94
+	size_t max_len = base64_decoded_max_len ( (test)->encoded );	\
95
+	uint8_t buf[max_len];						\
96
+	int len;							\
97
+	len = base64_decode ( (test)->encoded, buf );			\
98
+	ok ( len >= 0 );						\
99
+	ok ( ( size_t ) len <= max_len );				\
100
+	ok ( ( size_t ) len == (test)->len );				\
101
+	ok ( memcmp ( (test)->data, buf, len ) == 0 );			\
102
+	} while ( 0 )
103
+
104
+/**
105
+ * Perform Base64 self-tests
106
+ *
107
+ */
108
+static void base64_test_exec ( void ) {
109
+
110
+	base64_encode_ok ( &empty_test );
111
+	base64_decode_ok ( &empty_test );
112
+
113
+	base64_encode_ok ( &hw_test );
114
+	base64_decode_ok ( &hw_test );
115
+
116
+	base64_encode_ok ( &random_test );
117
+	base64_decode_ok ( &random_test );
118
+}
119
+
120
+/** Base64 self-test */
121
+struct self_test base64_test __self_test = {
122
+	.name = "base64",
123
+	.exec = base64_test_exec,
124
+};

+ 1
- 0
src/tests/tests.c Ver fichero

@@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
27 27
 /* Drag in all applicable self-tests */
28 28
 REQUIRE_OBJECT ( list_test );
29 29
 REQUIRE_OBJECT ( byteswap_test );
30
+REQUIRE_OBJECT ( base64_test );
30 31
 REQUIRE_OBJECT ( settings_test );
31 32
 REQUIRE_OBJECT ( time_test );
32 33
 REQUIRE_OBJECT ( crc32_test );

Loading…
Cancelar
Guardar