michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const base64 = require("sdk/base64"); michael@0: michael@0: const text = "Awesome!"; michael@0: const b64text = "QXdlc29tZSE="; michael@0: michael@0: const utf8text = "✓ à la mode"; michael@0: const b64utf8text = "4pyTIMOgIGxhIG1vZGU="; michael@0: michael@0: exports["test base64.encode"] = function (assert) { michael@0: assert.equal(base64.encode(text), b64text, "encode correctly") michael@0: } michael@0: michael@0: exports["test base64.decode"] = function (assert) { michael@0: assert.equal(base64.decode(b64text), text, "decode correctly") michael@0: } michael@0: michael@0: exports["test base64.encode Unicode"] = function (assert) { michael@0: michael@0: assert.equal(base64.encode(utf8text, "utf-8"), b64utf8text, michael@0: "encode correctly Unicode strings.") michael@0: } michael@0: michael@0: exports["test base64.decode Unicode"] = function (assert) { michael@0: michael@0: assert.equal(base64.decode(b64utf8text, "utf-8"), utf8text, michael@0: "decode correctly Unicode strings.") michael@0: } michael@0: michael@0: exports["test base64.encode with wrong charset"] = function (assert) { michael@0: michael@0: assert.throws(function() { michael@0: base64.encode(utf8text, "utf-16"); michael@0: }, "The charset argument can be only 'utf-8'"); michael@0: michael@0: assert.throws(function() { michael@0: base64.encode(utf8text, ""); michael@0: }, "The charset argument can be only 'utf-8'"); michael@0: michael@0: assert.throws(function() { michael@0: base64.encode(utf8text, 8); michael@0: }, "The charset argument can be only 'utf-8'"); michael@0: michael@0: } michael@0: michael@0: exports["test base64.decode with wrong charset"] = function (assert) { michael@0: michael@0: assert.throws(function() { michael@0: base64.decode(utf8text, "utf-16"); michael@0: }, "The charset argument can be only 'utf-8'"); michael@0: michael@0: assert.throws(function() { michael@0: base64.decode(utf8text, ""); michael@0: }, "The charset argument can be only 'utf-8'"); michael@0: michael@0: assert.throws(function() { michael@0: base64.decode(utf8text, 8); michael@0: }, "The charset argument can be only 'utf-8'"); michael@0: michael@0: } michael@0: michael@0: exports["test encode/decode Unicode without utf-8 as charset"] = function (assert) { michael@0: michael@0: assert.notEqual(base64.decode(base64.encode(utf8text)), utf8text, michael@0: "Unicode strings needs 'utf-8' charset" michael@0: ); michael@0: michael@0: } michael@0: michael@0: require("test").run(exports);