1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. assert = require 'assert'
  2. {btoa, atob} = require '..'
  3. describe 'Base64.js', ->
  4. it 'can encode ASCII input', ->
  5. assert.strictEqual btoa(''), ''
  6. assert.strictEqual btoa('f'), 'Zg=='
  7. assert.strictEqual btoa('fo'), 'Zm8='
  8. assert.strictEqual btoa('foo'), 'Zm9v'
  9. assert.strictEqual btoa('quux'), 'cXV1eA=='
  10. assert.strictEqual btoa('!"#$%'), 'ISIjJCU='
  11. assert.strictEqual btoa("&'()*+"), 'JicoKSor'
  12. assert.strictEqual btoa(',-./012'), 'LC0uLzAxMg=='
  13. assert.strictEqual btoa('3456789:'), 'MzQ1Njc4OTo='
  14. assert.strictEqual btoa(';<=>?@ABC'), 'Ozw9Pj9AQUJD'
  15. assert.strictEqual btoa('DEFGHIJKLM'), 'REVGR0hJSktMTQ=='
  16. assert.strictEqual btoa('NOPQRSTUVWX'), 'Tk9QUVJTVFVWV1g='
  17. assert.strictEqual btoa('YZ[\\]^_`abc'), 'WVpbXF1eX2BhYmM='
  18. assert.strictEqual btoa('defghijklmnop'), 'ZGVmZ2hpamtsbW5vcA=='
  19. assert.strictEqual btoa('qrstuvwxyz{|}~'), 'cXJzdHV2d3h5ent8fX4='
  20. it 'cannot encode non-ASCII input', ->
  21. assert.throws (-> btoa '✈'), (err) ->
  22. err instanceof Error and
  23. err.name is 'InvalidCharacterError' and
  24. err.message is "'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
  25. it 'coerces input', ->
  26. assert.strictEqual btoa(42), btoa('42')
  27. assert.strictEqual btoa(null), btoa('null')
  28. assert.strictEqual btoa({x: 1}), btoa('[object Object]')
  29. it 'can decode Base64-encoded input', ->
  30. assert.strictEqual atob(''), ''
  31. assert.strictEqual atob('Zg=='), 'f'
  32. assert.strictEqual atob('Zm8='), 'fo'
  33. assert.strictEqual atob('Zm9v'), 'foo'
  34. assert.strictEqual atob('cXV1eA=='), 'quux'
  35. assert.strictEqual atob('ISIjJCU='), '!"#$%'
  36. assert.strictEqual atob('JicoKSor'), "&'()*+"
  37. assert.strictEqual atob('LC0uLzAxMg=='), ',-./012'
  38. assert.strictEqual atob('MzQ1Njc4OTo='), '3456789:'
  39. assert.strictEqual atob('Ozw9Pj9AQUJD'), ';<=>?@ABC'
  40. assert.strictEqual atob('REVGR0hJSktMTQ=='), 'DEFGHIJKLM'
  41. assert.strictEqual atob('Tk9QUVJTVFVWV1g='), 'NOPQRSTUVWX'
  42. assert.strictEqual atob('WVpbXF1eX2BhYmM='), 'YZ[\\]^_`abc'
  43. assert.strictEqual atob('ZGVmZ2hpamtsbW5vcA=='), 'defghijklmnop'
  44. assert.strictEqual atob('cXJzdHV2d3h5ent8fX4='), 'qrstuvwxyz{|}~'
  45. it 'cannot decode invalid input', ->
  46. assert.throws (-> atob 'a'), (err) ->
  47. err instanceof Error and
  48. err.name is 'InvalidCharacterError' and
  49. err.message is "'atob' failed: The string to be decoded is not correctly encoded."
  50. it 'coerces input', ->
  51. assert.strictEqual atob(42), atob('42')
  52. assert.strictEqual atob(null), atob('null')