tor-browser

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

test_utils_json.js (2453B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { FileUtils } = ChromeUtils.importESModule(
      5  "resource://gre/modules/FileUtils.sys.mjs"
      6 );
      7 
      8 add_task(async function test_roundtrip() {
      9  _("Do a simple write of an array to json and read");
     10  await Utils.jsonSave("foo", {}, ["v1", "v2"]);
     11 
     12  let foo = await Utils.jsonLoad("foo", {});
     13  Assert.equal(typeof foo, "object");
     14  Assert.equal(foo.length, 2);
     15  Assert.equal(foo[0], "v1");
     16  Assert.equal(foo[1], "v2");
     17 });
     18 
     19 add_task(async function test_string() {
     20  _("Try saving simple strings");
     21  await Utils.jsonSave("str", {}, "hi");
     22 
     23  let str = await Utils.jsonLoad("str", {});
     24  Assert.equal(typeof str, "string");
     25  Assert.equal(str.length, 2);
     26  Assert.equal(str[0], "h");
     27  Assert.equal(str[1], "i");
     28 });
     29 
     30 add_task(async function test_number() {
     31  _("Try saving a number");
     32  await Utils.jsonSave("num", {}, 42);
     33 
     34  let num = await Utils.jsonLoad("num", {});
     35  Assert.equal(typeof num, "number");
     36  Assert.equal(num, 42);
     37 });
     38 
     39 add_task(async function test_nonexistent_file() {
     40  _("Try loading a non-existent file.");
     41  let val = await Utils.jsonLoad("non-existent", {});
     42  Assert.equal(val, undefined);
     43 });
     44 
     45 add_task(async function test_save_logging() {
     46  _("Verify that writes are logged.");
     47  let trace;
     48  await Utils.jsonSave(
     49    "log",
     50    {
     51      _log: {
     52        trace(msg) {
     53          trace = msg;
     54        },
     55      },
     56    },
     57    "hi"
     58  );
     59  Assert.ok(!!trace);
     60 });
     61 
     62 add_task(async function test_load_logging() {
     63  _("Verify that reads and read errors are logged.");
     64 
     65  // Write a file with some invalid JSON
     66  let file = await IOUtils.getFile(PathUtils.profileDir, "weave", "log.json");
     67  let fos = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
     68    Ci.nsIFileOutputStream
     69  );
     70  let flags =
     71    FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;
     72  fos.init(file, flags, FileUtils.PERMS_FILE, fos.DEFER_OPEN);
     73  let stream = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(
     74    Ci.nsIConverterOutputStream
     75  );
     76  stream.init(fos, "UTF-8");
     77  stream.writeString("invalid json!");
     78  stream.close();
     79 
     80  let trace, debug;
     81  let obj = {
     82    _log: {
     83      trace(msg) {
     84        trace = msg;
     85      },
     86      debug(msg) {
     87        debug = msg;
     88      },
     89    },
     90  };
     91  let val = await Utils.jsonLoad("log", obj);
     92  Assert.ok(!val);
     93  Assert.ok(!!trace);
     94  Assert.ok(!!debug);
     95 });