summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_staticPartition_cache.js
blob: 9afe3180fba839f80a4c381b5ceb647aca24ffd0 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

const cacheURL =
  "http://example.org/browser/browser/components/originattributes/test/browser/file_cache.html";

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

async function checkCache(suffixes, originAttributes) {
  const loadContextInfo = Services.loadContextInfo.custom(
    false,
    originAttributes
  );

  const data = await new Promise(resolve => {
    let cacheEntries = [];
    let cacheVisitor = {
      onCacheStorageInfo() {},
      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);
  });

  for (let suffix of suffixes) {
    let foundEntryCount = countMatchingCacheEntries(
      data,
      "example.net",
      suffix
    );
    ok(
      foundEntryCount > 0,
      `Cache entries expected for ${suffix} and OA=${JSON.stringify(
        originAttributes
      )}`
    );
  }
}

add_task(async function () {
  info("Disable predictor and accept all");
  await SpecialPowers.pushPrefEnv({
    set: [
      ["network.predictor.enabled", false],
      ["network.predictor.enable-prefetch", false],
      ["network.cookie.cookieBehavior", 0],
    ],
  });

  const tests = [
    {
      prefValue: true,
      originAttributes: { partitionKey: "(http,example.org)" },
    },
    {
      prefValue: false,
      originAttributes: {},
    },
  ];

  for (let test of tests) {
    info("Clear image and network caches");
    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
    Services.cache2.clear();

    info("Enabling network state partitioning");
    await SpecialPowers.pushPrefEnv({
      set: [["privacy.partition.network_state", test.prefValue]],
    });

    info("Let's load a page to populate some entries");
    let tab = (gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser));
    BrowserTestUtils.startLoadingURIString(tab.linkedBrowser, cacheURL);
    await BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, cacheURL);

    let argObj = {
      randomSuffix: Math.random(),
      urlPrefix:
        "http://example.net/browser/browser/components/originattributes/test/browser/",
    };

    await SpecialPowers.spawn(
      tab.linkedBrowser,
      [argObj],
      async function (arg) {
        // The CSS cache needs to be cleared in-process.
        content.windowUtils.clearSharedStyleSheetCache();

        let videoURL = arg.urlPrefix + "file_thirdPartyChild.video.ogv";
        let audioURL = arg.urlPrefix + "file_thirdPartyChild.audio.ogg";
        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");

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

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

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

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

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

          // Assign attributes for the audio element.
          audioSource.setAttribute("src", audioURL + URLSuffix);
          audioSource.setAttribute("type", "audio/ogg");

          audio.appendChild(audioSource);
          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);
        });
      }
    );

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

    info("Query the cache (maybe) partitioned cache");
    await checkCache(maybePartitionedSuffixes, test.originAttributes);

    gBrowser.removeCurrentTab();
  }
});