tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_utils_encodeBase32.js (1918B)


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