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
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
const { Downloads } = ChromeUtils.importESModule(
"resource://gre/modules/Downloads.sys.mjs"
);
function backgroundScript() {
let complete = new Map();
function waitForComplete(id) {
if (complete.has(id)) {
return complete.get(id).promise;
}
let promise = new Promise(resolve => {
complete.set(id, { resolve });
});
complete.get(id).promise = promise;
return promise;
}
browser.downloads.onChanged.addListener(change => {
if (change.state && change.state.current == "complete") {
// Make sure we have a promise.
waitForComplete(change.id);
complete.get(change.id).resolve();
}
});
browser.test.onMessage.addListener(async (msg, ...args) => {
if (msg == "download.request") {
try {
let id = await browser.downloads.download(args[0]);
browser.test.sendMessage("download.done", { status: "success", id });
} catch (error) {
browser.test.sendMessage("download.done", {
status: "error",
errmsg: error.message,
});
}
} else if (msg == "search.request") {
try {
let downloads = await browser.downloads.search(args[0]);
browser.test.sendMessage("search.done", {
status: "success",
downloads,
});
} catch (error) {
browser.test.sendMessage("search.done", {
status: "error",
errmsg: error.message,
});
}
} else if (msg == "waitForComplete.request") {
await waitForComplete(args[0]);
browser.test.sendMessage("waitForComplete.done");
}
});
browser.test.sendMessage("ready");
}
async function clearDownloads() {
let list = await Downloads.getList(Downloads.ALL);
let downloads = await list.getAll();
await Promise.all(downloads.map(download => list.remove(download)));
return downloads;
}
add_task(async function test_decoded_filename_download() {
const server = createHttpServer();
server.registerPrefixHandler("/data/", (_, res) => res.write("length=8"));
const BASE = `http://localhost:${server.identity.primaryPort}/data`;
const FILE_NAME_ENCODED_1 = "file%2Fencode.txt";
const FILE_NAME_DECODED_1 = "file_encode.txt";
const FILE_NAME_ENCODED_URL_1 = BASE + "/" + FILE_NAME_ENCODED_1;
const FILE_NAME_ENCODED_2 = "file%F0%9F%9A%B2encoded.txt";
const FILE_NAME_DECODED_2 = "file\u{0001F6B2}encoded.txt";
const FILE_NAME_ENCODED_URL_2 = BASE + "/" + FILE_NAME_ENCODED_2;
const FILE_NAME_ENCODED_3 = "file%X%20encode.txt";
const FILE_NAME_DECODED_3 = "file_X encode.txt";
const FILE_NAME_ENCODED_URL_3 = BASE + "/" + FILE_NAME_ENCODED_3;
const FILE_NAME_ENCODED_4 = "file%E3%80%82encode.txt";
const FILE_NAME_DECODED_4 = "file\u3002encode.txt";
const FILE_NAME_ENCODED_URL_4 = BASE + "/" + FILE_NAME_ENCODED_4;
const FILE_ENCODED_LEN = 8;
const nsIFile = Ci.nsIFile;
let downloadDir = FileUtils.getDir("TmpD", ["downloads"]);
downloadDir.createUnique(nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
info(`downloadDir ${downloadDir.path}`);
function downloadPath(filename) {
let path = downloadDir.clone();
path.append(filename);
return path.path;
}
Services.prefs.setIntPref("browser.download.folderList", 2);
Services.prefs.setComplexValue("browser.download.dir", nsIFile, downloadDir);
registerCleanupFunction(async () => {
Services.prefs.clearUserPref("browser.download.folderList");
Services.prefs.clearUserPref("browser.download.dir");
await cleanupDir(downloadDir);
await clearDownloads();
});
await clearDownloads().then(downloads => {
info(`removed ${downloads.length} pre-existing downloads from history`);
});
let extension = ExtensionTestUtils.loadExtension({
background: backgroundScript,
manifest: {
permissions: ["downloads"],
},
});
async function download(options) {
extension.sendMessage("download.request", options);
let result = await extension.awaitMessage("download.done");
if (result.status == "success") {
info(`wait for onChanged event to indicate ${result.id} is complete`);
extension.sendMessage("waitForComplete.request", result.id);
await extension.awaitMessage("waitForComplete.done");
}
return result;
}
function search(query) {
extension.sendMessage("search.request", query);
return extension.awaitMessage("search.done");
}
await extension.startup();
await extension.awaitMessage("ready");
let downloadIds = {};
let msg = await download({ url: FILE_NAME_ENCODED_URL_1 });
equal(msg.status, "success", "download() succeeded");
downloadIds.fileEncoded1 = msg.id;
msg = await download({ url: FILE_NAME_ENCODED_URL_2 });
equal(msg.status, "success", "download() succeeded");
downloadIds.fileEncoded2 = msg.id;
msg = await download({ url: FILE_NAME_ENCODED_URL_3 });
equal(msg.status, "success", "download() succeeded");
downloadIds.fileEncoded3 = msg.id;
msg = await download({ url: FILE_NAME_ENCODED_URL_4 });
equal(msg.status, "success", "download() succeeded");
downloadIds.fileEncoded4 = msg.id;
// Search for each individual download and check
// the corresponding DownloadItem.
async function checkDownloadItem(id, expect) {
let item = await search({ id });
equal(item.status, "success", "search() succeeded");
equal(item.downloads.length, 1, "search() found exactly 1 download");
Object.keys(expect).forEach(function (field) {
equal(
item.downloads[0][field],
expect[field],
`DownloadItem.${field} is correct"`
);
});
}
await checkDownloadItem(downloadIds.fileEncoded1, {
url: FILE_NAME_ENCODED_URL_1,
filename: downloadPath(FILE_NAME_DECODED_1),
state: "complete",
bytesReceived: FILE_ENCODED_LEN,
totalBytes: FILE_ENCODED_LEN,
fileSize: FILE_ENCODED_LEN,
exists: true,
});
await checkDownloadItem(downloadIds.fileEncoded2, {
url: FILE_NAME_ENCODED_URL_2,
filename: downloadPath(FILE_NAME_DECODED_2),
state: "complete",
bytesReceived: FILE_ENCODED_LEN,
totalBytes: FILE_ENCODED_LEN,
fileSize: FILE_ENCODED_LEN,
exists: true,
});
await checkDownloadItem(downloadIds.fileEncoded3, {
url: FILE_NAME_ENCODED_URL_3,
filename: downloadPath(FILE_NAME_DECODED_3),
state: "complete",
bytesReceived: FILE_ENCODED_LEN,
totalBytes: FILE_ENCODED_LEN,
fileSize: FILE_ENCODED_LEN,
exists: true,
});
await checkDownloadItem(downloadIds.fileEncoded4, {
url: FILE_NAME_ENCODED_URL_4,
filename: downloadPath(FILE_NAME_DECODED_4),
state: "complete",
bytesReceived: FILE_ENCODED_LEN,
totalBytes: FILE_ENCODED_LEN,
fileSize: FILE_ENCODED_LEN,
exists: true,
});
// Searching for downloads by the decoded filename works correctly.
async function checkSearch(query, expected, description) {
let item = await search(query);
equal(item.status, "success", "search() succeeded");
equal(
item.downloads.length,
expected.length,
`search() for ${description} found exactly ${expected.length} downloads`
);
equal(
item.downloads[0].id,
downloadIds[expected[0]],
`search() for ${description} returned ${expected[0]} in position ${0}`
);
}
await checkSearch(
{ filename: downloadPath(FILE_NAME_DECODED_1) },
["fileEncoded1"],
"filename"
);
await checkSearch(
{ filename: downloadPath(FILE_NAME_DECODED_2) },
["fileEncoded2"],
"filename"
);
await checkSearch(
{ filename: downloadPath(FILE_NAME_DECODED_3) },
["fileEncoded3"],
"filename"
);
await checkSearch(
{ filename: downloadPath(FILE_NAME_DECODED_4) },
["fileEncoded4"],
"filename"
);
await extension.unload();
});
|