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.

base16_test.c 3.1KB

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