summaryrefslogtreecommitdiffstats
path: root/devtools/client/storage/test/browser_storage_dfpi.js
blob: 14d625910ea63cf04a87ce3f5b562868b886ac1b (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Basic test to assert that the storage tree and table corresponding to each
// item in the storage tree is correctly displayed

"use strict";

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

// Ensure iframe.src in storage-dfpi.html starts with PREFIX.
const PREFIX = "https://sub1.test1.example";
const ORIGIN = `${PREFIX}.org`;
const ORIGIN_THIRD_PARTY = `${PREFIX}.com`;
const TEST_URL = `${ORIGIN}/${PATH}storage-dfpi.html`;

function listOrigins() {
  return new Promise(resolve => {
    SpecialPowers.Services.qms.listOrigins().callback = req => {
      resolve(req.result);
    };
  });
}

add_task(async function () {
  await pushPref(
    "network.cookie.cookieBehavior",
    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN
  );

  await pushPref(
    "privacy.partition.always_partition_third_party_non_cookie_storage",
    false
  );

  registerCleanupFunction(SiteDataTestUtils.clear);

  // `Services.qms.listOrigins()` may or contain results created by other tests.
  // And it's unsafe to clear existing origins by `Services.qms.clear()`.
  // In order to obtain correct results, we need to compare the results before
  // and after `openTabAndSetupStorage` is called.
  // To ensure more accurate results, try choosing a uncommon origin for PREFIX.
  const EXISTING_ORIGINS = await listOrigins();
  ok(!EXISTING_ORIGINS.includes(ORIGIN), `${ORIGIN} doesn't exist`);

  await openTabAndSetupStorage(TEST_URL);

  const origins = await listOrigins();
  for (const origin of origins) {
    ok(
      EXISTING_ORIGINS.includes(origin) || origin === ORIGIN,
      `check origin: ${origin}`
    );
  }
  ok(origins.includes(ORIGIN), `${ORIGIN} is added`);

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

async function setPartitionedStorage(browser, type, key) {
  const handler = async (storageType, storageKey, storageValue) => {
    if (storageType == "cookie") {
      content.document.cookie = `${storageKey}=${storageValue}`;
      return;
    }
    content.localStorage.setItem(storageKey, storageValue);
  };

  // Set first party storage.
  await SpecialPowers.spawn(browser, [type, key, "first"], handler);
  // Set third-party (partitioned) storage in the iframe.
  await SpecialPowers.spawn(
    browser.browsingContext.children[0],
    [type, key, "third"],
    handler
  );
}

async function checkData(storageType, key, value) {
  if (storageType == "cookie") {
    checkCookieData(key, value);
    return;
  }
  await waitForStorageData(key, value);
}

async function testPartitionedStorage(
  storageType,
  treeItemLabel = storageType
) {
  await pushPref(
    "network.cookie.cookieBehavior",
    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN
  );
  // Bug 1617611: Fix all the tests broken by "cookies SameSite=lax by default"
  await pushPref("network.cookie.sameSite.laxByDefault", false);

  info(
    "Open the test url in a new tab and add storage entries *before* opening the storage panel."
  );
  await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
    await setPartitionedStorage(browser, storageType, "contextA");
  });

  await openTabAndSetupStorage(TEST_URL);

  const doc = gPanelWindow.document;

  info("check that both hosts appear in the storage tree");
  checkTree(doc, [treeItemLabel, ORIGIN]);
  checkTree(doc, [treeItemLabel, ORIGIN_THIRD_PARTY]);

  info(
    "check that items for both first and third party host have the initial storage entries"
  );

  await selectTreeItem([treeItemLabel, ORIGIN]);
  await checkData(storageType, "contextA", "first");

  await selectTreeItem([treeItemLabel, ORIGIN_THIRD_PARTY]);
  await checkData(storageType, "contextA", "third");

  info("Add more entries while the storage panel is open");
  const onUpdated = gUI.once("store-objects-edit");
  await setPartitionedStorage(
    gBrowser.selectedBrowser,
    storageType,
    "contextB"
  );
  await onUpdated;

  info("check that both hosts appear in the storage tree");
  checkTree(doc, [treeItemLabel, ORIGIN]);
  checkTree(doc, [treeItemLabel, ORIGIN_THIRD_PARTY]);

  info(
    "check that items for both first and third party host have the updated storage entries"
  );

  await selectTreeItem([treeItemLabel, ORIGIN]);
  await checkData(storageType, "contextA", "first");
  await checkData(storageType, "contextB", "first");

  await selectTreeItem([treeItemLabel, ORIGIN_THIRD_PARTY]);
  await checkData(storageType, "contextA", "third");
  await checkData(storageType, "contextB", "third");

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
}

// Tests that partitioned storage is shown in the storage panel.

add_task(async function test_partitioned_cookies() {
  registerCleanupFunction(SiteDataTestUtils.clear);
  await testPartitionedStorage("cookie", "cookies");
});

add_task(async function test_partitioned_localStorage() {
  registerCleanupFunction(SiteDataTestUtils.clear);
  await testPartitionedStorage("localStorage");
});