tor-browser

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

test_trr_idle_time.js (4400B)


      1 "use strict";
      2 
      3 const { AppConstants } = ChromeUtils.importESModule(
      4  "resource://gre/modules/AppConstants.sys.mjs"
      5 );
      6 
      7 /* import-globals-from trr_common.js */
      8 
      9 // Allow telemetry probes which may otherwise be disabled for some
     10 // applications (e.g. Thunderbird).
     11 Services.prefs.setBoolPref(
     12  "toolkit.telemetry.testing.overrideProductsCheck",
     13  true
     14 );
     15 
     16 let trrServer;
     17 add_setup(async function setup() {
     18  trr_test_setup();
     19 
     20  registerCleanupFunction(async () => {
     21    if (trrServer) {
     22      await trrServer.stop();
     23    }
     24    trr_clear_prefs();
     25  });
     26 });
     27 
     28 async function createAndStartServer() {
     29  trrServer = new TRRServer();
     30  await trrServer.start();
     31 
     32  await trrServer.registerDoHAnswers("testdomain.com", "A", {
     33    answers: [
     34      {
     35        name: "testdomain.com",
     36        ttl: 55,
     37        type: "A",
     38        flush: false,
     39        data: "5.5.5.5",
     40      },
     41    ],
     42  });
     43 
     44  Services.prefs.setCharPref(
     45    "network.trr.uri",
     46    `https://foo.example.com:${trrServer.port()}/dns-query`
     47  );
     48  // Disable backup connection
     49  Services.prefs.setBoolPref("network.dns.disableIPv6", true);
     50 
     51  Services.prefs.setIntPref("network.trr.mode", Ci.nsIDNSService.MODE_TRRONLY);
     52 }
     53 
     54 add_task(async function test_idle_telemetry() {
     55  await createAndStartServer();
     56  Services.dns.clearCache(true);
     57  Services.fog.testResetFOG();
     58  await new TRRDNSListener("testdomain.com", { expectedAnswer: "5.5.5.5" });
     59 
     60  let timeout = 2; // 2 seconds
     61  await new Promise(resolve => do_timeout(timeout * 1000, resolve));
     62 
     63  // gracefully shut down the server
     64  await trrServer.execute(`
     65      global.sessions.forEach(session => {
     66        session.close();
     67      });
     68      global.server.close()
     69    `);
     70 
     71  // Small timeout to make sure telemetry gets recorded
     72  await new Promise(resolve => do_timeout(1000, resolve));
     73  // Kill the server.
     74  await trrServer.stop();
     75 
     76  let distr = await Glean.network.trrIdleCloseTimeH2.other.testGetValue();
     77  Assert.equal(distr.count, 1, "just one connection being killed");
     78  Assert.greaterOrEqual(
     79    distr.sum,
     80    timeout * 1000000000,
     81    "should be slightly longer than the timeout. Note timeout is in microseconds"
     82  );
     83  Assert.less(
     84    distr.sum,
     85    timeout * 1000000000 * 1.2,
     86    "Shouldn't be much longer than the timeout."
     87  );
     88 
     89  // Test again, but this time kill connections. No idle connection telemetry should be recorded.
     90  await createAndStartServer();
     91  Services.dns.clearCache(true);
     92  Services.fog.testResetFOG();
     93  await new TRRDNSListener("testdomain.com", { expectedAnswer: "5.5.5.5" });
     94 
     95  Services.obs.notifyObservers(null, "net:cancel-all-connections");
     96  // Small timeout to make sure telemetry gets recorded
     97  await new Promise(resolve => do_timeout(1000, resolve));
     98  // No telemetry should be recorded, since this is not a clean shutdown.
     99  Assert.equal(
    100    await Glean.network.trrIdleCloseTimeH2.other.testGetValue(),
    101    null
    102  );
    103 });
    104 
    105 // No http3 server on Android 🙁
    106 add_task(
    107  { skip_if: () => AppConstants.platform == "android" },
    108  async function test_idle_telemetry_http3() {
    109    Services.prefs.setBoolPref("network.trr.useGET", false);
    110    let h3port = await create_h3_server();
    111    Assert.ok(Number.isInteger(h3port));
    112 
    113    // Disable the second DoH request for looking up AAAA record.
    114    Services.prefs.setBoolPref("network.dns.disableIPv6", true);
    115    Services.prefs.setCharPref(
    116      "network.trr.uri",
    117      `https://foo.example.com/closeafter1000ms`
    118    );
    119    Services.prefs.setCharPref(
    120      "network.http.http3.alt-svc-mapping-for-testing",
    121      `foo.example.com;h3=:${h3port}`
    122    );
    123    Services.prefs.setIntPref(
    124      "network.trr.mode",
    125      Ci.nsIDNSService.MODE_TRRONLY
    126    );
    127 
    128    Services.dns.clearCache(true);
    129    Services.fog.testResetFOG();
    130    await new TRRDNSListener("testdomain2.com", { expectedSuccess: false });
    131 
    132    let timeout = 1;
    133    // Gets closed after 1 second, but wait one more.
    134    await new Promise(resolve => do_timeout((timeout + 1) * 1000, resolve));
    135    let distr = await Glean.network.trrIdleCloseTimeH3.other.testGetValue();
    136    Assert.equal(distr.count, 1, "just one connection being killed");
    137    Assert.greaterOrEqual(
    138      distr.sum,
    139      timeout * 1000000000,
    140      "should be slightly longer than the timeout. Note timeout is in microseconds"
    141    );
    142    Assert.less(
    143      distr.sum,
    144      timeout * 1000000000 * 1.2,
    145      "Shouldn't be much longer than the timeout."
    146    );
    147  }
    148 );