summaryrefslogtreecommitdiffstats
path: root/browser/components/originattributes/test/browser/browser_imageCacheIsolation.js
blob: 8fee4a004389332a6f0aa611327dfe64196f4390 (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
/*
 * Bug 1264572 - A test case for image cache isolation.
 */

requestLongerTimeout(2);

let { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

const NUM_ISOLATION_LOADS = 2;
const NUM_CACHED_LOADS = 1;

let gHits = 0;

let server = new HttpServer();
server.registerPathHandler("/image.png", imageHandler);
server.registerPathHandler("/file.html", fileHandler);
server.start(-1);

// Disable rcwn to make cache behavior deterministic.
let rcwnEnabled = Services.prefs.getBoolPref("network.http.rcwn.enabled");
Services.prefs.setBoolPref("network.http.rcwn.enabled", false);

registerCleanupFunction(() => {
  Services.prefs.setBoolPref("network.http.rcwn.enabled", rcwnEnabled);

  server.stop(() => {
    server = null;
  });
});

let BASE_URI = "http://localhost:" + server.identity.primaryPort;
let IMAGE_URI = BASE_URI + "/image.png";
let FILE_URI = BASE_URI + "/file.html";

function imageHandler(metadata, response) {
  info("XXX: loading image from server");
  gHits++;
  response.setHeader("Cache-Control", "max-age=10000", false);
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "image/png", false);
  var body = atob(
    "iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAIAAADZSiLoAAAAEUlEQVQImWP4z8AAQTAamQkAhpcI+DeMzFcAAAAASUVORK5CYII="
  );
  response.bodyOutputStream.write(body, body.length);
}

function fileHandler(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/html", false);
  let body = `<html><body><image src=${IMAGE_URI}></body></html>`;
  response.bodyOutputStream.write(body, body.length);
}

async function doBefore() {
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.partition.network_state", false]],
  });

  // reset hit counter
  info("XXX resetting gHits");
  gHits = 0;
  info("XXX clearing image cache");
  let imageCache = Cc["@mozilla.org/image/tools;1"]
    .getService(Ci.imgITools)
    .getImgCacheForDocument(null);
  imageCache.clearCache(true);
  imageCache.clearCache(false);
  info("XXX clearning network cache");
  Services.cache2.clear();
}

// the test function does nothing on purpose.
function doTest(aBrowser) {
  return 0;
}

// the check function
function doCheck(shouldIsolate, a, b) {
  // if we're doing first party isolation and the image cache isolation is
  // working, then gHits should be 2 because the image would have been loaded
  // one per first party domain.  if first party isolation is disabled, then
  // gHits should be 1 since there would be one image load from the server and
  // one load from the image cache.
  info(`XXX check: gHits == ${gHits}, shouldIsolate == ${shouldIsolate}`);
  return shouldIsolate
    ? gHits == NUM_ISOLATION_LOADS
    : gHits == NUM_CACHED_LOADS;
}

IsolationTestTools.runTests(FILE_URI, doTest, doCheck, doBefore);