Преглед на файлове

[test] Add self-tests for base16

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown преди 11 години
родител
ревизия
362a628e52
променени са 2 файла, в които са добавени 122 реда и са изтрити 0 реда
  1. 121
    0
      src/tests/base16_test.c
  2. 1
    0
      src/tests/tests.c

+ 121
- 0
src/tests/base16_test.c Целия файл

@@ -0,0 +1,121 @@
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
+ * Base16 tests
25
+ *
26
+ */
27
+
28
+/* Forcibly enable assertions */
29
+#undef NDEBUG
30
+
31
+#include <stdint.h>
32
+#include <string.h>
33
+#include <ipxe/base16.h>
34
+#include <ipxe/test.h>
35
+
36
+/** A Base16 test */
37
+struct base16_test {
38
+	/** Raw data */
39
+	const void *data;
40
+	/** Length of raw data */
41
+	size_t len;
42
+	/** Base16-encoded data */
43
+	const char *encoded;
44
+};
45
+
46
+/** Define inline data */
47
+#define DATA(...) { __VA_ARGS__ }
48
+
49
+/** Define a base16 test */
50
+#define BASE16( name, DATA, ENCODED )					\
51
+	static const uint8_t name ## _data[] = DATA;			\
52
+	static struct base16_test name = {				\
53
+		.data = name ## _data,					\
54
+		.len = sizeof ( name ## _data ),			\
55
+		.encoded = ENCODED,					\
56
+	}
57
+
58
+/** Empty data test */
59
+BASE16 ( empty_test, DATA(), "" );
60
+
61
+/** "Hello world" test */
62
+BASE16 ( hw_test,
63
+	 DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
64
+	 "48656c6c6f20776f726c64" );
65
+
66
+/** Random data test */
67
+BASE16 ( random_test,
68
+	 DATA ( 0x8b, 0x1a, 0xa2, 0x6c, 0xa9, 0x38, 0x43, 0xb8, 0x81, 0xf8,
69
+		0x30, 0x44, 0xb2, 0x32, 0x6e, 0x82, 0xfe, 0x0f, 0x84, 0x91 ),
70
+	 "8b1aa26ca93843b881f83044b2326e82fe0f8491" );
71
+
72
+/**
73
+ * Report a base16 encoding test result
74
+ *
75
+ * @v test		Base16 test
76
+ */
77
+#define base16_encode_ok( test ) do {					\
78
+	size_t len = base16_encoded_len ( (test)->len );		\
79
+	char buf[ len + 1 /* NUL */ ];					\
80
+	ok ( len == strlen ( (test)->encoded ) );			\
81
+	base16_encode ( (test)->data, (test)->len, buf );		\
82
+	ok ( strcmp ( (test)->encoded, buf ) == 0 );			\
83
+	} while ( 0 )
84
+
85
+/**
86
+ * Report a base16 decoding test result
87
+ *
88
+ * @v test		Base16 test
89
+ */
90
+#define base16_decode_ok( test ) do {					\
91
+	size_t max_len = base16_decoded_max_len ( (test)->encoded );	\
92
+	uint8_t buf[max_len];						\
93
+	int len;							\
94
+	len = base16_decode ( (test)->encoded, buf );			\
95
+	ok ( len >= 0 );						\
96
+	ok ( ( size_t ) len <= max_len );				\
97
+	ok ( ( size_t ) len == (test)->len );				\
98
+	ok ( memcmp ( (test)->data, buf, len ) == 0 );			\
99
+	} while ( 0 )
100
+
101
+/**
102
+ * Perform Base16 self-tests
103
+ *
104
+ */
105
+static void base16_test_exec ( void ) {
106
+
107
+	base16_encode_ok ( &empty_test );
108
+	base16_decode_ok ( &empty_test );
109
+
110
+	base16_encode_ok ( &hw_test );
111
+	base16_decode_ok ( &hw_test );
112
+
113
+	base16_encode_ok ( &random_test );
114
+	base16_decode_ok ( &random_test );
115
+}
116
+
117
+/** Base16 self-test */
118
+struct self_test base16_test __self_test = {
119
+	.name = "base16",
120
+	.exec = base16_test_exec,
121
+};

+ 1
- 0
src/tests/tests.c Целия файл

@@ -31,6 +31,7 @@ REQUIRE_OBJECT ( string_test );
31 31
 REQUIRE_OBJECT ( list_test );
32 32
 REQUIRE_OBJECT ( byteswap_test );
33 33
 REQUIRE_OBJECT ( base64_test );
34
+REQUIRE_OBJECT ( base16_test );
34 35
 REQUIRE_OBJECT ( settings_test );
35 36
 REQUIRE_OBJECT ( time_test );
36 37
 REQUIRE_OBJECT ( tcpip_test );

Loading…
Отказ
Запис