test-base64.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const base64 = require("sdk/base64");
  6. const text = "Awesome!";
  7. const b64text = "QXdlc29tZSE=";
  8. const utf8text = "✓ à la mode";
  9. const b64utf8text = "4pyTIMOgIGxhIG1vZGU=";
  10. exports["test base64.encode"] = function (assert) {
  11. assert.equal(base64.encode(text), b64text, "encode correctly")
  12. }
  13. exports["test base64.decode"] = function (assert) {
  14. assert.equal(base64.decode(b64text), text, "decode correctly")
  15. }
  16. exports["test base64.encode Unicode"] = function (assert) {
  17. assert.equal(base64.encode(utf8text, "utf-8"), b64utf8text,
  18. "encode correctly Unicode strings.")
  19. }
  20. exports["test base64.decode Unicode"] = function (assert) {
  21. assert.equal(base64.decode(b64utf8text, "utf-8"), utf8text,
  22. "decode correctly Unicode strings.")
  23. }
  24. exports["test base64.encode with wrong charset"] = function (assert) {
  25. assert.throws(function() {
  26. base64.encode(utf8text, "utf-16");
  27. }, "The charset argument can be only 'utf-8'");
  28. assert.throws(function() {
  29. base64.encode(utf8text, "");
  30. }, "The charset argument can be only 'utf-8'");
  31. assert.throws(function() {
  32. base64.encode(utf8text, 8);
  33. }, "The charset argument can be only 'utf-8'");
  34. }
  35. exports["test base64.decode with wrong charset"] = function (assert) {
  36. assert.throws(function() {
  37. base64.decode(utf8text, "utf-16");
  38. }, "The charset argument can be only 'utf-8'");
  39. assert.throws(function() {
  40. base64.decode(utf8text, "");
  41. }, "The charset argument can be only 'utf-8'");
  42. assert.throws(function() {
  43. base64.decode(utf8text, 8);
  44. }, "The charset argument can be only 'utf-8'");
  45. }
  46. exports["test encode/decode Unicode without utf-8 as charset"] = function (assert) {
  47. assert.notEqual(base64.decode(base64.encode(utf8text)), utf8text,
  48. "Unicode strings needs 'utf-8' charset"
  49. );
  50. }
  51. require("test").run(exports);