summaryrefslogtreecommitdiffstats
path: root/browser/components/originattributes/test/browser/browser_cache.js
blob: ea8f0fe80372bf53b23cfc047f46cd954536b361 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * Bug 1264577 - A test case for testing caches of various submodules.
 *   This test case will load two pages that each page loads various resources
 *   within the same third party domain for the same originAttributes or different
 *   originAttributes. And then, it verifies the number of cache entries and
 *   the originAttributes of loading channels. If these two pages belong to
 *   the same originAttributes, the number of cache entries for a certain
 *   resource would be one. Otherwise, it would be two.
 */

const CC = Components.Constructor;

let protocolProxyService = Cc[
  "@mozilla.org/network/protocol-proxy-service;1"
].getService(Ci.nsIProtocolProxyService);

const TEST_DOMAIN = "http://example.net";
const TEST_PATH = "/browser/browser/components/originattributes/test/browser/";
const TEST_PAGE = TEST_DOMAIN + TEST_PATH + "file_cache.html";

let suffixes = [
  "iframe.html",
  "link.css",
  "script.js",
  "img.png",
  "object.png",
  "embed.png",
  "xhr.html",
  "worker.xhr.html",
  "audio.ogg",
  "video.ogv",
  "track.vtt",
  "fetch.html",
  "worker.fetch.html",
  "request.html",
  "worker.request.html",
  "import.js",
  "worker.js",
  "sharedworker.js",
  "font.woff",
];

// A random value for isolating video/audio elements across different tests.
let randomSuffix;

function clearAllImageCaches() {
  let tools = SpecialPowers.Cc["@mozilla.org/image/tools;1"].getService(
    SpecialPowers.Ci.imgITools
  );
  let imageCache = tools.getImgCacheForDocument(window.document);
  imageCache.clearCache(true); // true=chrome
  imageCache.clearCache(false); // false=content
}

function cacheDataForContext(loadContextInfo) {
  return new Promise(resolve => {
    let cacheEntries = [];
    let cacheVisitor = {
      onCacheStorageInfo(num, consumption) {},
      onCacheEntryInfo(uri, idEnhance) {
        cacheEntries.push({ uri, idEnhance });
      },
      onCacheEntryVisitCompleted() {
        resolve(cacheEntries);
      },
      QueryInterface: ChromeUtils.generateQI(["nsICacheStorageVisitor"]),
    };
    // Visiting the disk cache also visits memory storage so we do not
    // need to use Services.cache2.memoryCacheStorage() here.
    let storage = Services.cache2.diskCacheStorage(loadContextInfo);
    storage.asyncVisitStorage(cacheVisitor, true);
  });
}

let countMatchingCacheEntries = function (cacheEntries, domain, fileSuffix) {
  return cacheEntries
    .map(entry => entry.uri.asciiSpec)
    .filter(spec => spec.includes(domain))
    .filter(spec => spec.includes("file_thirdPartyChild." + fileSuffix)).length;
};

function observeChannels(onChannel) {
  // We use a dummy proxy filter to catch all channels, even those that do not
  // generate an "http-on-modify-request" notification, such as link preconnects.
  let proxyFilter = {
    applyFilter(aChannel, aProxy, aCallback) {
      // We have the channel; provide it to the callback.
      onChannel(aChannel);
      // Pass on aProxy unmodified.
      aCallback.onProxyFilterResult(aProxy);
    },
  };
  protocolProxyService.registerChannelFilter(proxyFilter, 0);
  // Return the stop() function:
  return () => protocolProxyService.unregisterChannelFilter(proxyFilter);
}

function startObservingChannels(aMode) {
  let stopObservingChannels = observeChannels(function (channel) {
    let originalURISpec = channel.originalURI.spec;
    if (originalURISpec.includes("example.net")) {
      let loadInfo = channel.loadInfo;

      switch (aMode) {
        case TEST_MODE_FIRSTPARTY:
          ok(
            loadInfo.originAttributes.firstPartyDomain === "example.com" ||
              loadInfo.originAttributes.firstPartyDomain === "example.org",
            "first party for " +
              originalURISpec +
              " is " +
              loadInfo.originAttributes.firstPartyDomain
          );
          break;

        case TEST_MODE_NO_ISOLATION:
          ok(
            ChromeUtils.isOriginAttributesEqual(
              loadInfo.originAttributes,
              ChromeUtils.fillNonDefaultOriginAttributes()
            ),
            "OriginAttributes for " + originalURISpec + " is default."
          );
          break;

        case TEST_MODE_CONTAINERS:
          ok(
            loadInfo.originAttributes.userContextId === 1 ||
              loadInfo.originAttributes.userContextId === 2,
            "userContextId for " +
              originalURISpec +
              " is " +
              loadInfo.originAttributes.userContextId
          );
          break;

        default:
          ok(false, "Unknown test mode.");
      }
    }
  });
  return stopObservingChannels;
}

let stopObservingChannels;

// The init function, which clears image and network caches, and generates
// the random value for isolating video and audio elements across different
// test runs.
async function doInit(aMode) {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["network.predictor.enabled", false],
      ["network.predictor.enable-prefetch", false],
      ["privacy.partition.network_state", false],
      ["dom.security.https_first", false],
    ],
  });
  clearAllImageCaches();

  Services.cache2.clear();

  randomSuffix = Math.random();
  stopObservingChannels = startObservingChannels(aMode);
}

