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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * @v file Test code file
  73. * @v line Test code line
  74. */
  75. static void base64_encode_okx ( struct base64_test *test, const char *file,
  76. unsigned int line ) {
  77. size_t len = base64_encoded_len ( test->len );
  78. char buf[ len + 1 /* NUL */ ];
  79. size_t check_len;
  80. okx ( len == strlen ( test->encoded ), file, line );
  81. check_len = base64_encode ( test->data, test->len, buf, sizeof ( buf ));
  82. okx ( check_len == len, file, line );
  83. okx ( strcmp ( test->encoded, buf ) == 0, file, line );
  84. }
  85. #define base64_encode_ok( test ) base64_encode_okx ( test, __FILE__, __LINE__ )
  86. /**
  87. * Report a base64 decoding test result
  88. *
  89. * @v test Base64 test
  90. * @v file Test code file
  91. * @v line Test code line
  92. */
  93. static void base64_decode_okx ( struct base64_test *test, const char *file,
  94. unsigned int line ) {
  95. size_t max_len = base64_decoded_max_len ( test->encoded );
  96. uint8_t buf[max_len];
  97. int len;
  98. len = base64_decode ( test->encoded, buf, sizeof ( buf ) );
  99. okx ( len >= 0, file, line );
  100. okx ( ( size_t ) len <= max_len, file, line );
  101. okx ( ( size_t ) len == test->len, file, line );
  102. okx ( memcmp ( test->data, buf, len ) == 0, file, line );
  103. }
  104. #define base64_decode_ok( test ) base64_decode_okx ( test, __FILE__, __LINE__ )
  105. /**
  106. * Perform Base64 self-tests
  107. *
  108. */
  109. static void base64_test_exec ( void ) {
  110. base64_encode_ok ( &empty_test );
  111. base64_decode_ok ( &empty_test );
  112. base64_encode_ok ( &hw_test );
  113. base64_decode_ok ( &hw_test );
  114. base64_encode_ok ( &random_test );
  115. base64_decode_ok ( &random_test );
  116. }
  117. /** Base64 self-test */
  118. struct self_test base64_test __self_test = {
  119. .name = "base64",
  120. .exec = base64_test_exec,
  121. };