-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_classifier.html
191 lines (176 loc) · 5.75 KB
/
test_classifier.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
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
183
184
185
186
187
188
189
190
191
<!DOCTYPE HTML>
<html>
<head>
<title>Test the URI Classifier</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>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
var firstLoad = true;
// Add some URLs to the malware database.
// Note that we intentionally don't touch test-phish-simple, and will
// use the URL registered by addMozEntries(). Otherwise, classifierHelper.resetDatabase()
// will reset this table, and waitForInit() can't find the known phishing
// URL in test-phish-simple, breaking the tests following this one.
var testData = [
{ url: "malware.example.com/",
db: "test-malware-simple",
},
{ url: "unwanted.example.com/",
db: "test-unwanted-simple",
},
{ url: "tracking.example.com/",
db: "test-track-simple",
},
{ url: "blocked.example.com/",
db: "test-block-simple",
},
{ url: "harmful.example.com/",
db: "test-harmful-simple",
},
];
const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;
const Cr = SpecialPowers.Cr;
var testURLs = [
{ url: "http://example.com",
trackingProtection: false,
table: "",
result: Cr.NS_OK,
},
{ url: "http://example.com",
trackingProtection: true,
table: "",
result: Cr.NS_OK,
},
{ url: "http://malware.example.com",
trackingProtection: false,
table: "test-malware-simple",
result: Cr.NS_ERROR_MALWARE_URI,
},
{ url: "http://malware.example.com",
trackingProtection: true,
table: "test-malware-simple",
result: Cr.NS_ERROR_MALWARE_URI,
},
{ url: "http://unwanted.example.com",
trackingProtection: false,
table: "test-unwanted-simple",
result: Cr.NS_ERROR_UNWANTED_URI,
},
{ url: "http://unwanted.example.com",
trackingProtection: true,
table: "test-unwanted-simple",
result: Cr.NS_ERROR_UNWANTED_URI,
},
{ url: "http://itisatrap.org/firefox/its-a-trap.html",
trackingProtection: false,
table: "test-phish-simple",
result: Cr.NS_ERROR_PHISHING_URI,
},
{ url: "http://itisatrap.org/firefox/its-a-trap.html",
trackingProtection: true,
table: "test-phish-simple",
result: Cr.NS_ERROR_PHISHING_URI,
},
{ url: "http://harmful.example.com",
trackingProtection: false,
table: "test-harmful-simple",
result: Cr.NS_ERROR_HARMFUL_URI,
},
{ url: "http://harmful.example.com",
trackingProtection: true,
table: "test-harmful-simple",
result: Cr.NS_ERROR_HARMFUL_URI,
},
{ url: "http://tracking.example.com",
trackingProtection: false,
table: "test-track-simple",
result: Cr.NS_OK,
},
{ url: "http://tracking.example.com",
trackingProtection: true,
table: "test-track-simple",
result: Cr.NS_ERROR_TRACKING_URI,
},
{ url: "http://blocked.example.com",
trackingProtection: false,
table: "test-block-simple",
result: Cr.NS_ERROR_BLOCKED_URI,
},
{ url: "http://blocked.example.com",
trackingProtection: true,
table: "test-block-simple",
result: Cr.NS_ERROR_BLOCKED_URI,
},
];
function loadTestFrame() {
document.getElementById("testFrame").src = "classifierFrame.html";
}
// Expected finish() call is in "classifierFrame.html".
SimpleTest.waitForExplicitFinish();
function updateSuccess() {
return SpecialPowers.pushPrefEnv(
{"set": [["browser.safebrowsing.malware.enabled", true]]});
}
function updateError(errorCode) {
ok(false, "Couldn't update classifier. Error code: " + errorCode);
// Abort test.
SimpleTest.finish();
}
function testService() {
return new Promise(resolve => {
let service = Cc["@mozilla.org/url-classifier/dbservice;1"].
getService(Ci.nsIURIClassifier);
function runNextTest() {
if (!testURLs.length) {
resolve();
return;
}
let test = testURLs.shift();
let tables = "test-malware-simple,test-unwanted-simple,test-phish-simple,test-track-simple,test-block-simple,test-harmful-simple";
let uri = SpecialPowers.Services.io.newURI(test.url);
let prin = SpecialPowers.Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
is(service.classifyLocal(uri, tables), test.table,
`Successful synchronous classification of ${test.url} with TP=${test.trackingProtection}`);
SpecialPowers.doUrlClassify(prin, null, test.trackingProtection, function(errorCode) {
is(errorCode, test.result,
`Successful asynchronous classification of ${test.url} with TP=${test.trackingProtection}`);
// Same as classifyLocal except for the 'async' call.
SpecialPowers.doUrlClassifyLocal(uri, tables, function(errorCode1, tables1) {
is(tables1, test.table,
`Successful asynchronous local classification of ${test.url} with TP=${test.trackingProtection}`);
runNextTest();
});
});
}
runNextTest(resolve);
});
}
SpecialPowers.pushPrefEnv(
{"set": [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple,test-harmful-simple"],
["urlclassifier.phishTable", "test-phish-simple"],
["urlclassifier.downloadBlockTable", "test-block-simple"],
["urlclassifier.trackingTable", "test-track-simple"],
["browser.safebrowsing.debug", true],
["privacy.trackingprotection.annotate_channels", true]]},
function() {
classifierHelper.waitForInit()
.then(() => classifierHelper.addUrlToDB(testData))
.then(updateSuccess)
.catch(err => {
updateError(err);
})
.then(testService)
.then(loadTestFrame);
});
</script>
</pre>
<iframe id="testFrame" onload=""></iframe>
</body>
</html>