test-base-64.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <cassert>
  2. #include <string.h>
  3. #include "Base.h"
  4. using namespace i2p::data;
  5. int main() {
  6. const char *in = "test";
  7. size_t in_len = strlen(in);
  8. char out[16];
  9. /* bytes -> b64 */
  10. assert(ByteStreamToBase64(NULL, 0, NULL, 0) == 0);
  11. assert(ByteStreamToBase64(NULL, 0, out, sizeof(out)) == 0);
  12. assert(Base64EncodingBufferSize(2) == 4);
  13. assert(Base64EncodingBufferSize(4) == 8);
  14. assert(Base64EncodingBufferSize(6) == 8);
  15. assert(Base64EncodingBufferSize(7) == 12);
  16. assert(Base64EncodingBufferSize(9) == 12);
  17. assert(Base64EncodingBufferSize(10) == 16);
  18. assert(Base64EncodingBufferSize(12) == 16);
  19. assert(Base64EncodingBufferSize(13) == 20);
  20. assert(ByteStreamToBase64((uint8_t *) in, in_len, out, sizeof(out)) == 8);
  21. assert(memcmp(out, "dGVzdA==", 8) == 0);
  22. /* b64 -> bytes */
  23. assert(Base64ToByteStream(NULL, 0, NULL, 0) == 0);
  24. assert(Base64ToByteStream(NULL, 0, (uint8_t *) out, sizeof(out)) == 0);
  25. in = "dGVzdA=="; /* valid b64 */
  26. assert(Base64ToByteStream(in, strlen(in), (uint8_t *) out, sizeof(out)) == 4);
  27. assert(memcmp(out, "test", 4) == 0);
  28. in = "dGVzdA="; /* invalid b64 : not padded */
  29. assert(Base64ToByteStream(in, strlen(in), (uint8_t *) out, sizeof(out)) == 0);
  30. in = "dG/z.A=="; /* invalid b64 : char not from alphabet */
  31. // assert(Base64ToByteStream(in, strlen(in), (uint8_t *) out, sizeof(out)) == 0);
  32. // ^^^ fails, current implementation not checks acceptable symbols
  33. return 0;
  34. }