tbase64.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. discard """
  2. targets: "c js"
  3. """
  4. import std/assertions
  5. import std/base64
  6. template main() =
  7. doAssert encode("a") == "YQ=="
  8. doAssert encode("Hello World") == "SGVsbG8gV29ybGQ="
  9. doAssert encode("leasure.") == "bGVhc3VyZS4="
  10. doAssert encode("easure.") == "ZWFzdXJlLg=="
  11. doAssert encode("asure.") == "YXN1cmUu"
  12. doAssert encode("sure.") == "c3VyZS4="
  13. doAssert encode([1,2,3]) == "AQID"
  14. doAssert encode(['h','e','y']) == "aGV5"
  15. doAssert encode("") == ""
  16. doAssert decode("") == ""
  17. const testInputExpandsTo76 = "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  18. const testInputExpands = "++++++++++++++++++++++++++++++"
  19. const longText = """Man is distinguished, not only by his reason, but by this
  20. singular passion from other animals, which is a lust of the mind,
  21. that by a perseverance of delight in the continued and indefatigable
  22. generation of knowledge, exceeds the short vehemence of any carnal
  23. pleasure."""
  24. const tests = ["", "abc", "xyz", "man", "leasure.", "sure.", "easure.",
  25. "asure.", longText, testInputExpandsTo76, testInputExpands]
  26. doAssert encodeMime("foobarbaz", lineLen=4) == "Zm9v\r\nYmFy\r\nYmF6"
  27. doAssert decode("Zm9v\r\nYmFy\r\nYmF6") == "foobarbaz"
  28. for t in items(tests):
  29. doAssert decode(encode(t)) == t
  30. doAssert decode(encodeMime(t, lineLen=40)) == t
  31. doAssert decode(encodeMime(t, lineLen=76)) == t
  32. doAssertRaises(ValueError): discard decode("SGVsbG\x008gV29ybGQ=")
  33. block base64urlSafe:
  34. doAssert encode("c\xf7>", safe = true) == "Y_c-"
  35. doAssert encode("c\xf7>", safe = false) == "Y/c+" # Not a nice URL :(
  36. doAssert decode("Y/c+") == decode("Y_c-")
  37. # Output must not change with safe=true
  38. doAssert encode("Hello World", safe = true) == "SGVsbG8gV29ybGQ="
  39. doAssert encode("leasure.", safe = true) == "bGVhc3VyZS4="
  40. doAssert encode("easure.", safe = true) == "ZWFzdXJlLg=="
  41. doAssert encode("asure.", safe = true) == "YXN1cmUu"
  42. doAssert encode("sure.", safe = true) == "c3VyZS4="
  43. doAssert encode([1,2,3], safe = true) == "AQID"
  44. doAssert encode(['h','e','y'], safe = true) == "aGV5"
  45. doAssert encode("", safe = true) == ""
  46. doAssert encode("the quick brown dog jumps over the lazy fox", safe = true) == "dGhlIHF1aWNrIGJyb3duIGRvZyBqdW1wcyBvdmVyIHRoZSBsYXp5IGZveA=="
  47. static: main()
  48. main()