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
|
ChromeUtils.defineModuleGetter(
this,
"Downloads",
"resource://gre/modules/Downloads.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"DownloadsCommon",
"resource:///modules/DownloadsCommon.jsm"
);
let INSECURE_BASE_URL =
getRootDirectory(gTestPath).replace(
"chrome://mochitests/content/",
"http://example.com/"
) + "download_page.html";
let SECURE_BASE_URL =
getRootDirectory(gTestPath).replace(
"chrome://mochitests/content/",
"https://example.com/"
) + "download_page.html";
function promiseFocus() {
return new Promise(resolve => {
waitForFocus(resolve);
});
}
function promisePanelOpened() {
if (DownloadsPanel.panel && DownloadsPanel.panel.state == "open") {
return Promise.resolve();
}
return BrowserTestUtils.waitForEvent(DownloadsPanel.panel, "popupshown");
}
async function task_openPanel() {
await promiseFocus();
let promise = promisePanelOpened();
DownloadsPanel.showPanel();
await promise;
}
function shouldPromptDownload() {
// Waits until the download Prompt is shown
return new Promise((resolve, reject) => {
Services.wm.addListener({
onOpenWindow(xulWin) {
Services.wm.removeListener(this);
let win = xulWin.docShell.domWindow;
waitForFocus(() => {
if (
win.location ==
"chrome://mozapps/content/downloads/unknownContentType.xhtml"
) {
let dialog = win.document.getElementById("unknownContentType");
let button = dialog.getButton("accept");
let saveRadio = win.document.getElementById("save");
saveRadio.click();
button.disabled = false;
dialog.acceptDialog();
resolve();
} else {
reject();
}
}, win);
},
});
});
}
const CONSOLE_ERROR_MESSAGE = "Blocked downloading insecure content";
function shouldConsoleError() {
// Waits until CONSOLE_ERROR_MESSAGE was logged
return new Promise((resolve, reject) => {
function listener(msgObj) {
let text = msgObj.message;
if (text.includes(CONSOLE_ERROR_MESSAGE)) {
Services.console.unregisterListener(listener);
resolve();
}
}
Services.console.registerListener(listener);
});
}
async function resetDownloads() {
// Removes all downloads from the download List
let publicList = await Downloads.getList(Downloads.PUBLIC);
let downloads = await publicList.getAll();
for (let download of downloads) {
publicList.remove(download);
await download.finalize(true);
}
}
async function shouldNotifyDownloadUI() {
// Waits until a Blocked download was added to the Download List
// -> returns the blocked Download
let list = await Downloads.getList(Downloads.ALL);
return new Promise(res => {
const view = {
onDownloadAdded: aDownload => {
let { error } = aDownload;
if (
error.becauseBlockedByReputationCheck &&
error.reputationCheckVerdict == Downloads.Error.BLOCK_VERDICT_INSECURE
) {
res(aDownload);
list.removeView(view);
}
},
};
list.addView(view);
});
}
async function runTest(url, link, checkFunction, decscription) {
await SpecialPowers.pushPrefEnv({
set: [["dom.block_download_insecure", true]],
});
await resetDownloads();
let tab = BrowserTestUtils.addTab(gBrowser, url);
gBrowser.selectedTab = tab;
let browser = gBrowser.getBrowserForTab(tab);
await BrowserTestUtils.browserLoaded(browser);
info("Checking: " + decscription);
let checkPromise = checkFunction();
// Click the Link to trigger the download
SpecialPowers.spawn(gBrowser.selectedBrowser, [link], contentLink => {
content.document.getElementById(contentLink).click();
});
await checkPromise;
ok(true, decscription);
BrowserTestUtils.removeTab(tab);
await SpecialPowers.popPrefEnv();
}
// Test Blocking
add_task(async function() {
await runTest(
INSECURE_BASE_URL,
"insecure",
shouldPromptDownload,
"Insecure -> Insecure should download"
);
await runTest(
INSECURE_BASE_URL,
"secure",
shouldPromptDownload,
"Insecure -> Secure should download"
);
await runTest(
SECURE_BASE_URL,
"insecure",
() =>
Promise.all([
shouldPromptDownload(),
shouldNotifyDownloadUI(),
shouldConsoleError(),
]),
"Secure -> Insecure should Error"
);
await runTest(
SECURE_BASE_URL,
"secure",
shouldPromptDownload,
"Secure -> Secure should Download"
);
});
// Test Manual Unblocking
add_task(async function() {
await runTest(
SECURE_BASE_URL,
"insecure",
async () => {
await shouldPromptDownload();
let download = await shouldNotifyDownloadUI();
await download.unblock();
ok(download.error == null, "There should be no error after unblocking");
},
"A Blocked Download Should succeeded to Download after a Manual unblock"
);
});
// Test Unblock Download Visible
add_task(async function() {
// Focus, open and close the panel once
// to make sure the panel is loaded and ready
await promiseFocus();
await runTest(
SECURE_BASE_URL,
"insecure",
async () => {
let panelHasOpened = promisePanelOpened();
info("awaiting that the Download Prompt is shown");
await shouldPromptDownload();
info("awaiting that the Download list adds the new download");
await shouldNotifyDownloadUI();
info("awaiting that the Download list shows itself");
await panelHasOpened;
DownloadsPanel.hidePanel();
ok(true, "The Download Panel should have opened on blocked download");
},
"A Blocked Download Should open the Download Panel"
);
});
|