atob.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * use mocha to test me
  3. * http://visionmedia.github.com/mocha/
  4. */
  5. var assert = assert || require("assert");
  6. var Base64 = Base64 || require('../base64.js').Base64;
  7. var is = function (a, e, m) {
  8. return function () {
  9. assert.equal(a, e, m)
  10. }
  11. };
  12. describe('atob', function () {
  13. describe('basic', function () {
  14. it('d', is(Base64.btoa('d'), 'ZA=='));
  15. it('da', is(Base64.btoa('da'), 'ZGE='));
  16. it('dan', is(Base64.btoa('dan'), 'ZGFu'));
  17. it('ZA==', is(Base64.atob('ZA=='), 'd' ));
  18. it('ZGE=', is(Base64.atob('ZGE='), 'da' ));
  19. it('ZGFu', is(Base64.atob('ZGFu'), 'dan' ));
  20. });
  21. describe('whitespace', function () {
  22. it('Z A==', is(Base64.atob('ZA =='), 'd' ));
  23. it('ZG E=', is(Base64.atob('ZG E='), 'da' ));
  24. it('ZGF u', is(Base64.atob('ZGF u'), 'dan' ));
  25. });
  26. describe('null', function () {
  27. it('\\0', is(Base64.btoa('\0'), 'AA=='));
  28. it('\\0\\0', is(Base64.btoa('\0\0'), 'AAA='));
  29. it('\\0\\0\\0', is(Base64.btoa('\0\0\0'), 'AAAA'));
  30. it('AA==', is(Base64.atob('AA=='), '\0' ));
  31. it('AAA=', is(Base64.atob('AAA='), '\0\0' ));
  32. it('AAAA', is(Base64.atob('AAAA'), '\0\0\0'));
  33. });
  34. describe('binary', function () {
  35. var pngBase64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';
  36. var pngBinary = '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x01\x00\x00\x00\x01\x08\x04\x00\x00\x00\xb5\x1c\x0c\x02\x00\x00\x00\x0b\x49\x44\x41\x54\x78\xda\x63\x64\x60\x00\x00\x00\x06\x00\x02\x30\x81\xd0\x2f\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82';
  37. it('.btoa', is(Base64.btoa(pngBinary), pngBase64));
  38. it('.atob', is(Base64.atob(pngBase64), pngBinary));
  39. });
  40. });