tor-browser

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

test_service_sync_specified.js (3997B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { Service } = ChromeUtils.importESModule(
      5  "resource://services-sync/service.sys.mjs"
      6 );
      7 
      8 let syncedEngines = [];
      9 
     10 function SteamEngine() {
     11  SyncEngine.call(this, "Steam", Service);
     12 }
     13 SteamEngine.prototype = {
     14  async _sync() {
     15    syncedEngines.push(this.name);
     16  },
     17 };
     18 Object.setPrototypeOf(SteamEngine.prototype, SyncEngine.prototype);
     19 
     20 function StirlingEngine() {
     21  SyncEngine.call(this, "Stirling", Service);
     22 }
     23 StirlingEngine.prototype = {
     24  async _sync() {
     25    syncedEngines.push(this.name);
     26  },
     27 };
     28 Object.setPrototypeOf(StirlingEngine.prototype, SteamEngine.prototype);
     29 
     30 // Tracking info/collections.
     31 var collectionsHelper = track_collections_helper();
     32 var upd = collectionsHelper.with_updated_collection;
     33 
     34 function sync_httpd_setup(handlers) {
     35  handlers["/1.1/johndoe/info/collections"] = collectionsHelper.handler;
     36  delete collectionsHelper.collections.crypto;
     37  delete collectionsHelper.collections.meta;
     38 
     39  let cr = new ServerWBO("keys");
     40  handlers["/1.1/johndoe/storage/crypto/keys"] = upd("crypto", cr.handler());
     41 
     42  let cl = new ServerCollection();
     43  handlers["/1.1/johndoe/storage/clients"] = upd("clients", cl.handler());
     44 
     45  return httpd_setup(handlers);
     46 }
     47 
     48 async function setUp() {
     49  syncedEngines = [];
     50  let engine = Service.engineManager.get("steam");
     51  engine.enabled = true;
     52  engine.syncPriority = 1;
     53 
     54  engine = Service.engineManager.get("stirling");
     55  engine.enabled = true;
     56  engine.syncPriority = 2;
     57 
     58  let server = sync_httpd_setup({
     59    "/1.1/johndoe/storage/meta/global": new ServerWBO("global", {}).handler(),
     60  });
     61  await SyncTestingInfrastructure(server, "johndoe", "ilovejane");
     62  return server;
     63 }
     64 
     65 add_task(async function setup() {
     66  await Service.engineManager.clear();
     67  validate_all_future_pings();
     68 
     69  await Service.engineManager.register(SteamEngine);
     70  await Service.engineManager.register(StirlingEngine);
     71 });
     72 
     73 add_task(async function test_noEngines() {
     74  enableValidationPrefs();
     75 
     76  _("Test: An empty array of engines to sync does nothing.");
     77  let server = await setUp();
     78 
     79  try {
     80    _("Sync with no engines specified.");
     81    await Service.sync({ engines: [] });
     82    deepEqual(syncedEngines, [], "no engines were synced");
     83  } finally {
     84    await Service.startOver();
     85    await promiseStopServer(server);
     86  }
     87 });
     88 
     89 add_task(async function test_oneEngine() {
     90  enableValidationPrefs();
     91 
     92  _("Test: Only one engine is synced.");
     93  let server = await setUp();
     94 
     95  try {
     96    _("Sync with 1 engine specified.");
     97    await Service.sync({ engines: ["steam"] });
     98    deepEqual(syncedEngines, ["steam"]);
     99  } finally {
    100    await Service.startOver();
    101    await promiseStopServer(server);
    102  }
    103 });
    104 
    105 add_task(async function test_bothEnginesSpecified() {
    106  enableValidationPrefs();
    107 
    108  _("Test: All engines are synced when specified in the correct order (1).");
    109  let server = await setUp();
    110 
    111  try {
    112    _("Sync with both engines specified.");
    113    await Service.sync({ engines: ["steam", "stirling"] });
    114    deepEqual(syncedEngines, ["steam", "stirling"]);
    115  } finally {
    116    await Service.startOver();
    117    await promiseStopServer(server);
    118  }
    119 });
    120 
    121 add_task(async function test_bothEnginesSpecified() {
    122  enableValidationPrefs();
    123 
    124  _("Test: All engines are synced when specified in the correct order (2).");
    125  let server = await setUp();
    126 
    127  try {
    128    _("Sync with both engines specified.");
    129    await Service.sync({ engines: ["stirling", "steam"] });
    130    deepEqual(syncedEngines, ["stirling", "steam"]);
    131  } finally {
    132    await Service.startOver();
    133    await promiseStopServer(server);
    134  }
    135 });
    136 
    137 add_task(async function test_bothEnginesDefault() {
    138  enableValidationPrefs();
    139 
    140  _("Test: All engines are synced when nothing is specified.");
    141  let server = await setUp();
    142 
    143  try {
    144    await Service.sync();
    145    deepEqual(syncedEngines, ["steam", "stirling"]);
    146  } finally {
    147    await Service.startOver();
    148    await promiseStopServer(server);
    149  }
    150 });