summaryrefslogtreecommitdiffstats
path: root/browser/components/originattributes/test/browser/browser_sanitize.js
blob: 61d236f2494ee79a0b1895e67437778368c854c5 (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
/**
 * Bug 1270338 - Add a mochitest to ensure Sanitizer clears data for all containers
 */

if (SpecialPowers.useRemoteSubframes) {
  requestLongerTimeout(4);
}

const CC = Components.Constructor;

const TEST_DOMAIN = "https://example.net/";

async function setCookies(aBrowser) {
  await SpecialPowers.spawn(aBrowser, [], function () {
    content.document.cookie = "key=value";
  });
}

function cacheDataForContext(loadContextInfo) {
  return new Promise(resolve => {
    let cachedURIs = [];
    let cacheVisitor = {
      onCacheStorageInfo(num, consumption) {},
      onCacheEntryInfo(uri, idEnhance) {
        cachedURIs.push(uri.asciiSpec);
      },
      onCacheEntryVisitCompleted() {
        resolve(cachedURIs);
      },
      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);
  });
}

async function checkCookiesSanitized(aBrowser) {
  await SpecialPowers.spawn(aBrowser, [], function () {
    Assert.equal(
      content.document.cookie,
      "",
      "Cookies of all origin attributes should be cleared."
    );
  });
}

function checkCacheExists(aShouldExist) {
  return async function () {
    let loadContextInfos = [
      Services.loadContextInfo.default,
      Services.loadContextInfo.custom(false, { userContextId: 1 }),
      Services.loadContextInfo.custom(false, { userContextId: 2 }),
      Services.loadContextInfo.custom(false, {
        firstPartyDomain: "example.com",
      }),
      Services.loadContextInfo.custom(false, {
        firstPartyDomain: "example.org",
      }),
    ];
    let i = 0;
    for (let loadContextInfo of loadContextInfos) {
      let cacheURIs = await cacheDataForContext(loadContextInfo);
      is(
        cacheURIs.includes(TEST_DOMAIN),
        aShouldExist,
        TEST_DOMAIN +
          " should " +
          (aShouldExist ? "not " : "") +
          "be cached for all origin attributes." +
          i++
      );
    }
  };
}

add_setup(async function () {
  Services.cache2.clear();
});

// This will set the cookies and the cache.
IsolationTestTools.runTests(TEST_DOMAIN, setCookies, () => true);

add_task(checkCacheExists(true));

add_task(async function sanitize() {
  await Sanitizer.sanitize(["cookies", "cache"]);
});

add_task(checkCacheExists(false));
IsolationTestTools.runTests(TEST_DOMAIN, checkCookiesSanitized, () => true);