michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://services-common/utils.js"); michael@0: michael@0: function run_test() { michael@0: // Testing byte array manipulation. michael@0: do_check_eq("FOOBAR", CommonUtils.byteArrayToString([70, 79, 79, 66, 65, 82])); michael@0: do_check_eq("", CommonUtils.byteArrayToString([])); michael@0: michael@0: _("Testing encoding..."); michael@0: // Test vectors from RFC 4648 michael@0: do_check_eq(CommonUtils.encodeBase32(""), ""); michael@0: do_check_eq(CommonUtils.encodeBase32("f"), "MY======"); michael@0: do_check_eq(CommonUtils.encodeBase32("fo"), "MZXQ===="); michael@0: do_check_eq(CommonUtils.encodeBase32("foo"), "MZXW6==="); michael@0: do_check_eq(CommonUtils.encodeBase32("foob"), "MZXW6YQ="); michael@0: do_check_eq(CommonUtils.encodeBase32("fooba"), "MZXW6YTB"); michael@0: do_check_eq(CommonUtils.encodeBase32("foobar"), "MZXW6YTBOI======"); michael@0: michael@0: do_check_eq(CommonUtils.encodeBase32("Bacon is a vegetable."), michael@0: "IJQWG33OEBUXGIDBEB3GKZ3FORQWE3DFFY======"); michael@0: michael@0: _("Checking assumptions..."); michael@0: for (let i = 0; i <= 255; ++i) michael@0: do_check_eq(undefined | i, i); michael@0: michael@0: _("Testing decoding..."); michael@0: do_check_eq(CommonUtils.decodeBase32(""), ""); michael@0: do_check_eq(CommonUtils.decodeBase32("MY======"), "f"); michael@0: do_check_eq(CommonUtils.decodeBase32("MZXQ===="), "fo"); michael@0: do_check_eq(CommonUtils.decodeBase32("MZXW6YTB"), "fooba"); michael@0: do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI======"), "foobar"); michael@0: michael@0: // Same with incorrect or missing padding. michael@0: do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI=="), "foobar"); michael@0: do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI"), "foobar"); michael@0: michael@0: let encoded = CommonUtils.encodeBase32("Bacon is a vegetable."); michael@0: _("Encoded to " + JSON.stringify(encoded)); michael@0: do_check_eq(CommonUtils.decodeBase32(encoded), "Bacon is a vegetable."); michael@0: michael@0: // Test failure. michael@0: let err; michael@0: try { michael@0: CommonUtils.decodeBase32("000"); michael@0: } catch (ex) { michael@0: err = ex; michael@0: } michael@0: do_check_eq(err, "Unknown character in base32: 0"); michael@0: }