summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/mochitest/test_classified_annotations.html
blob: 44a0c92549ac934a725e7714cebacbcccbd31529 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test the URI Classifier</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>

<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">

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

var mainWindow = window.browsingContext.topChromeWindow;
var contentPage = "http://mochi.test:8888/chrome/toolkit/components/url-classifier/tests/mochitest/classifiedAnnotatedFrame.html";

var Cc = SpecialPowers.Cc;
var Ci = SpecialPowers.Ci;

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

function testOnWindow() {
  return new Promise((resolve, reject) => {
    let win = mainWindow.OpenBrowserWindow();
    win.addEventListener("load", function() {
      TestUtils.topicObserved("browser-delayed-startup-finished",
                              subject => subject == win).then(() => {
        win.addEventListener("DOMContentLoaded", function onInnerLoad() {
          if (win.content.location.href != contentPage) {
            win.gBrowser.loadURI(Services.io.newURI(contentPage), {
              triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({}),
            });
            return;
          }
          win.removeEventListener("DOMContentLoaded", onInnerLoad, true);

          win.content.addEventListener("OnLoadComplete", function innerLoad2(e) {
            win.content.removeEventListener("OnLoadComplete", innerLoad2);
            SimpleTest.executeSoon(function() {
              checkLoads(win, JSON.parse(e.detail));
              resolve(win);
            });
          }, false, true);
        }, true);
        SimpleTest.executeSoon(function() {
          win.gBrowser.loadURI(Services.io.newURI(contentPage), {
            triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({}),
          });
        });
      });
    }, {capture: true, once: true});
  });
}

var badids = [
  "badscript1",
  "badscript2",
  "badimage1",
  "badimage2",
  "badframe1",
  "badframe2",
  "badmedia1",
  "badcss",
];

function checkLoads(aWindow, aData) {
  var win = aWindow.content;

  is(aData.scriptItem1, "untouched", "Should not load tracking javascript");
  is(aData.scriptItem2, "untouched", "Should not load tracking javascript (2)");

  is(aData.imageItem1, "untouched", "Should not load tracking images");
  is(aData.imageItem2, "untouched", "Should not load tracking images (2)");

  is(aData.frameItem1, "untouched", "Should not load tracking iframes");
  is(aData.frameItem2, "untouched", "Should not load tracking iframes (2)");
  is(aData.mediaItem1, "error", "Should not load tracking videos");
  is(aData.xhrItem, "failed", "Should not load tracking XHRs");
  is(aData.fetchItem, "error", "Should not fetch from tracking URLs");

  var elt = win.document.getElementById("styleCheck");
  var style = win.document.defaultView.getComputedStyle(elt);
  isnot(style.visibility, "hidden", "Should not load tracking css");

  is(win.document.blockedNodeByClassifierCount, badids.length,
    "Should identify all tracking elements");

  var blockedNodes = win.document.blockedNodesByClassifier;

  // Make sure that every node in blockedNodes exists in the tree
  // (that may not always be the case but do not expect any nodes to disappear
  // from the tree here)
  var allNodeMatch = true;
  for (let i = 0; i < blockedNodes.length; i++) {
    let nodeMatch = false;
    for (let j = 0; j < badids.length && !nodeMatch; j++) {
      nodeMatch = nodeMatch ||
        (blockedNodes[i] == win.document.getElementById(badids[j]));
    }

    allNodeMatch = allNodeMatch && nodeMatch;
  }
  ok(allNodeMatch, "All annotated nodes are expected in the tree");

  // Make sure that every node with a badid (see badids) is found in the
  // blockedNodes. This tells us if we are neglecting to annotate
  // some nodes
  allNodeMatch = true;
  for (let j = 0; j < badids.length; j++) {
    let nodeMatch = false;
    for (let i = 0; i < blockedNodes.length && !nodeMatch; i++) {
      nodeMatch = nodeMatch ||
        (blockedNodes[i] == win.document.getElementById(badids[j]));
    }

    if (!nodeMatch) {
      console.log(badids[j] + " was not found in blockedNodes");
    }
    allNodeMatch = allNodeMatch && nodeMatch;
  }
  ok(allNodeMatch, "All tracking nodes are expected to be annotated as such");

  // End (parent) test.
}

function cleanup() {
  SpecialPowers.clearUserPref("privacy.trackingprotection.enabled");
  SpecialPowers.clearUserPref("channelclassifier.allowlist_example");
  SpecialPowers.clearUserPref("privacy.trackingprotection.testing.report_blocked_node");

  UrlClassifierTestUtils.cleanupTestTrackers();
}

SpecialPowers.pushPrefEnv(
  {"set": [["urlclassifier.trackingTable", "moztest-track-simple"]]},
  test);

async function test() {
  SimpleTest.registerCleanupFunction(cleanup);
  await UrlClassifierTestUtils.addTestTrackers();

  await SpecialPowers.setBoolPref("privacy.trackingprotection.enabled", true);
  // Make sure chrome:// URIs are processed. This does not white-list
  // any URIs unless 'https://allowlisted.example.com' is added in the
  // permission manager (see test_allowlisted_annotations.html)
  await SpecialPowers.setBoolPref("channelclassifier.allowlist_example", true);
  await SpecialPowers.setBoolPref("privacy.trackingprotection.testing.report_blocked_node", true);

  await testOnWindow().then(function(aWindow) {
    aWindow.close();
  });

  SimpleTest.finish();
}

// Expected finish() call is in "classifiedAnnotatedFrame.html".
SimpleTest.waitForExplicitFinish();

</script>

</pre>
</body>
</html>