tor-browser

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

test_declined.js (5584B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { DeclinedEngines } = ChromeUtils.importESModule(
      5  "resource://services-sync/stages/declined.sys.mjs"
      6 );
      7 const { EngineSynchronizer } = ChromeUtils.importESModule(
      8  "resource://services-sync/stages/enginesync.sys.mjs"
      9 );
     10 const { Service } = ChromeUtils.importESModule(
     11  "resource://services-sync/service.sys.mjs"
     12 );
     13 const { Observers } = ChromeUtils.importESModule(
     14  "resource://services-common/observers.sys.mjs"
     15 );
     16 
     17 function PetrolEngine() {}
     18 PetrolEngine.prototype.name = "petrol";
     19 
     20 function DieselEngine() {}
     21 DieselEngine.prototype.name = "diesel";
     22 
     23 function DummyEngine() {}
     24 DummyEngine.prototype.name = "dummy";
     25 
     26 function ActualEngine() {}
     27 ActualEngine.prototype.name = "actual";
     28 Object.setPrototypeOf(ActualEngine.prototype, SyncEngine.prototype);
     29 
     30 function getEngineManager() {
     31  let manager = new EngineManager(Service);
     32  Service.engineManager = manager;
     33  manager._engines = {
     34    petrol: new PetrolEngine(),
     35    diesel: new DieselEngine(),
     36    dummy: new DummyEngine(),
     37    actual: new ActualEngine(),
     38  };
     39  return manager;
     40 }
     41 
     42 /**
     43 * 'Fetch' a meta/global record that doesn't mention declined.
     44 *
     45 * Push it into the EngineSynchronizer to set enabled; verify that those are
     46 * correct.
     47 *
     48 * Then push it into DeclinedEngines to set declined; verify that none are
     49 * declined, and a notification is sent for our locally disabled-but-not-
     50 * declined engines.
     51 */
     52 add_task(async function testOldMeta() {
     53  let meta = {
     54    payload: {
     55      engines: {
     56        petrol: 1,
     57        diesel: 2,
     58        nonlocal: 3, // Enabled but not supported.
     59      },
     60    },
     61  };
     62 
     63  _("Record: " + JSON.stringify(meta));
     64 
     65  let manager = getEngineManager();
     66 
     67  // Update enabled from meta/global.
     68  let engineSync = new EngineSynchronizer(Service);
     69  await engineSync._updateEnabledFromMeta(meta, 3, manager);
     70 
     71  Assert.ok(manager._engines.petrol.enabled, "'petrol' locally enabled.");
     72  Assert.ok(manager._engines.diesel.enabled, "'diesel' locally enabled.");
     73  Assert.ok(
     74    !("nonlocal" in manager._engines),
     75    "We don't know anything about the 'nonlocal' engine."
     76  );
     77  Assert.ok(!manager._engines.actual.enabled, "'actual' not locally enabled.");
     78  Assert.ok(!manager.isDeclined("actual"), "'actual' not declined, though.");
     79 
     80  let declinedEngines = new DeclinedEngines(Service);
     81 
     82  function onNotDeclined(subject) {
     83    Observers.remove("weave:engines:notdeclined", onNotDeclined);
     84    Assert.ok(
     85      subject.undecided.has("actual"),
     86      "EngineManager observed that 'actual' was undecided."
     87    );
     88 
     89    let declined = manager.getDeclined();
     90    _("Declined: " + JSON.stringify(declined));
     91 
     92    Assert.ok(!meta.changed, "No need to upload a new meta/global.");
     93    run_next_test();
     94  }
     95 
     96  Observers.add("weave:engines:notdeclined", onNotDeclined);
     97 
     98  declinedEngines.updateDeclined(meta, manager);
     99 });
    100 
    101 /**
    102 * 'Fetch' a meta/global that declines an engine we don't
    103 * recognize. Ensure that we track that declined engine along
    104 * with any we locally declined, and that the meta/global
    105 * record is marked as changed and includes all declined
    106 * engines.
    107 */
    108 add_task(async function testDeclinedMeta() {
    109  let meta = {
    110    payload: {
    111      engines: {
    112        petrol: 1,
    113        diesel: 2,
    114        nonlocal: 3, // Enabled but not supported.
    115      },
    116      declined: ["nonexistent"], // Declined and not supported.
    117    },
    118  };
    119 
    120  _("Record: " + JSON.stringify(meta));
    121 
    122  let manager = getEngineManager();
    123  manager._engines.petrol.enabled = true;
    124  manager._engines.diesel.enabled = true;
    125  manager._engines.dummy.enabled = true;
    126  manager._engines.actual.enabled = false; // Disabled but not declined.
    127 
    128  manager.decline(["localdecline"]); // Declined and not supported.
    129 
    130  let declinedEngines = new DeclinedEngines(Service);
    131 
    132  function onNotDeclined(subject) {
    133    Observers.remove("weave:engines:notdeclined", onNotDeclined);
    134    Assert.ok(
    135      subject.undecided.has("actual"),
    136      "EngineManager observed that 'actual' was undecided."
    137    );
    138 
    139    let declined = manager.getDeclined();
    140    _("Declined: " + JSON.stringify(declined));
    141 
    142    Assert.equal(
    143      declined.indexOf("actual"),
    144      -1,
    145      "'actual' is locally disabled, but not marked as declined."
    146    );
    147 
    148    Assert.equal(
    149      declined.indexOf("clients"),
    150      -1,
    151      "'clients' is enabled and not remotely declined."
    152    );
    153    Assert.equal(
    154      declined.indexOf("petrol"),
    155      -1,
    156      "'petrol' is enabled and not remotely declined."
    157    );
    158    Assert.equal(
    159      declined.indexOf("diesel"),
    160      -1,
    161      "'diesel' is enabled and not remotely declined."
    162    );
    163    Assert.equal(
    164      declined.indexOf("dummy"),
    165      -1,
    166      "'dummy' is enabled and not remotely declined."
    167    );
    168 
    169    Assert.lessOrEqual(
    170      0,
    171      declined.indexOf("nonexistent"),
    172      "'nonexistent' was declined on the server."
    173    );
    174 
    175    Assert.lessOrEqual(
    176      0,
    177      declined.indexOf("localdecline"),
    178      "'localdecline' was declined locally."
    179    );
    180 
    181    // The meta/global is modified, too.
    182    Assert.lessOrEqual(
    183      0,
    184      meta.payload.declined.indexOf("nonexistent"),
    185      "meta/global's declined contains 'nonexistent'."
    186    );
    187    Assert.lessOrEqual(
    188      0,
    189      meta.payload.declined.indexOf("localdecline"),
    190      "meta/global's declined contains 'localdecline'."
    191    );
    192    Assert.strictEqual(true, meta.changed, "meta/global was changed.");
    193  }
    194 
    195  Observers.add("weave:engines:notdeclined", onNotDeclined);
    196 
    197  declinedEngines.updateDeclined(meta, manager);
    198 });