base64test.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/base64.h>
  13. #include <gmssl/error.h>
  14. static int test_base64(void)
  15. {
  16. uint8_t bin1[50];
  17. uint8_t bin2[100];
  18. uint8_t bin3[200];
  19. uint8_t buf1[8000] = {0};
  20. uint8_t buf2[8000] = {0};
  21. BASE64_CTX ctx;
  22. uint8_t *p;
  23. int len;
  24. memset(bin1, 0x01, sizeof(bin1));
  25. memset(bin2, 0xA5, sizeof(bin2));
  26. memset(bin3, 0xff, sizeof(bin3));
  27. p = buf1;
  28. base64_encode_init(&ctx);
  29. base64_encode_update(&ctx, bin1, sizeof(bin1), p, &len); p += len;
  30. base64_encode_update(&ctx, bin2, sizeof(bin2), p, &len); p += len;
  31. base64_encode_update(&ctx, bin3, sizeof(bin3), p, &len); p += len;
  32. base64_encode_finish(&ctx, p, &len); p += len;
  33. len = (int)(p - buf1);
  34. p = buf2;
  35. base64_decode_init(&ctx);
  36. base64_decode_update(&ctx, buf1, len, p, &len); p += len;
  37. base64_decode_finish(&ctx, p, &len); p += len;
  38. len = (int)(p - buf2);
  39. printf("base64 test ");
  40. if (len != sizeof(bin1) + sizeof(bin2) + sizeof(bin3)
  41. || memcmp(buf2, bin1, sizeof(bin1)) != 0
  42. || memcmp(buf2 + sizeof(bin1), bin2, sizeof(bin2)) != 0
  43. || memcmp(buf2 + sizeof(bin1) + sizeof(bin2), bin3, sizeof(bin3)) != 0) {
  44. printf("failed\n");
  45. return -1;
  46. } else {
  47. printf("ok\n");
  48. }
  49. return 1;
  50. }
  51. int main(void)
  52. {
  53. if (test_base64() != 1) goto err;
  54. printf("%s all tests passed\n", __FILE__);
  55. return 0;
  56. err:
  57. error_print();
  58. return -1;
  59. }