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