iPXE
crc32_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * CRC32 tests
00029  *
00030  *
00031  * Test vectors generated using Perl's Digest::CRC:
00032  *
00033  *    use Digest::CRC qw ( crc );
00034  *
00035  *    printf "%#08x", crc ( $data, 32, $seed, 0, 1, 0x04c11db7, 1 );
00036  *
00037  */
00038 
00039 /* Forcibly enable assertions */
00040 #undef NDEBUG
00041 
00042 #include <stdint.h>
00043 #include <ipxe/crc32.h>
00044 #include <ipxe/test.h>
00045 
00046 /** Define inline data */
00047 #define DATA(...) { __VA_ARGS__ }
00048 
00049 /** A CRC32 test */
00050 struct crc32_test {
00051         /** Test data */
00052         const void *data;
00053         /** Length of test data */
00054         size_t len;
00055         /** Seed */
00056         uint32_t seed;
00057         /** Expected CRC32 */
00058         uint32_t crc32;
00059 };
00060 
00061 /**
00062  * Define a CRC32 test
00063  *
00064  * @v name              Test name
00065  * @v DATA              Test data
00066  * @v SEED              Seed
00067  * @v CRC32             Expected CRC32
00068  * @ret test            CRC32 test
00069  */
00070 #define CRC32_TEST( name, DATA, SEED, CRC32 )                           \
00071         static const uint8_t name ## _data[] = DATA;                    \
00072         static struct crc32_test name = {                               \
00073                 .data = name ## _data,                                  \
00074                 .len = sizeof ( name ## _data ),                        \
00075                 .seed = SEED,                                           \
00076                 .crc32 = CRC32,                                         \
00077         };
00078 
00079 /**
00080  * Report a CRC32 test result
00081  *
00082  * @v test              CRC32 test
00083  */
00084 #define crc32_ok( test ) do {                                           \
00085         uint32_t crc32;                                                 \
00086         crc32 = crc32_le ( (test)->seed, (test)->data, (test)->len );   \
00087         ok ( crc32 == (test)->crc32 );                                  \
00088         } while ( 0 )
00089 
00090 /* CRC32 tests */
00091 CRC32_TEST ( empty_test,
00092              DATA ( ),
00093              0x12345678UL, 0x12345678UL );
00094 CRC32_TEST ( hw_test,
00095              DATA ( 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
00096              0xffffffffUL, 0xf2b5ee7aUL );
00097 CRC32_TEST ( hw_split_part1_test,
00098              DATA ( 'h', 'e', 'l', 'l', 'o' ),
00099              0xffffffffUL, 0xc9ef5979UL );
00100 CRC32_TEST ( hw_split_part2_test,
00101              DATA ( ' ', 'w', 'o', 'r', 'l', 'd' ),
00102              0xc9ef5979UL, 0xf2b5ee7aUL );
00103 
00104 /**
00105  * Perform CRC32 self-tests
00106  *
00107  */
00108 static void crc32_test_exec ( void ) {
00109 
00110         crc32_ok ( &empty_test );
00111         crc32_ok ( &hw_test );
00112         crc32_ok ( &hw_split_part1_test );
00113         crc32_ok ( &hw_split_part2_test );
00114 }
00115 
00116 /** CRC32 self-test */
00117 struct self_test crc32_test __self_test = {
00118         .name = "crc32",
00119         .exec = crc32_test_exec,
00120 };