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