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.

85 lines
2.6KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include "libavutil/common.h"
  21. #include "libavutil/base64.h"
  22. #define MAX_DATA_SIZE 1024
  23. #define MAX_ENCODED_SIZE 2048
  24. static int test_encode_decode(const uint8_t *data, unsigned int data_size,
  25. const char *encoded_ref)
  26. {
  27. char encoded[MAX_ENCODED_SIZE];
  28. uint8_t data2[MAX_DATA_SIZE];
  29. int data2_size, max_data2_size = MAX_DATA_SIZE;
  30. if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
  31. printf("Failed: cannot encode the input data\n");
  32. return 1;
  33. }
  34. if (encoded_ref && strcmp(encoded, encoded_ref)) {
  35. printf("Failed: encoded string differs from reference\n"
  36. "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref);
  37. return 1;
  38. }
  39. if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) {
  40. printf("Failed: cannot decode the encoded string\n"
  41. "Encoded:\n%s\n", encoded);
  42. return 1;
  43. }
  44. if (memcmp(data2, data, data_size)) {
  45. printf("Failed: encoded/decoded data differs from original data\n");
  46. return 1;
  47. }
  48. printf("Passed!\n");
  49. return 0;
  50. }
  51. int main(void)
  52. {
  53. int i, error_count = 0;
  54. struct test {
  55. const uint8_t *data;
  56. const char *encoded_ref;
  57. } tests[] = {
  58. { "", ""},
  59. { "1", "MQ=="},
  60. { "22", "MjI="},
  61. { "333", "MzMz"},
  62. { "4444", "NDQ0NA=="},
  63. { "55555", "NTU1NTU="},
  64. { "666666", "NjY2NjY2"},
  65. { "abc:def", "YWJjOmRlZg=="},
  66. };
  67. printf("Encoding/decoding tests\n");
  68. for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
  69. error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref);
  70. if (error_count)
  71. printf("Error Count: %d.\n", error_count);
  72. return !!error_count;
  73. }