-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_classify_ping.html
139 lines (117 loc) · 3.93 KB
/
test_classify_ping.html
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
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1233914 - ping doesn't honor the TP list here.</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="classifierHelper.js"></script>
<link rel="stylesheet" type="text/css" href="/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">
SimpleTest.requestFlakyTimeout("Delay to make sure ping is made prior than XHR");
const timeout = 200;
const host_nottrack = "http://not-tracking.example.com/";
const host_track = "http://trackertest.org/";
const path_ping = "tests/toolkit/components/url-classifier/tests/mochitest/ping.sjs";
const TP_ENABLE_PREF = "privacy.trackingprotection.enabled";
const RETRY_TIMEOUT_MS = 200;
var testData = [
{ url: "trackertest.org/",
db: "test-track-simple",
},
];
function testPingNonBlacklist() {
SpecialPowers.setBoolPref(TP_ENABLE_PREF, true);
var msg = "ping should reach page not in blacklist";
var expectPing = true;
var id = "1111";
ping(id, host_nottrack);
return new Promise(function(resolve, reject) {
// Retry at most 30 seconds.
isPingedWithRetry(id, expectPing, msg, resolve, 30 * 1000 / RETRY_TIMEOUT_MS);
});
}
function testPingBlacklistSafebrowsingOff() {
SpecialPowers.setBoolPref(TP_ENABLE_PREF, false);
var msg = "ping should reach page in blacklist when tracking protection is off";
var expectPing = true;
var id = "2222";
ping(id, host_track);
return new Promise(function(resolve, reject) {
// Retry at most 30 seconds.
isPingedWithRetry(id, expectPing, msg, resolve, 30 * 1000 / RETRY_TIMEOUT_MS);
});
}
function testPingBlacklistSafebrowsingOn() {
SpecialPowers.setBoolPref(TP_ENABLE_PREF, true);
var msg = "ping should not reach page in blacklist when tracking protection is on";
var expectPing = false;
var id = "3333";
ping(id, host_track);
return new Promise(function(resolve, reject) {
setTimeout(function() {
isPinged(id, expectPing, msg, resolve);
}, timeout);
});
}
function ping(id, host) {
var elm = document.createElement("a");
elm.setAttribute("ping", host + path_ping + "?id=" + id);
elm.setAttribute("href", "#");
document.body.appendChild(elm);
// Trigger ping.
elm.click();
document.body.removeChild(elm);
}
function isPingedWithRetry(id, expected, msg, callback, retryCnt) {
var url = "http://mochi.test:8888/" + path_ping;
var xhr = new XMLHttpRequest();
xhr.open("GET", url + "?id=" + id);
xhr.onload = function() {
let pinged = xhr.response === "ping";
let success = pinged === expected;
if (success || 0 === retryCnt) {
is(expected, pinged, msg);
callback();
return;
}
// Retry on failure.
setTimeout(() => {
isPingedWithRetry(id, expected, msg, callback, retryCnt - 1);
}, RETRY_TIMEOUT_MS);
};
xhr.send();
}
function isPinged(id, expected, msg, callback) {
isPingedWithRetry(id, expected, msg, callback, 0);
}
function cleanup() {
SpecialPowers.clearUserPref(TP_ENABLE_PREF);
}
function runTest() {
classifierHelper.waitForInit()
.then(() => classifierHelper.addUrlToDB(testData))
.then(testPingNonBlacklist)
.then(testPingBlacklistSafebrowsingOff)
.then(testPingBlacklistSafebrowsingOn)
.then(function() {
SimpleTest.finish();
}).catch(function(e) {
ok(false, "Some test failed with error " + e);
SimpleTest.finish();
});
}
SimpleTest.waitForExplicitFinish();
SimpleTest.registerCleanupFunction(cleanup);
SpecialPowers.pushPrefEnv({"set": [
["browser.send_pings", true],
["urlclassifier.trackingTable", "test-track-simple"],
]}, runTest);
</script>
</pre>
</body>
</html>