json_encode64.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "json_encode64.h"
  2. #include "../../Source/JSON_Base64.h"
  3. /**
  4. * Make sure that these two function reverse each other
  5. */
  6. void testJSON_Base64__json_encode64::testReverseEachOther(void){
  7. #if defined(JSON_BINARY) || defined(JSON_EXPOSE_BASE64)
  8. #ifdef JSON_SAFE
  9. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"", 0)), "");
  10. #endif
  11. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"A", 1)), "A");
  12. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"AB", 2)), "AB");
  13. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABC", 3)), "ABC");
  14. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABCD", 4)), "ABCD");
  15. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABCDE", 5)), "ABCDE");
  16. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABCDEF", 6)), "ABCDEF");
  17. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABCDEFG", 7)), "ABCDEFG");
  18. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABCDEFGH", 8)), "ABCDEFGH");
  19. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABCDEFGHI", 9)), "ABCDEFGHI");
  20. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABCDEFGHIJ", 10)), "ABCDEFGHIJ");
  21. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABCDEFGHIJK", 11)), "ABCDEFGHIJK");
  22. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABCDEFGHIJKL", 12)), "ABCDEFGHIJKL");
  23. assertEquals(JSONBase64::json_decode64(JSONBase64::json_encode64((unsigned char *)"ABCDEFGHIJKLM", 13)), "ABCDEFGHIJKLM");
  24. #endif
  25. }
  26. /**
  27. * Make sure all characters work in the code
  28. */
  29. void testJSON_Base64__json_encode64::testAllChars(void){
  30. #if defined(JSON_BINARY) || defined(JSON_EXPOSE_BASE64)
  31. //create a binary chunk of data to use with every char
  32. unsigned char temp[255];
  33. for(unsigned int i = 0; i < 255; ++i){
  34. temp[i] = (unsigned char)i;
  35. }
  36. //loop through all of the lengths
  37. for(unsigned int length = 1; length < 255; ++length){
  38. json_string ts = JSONBase64::json_encode64(temp, length);
  39. std::string rs = JSONBase64::json_decode64(ts);
  40. assertEquals(rs.size(), length);
  41. assertEquals(memcmp(rs.data(), temp, length), 0);
  42. }
  43. #endif
  44. }