summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_socialtracking.js
blob: 1cbf5d927875afea0fd142e0fe0b0209b88e21fd (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
function runTest(obj) {
  add_task(async _ => {
    info("Test: " + obj.testName);
    await SpecialPowers.pushPrefEnv({
      set: [
        ["dom.ipc.processCount", 1],
        [
          "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],
        [
          "privacy.trackingprotection.socialtracking.enabled",
          obj.protectionEnabled,
        ],
        ["privacy.socialtracking.block_cookies.enabled", obj.cookieBlocking],
      ],
    });

    await UrlClassifierTestUtils.addTestTrackers();

    info("Creating a non-tracker top-level context");
    let tab = BrowserTestUtils.addTab(gBrowser, TEST_TOP_PAGE);
    let browser = gBrowser.getBrowserForTab(tab);
    await BrowserTestUtils.browserLoaded(browser);

    info("The non-tracker page opens a tracker iframe");
    await SpecialPowers.spawn(
      browser,
      [
        {
          page: TEST_3RD_PARTY_DOMAIN_STP + TEST_PATH + "localStorage.html",
          image: TEST_3RD_PARTY_DOMAIN_STP + TEST_PATH + "raptor.jpg",
          loading: obj.loading,
          result: obj.result,
        },
      ],
      async obj => {
        let loading = await new content.Promise(resolve => {
          let image = new content.Image();
          image.src = obj.image + "?" + Math.random();
          image.onload = _ => resolve(true);
          image.onerror = _ => resolve(false);
        });

        is(loading, obj.loading, "Loading expected");

        if (obj.loading) {
          let ifr = content.document.createElement("iframe");
          ifr.setAttribute("id", "ifr");
          ifr.setAttribute("src", obj.page);

          info("Iframe loading...");
          await new content.Promise(resolve => {
            ifr.onload = resolve;
            content.document.body.appendChild(ifr);
          });

          let p = new Promise(resolve => {
            content.addEventListener(
              "message",
              e => {
                resolve(e.data);
              },
              { once: true }
            );
          });

          info("Setting localStorage value...");
          ifr.contentWindow.postMessage("test", "*");

          info("Getting the value...");
          let value = await p;
          is(value.status, obj.result, "We expect to succeed");
        }
      }
    );

    info("Checking content blocking log.");
    let contentBlockingLog = JSON.parse(await browser.getContentBlockingLog());
    let origins = Object.keys(contentBlockingLog);
    is(origins.length, 1, "There should be one origin entry in the log.");
    for (let origin of origins) {
      is(
        origin + "/",
        TEST_3RD_PARTY_DOMAIN_STP,
        "Correct tracker origin must be reported"
      );
      Assert.deepEqual(
        contentBlockingLog[origin],
        obj.expectedLogItems,
        "Content blocking log should be as expected"
      );
    }

    BrowserTestUtils.removeTab(tab);

    UrlClassifierTestUtils.cleanupTestTrackers();
  });
}

runTest({
  testName:
    "Socialtracking-annotation feature enabled but not considered for tracking detection.",
  protectionEnabled: false,
  loading: true,
  cookieBlocking: false,
  result: true,
  expectedLogItems: [
    [Ci.nsIWebProgressListener.STATE_COOKIES_LOADED, true, 1],
    [Ci.nsIWebProgressListener.STATE_COOKIES_LOADED_SOCIALTRACKER, true, 1],
  ],
});

runTest({
  testName:
    "Socialtracking-annotation feature enabled and considered for tracking detection.",
  protectionEnabled: false,
  loading: true,
  cookieBlocking: true,
  result: false,
  expectedLogItems: [
    [Ci.nsIWebProgressListener.STATE_COOKIES_LOADED, true, 1],
    [Ci.nsIWebProgressListener.STATE_COOKIES_LOADED_SOCIALTRACKER, true, 1],
    [Ci.nsIWebProgressListener.STATE_LOADED_SOCIALTRACKING_CONTENT, true, 2],
    // We cache the storage allowed decision, so we will only get one block
    // event per window and origin.
    [Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_SOCIALTRACKER, true, 1],
  ],
});

runTest({
  testName: "Socialtracking-protection feature enabled.",
  protectionEnabled: true,
  loading: false,
  cookieBlocking: true,
  result: false,
  expectedLogItems: [
    [Ci.nsIWebProgressListener.STATE_BLOCKED_SOCIALTRACKING_CONTENT, true, 1],
  ],
});