-
Notifications
You must be signed in to change notification settings - Fork 54
/
classifierTester.js
347 lines (322 loc) · 12.7 KB
/
classifierTester.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var classifierTester = {
URL_PATH: "/browser/toolkit/components/url-classifier/tests/browser/flash_block_frame.html",
OBJECT_ID: "testObject",
IFRAME_ID: "testFrame",
FLASHBLOCK_ENABLE_PREF: "plugins.flashBlock.enabled",
FLASH_PLUGIN_USER_SETTING_PREF: "plugin.state.flash",
URLCLASSIFIER_DISALLOW_COMPLETIONS_PREF: "urlclassifier.disallow_completions",
NEVER_ACTIVATE_PREF_VALUE: 0,
ASK_TO_ACTIVATE_PREF_VALUE: 1,
ALWAYS_ACTIVATE_PREF_VALUE: 2,
ALLOW_CTA_PREF: "plugins.click_to_play",
dbUrls: [
{
url: "flashallow.example.com/",
db: "test-flashallow-simple",
pref: "urlclassifier.flashAllowTable",
},
{
url: "exception.flashallow.example.com/",
db: "testexcept-flashallow-simple",
pref: "urlclassifier.flashAllowExceptTable",
},
{
url: "flashblock.example.com/",
db: "test-flash-simple",
pref: "urlclassifier.flashTable",
},
{
url: "exception.flashblock.example.com/",
db: "testexcept-flash-simple",
pref: "urlclassifier.flashExceptTable",
},
{
url: "subdocument.example.com/",
db: "test-flashsubdoc-simple",
pref: "urlclassifier.flashSubDocTable",
},
{
url: "exception.subdocument.example.com/",
db: "testexcept-flashsubdoc-simple",
pref: "urlclassifier.flashSubDocExceptTable",
},
],
setPrefs({setDBs = true, flashBlockEnable = true, flashSetting = classifierTester.ALWAYS_ACTIVATE_PREF_VALUE} = {}) {
if (setDBs) {
let DBs = [];
for (let dbData of classifierTester.dbUrls) {
Services.prefs.setCharPref(dbData.pref, dbData.db);
DBs.push(dbData.db);
}
let completions = Services.prefs.getCharPref(classifierTester.URLCLASSIFIER_DISALLOW_COMPLETIONS_PREF);
completions += "," + DBs.join(",");
Services.prefs.setCharPref(classifierTester.URLCLASSIFIER_DISALLOW_COMPLETIONS_PREF, completions);
}
Services.prefs.setBoolPref(classifierTester.FLASHBLOCK_ENABLE_PREF,
flashBlockEnable);
Services.prefs.setIntPref(classifierTester.FLASH_PLUGIN_USER_SETTING_PREF,
flashSetting);
Services.prefs.setBoolPref(classifierTester.ALLOW_CTA_PREF, true);
},
unsetPrefs() {
for (let dbData of classifierTester.dbUrls) {
Services.prefs.clearUserPref(dbData.pref);
}
Services.prefs.clearUserPref(classifierTester.URLCLASSIFIER_DISALLOW_COMPLETIONS_PREF);
Services.prefs.clearUserPref(classifierTester.FLASHBLOCK_ENABLE_PREF);
Services.prefs.clearUserPref(classifierTester.FLASH_PLUGIN_USER_SETTING_PREF);
Services.prefs.clearUserPref(classifierTester.ALLOW_CTA_PREF);
},
// The |domains| property describes the domains of the nested documents making
// up the page. |domains[0]| represents the domain in the URL bar. The last
// domain in the list is the domain of the most deeply nested iframe.
// Only the plugin in the most deeply nested document will be checked.
testCases: [
{
name: "Unknown domain",
domains: ["http://example.com"],
expectedFlashClassification: "unknown",
},
{
name: "Nested unknown domains",
domains: ["http://example.com", "http://example.org"],
expectedFlashClassification: "unknown",
},
{
name: "Allowed domain",
domains: ["http://flashallow.example.com"],
expectedFlashClassification: "allowed",
},
{
name: "Allowed nested domain",
domains: ["http://example.com", "http://flashallow.example.com"],
expectedFlashClassification: "allowed",
},
{
name: "Subdocument of allowed domain",
domains: ["http://flashallow.example.com", "http://example.com"],
expectedFlashClassification: "allowed",
},
{
name: "Exception to allowed domain",
domains: ["http://exception.flashallow.example.com"],
expectedFlashClassification: "unknown",
},
{
name: "Blocked domain",
domains: ["http://flashblock.example.com"],
expectedFlashClassification: "denied",
},
{
name: "Nested blocked domain",
domains: ["http://example.com", "http://flashblock.example.com"],
expectedFlashClassification: "denied",
},
{
name: "Subdocument of blocked subdocument",
domains: ["http://example.com", "http://flashblock.example.com", "http://example.com"],
expectedFlashClassification: "denied",
},
{
name: "Blocked subdocument nested among in allowed documents",
domains: ["http://flashallow.example.com", "http://flashblock.example.com", "http://flashallow.example.com"],
expectedFlashClassification: "denied",
},
{
name: "Exception to blocked domain",
domains: ["http://exception.flashblock.example.com"],
expectedFlashClassification: "unknown",
},
{
name: "Sub-document blocked domain in top-level context",
domains: ["http://subdocument.example.com"],
expectedFlashClassification: "unknown",
},
{
name: "Sub-document blocked domain",
domains: ["http://example.com", "http://subdocument.example.com"],
expectedFlashClassification: "denied",
},
{
name: "Sub-document blocked domain in non-Third-Party context",
domains: ["http://subdocument.example.com", "http://subdocument.example.com"],
expectedFlashClassification: "unknown",
},
{
name: "Sub-document blocked domain differing only by scheme",
domains: ["http://subdocument.example.com", "https://subdocument.example.com"],
expectedFlashClassification: "denied",
},
{
name: "Sub-document blocked subdocument of an allowed domain",
domains: ["http://flashallow.example.com", "http://subdocument.example.com"],
expectedFlashClassification: "denied",
},
{
name: "Subdocument of Sub-document blocked domain",
domains: ["http://example.com", "http://subdocument.example.com", "http://example.com"],
expectedFlashClassification: "denied",
},
{
name: "Sub-document exception in top-level context",
domains: ["http://exception.subdocument.example.com"],
expectedFlashClassification: "unknown",
},
{
name: "Sub-document blocked domain exception",
domains: ["http://example.com", "http://exception.subdocument.example.com"],
expectedFlashClassification: "unknown",
},
],
// Returns null if this value should not be verified given the combination
// of inputs
expectedPluginFallbackType(classification, flashSetting) {
switch (classification) {
case "unknown":
if (flashSetting == classifierTester.ALWAYS_ACTIVATE_PREF_VALUE) {
return null;
} else if (flashSetting == classifierTester.ASK_TO_ACTIVATE_PREF_VALUE) {
return Ci.nsIObjectLoadingContent.PLUGIN_CLICK_TO_PLAY;
} else if (flashSetting == classifierTester.NEVER_ACTIVATE_PREF_VALUE) {
return Ci.nsIObjectLoadingContent.PLUGIN_DISABLED;
}
break;
case "allowed":
if (flashSetting == classifierTester.NEVER_ACTIVATE_PREF_VALUE) {
return Ci.nsIObjectLoadingContent.PLUGIN_DISABLED;
}
return null;
case "denied":
return Ci.nsIObjectLoadingContent.PLUGIN_USER_DISABLED;
}
throw new Error("Invalid classification or flash setting");
},
// Returns null if this value should not be verified given the combination
// of inputs
expectedActivated(classification, flashSetting) {
switch (classification) {
case "unknown":
return (flashSetting == classifierTester.ALWAYS_ACTIVATE_PREF_VALUE);
case "allowed":
return (flashSetting != classifierTester.NEVER_ACTIVATE_PREF_VALUE);
case "denied":
return false;
}
throw new Error("Invalid classification or flash setting");
},
// Returns null if this value should not be verified given the combination
// of inputs
expectedHasRunningPlugin(classification, flashSetting) {
switch (classification) {
case "unknown":
return (flashSetting == classifierTester.ALWAYS_ACTIVATE_PREF_VALUE);
case "allowed":
return (flashSetting != classifierTester.NEVER_ACTIVATE_PREF_VALUE);
case "denied":
return false;
}
throw new Error("Invalid classification or flash setting");
},
// Returns null if this value should not be verified given the combination
// of inputs
expectedPluginListed(classification, flashSetting) {
if (flashSetting == classifierTester.ASK_TO_ACTIVATE_PREF_VALUE &&
Services.prefs.getCharPref("plugins.navigator.hidden_ctp_plugin") == "Shockwave Flash") {
return false;
}
switch (classification) {
case "unknown":
case "allowed":
return (flashSetting != classifierTester.NEVER_ACTIVATE_PREF_VALUE);
case "denied":
return false;
}
throw new Error("Invalid classification or flash setting");
},
buildTestCaseInNewTab(browser, testCase) {
return (async function() {
let iframeDomains = testCase.domains.slice();
let pageDomain = iframeDomains.shift();
let tab = await BrowserTestUtils.openNewForegroundTab(browser,
pageDomain + classifierTester.URL_PATH);
let depth = 0;
for (let domain of iframeDomains) {
// Firefox does not like to load the same page in its own iframe. Put some
// bogus query strings in the URL to make it happy.
let url = domain + classifierTester.URL_PATH + "?date=" + Date.now() + "rand=" + Math.random();
let domainLoaded = BrowserTestUtils.browserLoaded(tab.linkedBrowser, true, url);
ContentTask.spawn(tab.linkedBrowser, {iframeId: classifierTester.IFRAME_ID, url, depth},
async function({iframeId, url, depth}) {
let doc = content.document;
for (let i = 0; i < depth; ++i) {
doc = doc.getElementById(iframeId).contentDocument;
}
doc.getElementById(iframeId).src = url;
});
await domainLoaded;
++depth;
}
return tab;
})();
},
getPluginInfo(browser, depth) {
return ContentTask.spawn(browser,
{iframeId: classifierTester.IFRAME_ID, depth},
async function({iframeId, depth}) {
let doc = content.document;
let win = content.window;
for (let i = 0; i < depth; ++i) {
let frame = doc.getElementById(iframeId);
doc = frame.contentDocument;
win = frame.contentWindow;
}
let pluginObj = doc.getElementById("testObject");
if (!(pluginObj instanceof Ci.nsIObjectLoadingContent)) {
throw new Error("Unable to find plugin!");
}
return {
pluginFallbackType: pluginObj.pluginFallbackType,
activated: pluginObj.activated,
hasRunningPlugin: pluginObj.hasRunningPlugin,
listed: ("Shockwave Flash" in win.navigator.plugins),
flashClassification: doc.documentFlashClassification,
};
});
},
checkPluginInfo(pluginInfo, expectedClassification, flashSetting) {
is(pluginInfo.flashClassification, expectedClassification,
"Page's classification should match expected");
let expectedPluginFallbackType =
classifierTester.expectedPluginFallbackType(pluginInfo.flashClassification,
flashSetting);
if (expectedPluginFallbackType != null) {
is(pluginInfo.pluginFallbackType, expectedPluginFallbackType,
"Plugin should have the correct fallback type");
}
let expectedActivated =
classifierTester.expectedActivated(pluginInfo.flashClassification,
flashSetting);
if (expectedActivated != null) {
is(pluginInfo.activated, expectedActivated,
"Plugin should have the correct activation");
}
let expectedHasRunningPlugin =
classifierTester.expectedHasRunningPlugin(pluginInfo.flashClassification,
flashSetting);
if (expectedHasRunningPlugin != null) {
is(pluginInfo.hasRunningPlugin, expectedHasRunningPlugin,
"Plugin should have the correct 'plugin running' state");
}
let expectedPluginListed =
classifierTester.expectedPluginListed(pluginInfo.flashClassification,
flashSetting);
if (expectedPluginListed != null) {
is(pluginInfo.listed, expectedPluginListed,
"Plugin's existance in navigator.plugins should match expected");
}
},
};
registerCleanupFunction(classifierTester.unsetPrefs);