tbase64.nim 2.4 KB

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