summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/mochitest/test_emailtracking.html
blob: fd2996997cf3ea6995eed7e83f019477813bae99 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test the email tracking classifier</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>

<body>
<script class="testbody" type="text/javascript">

var tests = [
  // All disabled.
  { config: [ false, false ], loadExpected: true },

  // Just entitylisted.
  { config: [ false, true ], loadExpected: true },

  // Just blocklisted.
  { config: [ true, false ], loadExpected: false },

  // entitylist + blocklist: entitylist wins
  { config: [ true, true ], loadExpected: true },
];

function prefValue(value, what) {
  return value ? what : "";
}

async function runTest(test) {
  await SpecialPowers.pushPrefEnv({set: [
    [ "urlclassifier.features.emailtracking.blocklistHosts", prefValue(test.config[0], "example.com") ],
    [ "urlclassifier.features.emailtracking.allowlistHosts", prefValue(test.config[1], "mochi.test,mochi.xorigin-test") ],
    [ "urlclassifier.features.emailtracking.blocklistTables", prefValue(test.config[0], "mochitest5-track-simple") ],
    [ "urlclassifier.features.emailtracking.allowlistTables", "" ],
    [ "privacy.trackingprotection.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 ],
  ]});

  info("Testing: " + JSON.stringify(test.config) + "\n");

  // Let's load an image with a random query string to avoid network cache.
  let result = await new Promise(resolve => {
    let image = new Image();
    image.src = "http://example.com/tests/toolkit/components/url-classifier/tests/mochitest/raptor.jpg?" + Math.random();
    image.onload = _ => resolve(true);
    image.onerror = _ => resolve(false);
  });

  is(result, test.loadExpected, "Image loading happened correctly");

  // Let's load an image with a random query string to avoid network cache.
  result = await new Promise(resolve => {
    let image = new Image();
    image.src = "http://email-tracking.example.org/tests/toolkit/components/url-classifier/tests/mochitest/raptor.jpg?" + Math.random();
    image.onload = _ => resolve(true);
    image.onerror = _ => resolve(false);
  });

  is(result, test.loadExpected, "Image loading happened correctly (by table)");

  // Let's load a script with a random query string to avoid network cache.
  result = await new Promise(resolve => {
    let script = document.createElement("script");
    script.setAttribute(
      "src",
      "http://example.com/tests/toolkit/components/url-classifier/tests/mochitest/evil.js?" +
      Math.random()
    );
    script.onload = _ => resolve(true);
    script.onerror = _ => resolve(false);
    document.body.appendChild(script);
  });

  is(result, test.loadExpected, "Script loading happened correctly");

  // Let's load a script with a random query string to avoid network cache.
  result = await new Promise(resolve => {
    let script = document.createElement("script");
    script.setAttribute(
      "src",
      "http://email-tracking.example.org/tests/toolkit/components/url-classifier/tests/mochitest/evil.js?" +
      Math.random()
    );
    script.onload = _ => resolve(true);
    script.onerror = _ => resolve(false);
    document.body.appendChild(script);
  });

  is(result, test.loadExpected, "Script loading happened correctly (by table)");
}

async function runTests() {
  let chromeScript = SpecialPowers.loadChromeScript(_ => {
    /* eslint-env mozilla/chrome-script */
    const {UrlClassifierTestUtils} = ChromeUtils.importESModule(
      "resource://testing-common/UrlClassifierTestUtils.sys.mjs"
    );

    addMessageListener("loadTrackers", __ => {
      return UrlClassifierTestUtils.addTestTrackers();
    });

    addMessageListener("unloadTrackers", __ => {
      UrlClassifierTestUtils.cleanupTestTrackers();
    });
  });

  await chromeScript.sendQuery("loadTrackers");

  for (let test in tests) {
    await runTest(tests[test]);
  }

  await chromeScript.sendQuery("unloadTrackers");

  chromeScript.destroy();
  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
runTests();

</script>
</body>
</html>