services/common/tests/unit/test_utils_encodeBase32.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/ */
     4 Cu.import("resource://services-common/utils.js");
     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([]));
    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======");
    21   do_check_eq(CommonUtils.encodeBase32("Bacon is a vegetable."),
    22               "IJQWG33OEBUXGIDBEB3GKZ3FORQWE3DFFY======");
    24   _("Checking assumptions...");
    25   for (let i = 0; i <= 255; ++i)
    26     do_check_eq(undefined | i, i);
    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");
    35   // Same with incorrect or missing padding.
    36   do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI=="), "foobar");
    37   do_check_eq(CommonUtils.decodeBase32("MZXW6YTBOI"), "foobar");
    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.");
    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 }

mercurial