tor-browser

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

test_register_timeout.js (2836B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const userAgentID = "a4be0df9-b16d-4b5f-8f58-0f93b6f1e23d";
      7 const channelID = "e1944e0b-48df-45e7-bdc0-d1fbaa7986d3";
      8 
      9 function run_test() {
     10  do_get_profile();
     11  setPrefs({
     12    requestTimeout: 1000,
     13    retryBaseInterval: 150,
     14  });
     15  run_next_test();
     16 }
     17 
     18 add_task(async function test_register_timeout() {
     19  let handshakes = 0;
     20  let timeoutDone;
     21  let timeoutPromise = new Promise(resolve => (timeoutDone = resolve));
     22  let registers = 0;
     23 
     24  let db = PushServiceWebSocket.newPushDB();
     25  registerCleanupFunction(() => {
     26    return db.drop().then(_ => db.close());
     27  });
     28 
     29  PushServiceWebSocket._generateID = () => channelID;
     30  PushService.init({
     31    serverURI: "wss://push.example.org/",
     32    db,
     33    makeWebSocket(uri) {
     34      return new MockWebSocket(uri, {
     35        onHello(request) {
     36          if (registers > 0) {
     37            equal(
     38              request.uaid,
     39              userAgentID,
     40              "Should include device ID on reconnect with subscriptions"
     41            );
     42          } else {
     43            ok(
     44              !request.uaid,
     45              "Should not send UAID in handshake without local subscriptions"
     46            );
     47          }
     48          if (handshakes > 1) {
     49            ok(false, "Unexpected reconnect attempt " + handshakes);
     50          }
     51          handshakes++;
     52          this.serverSendMsg(
     53            JSON.stringify({
     54              messageType: "hello",
     55              status: 200,
     56              uaid: userAgentID,
     57            })
     58          );
     59        },
     60        onRegister(request) {
     61          equal(
     62            request.channelID,
     63            channelID,
     64            "Wrong channel ID in register request"
     65          );
     66          // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     67          setTimeout(() => {
     68            // Should ignore replies for timed-out requests.
     69            this.serverSendMsg(
     70              JSON.stringify({
     71                messageType: "register",
     72                status: 200,
     73                channelID,
     74                uaid: userAgentID,
     75                pushEndpoint: "https://example.com/update/timeout",
     76              })
     77            );
     78            registers++;
     79            timeoutDone();
     80          }, 2000);
     81        },
     82      });
     83    },
     84  });
     85 
     86  await Assert.rejects(
     87    PushService.register({
     88      scope: "https://example.net/page/timeout",
     89      originAttributes: ChromeUtils.originAttributesToSuffix({
     90        inIsolatedMozBrowser: false,
     91      }),
     92    }),
     93    /Registration error/,
     94    "Expected error for request timeout"
     95  );
     96 
     97  let record = await db.getByKeyID(channelID);
     98  ok(!record, "Should not store records for timed-out responses");
     99 
    100  await timeoutPromise;
    101  equal(registers, 1, "Should not handle timed-out register requests");
    102 });