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 3.0KB

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