summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_emailtracking.js
blob: 90616aba6d9c72a9a748456ee103d2dbd0f601a5 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

add_setup(async function () {
  // Disable other tracking protection feature to avoid interfering with the
  // current test. This also setup prefs for testing email tracking.
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.trackingprotection.enabled", false],
      ["privacy.trackingprotection.pbmode.enabled", false],
      ["privacy.trackingprotection.annotate_channels", false],
      ["privacy.trackingprotection.cryptomining.enabled", false],
      ["privacy.trackingprotection.emailtracking.enabled", true],
      ["privacy.trackingprotection.fingerprinting.enabled", false],
      ["privacy.trackingprotection.socialtracking.enabled", false],
      [
        "urlclassifier.features.emailtracking.blocklistTables",
        "mochitest5-track-simple",
      ],
      ["urlclassifier.features.emailtracking.allowlistTables", ""],
      [
        "urlclassifier.features.emailtracking.datacollection.blocklistTables",
        "mochitest5-track-simple",
      ],
      [
        "urlclassifier.features.emailtracking.datacollection.allowlistTables",
        "",
      ],
    ],
  });

  await UrlClassifierTestUtils.addTestTrackers();

  registerCleanupFunction(_ => {
    UrlClassifierTestUtils.cleanupTestTrackers();
  });
});

function runTest(obj) {
  add_task(async _ => {
    info("Test: " + obj.testName);

    await SpecialPowers.pushPrefEnv({
      set: [
        [
          "privacy.trackingprotection.emailtracking.enabled",
          obj.protectionEnabled,
        ],
        [
          "privacy.trackingprotection.emailtracking.pbmode.enabled",
          obj.protectionPrivateEnabled,
        ],
      ],
    });

    let win;

    if (obj.testPrivate) {
      win = await BrowserTestUtils.openNewBrowserWindow({ private: true });
    } else {
      win = window;
    }

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

    info("The non-tracker page opens an email tracker iframe");
    await SpecialPowers.spawn(
      browser,
      [
        {
          image: TEST_EMAIL_TRACKER_DOMAIN + TEST_PATH + "raptor.jpg",
          script: TEST_EMAIL_TRACKER_DOMAIN + TEST_PATH + "empty.js",
          loading: obj.loading,
        },
      ],
      async obj => {
        info("Image loading ...");
        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, "Image loading expected");

        let script = content.document.createElement("script");
        script.setAttribute("src", obj.script);

        info("Script loading ...");
        loading = await new content.Promise(resolve => {
          script.onload = _ => resolve(true);
          script.onerror = _ => resolve(false);
          content.document.body.appendChild(script);
        });

        is(loading, obj.loading, "Script loading expected");
      }
    );

    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_EMAIL_TRACKER_DOMAIN,
        "Correct tracker origin must be reported"
      );
      Assert.deepEqual(
        contentBlockingLog[origin],
        obj.expectedLogItems,
        "Content blocking log should be as expected"
      );
    }

    BrowserTestUtils.removeTab(tab);
    if (obj.testPrivate) {
      await BrowserTestUtils.closeWindow(win);
    }
    await SpecialPowers.popPrefEnv();
  });
}

runTest({
  testName:
    "EmailTracking-dataCollection feature enabled but not considered for tracking detection.",
  protectionEnabled: false,
  protectionPrivateEnabled: false,
  loading: true,
  expectedLogItems: [
    [
      Ci.nsIWebProgressListener.STATE_LOADED_EMAILTRACKING_LEVEL_1_CONTENT,
      true,
      2,
    ],
  ],
});

runTest({
  testName: "Emailtracking-protection feature enabled.",
  protectionEnabled: true,
  protectionPrivateEnabled: true,
  loading: false,
  expectedLogItems: [
    [Ci.nsIWebProgressListener.STATE_BLOCKED_EMAILTRACKING_CONTENT, true, 2],
  ],
});

runTest({
  testName:
    "Emailtracking-protection feature enabled for private windows and doesn't block in normal windows",
  protectionEnabled: false,
  protectionPrivateEnabled: true,
  loading: true,
  expectedLogItems: [
    [
      Ci.nsIWebProgressListener.STATE_LOADED_EMAILTRACKING_LEVEL_1_CONTENT,
      true,
      2,
    ],
  ],
});

runTest({
  testName:
    "Emailtracking-protection feature enabled for private windows and block in private windows",
  testPrivate: true,
  protectionEnabled: true,
  protectionPrivateEnabled: true,
  loading: false,
  expectedLogItems: [
    [Ci.nsIWebProgressListener.STATE_BLOCKED_EMAILTRACKING_CONTENT, true, 1],
  ],
});