Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * @v file Test code file
  70. * @v line Test code line
  71. */
  72. static void base16_encode_okx ( struct base16_test *test, const char *file,
  73. unsigned int line ) {
  74. size_t len = base16_encoded_len ( test->len );
  75. char buf[ len + 1 /* NUL */ ];
  76. size_t check_len;
  77. okx ( len == strlen ( test->encoded ), file, line );
  78. check_len = base16_encode ( test->data, test->len, buf, sizeof ( buf ));
  79. okx ( check_len == len, file, line );
  80. okx ( strcmp ( test->encoded, buf ) == 0, file, line );
  81. }
  82. #define base16_encode_ok( test ) base16_encode_okx ( test, __FILE__, __LINE__ )
  83. /**
  84. * Report a base16 decoding test result
  85. *
  86. * @v test Base16 test
  87. * @v file Test code file
  88. * @v line Test code line
  89. */
  90. static void base16_decode_okx ( struct base16_test *test, const char *file,
  91. unsigned int line ) {
  92. size_t max_len = base16_decoded_max_len ( test->encoded );
  93. uint8_t buf[max_len];
  94. int len;
  95. len = base16_decode ( test->encoded, buf, sizeof ( buf ) );
  96. okx ( len >= 0, file, line );
  97. okx ( ( size_t ) len <= max_len, file, line );
  98. okx ( ( size_t ) len == test->len, file, line );
  99. okx ( memcmp ( test->data, buf, len ) == 0, file, line );
  100. }
  101. #define base16_decode_ok( test ) base16_decode_okx ( test, __FILE__, __LINE__ )
  102. /**
  103. * Perform Base16 self-tests
  104. *
  105. */
  106. static void base16_test_exec ( void ) {
  107. base16_encode_ok ( &empty_test );
  108. base16_decode_ok ( &empty_test );
  109. base16_encode_ok ( &hw_test );
  110. base16_decode_ok ( &hw_test );
  111. base16_encode_ok ( &random_test );
  112. base16_decode_ok ( &random_test );
  113. }
  114. /** Base16 self-test */
  115. struct self_test base16_test __self_test = {
  116. .name = "base16",
  117. .exec = base16_test_exec,
  118. };