tor-browser

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

test_service_sync_401.js (2692B)


      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 function login_handling(handler) {
      9  return function (request, response) {
     10    if (
     11      request.hasHeader("Authorization") &&
     12      request.getHeader("Authorization").includes('Hawk id="id"')
     13    ) {
     14      handler(request, response);
     15    } else {
     16      let body = "Unauthorized";
     17      response.setStatusLine(request.httpVersion, 401, "Unauthorized");
     18      response.bodyOutputStream.write(body, body.length);
     19    }
     20  };
     21 }
     22 
     23 add_task(async function run_test() {
     24  Log.repository.rootLogger.addAppender(new Log.DumpAppender());
     25 
     26  let collectionsHelper = track_collections_helper();
     27  let upd = collectionsHelper.with_updated_collection;
     28 
     29  let server = httpd_setup({
     30    "/1.1/johndoe/storage/crypto/keys": upd(
     31      "crypto",
     32      new ServerWBO("keys").handler()
     33    ),
     34    "/1.1/johndoe/storage/meta/global": upd(
     35      "meta",
     36      new ServerWBO("global").handler()
     37    ),
     38    "/1.1/johndoe/info/collections": login_handling(collectionsHelper.handler),
     39  });
     40 
     41  const GLOBAL_SCORE = 42;
     42 
     43  try {
     44    _("Set up test fixtures.");
     45    await SyncTestingInfrastructure(server, "johndoe", "ilovejane");
     46    Service.scheduler.globalScore = GLOBAL_SCORE;
     47    // Avoid daily ping
     48    Svc.PrefBranch.setIntPref("lastPing", Math.floor(Date.now() / 1000));
     49 
     50    let threw = false;
     51    Svc.Obs.add("weave:service:sync:error", function () {
     52      threw = true;
     53    });
     54 
     55    _("Initial state: We're successfully logged in.");
     56    await Service.login();
     57    Assert.ok(Service.isLoggedIn);
     58    Assert.equal(Service.status.login, LOGIN_SUCCEEDED);
     59 
     60    _("Simulate having changed the password somewhere else.");
     61    Service.identity._token.id = "somethingelse";
     62    Service.identity.unlockAndVerifyAuthState = () =>
     63      Promise.resolve(LOGIN_FAILED_LOGIN_REJECTED);
     64 
     65    _("Let's try to sync.");
     66    await Service.sync();
     67 
     68    _("Verify that sync() threw an exception.");
     69    Assert.ok(threw);
     70 
     71    _("We're no longer logged in.");
     72    Assert.ok(!Service.isLoggedIn);
     73 
     74    _("Sync status won't have changed yet, because we haven't tried again.");
     75 
     76    _("globalScore is reset upon starting a sync.");
     77    Assert.equal(Service.scheduler.globalScore, 0);
     78 
     79    _("Our next sync will fail appropriately.");
     80    try {
     81      await Service.sync();
     82    } catch (ex) {}
     83    Assert.equal(Service.status.login, LOGIN_FAILED_LOGIN_REJECTED);
     84  } finally {
     85    for (const pref of Svc.PrefBranch.getChildList("")) {
     86      Svc.PrefBranch.clearUserPref(pref);
     87    }
     88    await promiseStopServer(server);
     89  }
     90 });