You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crc32_test.c 2.8KB

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