// In the test function, we dynamically generate the video and audio element,
// and assign a random suffix to their URL to isolate them across different
// test runs.
async function doTest(aBrowser) {
  let argObj = {
    randomSuffix,
    urlPrefix: TEST_DOMAIN + TEST_PATH,
  };

  await SpecialPowers.spawn(aBrowser, [argObj], async function (arg) {
    content.windowUtils.clearSharedStyleSheetCache();

    let videoURL = arg.urlPrefix + "file_thirdPartyChild.video.ogv";
    let audioURL = arg.urlPrefix + "file_thirdPartyChild.audio.ogg";
    let trackURL = arg.urlPrefix + "file_thirdPartyChild.track.vtt";
    let URLSuffix = "?r=" + arg.randomSuffix;

    // Create the audio and video elements.
    let audio = content.document.createElement("audio");
    let video = content.document.createElement("video");
    let audioSource = content.document.createElement("source");
    let audioTrack = content.document.createElement("track");

    // Append the audio and track element into the body, and wait until they're finished.
    await new content.Promise(resolve => {
      let audioLoaded = false;
      let trackLoaded = false;

      let audioListener = () => {
        Assert.ok(true, `Audio suspended: ${audioURL + URLSuffix}`);
        audio.removeEventListener("suspend", audioListener);

        audioLoaded = true;
        if (audioLoaded && trackLoaded) {
          resolve();
        }
      };

      let trackListener = () => {
        Assert.ok(true, `Audio track loaded: ${audioURL + URLSuffix}`);
        audioTrack.removeEventListener("load", trackListener);

        trackLoaded = true;
        if (audioLoaded && trackLoaded) {
          resolve();
        }
      };

      Assert.ok(true, `Loading audio: ${audioURL + URLSuffix}`);

      // Add the event listeners before everything in case we lose events.
      audioTrack.addEventListener("load", trackListener);
      audio.addEventListener("suspend", audioListener);

      // Assign attributes for the audio element.
      audioSource.setAttribute("src", audioURL + URLSuffix);
      audioSource.setAttribute("type", "audio/ogg");
      audioTrack.setAttribute("src", trackURL);
      audioTrack.setAttribute("kind", "subtitles");
      audioTrack.setAttribute("default", true);

      audio.appendChild(audioSource);
      audio.appendChild(audioTrack);
      audio.autoplay = true;

      content.document.body.appendChild(audio);
    });

    // Append the video element into the body, and wait until it's finished.
    await new content.Promise(resolve => {
      let listener = () => {
        Assert.ok(true, `Video suspended: ${videoURL + URLSuffix}`);
        video.removeEventListener("suspend", listener);
        resolve();
      };

      Assert.ok(true, `Loading video: ${videoURL + URLSuffix}`);

      // Add the event listener before everything in case we lose the event.
      video.addEventListener("suspend", listener);

      // Assign attributes for the video element.
      video.setAttribute("src", videoURL + URLSuffix);
      video.setAttribute("type", "video/ogg");

      content.document.body.appendChild(video);
    });
  });

  return 0;
}

// The check function, which checks the number of cache entries.
async function doCheck(aShouldIsolate, aInputA, aInputB) {
  let expectedEntryCount = 1;
  let data = [];
  data = data.concat(
    await cacheDataForContext(Services.loadContextInfo.default)
  );
  data = data.concat(
    await cacheDataForContext(Services.loadContextInfo.private)
  );
  data = data.concat(
    await cacheDataForContext(Services.loadContextInfo.custom(true, {}))
  );
  data = data.concat(
    await cacheDataForContext(
      Services.loadContextInfo.custom(false, { userContextId: 1 })
    )
  );
  data = data.concat(
    await cacheDataForContext(
      Services.loadContextInfo.custom(true, { userContextId: 1 })
    )
  );
  data = data.concat(
    await cacheDataForContext(
      Services.loadContextInfo.custom(false, { userContextId: 2 })
    )
  );
  data = data.concat(
    await cacheDataForContext(
      Services.loadContextInfo.custom(true, { userContextId: 2 })
    )
  );
  data = data.concat(
    await cacheDataForContext(
      Services.loadContextInfo.custom(false, {
        firstPartyDomain: "example.com",
      })
    )
  );
  data = data.concat(
    await cacheDataForContext(
      Services.loadContextInfo.custom(true, { firstPartyDomain: "example.com" })
    )
  );
  data = data.concat(
    await cacheDataForContext(
      Services.loadContextInfo.custom(false, {
        firstPartyDomain: "example.org",
      })
    )
  );
  data = data.concat(
    await cacheDataForContext(
      Services.loadContextInfo.custom(true, { firstPartyDomain: "example.org" })
    )
  );

  if (aShouldIsolate) {
    expectedEntryCount = 2;
  }

  for (let suffix of suffixes) {
    let foundEntryCount = countMatchingCacheEntries(
      data,
      "example.net",
      suffix
    );
    let result = expectedEntryCount === foundEntryCount;
    ok(
      result,
      "Cache entries expected for " +
        suffix +
        ": " +
        expectedEntryCount +
        ", and found " +
        foundEntryCount
    );
  }

  stopObservingChannels();
  stopObservingChannels = undefined;
  return true;
}

let testArgs = {
  url: TEST_PAGE,
  firstFrameSetting: DEFAULT_FRAME_SETTING,
  secondFrameSetting: [TEST_TYPE_FRAME],
};

IsolationTestTools.runTests(testArgs, doTest, doCheck, doInit);