-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_reporturl.html
214 lines (177 loc) · 7.75 KB
/
test_reporturl.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE HTML>
<!-- Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ -->
<html>
<head>
<title>Test report matched URL info (Bug #1288633)</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="classifierHelper.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">
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {BrowserTestUtils} = ChromeUtils.import("resource://testing-common/BrowserTestUtils.jsm");
;
const {TestUtils} = ChromeUtils.import("resource://testing-common/TestUtils.jsm");
var mainWindow = window.docShell.rootTreeItem.domWindow;
const SJS = "mochi.test:8888/chrome/toolkit/components/url-classifier/tests/mochitest/report.sjs";
const BASE_URL = "http://" + SJS + "?";
var pushPrefs = (...p) => SpecialPowers.pushPrefEnv({set: p});
function addUrlToDB(list, url) {
let testData = [{ db: list, url}];
return classifierHelper.addUrlToDB(testData)
.catch(function(err) {
ok(false, "Couldn't update classifier. Error code: " + err);
// Abort test.
SimpleTest.finish();
});
}
function setupTestData(data) {
let promises = [];
let providerList = "browser.safebrowsing.provider." + data.provider + ".lists";
if (!Services.prefs.prefHasUserValue(providerList)) {
promises.push(pushPrefs([providerList, data.list]));
} else {
let pref = SpecialPowers.getCharPref(providerList);
pref += "," + data.list;
promises.push(pushPrefs([providerList, pref]));
}
let activeTablePref = "urlclassifier.phishTable";
let activeTable = SpecialPowers.getCharPref(activeTablePref);
activeTable += "," + data.list;
promises.push(pushPrefs([activeTablePref, activeTable]));
promises.push(addUrlToDB(data.list, data.testUrl));
return Promise.all(promises);
}
function testOnWindow(aTestData, aCallback, aTestCreater) {
return new Promise(resolve => {
let win = mainWindow.OpenBrowserWindow();
(async function() {
await TestUtils.topicObserved("browser-delayed-startup-finished",
subject => subject == win);
let browser = win.gBrowser.selectedBrowser;
aTestCreater(win, browser, aTestData.topUrl, aTestData.testUrl);
let notification = await BrowserTestUtils.waitForNotificationBar(win.gBrowser, browser, "blocked-badware-page");
ok(notification, "Notification box should be displayed");
let buttons = notification.getElementsByTagName("button");
let button = buttons[1];
if (aTestData.provider != "google" && aTestData.provider != "google4") {
is(button, undefined, "Report button should not be showed");
win.close();
resolve();
return;
}
button.click();
let newTabBrowser = win.gBrowser.selectedTab.linkedBrowser;
await BrowserTestUtils.browserLoaded(newTabBrowser);
aCallback(newTabBrowser);
win.close();
resolve();
})();
});
}
var createBlockedIframe = function(aWindow, aBrowser, aTopUrl, aUrl) {
(async function() {
await BrowserTestUtils.loadURI(aBrowser, aTopUrl);
await BrowserTestUtils.browserLoaded(aBrowser);
await SpecialPowers.spawn(aBrowser, [aUrl], async function(url) {
return new Promise(resolve => {
let listener = e => {
docShell.chromeEventHandler.removeEventListener("AboutBlockedLoaded", listener, false, true);
resolve();
};
docShell.chromeEventHandler.addEventListener("AboutBlockedLoaded", listener, false, true);
let frame = content.document.getElementById("phishingFrame");
frame.setAttribute("src", "http://" + url);
});
});
let doc = aWindow.gBrowser.contentDocument.getElementsByTagName("iframe")[0].contentDocument;
let ignoreWarningLink = doc.getElementById("ignore_warning_link");
ok(ignoreWarningLink, "Ignore warning link should exist");
ignoreWarningLink.click();
})();
};
var createBlockedPage = function(aWindow, aBrowser, aTopUrl, aUrl) {
(async function() {
await BrowserTestUtils.loadURI(aBrowser, aTopUrl);
await BrowserTestUtils.waitForContentEvent(aBrowser, "DOMContentLoaded");
let doc = aWindow.gBrowser.contentDocument;
let ignoreWarningLink = doc.getElementById("ignore_warning_link");
ok(ignoreWarningLink, "Ignore warning link should exist");
ignoreWarningLink.click();
})();
};
function checkReportURL(aReportBrowser, aUrl) {
let expectedReportUrl = BASE_URL + "action=reporturl&reporturl=" + encodeURIComponent(aUrl);
is(aReportBrowser.contentDocument.location.href, expectedReportUrl, "Correct report URL");
}
var testDatas = [
{ topUrl: "http://itisaphishingsite.org/phishing.html",
testUrl: "itisaphishingsite.org/phishing.html",
list: "mochi1-phish-simple",
provider: "google",
blockCreater: createBlockedPage,
expectedReportUri: "http://itisaphishingsite.org/phishing.html",
},
// Non-google provider, no report button is showed.
// Test provider needs a valid update URL (mozilla for example) otherwise
// the updates inserting the test data will fail.
{ topUrl: "http://fakeitisaphishingsite.org/phishing.html",
testUrl: "fakeitisaphishingsite.org/phishing.html",
list: "fake-phish-simple",
provider: "mozilla",
blockCreater: createBlockedPage,
},
// Iframe case:
// A top level page at
// http://mochi.test:8888/chrome/toolkit/components/url-classifier/tests/mochitest/report.sjs?action=create-blocked-iframe
// contains an iframe to http://phishing.example.com/test.html (blocked).
{ topUrl: "http://mochi.test:8888/chrome/toolkit/components/url-classifier/tests/mochitest/report.sjs?action=create-blocked-iframe",
testUrl: "phishing.example.com/test.html",
list: "mochi2-phish-simple",
provider: "google4",
blockCreater: createBlockedIframe,
expectedReportUri: "http://phishing.example.com/test.html",
},
// Redirect case:
// A top level page at
// http://prefixexample.com/chrome/toolkit/components/url-classifier/tests/mochitest/report.sjs?action=create-blocked-redirect (blocked)
// will get redirected to
// https://mochi.test:8888/chrome/toolkit/components/url-classifier/tests/mochitest/report.sjs?action=create-blocked-redirect.
{ topUrl: "http://prefixexample.com/chrome/toolkit/components/url-classifier/tests/mochitest/report.sjs?action=create-blocked-redirect",
testUrl: "prefixexample.com/chrome/toolkit/components/url-classifier/tests/mochitest/report.sjs?action=create-blocked-redirect",
list: "mochi3-phish-simple",
provider: "google4",
blockCreater: createBlockedPage,
expectedReportUri: "http://prefixexample.com/chrome/toolkit/components/url-classifier/tests/mochitest/report.sjs",
},
];
SpecialPowers.pushPrefEnv(
{"set": [["browser.safebrowsing.provider.google.reportPhishMistakeURL", BASE_URL + "action=reporturl&reporturl="],
["browser.safebrowsing.provider.google4.reportPhishMistakeURL", BASE_URL + "action=reporturl&reporturl="],
["browser.safebrowsing.phishing.enabled", true]]},
test);
function test() {
(async function() {
await classifierHelper.waitForInit();
for (let testData of testDatas) {
await setupTestData(testData);
await testOnWindow(testData, function(browser) {
checkReportURL(browser, testData.expectedReportUri);
}, testData.blockCreater);
await classifierHelper._cleanup();
}
SimpleTest.finish();
})();
}
SimpleTest.waitForExplicitFinish();
</script>
</pre>
<iframe id="testFrame" width="100%" height="100%" onload=""></iframe>
</body>
</html>