summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cleardata/tests/browser/browser_preflight_cache.js
blob: 8973cc981c7a605b5b3b1cf88f8be01ae7a79728 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

const uuidGenerator = Services.uuid;

const ORIGIN_A = "https://example.net";
const ORIGIN_B = "https://example.org";

const PREFLIGHT_URL_PATH =
  "/browser/toolkit/components/cleardata/tests/browser/file_cors_preflight.sjs";

const PREFLIGHT_URL_A = ORIGIN_A + PREFLIGHT_URL_PATH;
const PREFLIGHT_URL_B = ORIGIN_B + PREFLIGHT_URL_PATH;

function testPreflightCached(browser, url, token, isCached) {
  return SpecialPowers.spawn(
    browser,
    [url, token, isCached],
    async (url, token, isCached) => {
      let response = await content.fetch(
        new content.Request(`${url}?token=${token}`, {
          mode: "cors",
          method: "GET",
          headers: [["x-test-header", "check"]],
        })
      );

      let expected = isCached ? "0" : "1";
      is(
        await response.text(),
        expected,
        `Preflight cache for ${url} ${isCached ? "HIT" : "MISS"}.`
      );
    }
  );
}

async function testDeleteAll(
  clearDataFlag,
  { deleteBy = "all", hasUserInput = false } = {}
) {
  await BrowserTestUtils.withNewTab("https://example.com", async browser => {
    let token = uuidGenerator.generateUUID().toString();

    // Populate the preflight cache.
    await testPreflightCached(browser, PREFLIGHT_URL_A, token, false);
    await testPreflightCached(browser, PREFLIGHT_URL_B, token, false);
    // Cache should be populated.
    await testPreflightCached(browser, PREFLIGHT_URL_A, token, true);
    await testPreflightCached(browser, PREFLIGHT_URL_B, token, true);

    await new Promise(resolve => {
      if (deleteBy == "principal") {
        Services.clearData.deleteDataFromPrincipal(
          browser.contentPrincipal,
          hasUserInput,
          clearDataFlag,
          value => {
            Assert.equal(value, 0);
            resolve();
          }
        );
      } else if (deleteBy == "baseDomain") {
        Services.clearData.deleteDataFromBaseDomain(
          browser.contentPrincipal.baseDomain,
          hasUserInput,
          clearDataFlag,
          value => {
            Assert.equal(value, 0);
            resolve();
          }
        );
      } else {
        Services.clearData.deleteData(clearDataFlag, value => {
          Assert.equal(value, 0);
          resolve();
        });
      }
    });

    // The preflight cache cleaner cannot delete by principal or baseDomain
    // (Bug 1727141). If this method is called, it will check whether the used
    // requested the clearing. If the user requested clearing, it will
    // over-clear (clear all data). If the request didn't come from the user,
    // for example from the PurgeTrackerService, it will not clear anything to
    // avoid clearing storage unrelated to the baseDomain or principal.
    let clearedAll = deleteBy == "all" || hasUserInput;

    // Cache should be cleared.
    await testPreflightCached(browser, PREFLIGHT_URL_A, token, !clearedAll);
    await testPreflightCached(browser, PREFLIGHT_URL_B, token, !clearedAll);
  });

  SiteDataTestUtils.clear();
}

add_task(async function test_deleteAll() {
  // The cleaner should be called when we target all cleaners, all cache
  // cleaners, or just the preflight cache.
  let { CLEAR_ALL, CLEAR_ALL_CACHES, CLEAR_PREFLIGHT_CACHE } =
    Ci.nsIClearDataService;

  for (let flag of [CLEAR_ALL, CLEAR_ALL_CACHES, CLEAR_PREFLIGHT_CACHE]) {
    await testDeleteAll(flag);
  }
});

add_task(async function test_deleteByPrincipal() {
  // The cleaner should be called when we target all cleaners, all cache
  // cleaners, or just the preflight cache.
  let { CLEAR_ALL, CLEAR_ALL_CACHES, CLEAR_PREFLIGHT_CACHE } =
    Ci.nsIClearDataService;

  for (let flag of [CLEAR_ALL, CLEAR_ALL_CACHES, CLEAR_PREFLIGHT_CACHE]) {
    for (let hasUserInput of [true, false]) {
      await testDeleteAll(flag, { deleteBy: "principal", hasUserInput });
    }
  }
});

add_task(async function test_deletePrivateBrowsingCache() {
  async function deletePrivateBrowsingCache(token) {
    const browser = await BrowserTestUtils.openNewBrowserWindow({
      private: true,
    });

    const tab = (browser.gBrowser.selectedTab = BrowserTestUtils.addTab(
      browser.gBrowser,
      "https://example.com"
    ));
    await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

    // Populate the preflight cache and make sure it isn't populated right now
    await testPreflightCached(tab.linkedBrowser, PREFLIGHT_URL_A, token, false);
    await testPreflightCached(tab.linkedBrowser, PREFLIGHT_URL_B, token, false);
    // Cache should be populated.
    await testPreflightCached(tab.linkedBrowser, PREFLIGHT_URL_A, token, true);
    await testPreflightCached(tab.linkedBrowser, PREFLIGHT_URL_B, token, true);

    await browser.close();
  }

  // Disable https_first mode to not upgrade the connection of the main page
  // and get "Blocked loading mixed active content" for the CORS request
  // making this test case fail. Another solution would be to change all URLs
  // to https.
  await SpecialPowers.pushPrefEnv({
    set: [["dom.security.https_first_pbm", false]],
  });

  let token = uuidGenerator.generateUUID().toString();

  // Make sure the CORS preflight cache is cleared between two private
  // browsing sessions. Calling this function twice to see if the cache isn't
  // populated anymore after the first call.
  await deletePrivateBrowsingCache(token);
  await deletePrivateBrowsingCache(token);

  await SpecialPowers.clearUserPref("dom.security.https_first_pbm");
});