tor-browser

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

test_trr_telemetry.js (3098B)


      1 "use strict";
      2 
      3 /* import-globals-from trr_common.js */
      4 
      5 // Allow telemetry probes which may otherwise be disabled for some
      6 // applications (e.g. Thunderbird).
      7 Services.prefs.setBoolPref(
      8  "toolkit.telemetry.testing.overrideProductsCheck",
      9  true
     10 );
     11 
     12 const { TelemetryTestUtils } = ChromeUtils.importESModule(
     13  "resource://testing-common/TelemetryTestUtils.sys.mjs"
     14 );
     15 
     16 let trrServer;
     17 add_setup(async function setup() {
     18  trr_test_setup();
     19  Services.prefs.setBoolPref("network.trr.useGET", false);
     20 
     21  trrServer = new TRRServer();
     22  await trrServer.start();
     23  h2Port = trrServer.port();
     24 });
     25 
     26 registerCleanupFunction(async () => {
     27  trr_clear_prefs();
     28  if (trrServer) {
     29    await trrServer.stop();
     30  }
     31 });
     32 
     33 async function trrLookup(mode, rolloutMode) {
     34  let hist = TelemetryTestUtils.getAndClearKeyedHistogram(
     35    "TRR_SKIP_REASON_TRR_FIRST2"
     36  );
     37 
     38  if (rolloutMode) {
     39    info("Testing doh-rollout.mode");
     40    setModeAndURI(0, "doh?responseIP=2.2.2.2");
     41    Services.prefs.setIntPref("doh-rollout.mode", rolloutMode);
     42  } else {
     43    setModeAndURI(mode, "doh?responseIP=2.2.2.2");
     44  }
     45 
     46  Services.dns.clearCache(true);
     47  await new TRRDNSListener("test.example.com", "2.2.2.2");
     48  let expectedKey = `(other)_${mode}`;
     49  if (mode == 0) {
     50    expectedKey = "(other)";
     51  }
     52 
     53  let snapshot = hist.snapshot();
     54  await TestUtils.waitForCondition(() => {
     55    snapshot = hist.snapshot();
     56    info("snapshot:" + JSON.stringify(snapshot));
     57    return snapshot;
     58  });
     59  TelemetryTestUtils.assertKeyedHistogramValue(
     60    hist,
     61    expectedKey,
     62    Ci.nsITRRSkipReason.TRR_OK,
     63    1
     64  );
     65 }
     66 
     67 add_task(async function test_trr_lookup_mode_2() {
     68  await trrLookup(Ci.nsIDNSService.MODE_TRRFIRST);
     69 });
     70 
     71 add_task(async function test_trr_lookup_mode_3() {
     72  await trrLookup(Ci.nsIDNSService.MODE_TRRONLY);
     73 });
     74 
     75 add_task(async function test_trr_lookup_mode_0() {
     76  await trrLookup(
     77    Ci.nsIDNSService.MODE_NATIVEONLY,
     78    Ci.nsIDNSService.MODE_TRRFIRST
     79  );
     80 });
     81 
     82 async function trrByTypeLookup(trrURI, expectedSuccess, expectedSkipReason) {
     83  Services.prefs.setIntPref(
     84    "doh-rollout.mode",
     85    Ci.nsIDNSService.MODE_NATIVEONLY
     86  );
     87 
     88  let hist = TelemetryTestUtils.getAndClearKeyedHistogram(
     89    "TRR_RELEVANT_SKIP_REASON_TRR_FIRST_TYPE_REC"
     90  );
     91 
     92  setModeAndURI(Ci.nsIDNSService.MODE_TRRFIRST, trrURI);
     93 
     94  Services.dns.clearCache(true);
     95  await new TRRDNSListener("test.httpssvc.com", {
     96    type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
     97    expectedSuccess,
     98  });
     99  let expectedKey = `(other)_2`;
    100 
    101  let snapshot = hist.snapshot();
    102  await TestUtils.waitForCondition(() => {
    103    snapshot = hist.snapshot();
    104    info("snapshot:" + JSON.stringify(snapshot));
    105    return snapshot;
    106  });
    107 
    108  TelemetryTestUtils.assertKeyedHistogramValue(
    109    hist,
    110    expectedKey,
    111    expectedSkipReason,
    112    1
    113  );
    114 }
    115 
    116 add_task(async function test_trr_by_type_lookup_success() {
    117  await trrByTypeLookup("doh?httpssvc=1", true, Ci.nsITRRSkipReason.TRR_OK);
    118 });
    119 
    120 add_task(async function test_trr_by_type_lookup_fail() {
    121  await trrByTypeLookup(
    122    "doh?responseIP=none",
    123    false,
    124    Ci.nsITRRSkipReason.TRR_NO_ANSWERS
    125  );
    126 });