Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

base16_test.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * Base16 tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <ipxe/base16.h>
  34. #include <ipxe/test.h>
  35. /** A Base16 test */
  36. struct base16_test {
  37. /** Raw data */
  38. const void *data;
  39. /** Length of raw data */
  40. size_t len;
  41. /** Base16-encoded data */
  42. const char *encoded;
  43. };
  44. /** Define inline data */
  45. #define DATA(...) { __VA_ARGS__ }
  46. /** Define a base16 test */
  47. #define BASE16( name, DATA, ENCODED ) \
  48. static const uint8_t name ## _data[] = DATA; \
  49. static struct base16_test name = { \
  50. .data = name ## _data, \
  51. .len = sizeof ( name ## _data ), \
  52. .encoded = ENCODED, \
  53. }
  54. /** Empty data test */
  55. BASE16 ( empty_test, DATA(), "" );
  56. /** "Hello world" test */
  57. BASE16 ( hw_test,
  58. DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
  59. "48656c6c6f20776f726c64" );
  60. /** Random data test */
  61. BASE16 ( random_test,
  62. DATA ( 0x8b, 0x1a, 0xa2, 0x6c, 0xa9, 0x38, 0x43, 0xb8, 0x81, 0xf8,
  63. 0x30, 0x44, 0xb2, 0x32, 0x6e, 0x82, 0xfe, 0x0f, 0x84, 0x91 ),
  64. "8b1aa26ca93843b881f83044b2326e82fe0f8491" );
  65. /**
  66. * Report a base16 encoding test result
  67. *
  68. * @v test Base16 test
  69. */
  70. #define base16_encode_ok( test ) do { \
  71. size_t len = base16_encoded_len ( (test)->len ); \
  72. char buf[ len + 1 /* NUL */ ]; \
  73. ok ( len == strlen ( (test)->encoded ) ); \
  74. base16_encode ( (test)->data, (test)->len, buf ); \
  75. ok ( strcmp ( (test)->encoded, buf ) == 0 ); \
  76. } while ( 0 )
  77. /**
  78. * Report a base16 decoding test result
  79. *
  80. * @v test Base16 test
  81. */
  82. #define base16_decode_ok( test ) do { \
  83. size_t max_len = base16_decoded_max_len ( (test)->encoded ); \
  84. uint8_t buf[max_len]; \
  85. int len; \
  86. len = base16_decode ( (test)->encoded, buf ); \
  87. ok ( len >= 0 ); \
  88. ok ( ( size_t ) len <= max_len ); \
  89. ok ( ( size_t ) len == (test)->len ); \
  90. ok ( memcmp ( (test)->data, buf, len ) == 0 ); \
  91. } while ( 0 )
  92. /**
  93. * Perform Base16 self-tests
  94. *
  95. */
  96. static void base16_test_exec ( void ) {
  97. base16_encode_ok ( &empty_test );
  98. base16_decode_ok ( &empty_test );
  99. base16_encode_ok ( &hw_test );
  100. base16_decode_ok ( &hw_test );
  101. base16_encode_ok ( &random_test );
  102. base16_decode_ok ( &random_test );
  103. }
  104. /** Base16 self-test */
  105. struct self_test base16_test __self_test = {
  106. .name = "base16",
  107. .exec = base16_test_exec,
  108. };