|
@@ -0,0 +1,116 @@
|
|
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
|
+ * CRC32 tests
|
|
25
|
+ *
|
|
26
|
+ *
|
|
27
|
+ * Test vectors generated using Perl's Digest::CRC:
|
|
28
|
+ *
|
|
29
|
+ * use Digest::CRC qw ( crc );
|
|
30
|
+ *
|
|
31
|
+ * printf "%#08x", crc ( $data, 32, $seed, 0, 1, 0x04c11db7, 1 );
|
|
32
|
+ *
|
|
33
|
+ */
|
|
34
|
+
|
|
35
|
+/* Forcibly enable assertions */
|
|
36
|
+#undef NDEBUG
|
|
37
|
+
|
|
38
|
+#include <stdint.h>
|
|
39
|
+#include <ipxe/crc32.h>
|
|
40
|
+#include <ipxe/test.h>
|
|
41
|
+
|
|
42
|
+/** Define inline data */
|
|
43
|
+#define DATA(...) { __VA_ARGS__ }
|
|
44
|
+
|
|
45
|
+/** A CRC32 test */
|
|
46
|
+struct crc32_test {
|
|
47
|
+ /** Test data */
|
|
48
|
+ const void *data;
|
|
49
|
+ /** Length of test data */
|
|
50
|
+ size_t len;
|
|
51
|
+ /** Seed */
|
|
52
|
+ uint32_t seed;
|
|
53
|
+ /** Expected CRC32 */
|
|
54
|
+ uint32_t crc32;
|
|
55
|
+};
|
|
56
|
+
|
|
57
|
+/**
|
|
58
|
+ * Define a CRC32 test
|
|
59
|
+ *
|
|
60
|
+ * @v name Test name
|
|
61
|
+ * @v DATA Test data
|
|
62
|
+ * @v SEED Seed
|
|
63
|
+ * @v CRC32 Expected CRC32
|
|
64
|
+ * @ret test CRC32 test
|
|
65
|
+ */
|
|
66
|
+#define CRC32_TEST( name, DATA, SEED, CRC32 ) \
|
|
67
|
+ static const uint8_t name ## _data[] = DATA; \
|
|
68
|
+ static struct crc32_test name = { \
|
|
69
|
+ .data = name ## _data, \
|
|
70
|
+ .len = sizeof ( name ## _data ), \
|
|
71
|
+ .seed = SEED, \
|
|
72
|
+ .crc32 = CRC32, \
|
|
73
|
+ };
|
|
74
|
+
|
|
75
|
+/**
|
|
76
|
+ * Report a CRC32 test result
|
|
77
|
+ *
|
|
78
|
+ * @v test CRC32 test
|
|
79
|
+ */
|
|
80
|
+#define crc32_ok( test ) do { \
|
|
81
|
+ uint32_t crc32; \
|
|
82
|
+ crc32 = crc32_le ( (test)->seed, (test)->data, (test)->len ); \
|
|
83
|
+ ok ( crc32 == (test)->crc32 ); \
|
|
84
|
+ } while ( 0 )
|
|
85
|
+
|
|
86
|
+/* CRC32 tests */
|
|
87
|
+CRC32_TEST ( empty_test,
|
|
88
|
+ DATA ( ),
|
|
89
|
+ 0x12345678UL, 0x12345678UL );
|
|
90
|
+CRC32_TEST ( hw_test,
|
|
91
|
+ DATA ( 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
|
|
92
|
+ 0xffffffffUL, 0xf2b5ee7aUL );
|
|
93
|
+CRC32_TEST ( hw_split_part1_test,
|
|
94
|
+ DATA ( 'h', 'e', 'l', 'l', 'o' ),
|
|
95
|
+ 0xffffffffUL, 0xc9ef5979UL );
|
|
96
|
+CRC32_TEST ( hw_split_part2_test,
|
|
97
|
+ DATA ( ' ', 'w', 'o', 'r', 'l', 'd' ),
|
|
98
|
+ 0xc9ef5979UL, 0xf2b5ee7aUL );
|
|
99
|
+
|
|
100
|
+/**
|
|
101
|
+ * Perform CRC32 self-tests
|
|
102
|
+ *
|
|
103
|
+ */
|
|
104
|
+static void crc32_test_exec ( void ) {
|
|
105
|
+
|
|
106
|
+ crc32_ok ( &empty_test );
|
|
107
|
+ crc32_ok ( &hw_test );
|
|
108
|
+ crc32_ok ( &hw_split_part1_test );
|
|
109
|
+ crc32_ok ( &hw_split_part2_test );
|
|
110
|
+}
|
|
111
|
+
|
|
112
|
+/** CRC32 self-test */
|
|
113
|
+struct self_test crc32_test __self_test = {
|
|
114
|
+ .name = "crc32",
|
|
115
|
+ .exec = crc32_test_exec,
|
|
116
|
+};
|