test-base64.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * QEMU base64 helper test
  3. *
  4. * Copyright (c) 2015 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qemu/base64.h"
  23. static void test_base64_good(void)
  24. {
  25. const char input[] =
  26. "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\n"
  27. "lzc2VkIHRoZSBzY29ycGlvbi4=";
  28. const char expect[] = "Because we focused on the snake, "
  29. "we missed the scorpion.";
  30. size_t len;
  31. uint8_t *actual = qbase64_decode(input,
  32. -1,
  33. &len,
  34. &error_abort);
  35. g_assert(actual != NULL);
  36. g_assert_cmpint(len, ==, strlen(expect));
  37. g_assert_cmpstr((char *)actual, ==, expect);
  38. g_free(actual);
  39. }
  40. static void test_base64_bad(const char *input,
  41. size_t input_len)
  42. {
  43. size_t len;
  44. Error *err = NULL;
  45. uint8_t *actual = qbase64_decode(input,
  46. input_len,
  47. &len,
  48. &err);
  49. g_assert(err != NULL);
  50. g_assert(actual == NULL);
  51. g_assert_cmpint(len, ==, 0);
  52. error_free(err);
  53. }
  54. static void test_base64_embedded_nul(void)
  55. {
  56. /* We put a NUL character in the middle of the base64
  57. * text which is invalid data, given the expected length */
  58. const char input[] =
  59. "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\0"
  60. "lzc2VkIHRoZSBzY29ycGlvbi4=";
  61. test_base64_bad(input, G_N_ELEMENTS(input) - 1);
  62. }
  63. static void test_base64_not_nul_terminated(void)
  64. {
  65. const char input[] =
  66. "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW\n"
  67. "lzc2VkIHRoZSBzY29ycGlvbi4=";
  68. /* Using '-2' to make us drop the trailing NUL, thus
  69. * creating an invalid base64 sequence for decoding */
  70. test_base64_bad(input, G_N_ELEMENTS(input) - 2);
  71. }
  72. static void test_base64_invalid_chars(void)
  73. {
  74. /* We put a single quote character in the middle
  75. * of the base64 text which is invalid data */
  76. const char input[] =
  77. "QmVjYXVzZSB3ZSBmb2N1c2VkIG9uIHRoZSBzbmFrZSwgd2UgbW'"
  78. "lzc2VkIHRoZSBzY29ycGlvbi4=";
  79. test_base64_bad(input, strlen(input));
  80. }
  81. int main(int argc, char **argv)
  82. {
  83. g_test_init(&argc, &argv, NULL);
  84. g_test_add_func("/util/base64/good", test_base64_good);
  85. g_test_add_func("/util/base64/embedded-nul", test_base64_embedded_nul);
  86. g_test_add_func("/util/base64/not-nul-terminated",
  87. test_base64_not_nul_terminated);
  88. g_test_add_func("/util/base64/invalid-chars", test_base64_invalid_chars);
  89. return g_test_run();
  90. }