tor-browser

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

test_empty_jar_telemetry.js (3684B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/
      3 */
      4 
      5 "use strict";
      6 
      7 const { NetUtil } = ChromeUtils.importESModule(
      8  "resource://gre/modules/NetUtil.sys.mjs"
      9 );
     10 
     11 const { TelemetryTestUtils } = ChromeUtils.importESModule(
     12  "resource://testing-common/TelemetryTestUtils.sys.mjs"
     13 );
     14 
     15 const nsIBinaryInputStream = Components.Constructor(
     16  "@mozilla.org/binaryinputstream;1",
     17  "nsIBinaryInputStream",
     18  "setInputStream"
     19 );
     20 
     21 // Enable the collection (during test) for all products so even products
     22 // that don't collect the data will be able to run the test without failure.
     23 Services.prefs.setBoolPref(
     24  "toolkit.telemetry.testing.overrideProductsCheck",
     25  true
     26 );
     27 
     28 Services.prefs.setBoolPref("network.jar.record_failure_reason", true);
     29 
     30 const fileBase = "test_empty_file.zip";
     31 const file = do_get_file("data/" + fileBase);
     32 const tmpDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
     33 var copy;
     34 
     35 function setup() {
     36  copy = tmpDir.clone();
     37  copy.append("zzdxphd909dbc6r2bxtqss2m000017");
     38  copy.append("zzdxphd909dbc6r2bxtqss2m000017");
     39  copy.append(fileBase);
     40  file.copyTo(copy.parent, copy.leafName);
     41 }
     42 
     43 setup();
     44 
     45 registerCleanupFunction(async () => {
     46  Services.prefs.clearUserPref("network.jar.record_failure_reason");
     47  try {
     48    copy.remove(false);
     49  } catch (e) {}
     50 });
     51 
     52 function Listener(callback) {
     53  this._callback = callback;
     54 }
     55 Listener.prototype = {
     56  gotStartRequest: false,
     57  available: -1,
     58  gotStopRequest: false,
     59  QueryInterface: ChromeUtils.generateQI(["nsIRequestObserver"]),
     60  onDataAvailable(request, stream, offset, count) {
     61    try {
     62      this.available = stream.available();
     63      Assert.equal(this.available, count);
     64      // Need to consume stream to avoid assertion
     65      new nsIBinaryInputStream(stream).readBytes(count);
     66    } catch (ex) {
     67      do_throw(ex);
     68    }
     69  },
     70  onStartRequest() {
     71    this.gotStartRequest = true;
     72  },
     73  onStopRequest(request, status) {
     74    this.gotStopRequest = true;
     75    Assert.equal(status, 0);
     76    if (this._callback) {
     77      this._callback.call(null, this);
     78    }
     79  },
     80 };
     81 
     82 const TELEMETRY_EVENTS_FILTERS = {
     83  category: "zero_byte_load",
     84  method: "load",
     85 };
     86 
     87 function makeChan() {
     88  var uri = "jar:" + Services.io.newFileURI(copy).spec + "!/test.txt";
     89  var chan = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true });
     90  return chan;
     91 }
     92 
     93 add_task(async function test_empty_jar_file_async() {
     94  var chan = makeChan();
     95 
     96  Services.telemetry.clearEvents();
     97 
     98  await new Promise(resolve => {
     99    chan.asyncOpen(
    100      new Listener(function () {
    101        Assert.equal(chan.contentLength, 0);
    102        resolve();
    103      })
    104    );
    105  });
    106 
    107  TelemetryTestUtils.assertEvents(
    108    [
    109      {
    110        category: "zero_byte_load",
    111        method: "load",
    112        object: "others",
    113        value: null,
    114        extra: {
    115          sync: "false",
    116          file_name: `${fileBase}!/test.txt`,
    117          status: "NS_OK",
    118          cancelled: "false",
    119          cancel_reason: "",
    120        },
    121      },
    122    ],
    123    TELEMETRY_EVENTS_FILTERS
    124  );
    125 });
    126 
    127 add_task(async function test_empty_jar_file_sync() {
    128  var chan = makeChan();
    129 
    130  Services.telemetry.clearEvents();
    131 
    132  await new Promise(resolve => {
    133    let stream = chan.open();
    134    Assert.equal(stream.available(), 0);
    135    resolve();
    136  });
    137 
    138  TelemetryTestUtils.assertEvents(
    139    [
    140      {
    141        category: "zero_byte_load",
    142        method: "load",
    143        object: "others",
    144        value: null,
    145        extra: {
    146          sync: "true",
    147          file_name: `${fileBase}!/test.txt`,
    148          status: "NS_OK",
    149          cancelled: "false",
    150          cancel_reason: "",
    151        },
    152      },
    153    ],
    154    TELEMETRY_EVENTS_FILTERS
    155  );
    156 });