1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/common/tests/unit/test_utils_encodeBase32.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,51 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://services-common/utils.js"); 1.8 + 1.9 +function run_test() { 1.10 + // Testing byte array manipulation. 1.11 + do_check_eq("FOOBAR", CommonUtils.byteArrayToString([70, 79, 79, 66, 65, 82])); 1.12 + do_check_eq("", CommonUtils.byteArrayToString([])); 1.13 + 1.14 + _("Testing encoding..."); 1.15 + // Test vectors from RFC 4648 1.16 + do_check_eq(CommonUtils.encodeBase32(""), ""); 1.17 + do_check_eq(CommonUtils.encodeBase32("f"), "MY======"); 1.18 + do_check_eq(CommonUtils.encodeBase32("fo"), "MZXQ===="); 1.19 + do_check_eq(CommonUtils.encodeBase32("foo"), "MZXW6==="); 1.20 + do_check_eq(CommonUtils.encodeBase32("foob"), "MZXW6YQ="); 1.21 + do_check_eq(CommonUtils.encodeBase32("fooba"), "MZXW6YTB"); 1.22 + do_check_eq(CommonUtils.encodeBase32("foobar"), "MZXW6YTBOI======"); 1.23 + 1.24 + do_check_eq(CommonUtils.encodeBase32("Bacon is a vegetable."), 1.25 + "IJQWG33OEBUXGIDBEB3GKZ3FORQWE3DFFY======"); 1.26 + 1.27 + _("Checking assumptions..."); 1.28 + for (let i = 0; i <= 255; ++i) 1.29 + do_check_eq(undefined | i, i); 1.30 + 1.31 + _("Testing decoding..."); 1.32 + do_check_eq(CommonUtils.decodeBase32(""), ""); 1.33 + do_check_eq(CommonUtils.decodeBase32("MY======"), "f"); 1.34 + do_check_eq(CommonUtils.decodeBase32("MZXQ===="), "fo"); 1.35 + do_check_eq(CommonUtils.decodeBase32("MZXW6YTB"), "fooba"); 1.36 + do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI======"), "foobar"); 1.37 + 1.38 + // Same with incorrect or missing padding. 1.39 + do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI=="), "foobar"); 1.40 + do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI"), "foobar"); 1.41 + 1.42 + let encoded = CommonUtils.encodeBase32("Bacon is a vegetable."); 1.43 + _("Encoded to " + JSON.stringify(encoded)); 1.44 + do_check_eq(CommonUtils.decodeBase32(encoded), "Bacon is a vegetable."); 1.45 + 1.46 + // Test failure. 1.47 + let err; 1.48 + try { 1.49 + CommonUtils.decodeBase32("000"); 1.50 + } catch (ex) { 1.51 + err = ex; 1.52 + } 1.53 + do_check_eq(err, "Unknown character in base32: 0"); 1.54 +}