summaryrefslogtreecommitdiffstats
path: root/modules/libjar/test/unit/test_empty_jar_telemetry.js
blob: f63b4028d7dd0a286617df49aae7b35b9752d355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");

const { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);

const nsIBinaryInputStream = Components.Constructor(
  "@mozilla.org/binaryinputstream;1",
  "nsIBinaryInputStream",
  "setInputStream"
);

// Enable the collection (during test) for all products so even products
// that don't collect the data will be able to run the test without failure.
Services.prefs.setBoolPref(
  "toolkit.telemetry.testing.overrideProductsCheck",
  true
);

Services.prefs.setBoolPref("network.jar.record_failure_reason", true);

const fileBase = "test_empty_file.zip";
const file = do_get_file("data/" + fileBase);
const tmpDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
var copy;

function setup() {
  copy = tmpDir.clone();
  copy.append("zzdxphd909dbc6r2bxtqss2m000017");
  copy.append("zzdxphd909dbc6r2bxtqss2m000017");
  copy.append(fileBase);
  file.copyTo(copy.parent, copy.leafName);
}

setup();

registerCleanupFunction(async () => {
  Services.prefs.clearUserPref("network.jar.record_failure_reason");
  try {
    copy.remove(false);
  } catch (e) {}
});

function Listener(callback) {
  this._callback = callback;
}
Listener.prototype = {
  gotStartRequest: false,
  available: -1,
  gotStopRequest: false,
  QueryInterface: ChromeUtils.generateQI(["nsIRequestObserver"]),
  onDataAvailable(request, stream, offset, count) {
    try {
      this.available = stream.available();
      Assert.equal(this.available, count);
      // Need to consume stream to avoid assertion
      new nsIBinaryInputStream(stream).readBytes(count);
    } catch (ex) {
      do_throw(ex);
    }
  },
  onStartRequest(request) {
    this.gotStartRequest = true;
  },
  onStopRequest(request, status) {
    this.gotStopRequest = true;
    Assert.equal(status, 0);
    if (this._callback) {
      this._callback.call(null, this);
    }
  },
};

const TELEMETRY_EVENTS_FILTERS = {
  category: "zero_byte_load",
  method: "load",
};

function makeChan() {
  var uri = "jar:" + Services.io.newFileURI(copy).spec + "!/test.txt";
  var chan = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true });
  return chan;
}

add_task(async function test_empty_jar_file_async() {
  var chan = makeChan();

  Services.telemetry.setEventRecordingEnabled("zero_byte_load", true);
  Services.telemetry.clearEvents();

  await new Promise(resolve => {
    chan.asyncOpen(
      new Listener(function (l) {
        Assert.ok(chan.contentLength == 0);
        resolve();
      })
    );
  });

  TelemetryTestUtils.assertEvents(
    [
      {
        category: "zero_byte_load",
        method: "load",
        object: "others",
        value: null,
        extra: {
          sync: "false",
          file_name: `${fileBase}!/test.txt`,
          status: "NS_OK",
          cancelled: "false",
        },
      },
    ],
    TELEMETRY_EVENTS_FILTERS
  );
});

add_task(async function test_empty_jar_file_sync() {
  var chan = makeChan();

  Services.telemetry.setEventRecordingEnabled("zero_byte_load", true);
  Services.telemetry.clearEvents();

  await new Promise(resolve => {
    let stream = chan.open();
    Assert.equal(stream.available(), 0);
    resolve();
  });

  TelemetryTestUtils.assertEvents(
    [
      {
        category: "zero_byte_load",
        method: "load",
        object: "others",
        value: null,
        extra: {
          sync: "true",
          file_name: `${fileBase}!/test.txt`,
          status: "NS_OK",
          cancelled: "false",
        },
      },
    ],
    TELEMETRY_EVENTS_FILTERS
  );
});