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.

base64_test.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * Base64 tests
  23. *
  24. * Test vectors generated using "base64 -w 0"
  25. *
  26. */
  27. /* Forcibly enable assertions */
  28. #undef NDEBUG
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <ipxe/base64.h>
  32. #include <ipxe/test.h>
  33. /** A Base64 test */
  34. struct base64_test {
  35. /** Raw data */
  36. const void *data;
  37. /** Length of raw data */
  38. size_t len;
  39. /** Base64-encoded data */
  40. const char *encoded;
  41. };
  42. /** Define inline data */
  43. #define DATA(...) { __VA_ARGS__ }
  44. /** Define a base64 test */
  45. #define BASE64( name, DATA, ENCODED ) \
  46. static const uint8_t name ## _data[] = DATA; \
  47. static struct base64_test name = { \
  48. .data = name ## _data, \
  49. .len = sizeof ( name ## _data ), \
  50. .encoded = ENCODED, \
  51. }
  52. /** Empty data test */
  53. BASE64 ( empty_test, DATA(), "" );
  54. /** "Hello world" test */
  55. BASE64 ( hw_test,
  56. DATA ( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
  57. "SGVsbG8gd29ybGQ=" );
  58. /** Random data test */
  59. BASE64 ( random_test,
  60. DATA ( 0x36, 0x03, 0x84, 0xdc, 0x4e, 0x03, 0x46, 0xa0, 0xb5, 0x2d,
  61. 0x03, 0x6e, 0xd0, 0x56, 0xed, 0xa0, 0x37, 0x02, 0xac, 0xc6,
  62. 0x65, 0xd1 ),
  63. "NgOE3E4DRqC1LQNu0FbtoDcCrMZl0Q==" );
  64. /**
  65. * Report a base64 encoding test result
  66. *
  67. * @v test Base64 test
  68. */
  69. #define base64_encode_ok( test ) do { \
  70. size_t len = base64_encoded_len ( (test)->len ); \
  71. char buf[ len + 1 /* NUL */ ]; \
  72. ok ( len == strlen ( (test)->encoded ) ); \
  73. base64_encode ( (test)->data, (test)->len, buf ); \
  74. ok ( strcmp ( (test)->encoded, buf ) == 0 ); \
  75. } while ( 0 )
  76. /**
  77. * Report a base64 decoding test result
  78. *
  79. * @v test Base64 test
  80. */
  81. #define base64_decode_ok( test ) do { \
  82. size_t max_len = base64_decoded_max_len ( (test)->encoded ); \
  83. uint8_t buf[max_len]; \
  84. int len; \
  85. len = base64_decode ( (test)->encoded, buf ); \
  86. ok ( len >= 0 ); \
  87. ok ( ( size_t ) len <= max_len ); \
  88. ok ( ( size_t ) len == (test)->len ); \
  89. ok ( memcmp ( (test)->data, buf, len ) == 0 ); \
  90. } while ( 0 )
  91. /**
  92. * Perform Base64 self-tests
  93. *
  94. */
  95. static void base64_test_exec ( void ) {
  96. base64_encode_ok ( &empty_test );
  97. base64_decode_ok ( &empty_test );
  98. base64_encode_ok ( &hw_test );
  99. base64_decode_ok ( &hw_test );
  100. base64_encode_ok ( &random_test );
  101. base64_decode_ok ( &random_test );
  102. }
  103. /** Base64 self-test */
  104. struct self_test base64_test __self_test = {
  105. .name = "base64",
  106. .exec = base64_test_exec,
  107. };