summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_localStorageEvents.js
blob: b7abad2d941e28422e09e6be21b3b7c730cb63be (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
add_task(async function () {
  info("Starting subResources test");

  await SpecialPowers.flushPrefEnv();
  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.storage_access.enabled", true],
      [
        "network.cookie.cookieBehavior",
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
      ],
      [
        "network.cookie.cookieBehavior.pbmode",
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
      ],
      ["privacy.trackingprotection.enabled", false],
      ["privacy.trackingprotection.pbmode.enabled", false],
      ["privacy.trackingprotection.annotate_channels", true],
    ],
  });

  await UrlClassifierTestUtils.addTestTrackers();
});

add_task(async function testLocalStorageEventPropagation() {
  info("Creating a new tab");
  let tab = BrowserTestUtils.addTab(gBrowser, TEST_TOP_PAGE);
  gBrowser.selectedTab = tab;

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  info("Loading tracking scripts");
  await SpecialPowers.spawn(
    browser,
    [
      {
        page: TEST_3RD_PARTY_DOMAIN + TEST_PATH + "localStorage.html",
      },
    ],
    async obj => {
      info("Creating tracker iframe");

      let ifr = content.document.createElement("iframe");
      ifr.src = obj.page;

      await new content.Promise(resolve => {
        ifr.onload = function () {
          resolve();
        };
        content.document.body.appendChild(ifr);
      });

      info("LocalStorage should be blocked.");
      await new content.Promise(resolve => {
        content.addEventListener(
          "message",
          e => {
            if (e.data.type == "test") {
              is(e.data.status, false, "LocalStorage blocked");
            } else {
              ok(false, "Unknown message");
            }
            resolve();
          },
          { once: true }
        );
        ifr.contentWindow.postMessage("test", "*");
      });

      info("Let's open the popup");
      await new content.Promise(resolve => {
        content.addEventListener(
          "message",
          e => {
            if (e.data.type == "test") {
              is(e.data.status, true, "LocalStorage unblocked");
            } else {
              ok(false, "Unknown message");
            }
            resolve();
          },
          { once: true }
        );
        ifr.contentWindow.postMessage("open", "*");
      });
    }
  );

  info("Removing the tab");
  BrowserTestUtils.removeTab(tab);

  info("Cleaning up.");
  await new Promise(resolve => {
    Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
      resolve()
    );
  });
});

add_task(async function testBlockedLocalStorageEventPropagation() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "privacy.restrict3rdpartystorage.userInteractionRequiredForHosts",
        "tracking.example.com,tracking.example.org",
      ],
    ],
  });

  info("Creating a new tab");
  let tab = BrowserTestUtils.addTab(gBrowser, TEST_TOP_PAGE);
  gBrowser.selectedTab = tab;

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  info("Loading tracking scripts");
  await SpecialPowers.spawn(
    browser,
    [
      {
        page: TEST_3RD_PARTY_DOMAIN + TEST_PATH + "localStorage.html",
      },
    ],
    async obj => {
      info("Creating tracker iframe");

      let ifr = content.document.createElement("iframe");
      ifr.src = obj.page;

      await new content.Promise(resolve => {
        ifr.onload = function () {
          resolve();
        };
        content.document.body.appendChild(ifr);
      });

      info("LocalStorage should be blocked.");
      await new content.Promise(resolve => {
        content.addEventListener(
          "message",
          e => {
            if (e.data.type == "test") {
              is(e.data.status, false, "LocalStorage blocked");
            } else {
              ok(false, "Unknown message");
            }
            resolve();
          },
          { once: true }
        );
        ifr.contentWindow.postMessage("test", "*");
      });

      info("Let's open the popup");
      await new content.Promise(resolve => {
        content.addEventListener(
          "message",
          e => {
            if (e.data.type == "test") {
              is(e.data.status, false, "LocalStorage still blocked");
            } else {
              ok(false, "Unknown message");
            }
            resolve();
          },
          { once: true }
        );
        ifr.contentWindow.postMessage("open and test", "*");
      });
    }
  );

  info("Removing the tab");
  BrowserTestUtils.removeTab(tab);

  UrlClassifierTestUtils.cleanupTestTrackers();

  info("Cleaning up.");
  await new Promise(resolve => {
    Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
      resolve()
    );
  });
});