tor-browser

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

test_service_verifyLogin.js (4010B)


      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 (has_hawk_header(request)) {
     11      handler(request, response);
     12    } else {
     13      let body = "Unauthorized";
     14      response.setStatusLine(request.httpVersion, 401, "Unauthorized");
     15      response.bodyOutputStream.write(body, body.length);
     16    }
     17  };
     18 }
     19 
     20 function service_unavailable(request, response) {
     21  let body = "Service Unavailable";
     22  response.setStatusLine(request.httpVersion, 503, "Service Unavailable");
     23  response.setHeader("Retry-After", "42");
     24  response.bodyOutputStream.write(body, body.length);
     25 }
     26 
     27 function run_test() {
     28  Log.repository.rootLogger.addAppender(new Log.DumpAppender());
     29 
     30  run_next_test();
     31 }
     32 
     33 add_task(async function test_verifyLogin() {
     34  // This test expects a clean slate -- no saved passphrase.
     35  Services.logins.removeAllUserFacingLogins();
     36  let johnHelper = track_collections_helper();
     37  let johnU = johnHelper.with_updated_collection;
     38 
     39  do_test_pending();
     40 
     41  let server = httpd_setup({
     42    "/1.1/johndoe/info/collections": login_handling(johnHelper.handler),
     43    "/1.1/janedoe/info/collections": service_unavailable,
     44 
     45    "/1.1/johndoe/storage/crypto/keys": johnU(
     46      "crypto",
     47      new ServerWBO("keys").handler()
     48    ),
     49    "/1.1/johndoe/storage/meta/global": johnU(
     50      "meta",
     51      new ServerWBO("global").handler()
     52    ),
     53  });
     54 
     55  try {
     56    _("Force the initial state.");
     57    Service.status.service = STATUS_OK;
     58    Assert.equal(Service.status.service, STATUS_OK);
     59 
     60    _("Credentials won't check out because we're not configured yet.");
     61    Service.status.resetSync();
     62    Assert.equal(false, await Service.verifyLogin());
     63    Assert.equal(Service.status.service, CLIENT_NOT_CONFIGURED);
     64    Assert.equal(Service.status.login, LOGIN_FAILED_NO_USERNAME);
     65 
     66    _("Success if syncBundleKey is set.");
     67    Service.status.resetSync();
     68    await configureIdentity({ username: "johndoe" }, server);
     69    Assert.ok(await Service.verifyLogin());
     70    Assert.equal(Service.status.service, STATUS_OK);
     71    Assert.equal(Service.status.login, LOGIN_SUCCEEDED);
     72 
     73    _(
     74      "If verifyLogin() encounters a server error, it flips on the backoff flag and notifies observers on a 503 with Retry-After."
     75    );
     76    Service.status.resetSync();
     77    await configureIdentity({ username: "janedoe" }, server);
     78    Service._updateCachedURLs();
     79    Assert.ok(!Service.status.enforceBackoff);
     80    let backoffInterval;
     81    Svc.Obs.add("weave:service:backoff:interval", function observe(subject) {
     82      Svc.Obs.remove("weave:service:backoff:interval", observe);
     83      backoffInterval = subject;
     84    });
     85    Assert.equal(false, await Service.verifyLogin());
     86    Assert.ok(Service.status.enforceBackoff);
     87    Assert.equal(backoffInterval, 42);
     88    Assert.equal(Service.status.service, LOGIN_FAILED);
     89    Assert.equal(Service.status.login, SERVER_MAINTENANCE);
     90 
     91    _(
     92      "Ensure a network error when finding the cluster sets the right Status bits."
     93    );
     94    Service.status.resetSync();
     95    Service.clusterURL = "";
     96    Service.identity._findCluster = () => "http://localhost:12345/";
     97    Assert.equal(false, await Service.verifyLogin());
     98    Assert.equal(Service.status.service, LOGIN_FAILED);
     99    Assert.equal(Service.status.login, LOGIN_FAILED_NETWORK_ERROR);
    100 
    101    _(
    102      "Ensure a network error when getting the collection info sets the right Status bits."
    103    );
    104    Service.status.resetSync();
    105    Service.clusterURL = "http://localhost:12345/";
    106    Assert.equal(false, await Service.verifyLogin());
    107    Assert.equal(Service.status.service, LOGIN_FAILED);
    108    Assert.equal(Service.status.login, LOGIN_FAILED_NETWORK_ERROR);
    109  } finally {
    110    for (const pref of Svc.PrefBranch.getChildList("")) {
    111      Svc.PrefBranch.clearUserPref(pref);
    112    }
    113    server.stop(do_test_finished);
    114  }
    115 });