tor-browser

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

test_service_wipeServer.js (7276B)


      1 Svc.PrefBranch.setStringPref("registerEngines", "");
      2 const { Service } = ChromeUtils.importESModule(
      3  "resource://services-sync/service.sys.mjs"
      4 );
      5 
      6 // configure the identity we use for this test.
      7 const identityConfig = makeIdentityConfig({ username: "johndoe" });
      8 
      9 function FakeCollection() {
     10  this.deleted = false;
     11 }
     12 FakeCollection.prototype = {
     13  handler() {
     14    let self = this;
     15    return function (request, response) {
     16      let body = "";
     17      self.timestamp = new_timestamp();
     18      let timestamp = "" + self.timestamp;
     19      if (request.method == "DELETE") {
     20        body = timestamp;
     21        self.deleted = true;
     22      }
     23      response.setHeader("X-Weave-Timestamp", timestamp);
     24      response.setStatusLine(request.httpVersion, 200, "OK");
     25      response.bodyOutputStream.write(body, body.length);
     26    };
     27  },
     28 };
     29 
     30 async function setUpTestFixtures(server) {
     31  Service.clusterURL = server.baseURI + "/";
     32 
     33  await configureIdentity(identityConfig);
     34 }
     35 
     36 add_task(async function test_wipeServer_list_success() {
     37  _("Service.wipeServer() deletes collections given as argument.");
     38 
     39  let steam_coll = new FakeCollection();
     40  let diesel_coll = new FakeCollection();
     41 
     42  let server = httpd_setup({
     43    "/1.1/johndoe/storage/steam": steam_coll.handler(),
     44    "/1.1/johndoe/storage/diesel": diesel_coll.handler(),
     45    "/1.1/johndoe/storage/petrol": httpd_handler(404, "Not Found"),
     46  });
     47 
     48  try {
     49    await setUpTestFixtures(server);
     50    await SyncTestingInfrastructure(server, "johndoe", "irrelevant");
     51 
     52    _("Confirm initial environment.");
     53    Assert.ok(!steam_coll.deleted);
     54    Assert.ok(!diesel_coll.deleted);
     55 
     56    _(
     57      "wipeServer() will happily ignore the non-existent collection and use the timestamp of the last DELETE that was successful."
     58    );
     59    let timestamp = await Service.wipeServer(["steam", "diesel", "petrol"]);
     60    Assert.equal(timestamp, diesel_coll.timestamp);
     61 
     62    _(
     63      "wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted."
     64    );
     65    Assert.ok(steam_coll.deleted);
     66    Assert.ok(diesel_coll.deleted);
     67  } finally {
     68    await promiseStopServer(server);
     69    for (const pref of Svc.PrefBranch.getChildList("")) {
     70      Svc.PrefBranch.clearUserPref(pref);
     71    }
     72  }
     73 });
     74 
     75 add_task(async function test_wipeServer_list_503() {
     76  _("Service.wipeServer() deletes collections given as argument.");
     77 
     78  let steam_coll = new FakeCollection();
     79  let diesel_coll = new FakeCollection();
     80 
     81  let server = httpd_setup({
     82    "/1.1/johndoe/storage/steam": steam_coll.handler(),
     83    "/1.1/johndoe/storage/petrol": httpd_handler(503, "Service Unavailable"),
     84    "/1.1/johndoe/storage/diesel": diesel_coll.handler(),
     85  });
     86 
     87  try {
     88    await setUpTestFixtures(server);
     89    await SyncTestingInfrastructure(server, "johndoe", "irrelevant");
     90 
     91    _("Confirm initial environment.");
     92    Assert.ok(!steam_coll.deleted);
     93    Assert.ok(!diesel_coll.deleted);
     94 
     95    _(
     96      "wipeServer() will happily ignore the non-existent collection, delete the 'steam' collection and abort after an receiving an error on the 'petrol' collection."
     97    );
     98    let error;
     99    try {
    100      await Service.wipeServer(["non-existent", "steam", "petrol", "diesel"]);
    101      do_throw("Should have thrown!");
    102    } catch (ex) {
    103      error = ex;
    104    }
    105    _("wipeServer() threw this exception: " + error);
    106    Assert.equal(error.status, 503);
    107 
    108    _(
    109      "wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted."
    110    );
    111    Assert.ok(steam_coll.deleted);
    112    Assert.ok(!diesel_coll.deleted);
    113  } finally {
    114    await promiseStopServer(server);
    115    for (const pref of Svc.PrefBranch.getChildList("")) {
    116      Svc.PrefBranch.clearUserPref(pref);
    117    }
    118  }
    119 });
    120 
    121 add_task(async function test_wipeServer_all_success() {
    122  _("Service.wipeServer() deletes all the things.");
    123 
    124  /**
    125   * Handle the bulk DELETE request sent by wipeServer.
    126   */
    127  let deleted = false;
    128  let serverTimestamp;
    129  function storageHandler(request, response) {
    130    Assert.equal("DELETE", request.method);
    131    Assert.ok(request.hasHeader("X-Confirm-Delete"));
    132    deleted = true;
    133    serverTimestamp = return_timestamp(request, response);
    134  }
    135 
    136  let server = httpd_setup({
    137    "/1.1/johndoe/storage": storageHandler,
    138  });
    139  await setUpTestFixtures(server);
    140 
    141  _("Try deletion.");
    142  await SyncTestingInfrastructure(server, "johndoe", "irrelevant");
    143  let returnedTimestamp = await Service.wipeServer();
    144  Assert.ok(deleted);
    145  Assert.equal(returnedTimestamp, serverTimestamp);
    146 
    147  await promiseStopServer(server);
    148  for (const pref of Svc.PrefBranch.getChildList("")) {
    149    Svc.PrefBranch.clearUserPref(pref);
    150  }
    151 });
    152 
    153 add_task(async function test_wipeServer_all_404() {
    154  _("Service.wipeServer() accepts a 404.");
    155 
    156  /**
    157   * Handle the bulk DELETE request sent by wipeServer. Returns a 404.
    158   */
    159  let deleted = false;
    160  let serverTimestamp;
    161  function storageHandler(request, response) {
    162    Assert.equal("DELETE", request.method);
    163    Assert.ok(request.hasHeader("X-Confirm-Delete"));
    164    deleted = true;
    165    serverTimestamp = new_timestamp();
    166    response.setHeader("X-Weave-Timestamp", "" + serverTimestamp);
    167    response.setStatusLine(request.httpVersion, 404, "Not Found");
    168  }
    169 
    170  let server = httpd_setup({
    171    "/1.1/johndoe/storage": storageHandler,
    172  });
    173  await setUpTestFixtures(server);
    174 
    175  _("Try deletion.");
    176  await SyncTestingInfrastructure(server, "johndoe", "irrelevant");
    177  let returnedTimestamp = await Service.wipeServer();
    178  Assert.ok(deleted);
    179  Assert.equal(returnedTimestamp, serverTimestamp);
    180 
    181  await promiseStopServer(server);
    182  for (const pref of Svc.PrefBranch.getChildList("")) {
    183    Svc.PrefBranch.clearUserPref(pref);
    184  }
    185 });
    186 
    187 add_task(async function test_wipeServer_all_503() {
    188  _("Service.wipeServer() throws if it encounters a non-200/404 response.");
    189 
    190  /**
    191   * Handle the bulk DELETE request sent by wipeServer. Returns a 503.
    192   */
    193  function storageHandler(request, response) {
    194    Assert.equal("DELETE", request.method);
    195    Assert.ok(request.hasHeader("X-Confirm-Delete"));
    196    response.setStatusLine(request.httpVersion, 503, "Service Unavailable");
    197  }
    198 
    199  let server = httpd_setup({
    200    "/1.1/johndoe/storage": storageHandler,
    201  });
    202  await setUpTestFixtures(server);
    203 
    204  _("Try deletion.");
    205  let error;
    206  try {
    207    await SyncTestingInfrastructure(server, "johndoe", "irrelevant");
    208    await Service.wipeServer();
    209    do_throw("Should have thrown!");
    210  } catch (ex) {
    211    error = ex;
    212  }
    213  Assert.equal(error.status, 503);
    214 
    215  await promiseStopServer(server);
    216  for (const pref of Svc.PrefBranch.getChildList("")) {
    217    Svc.PrefBranch.clearUserPref(pref);
    218  }
    219 });
    220 
    221 add_task(async function test_wipeServer_all_connectionRefused() {
    222  _("Service.wipeServer() throws if it encounters a network problem.");
    223  let server = httpd_setup({});
    224  await setUpTestFixtures(server);
    225 
    226  Service.clusterURL = "http://localhost:4352/";
    227 
    228  _("Try deletion.");
    229  try {
    230    await Service.wipeServer();
    231    do_throw("Should have thrown!");
    232  } catch (ex) {
    233    Assert.equal(ex.result, Cr.NS_ERROR_CONNECTION_REFUSED);
    234  }
    235 
    236  for (const pref of Svc.PrefBranch.getChildList("")) {
    237    Svc.PrefBranch.clearUserPref(pref);
    238  }
    239  await promiseStopServer(server);
    240 });