Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Cu.import("resource://services-common/utils.js"); |
michael@0 | 5 | |
michael@0 | 6 | function run_test() { |
michael@0 | 7 | // Testing byte array manipulation. |
michael@0 | 8 | do_check_eq("FOOBAR", CommonUtils.byteArrayToString([70, 79, 79, 66, 65, 82])); |
michael@0 | 9 | do_check_eq("", CommonUtils.byteArrayToString([])); |
michael@0 | 10 | |
michael@0 | 11 | _("Testing encoding..."); |
michael@0 | 12 | // Test vectors from RFC 4648 |
michael@0 | 13 | do_check_eq(CommonUtils.encodeBase32(""), ""); |
michael@0 | 14 | do_check_eq(CommonUtils.encodeBase32("f"), "MY======"); |
michael@0 | 15 | do_check_eq(CommonUtils.encodeBase32("fo"), "MZXQ===="); |
michael@0 | 16 | do_check_eq(CommonUtils.encodeBase32("foo"), "MZXW6==="); |
michael@0 | 17 | do_check_eq(CommonUtils.encodeBase32("foob"), "MZXW6YQ="); |
michael@0 | 18 | do_check_eq(CommonUtils.encodeBase32("fooba"), "MZXW6YTB"); |
michael@0 | 19 | do_check_eq(CommonUtils.encodeBase32("foobar"), "MZXW6YTBOI======"); |
michael@0 | 20 | |
michael@0 | 21 | do_check_eq(CommonUtils.encodeBase32("Bacon is a vegetable."), |
michael@0 | 22 | "IJQWG33OEBUXGIDBEB3GKZ3FORQWE3DFFY======"); |
michael@0 | 23 | |
michael@0 | 24 | _("Checking assumptions..."); |
michael@0 | 25 | for (let i = 0; i <= 255; ++i) |
michael@0 | 26 | do_check_eq(undefined | i, i); |
michael@0 | 27 | |
michael@0 | 28 | _("Testing decoding..."); |
michael@0 | 29 | do_check_eq(CommonUtils.decodeBase32(""), ""); |
michael@0 | 30 | do_check_eq(CommonUtils.decodeBase32("MY======"), "f"); |
michael@0 | 31 | do_check_eq(CommonUtils.decodeBase32("MZXQ===="), "fo"); |
michael@0 | 32 | do_check_eq(CommonUtils.decodeBase32("MZXW6YTB"), "fooba"); |
michael@0 | 33 | do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI======"), "foobar"); |
michael@0 | 34 | |
michael@0 | 35 | // Same with incorrect or missing padding. |
michael@0 | 36 | do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI=="), "foobar"); |
michael@0 | 37 | do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI"), "foobar"); |
michael@0 | 38 | |
michael@0 | 39 | let encoded = CommonUtils.encodeBase32("Bacon is a vegetable."); |
michael@0 | 40 | _("Encoded to " + JSON.stringify(encoded)); |
michael@0 | 41 | do_check_eq(CommonUtils.decodeBase32(encoded), "Bacon is a vegetable."); |
michael@0 | 42 | |
michael@0 | 43 | // Test failure. |
michael@0 | 44 | let err; |
michael@0 | 45 | try { |
michael@0 | 46 | CommonUtils.decodeBase32("000"); |
michael@0 | 47 | } catch (ex) { |
michael@0 | 48 | err = ex; |
michael@0 | 49 | } |
michael@0 | 50 | do_check_eq(err, "Unknown character in base32: 0"); |
michael@0 | 51 | } |