summaryrefslogtreecommitdiffstats
path: root/dom/quota/test/xpcshell/telemetry/test_dom_quota_try.js
blob: 28bb2d63b4bb7f02e360d2725223ca331f4b2168 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

const { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);

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

// This is a list of all extra keys that are exposed to telemetry. Please only
// add things to this list with great care and proper code review from relevant
// module owner/peers and proper data review from data stewards.
const allowedExtraKeys = [
  "context",
  "frame_id",
  "process_id",
  "result",
  "seq",
  "severity",
  "source_file",
  "source_line",
  "stack_id",
];

const originSchemes = [
  "http",
  "https",
  "ftp",
  "ws",
  "wss",
  "gopher",
  "blob",
  "file",
  "data",
];

const testcases = [
  // Test temporary storage initialization with and without a broken origin
  // directory.
  {
    async setup(expectedInitResult) {
      Services.prefs.setBoolPref("dom.quotaManager.loadQuotaFromCache", false);

      let request = init();
      await requestFinished(request);

      request = initTemporaryStorage();
      await requestFinished(request);

      request = initTemporaryOrigin(
        "default",
        getPrincipal("https://example.com")
      );
      await requestFinished(request);

      const usageFile = getRelativeFile(
        "storage/default/https+++example.com/ls/usage"
      );

      if (!expectedInitResult) {
        usageFile.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
      } else {
        usageFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
      }

      // XXX It would be nice to have a method which shuts down temporary
      // storage only. Now we have to shut down entire storage (including
      // temporary storage) and then initialize storage again.
      request = reset();
      await requestFinished(request);

      request = init();
      await requestFinished(request);
    },
    initFunction: initTemporaryStorage,
    getExpectedNumberOfEvents() {
      if (AppConstants.EARLY_BETA_OR_EARLIER || AppConstants.DEBUG) {
        if (AppConstants.NIGHTLY_BUILD) {
          return {
            initFailure: 9,
            initSuccess: 0,
          };
        }

        return {
          initFailure: 14,
          initSuccess: 0,
        };
      }

      return {
        initFailure: 0,
        initSuccess: 0,
      };
    },
    async cleanup() {
      const request = clear();
      await requestFinished(request);

      Services.prefs.setBoolPref("dom.quotaManager.loadQuotaFromCache", true);
    },
  },
];

function verifyEvents(expectedNumberOfEvents) {
  const events = TelemetryTestUtils.getEvents({
    category: "dom.quota.try",
    method: "error",
  });

  is(
    events.length,
    expectedNumberOfEvents,
    "The number of events must match the expected number of events"
  );

  for (const event of events) {
    for (const extraKey in event.extra) {
      ok(
        allowedExtraKeys.includes(extraKey),
        `The extra key ${extraKey} must be in the allow list`
      );

      const extraValue = event.extra[extraKey];

      // These are extra paranoia checks to ensure extra values don't contain
      // origin like strings.
      for (const suffix of ["://", "+++"]) {
        ok(
          originSchemes.every(
            originScheme => !extraValue.includes(originScheme + suffix)
          ),
          `The extra value ${extraValue} must not contain origin like strings`
        );
      }
    }
  }
}

async function testSteps() {
  for (const testcase of testcases) {
    for (const expectedInitResult of [false, true]) {
      // Clear all events.
      Services.telemetry.clearEvents();

      info(
        `Verifying the events when the initialization ` +
          `${expectedInitResult ? "succeeds" : "fails"}`
      );

      await testcase.setup(expectedInitResult);

      const msg = `Should ${expectedInitResult ? "not " : ""} have thrown`;

      let request = testcase.initFunction();
      try {
        await requestFinished(request);
        ok(expectedInitResult, msg);
      } catch (ex) {
        ok(!expectedInitResult, msg);
      }

      const expectedNumberOfEventsObject = testcase.getExpectedNumberOfEvents
        ? testcase.getExpectedNumberOfEvents()
        : testcase.expectedNumberOfEvents;

      const expectedNumberOfEvents = expectedInitResult
        ? expectedNumberOfEventsObject.initSuccess
        : expectedNumberOfEventsObject.initFailure;

      verifyEvents(expectedNumberOfEvents);

      await testcase.cleanup();
    }
  }
}