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.4KB

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