diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/components/downloads/test | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/downloads/test')
17 files changed, 6885 insertions, 0 deletions
diff --git a/toolkit/components/downloads/test/data/empty.txt b/toolkit/components/downloads/test/data/empty.txt new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/toolkit/components/downloads/test/data/empty.txt diff --git a/toolkit/components/downloads/test/data/source.txt b/toolkit/components/downloads/test/data/source.txt new file mode 100644 index 0000000000..2156cb8c03 --- /dev/null +++ b/toolkit/components/downloads/test/data/source.txt @@ -0,0 +1 @@ +This test string is downloaded.
\ No newline at end of file diff --git a/toolkit/components/downloads/test/unit/common_test_Download.js b/toolkit/components/downloads/test/unit/common_test_Download.js new file mode 100644 index 0000000000..5e65cd7cbd --- /dev/null +++ b/toolkit/components/downloads/test/unit/common_test_Download.js @@ -0,0 +1,2751 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * This script is loaded by "test_DownloadCore.js" and "test_DownloadLegacy.js" + * with different values of the gUseLegacySaver variable, to apply tests to both + * the "copy" and "legacy" saver implementations. + */ + +/* import-globals-from head.js */ +/* global gUseLegacySaver */ + +"use strict"; + +// Globals + +const kDeleteTempFileOnExit = "browser.helperApps.deleteTempFileOnExit"; + +ChromeUtils.defineESModuleGetters(this, { + FileUtils: "resource://gre/modules/FileUtils.sys.mjs", +}); + +/** + * Creates and starts a new download, using either DownloadCopySaver or + * DownloadLegacySaver based on the current test run. + * + * @return {Promise} + * @resolves The newly created Download object. The download may be in progress + * or already finished. The promiseDownloadStopped function can be + * used to wait for completion. + * @rejects JavaScript exception. + */ +function promiseStartDownload(aSourceUrl) { + if (gUseLegacySaver) { + return promiseStartLegacyDownload(aSourceUrl); + } + + return promiseNewDownload(aSourceUrl).then(download => { + download.start().catch(() => {}); + return download; + }); +} + +/** + * Checks that the actual data written to disk matches the expected data as well + * as the properties of the given DownloadTarget object. + * + * @param downloadTarget + * The DownloadTarget object whose details have to be verified. + * @param expectedContents + * String containing the octets that are expected in the file. + * + * @return {Promise} + * @resolves When the properties have been verified. + * @rejects JavaScript exception. + */ +var promiseVerifyTarget = async function (downloadTarget, expectedContents) { + Assert.ok(downloadTarget.exists); + Assert.equal( + await expectNonZeroDownloadTargetSize(downloadTarget), + expectedContents.length + ); + await promiseVerifyContents(downloadTarget.path, expectedContents); +}; + +/** + * This is a temporary workaround for frequent intermittent Bug 1760112. + * For some reason the download target size is not updated, even if the code + * is "apparently" already executing and awaiting for refresh(). + * TODO(Bug 1814364): Figure out a proper fix for this. + */ +async function expectNonZeroDownloadTargetSize(downloadTarget) { + todo_check_true(downloadTarget.size, "Size should not be zero."); + if (!downloadTarget.size) { + await downloadTarget.refresh(); + } + return downloadTarget.size; +} + +/** + * Waits for an attempt to launch a file, and returns the nsIMIMEInfo used for + * the launch, or null if the file was launched with the default handler. + */ +function waitForFileLaunched() { + return new Promise(resolve => { + let waitFn = base => ({ + launchFile(file, mimeInfo) { + Integration.downloads.unregister(waitFn); + if ( + !mimeInfo || + mimeInfo.preferredAction == Ci.nsIMIMEInfo.useSystemDefault + ) { + resolve(null); + } else { + resolve(mimeInfo); + } + return Promise.resolve(); + }, + }); + Integration.downloads.register(waitFn); + }); +} + +/** + * Waits for an attempt to show the directory where a file is located, and + * returns the path of the file. + */ +function waitForDirectoryShown() { + return new Promise(resolve => { + let waitFn = base => ({ + showContainingDirectory(path) { + Integration.downloads.unregister(waitFn); + resolve(path); + return Promise.resolve(); + }, + }); + Integration.downloads.register(waitFn); + }); +} + +// Tests + +/** + * Executes a download and checks its basic properties after construction. + * The download is started by constructing the simplest Download object with + * the "copy" saver, or using the legacy nsITransfer interface. + */ +add_task(async function test_basic() { + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + + let download; + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we have control over the download, thus + // we can check its basic properties before it starts. + download = await Downloads.createDownload({ + source: { url: httpUrl("source.txt") }, + target: { path: targetFile.path }, + saver: { type: "copy" }, + }); + + Assert.equal(download.source.url, httpUrl("source.txt")); + Assert.equal(download.target.path, targetFile.path); + + await download.start(); + } else { + // When testing DownloadLegacySaver, the download is already started when it + // is created, thus we must check its basic properties while in progress. + download = await promiseStartLegacyDownload(null, { targetFile }); + + Assert.equal(download.source.url, httpUrl("source.txt")); + Assert.equal(download.target.path, targetFile.path); + + await promiseDownloadStopped(download); + } + + // Check additional properties on the finished download. + Assert.equal(download.source.referrerInfo, null); + + await promiseVerifyTarget(download.target, TEST_DATA_SHORT); +}); + +/** + * Executes a download with the tryToKeepPartialData property set, and ensures + * that the file is saved correctly. When testing DownloadLegacySaver, the + * download is executed using the nsIExternalHelperAppService component. + */ +add_task(async function test_basic_tryToKeepPartialData() { + let download = await promiseStartDownload_tryToKeepPartialData({ + useLegacySaver: gUseLegacySaver, + }); + continueResponses(); + await promiseDownloadStopped(download); + + // The target file should now have been created, and the ".part" file deleted. + await promiseVerifyTarget(download.target, TEST_DATA_SHORT + TEST_DATA_SHORT); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); + Assert.equal(32, download.saver.getSha256Hash().length); +}); + +/** + * Tests that the channelIsForDownload property is set for the network request, + * and that the request is marked as throttleable. + */ +add_task(async function test_channelIsForDownload_classFlags() { + let downloadChannel = null; + + // We use a different method based on whether we are testing legacy downloads. + if (!gUseLegacySaver) { + let download = await Downloads.createDownload({ + source: { + url: httpUrl("interruptible_resumable.txt"), + async adjustChannel(channel) { + downloadChannel = channel; + }, + }, + target: getTempFile(TEST_TARGET_FILE_NAME).path, + }); + await download.start(); + } else { + // Start a download using nsIExternalHelperAppService, but ensure it cannot + // finish before we retrieve the "request" property. + mustInterruptResponses(); + let download = await promiseStartExternalHelperAppServiceDownload(); + downloadChannel = download.saver.request; + continueResponses(); + await promiseDownloadStopped(download); + } + + Assert.ok( + downloadChannel.QueryInterface(Ci.nsIHttpChannelInternal) + .channelIsForDownload + ); + + // Throttleable is the only class flag assigned to downloads. + Assert.equal( + downloadChannel.QueryInterface(Ci.nsIClassOfService).classFlags, + Ci.nsIClassOfService.Throttleable + ); +}); + +/** + * Tests the permissions of the final target file once the download finished. + */ +add_task(async function test_unix_permissions() { + // This test is only executed on some Desktop systems. + if ( + Services.appinfo.OS != "Darwin" && + Services.appinfo.OS != "Linux" && + Services.appinfo.OS != "WINNT" + ) { + info("Skipping test."); + return; + } + + let launcherPath = getTempFile("app-launcher").path; + + for (let autoDelete of [false, true]) { + for (let isPrivate of [false, true]) { + for (let launchWhenSucceeded of [false, true]) { + info( + "Checking " + + JSON.stringify({ autoDelete, isPrivate, launchWhenSucceeded }) + ); + + Services.prefs.setBoolPref(kDeleteTempFileOnExit, autoDelete); + + let download; + if (!gUseLegacySaver) { + download = await Downloads.createDownload({ + source: { url: httpUrl("source.txt"), isPrivate }, + target: getTempFile(TEST_TARGET_FILE_NAME).path, + launchWhenSucceeded, + launcherPath, + }); + await download.start(); + } else { + download = await promiseStartLegacyDownload(httpUrl("source.txt"), { + isPrivate, + launchWhenSucceeded, + launcherPath: launchWhenSucceeded && launcherPath, + }); + await promiseDownloadStopped(download); + } + + let isTemporary = launchWhenSucceeded && isPrivate; + let stat = await IOUtils.stat(download.target.path); + if (Services.appinfo.OS == "WINNT") { + // On Windows + // Temporary downloads should be read-only + Assert.equal(stat.permissions, isTemporary ? 0o444 : 0o666); + } else { + // On Linux, Mac + // Temporary downloads should be read-only and not accessible to other + // users, while permanently downloaded files should be readable and + // writable as specified by the system umask. + Assert.equal( + stat.permissions, + isTemporary ? 0o400 : 0o666 & ~Services.sysinfo.getProperty("umask") + ); + } + } + } + } + + // Clean up the changes to the preference. + Services.prefs.clearUserPref(kDeleteTempFileOnExit); +}); + +/** + * Tests the zone information of the final target once the download finished. + */ +add_task(async function test_windows_zoneInformation() { + // This test is only executed on Windows, and in order to work correctly it + // requires the local user applicaton data directory to be on an NTFS file + // system. We use this directory because it is more likely to be on the local + // system installation drive, while the temporary directory used by the test + // environment is on the same drive as the test sources. + if (Services.appinfo.OS != "WINNT") { + info("Skipping test."); + return; + } + + let normalTargetFile = FileUtils.getFile("LocalAppData", [ + "xpcshell-download-test.txt", + ]); + + // The template file name lenght is more than MAX_PATH characters. The final + // full path will be shortened to MAX_PATH length by the createUnique call. + let longTargetFile = FileUtils.getFile("LocalAppData", [ + "T".repeat(256) + ".txt", + ]); + longTargetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600); + + const httpSourceUrl = httpUrl("source.txt"); + const dataSourceUrl = "data:text/html," + TEST_DATA_SHORT; + + function createReferrerInfo( + aReferrer, + aRefererPolicy = Ci.nsIReferrerInfo.EMPTY + ) { + return new ReferrerInfo(aRefererPolicy, true, NetUtil.newURI(aReferrer)); + } + + const tests = [ + { + expectedZoneId: + "[ZoneTransfer]\r\nZoneId=3\r\nHostUrl=" + httpSourceUrl + "\r\n", + }, + { + targetFile: longTargetFile, + expectedZoneId: + "[ZoneTransfer]\r\nZoneId=3\r\nHostUrl=" + httpSourceUrl + "\r\n", + }, + { + sourceUrl: dataSourceUrl, + expectedZoneId: + "[ZoneTransfer]\r\nZoneId=3\r\nHostUrl=about:internet\r\n", + }, + { + options: { + referrerInfo: createReferrerInfo( + TEST_REFERRER_URL, + Ci.nsIReferrerInfo.UNSAFE_URL + ), + }, + expectedZoneId: + "[ZoneTransfer]\r\nZoneId=3\r\n" + + "ReferrerUrl=" + + TEST_REFERRER_URL + + "\r\n" + + "HostUrl=" + + httpSourceUrl + + "\r\n", + }, + { + options: { referrerInfo: createReferrerInfo(dataSourceUrl) }, + expectedZoneId: + "[ZoneTransfer]\r\nZoneId=3\r\nHostUrl=" + httpSourceUrl + "\r\n", + }, + { + options: { + referrerInfo: createReferrerInfo("http://example.com/a\rb\nc"), + }, + expectedZoneId: + "[ZoneTransfer]\r\nZoneId=3\r\n" + + "ReferrerUrl=http://example.com/\r\n" + + "HostUrl=" + + httpSourceUrl + + "\r\n", + }, + { + options: { isPrivate: true }, + expectedZoneId: "[ZoneTransfer]\r\nZoneId=3\r\n", + }, + { + options: { + referrerInfo: createReferrerInfo(TEST_REFERRER_URL), + isPrivate: true, + }, + expectedZoneId: "[ZoneTransfer]\r\nZoneId=3\r\n", + }, + ]; + for (const test of tests) { + const sourceUrl = test.sourceUrl || httpSourceUrl; + const targetFile = test.targetFile || normalTargetFile; + info(targetFile.path); + try { + if (!gUseLegacySaver) { + let download = await Downloads.createDownload({ + source: test.options + ? Object.assign({ url: sourceUrl }, test.options) + : sourceUrl, + target: targetFile.path, + }); + await download.start(); + } else { + let download = await promiseStartLegacyDownload( + sourceUrl, + Object.assign({ targetFile }, test.options || {}) + ); + await promiseDownloadStopped(download); + } + await promiseVerifyContents(targetFile.path, TEST_DATA_SHORT); + + let path = targetFile.path + ":Zone.Identifier"; + if (Services.appinfo.OS === "WINNT") { + path = PathUtils.toExtendedWindowsPath(path); + } + + Assert.equal(await IOUtils.readUTF8(path), test.expectedZoneId); + } finally { + await IOUtils.remove(targetFile.path); + } + } +}); + +/** + * Checks the referrer for downloads. + */ +add_task(async function test_referrer() { + let sourcePath = "/test_referrer.txt"; + let sourceUrl = httpUrl("test_referrer.txt"); + let dataSourceUrl = "data:text/html,<html><body></body></html>"; + + function cleanup() { + gHttpServer.registerPathHandler(sourcePath, null); + } + registerCleanupFunction(cleanup); + + gHttpServer.registerPathHandler(sourcePath, function (aRequest, aResponse) { + aResponse.setHeader("Content-Type", "text/plain", false); + + Assert.ok(aRequest.hasHeader("Referer")); + Assert.equal(aRequest.getHeader("Referer"), TEST_REFERRER_URL); + }); + let download; + let referrerInfo = new ReferrerInfo( + Ci.nsIReferrerInfo.UNSAFE_URL, + true, + NetUtil.newURI(TEST_REFERRER_URL) + ); + + if (!gUseLegacySaver) { + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + let targetPath = targetFile.path; + + download = await Downloads.createDownload({ + source: { url: sourceUrl, referrerInfo }, + target: targetPath, + }); + + Assert.ok(download.source.referrerInfo.equals(referrerInfo)); + await download.start(); + + download = await Downloads.createDownload({ + source: { url: sourceUrl, referrerInfo, isPrivate: true }, + target: targetPath, + }); + Assert.ok(download.source.referrerInfo.equals(referrerInfo)); + await download.start(); + + // Test the download still works for non-HTTP channel with referrer. + download = await Downloads.createDownload({ + source: { url: dataSourceUrl, referrerInfo }, + target: targetPath, + }); + Assert.ok(download.source.referrerInfo.equals(referrerInfo)); + await download.start(); + } else { + download = await promiseStartLegacyDownload(sourceUrl, { + referrerInfo, + }); + await promiseDownloadStopped(download); + checkEqualReferrerInfos(download.source.referrerInfo, referrerInfo); + + download = await promiseStartLegacyDownload(sourceUrl, { + referrerInfo, + isPrivate: true, + }); + await promiseDownloadStopped(download); + checkEqualReferrerInfos(download.source.referrerInfo, referrerInfo); + + download = await promiseStartLegacyDownload(dataSourceUrl, { + referrerInfo, + }); + await promiseDownloadStopped(download); + Assert.equal(download.source.referrerInfo, null); + } + + cleanup(); +}); + +/** + * Checks the adjustChannel callback for downloads. + */ +add_task(async function test_adjustChannel() { + const sourcePath = "/test_post.txt"; + const sourceUrl = httpUrl("test_post.txt"); + const targetPath = getTempFile(TEST_TARGET_FILE_NAME).path; + const customHeader = { name: "X-Answer", value: "42" }; + const postData = "Don't Panic"; + + function cleanup() { + gHttpServer.registerPathHandler(sourcePath, null); + } + registerCleanupFunction(cleanup); + + gHttpServer.registerPathHandler(sourcePath, aRequest => { + Assert.equal(aRequest.method, "POST"); + + Assert.ok(aRequest.hasHeader(customHeader.name)); + Assert.equal(aRequest.getHeader(customHeader.name), customHeader.value); + + const stream = aRequest.bodyInputStream; + const body = NetUtil.readInputStreamToString(stream, stream.available()); + Assert.equal(body, postData); + }); + + function adjustChannel(channel) { + channel.QueryInterface(Ci.nsIHttpChannel); + channel.setRequestHeader(customHeader.name, customHeader.value, false); + + const stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance( + Ci.nsIStringInputStream + ); + stream.setData(postData, postData.length); + + channel.QueryInterface(Ci.nsIUploadChannel2); + channel.explicitSetUploadStream(stream, null, -1, "POST", false); + + return Promise.resolve(); + } + + const download = await Downloads.createDownload({ + source: { url: sourceUrl, adjustChannel }, + target: targetPath, + }); + Assert.equal(download.source.adjustChannel, adjustChannel); + Assert.equal(download.toSerializable(), null); + await download.start(); + + cleanup(); +}); + +/** + * Checks initial and final state and progress for a successful download. + */ +add_task(async function test_initial_final_state() { + let download; + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we have control over the download, thus + // we can check its state before it starts. + download = await promiseNewDownload(); + + Assert.ok(download.stopped); + Assert.ok(!download.succeeded); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + Assert.equal(download.progress, 0); + Assert.ok(download.startTime === null); + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); + + await download.start(); + } else { + // When testing DownloadLegacySaver, the download is already started when it + // is created, thus we cannot check its initial state. + download = await promiseStartLegacyDownload(); + await promiseDownloadStopped(download); + } + + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + Assert.equal(download.progress, 100); + Assert.ok(isValidDate(download.startTime)); + Assert.ok(download.target.exists); + Assert.equal( + await expectNonZeroDownloadTargetSize(download.target), + TEST_DATA_SHORT.length + ); +}); + +/** + * Checks the notification of the final download state. + */ +add_task(async function test_final_state_notified() { + mustInterruptResponses(); + + let download = await promiseStartDownload(httpUrl("interruptible.txt")); + + let onchangeNotified = false; + let lastNotifiedStopped; + let lastNotifiedProgress; + download.onchange = function () { + onchangeNotified = true; + lastNotifiedStopped = download.stopped; + lastNotifiedProgress = download.progress; + }; + + // Allow the download to complete. + let promiseAttempt = download.start(); + continueResponses(); + await promiseAttempt; + + // The view should have been notified before the download completes. + Assert.ok(onchangeNotified); + Assert.ok(lastNotifiedStopped); + Assert.equal(lastNotifiedProgress, 100); +}); + +/** + * Checks intermediate progress for a successful download. + */ +add_task(async function test_intermediate_progress() { + mustInterruptResponses(); + + let download = await promiseStartDownload(httpUrl("interruptible.txt")); + + await promiseDownloadMidway(download); + + Assert.ok(download.hasProgress); + Assert.equal(download.currentBytes, TEST_DATA_SHORT.length); + Assert.equal(download.totalBytes, TEST_DATA_SHORT.length * 2); + + // The final file size should not be computed for in-progress downloads. + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); + + // Continue after the first chunk of data is fully received. + continueResponses(); + await promiseDownloadStopped(download); + + Assert.ok(download.stopped); + Assert.equal(download.progress, 100); + + await promiseVerifyTarget(download.target, TEST_DATA_SHORT + TEST_DATA_SHORT); +}); + +/** + * Downloads a file with a "Content-Length" of 0 and checks the progress. + */ +add_task(async function test_empty_progress() { + let download = await promiseStartDownload(httpUrl("empty.txt")); + await promiseDownloadStopped(download); + + Assert.ok(download.stopped); + Assert.ok(download.hasProgress); + Assert.equal(download.progress, 100); + Assert.equal(download.currentBytes, 0); + Assert.equal(download.totalBytes, 0); + + // We should have received the content type even for an empty file. + Assert.equal(download.contentType, "text/plain"); + + Assert.equal((await IOUtils.stat(download.target.path)).size, 0); + Assert.ok(download.target.exists); + Assert.equal(download.target.size, 0); +}); + +/** + * Downloads a file with a "Content-Length" of 0 with the tryToKeepPartialData + * property set, and ensures that the file is saved correctly. + */ +add_task(async function test_empty_progress_tryToKeepPartialData() { + // Start a new download and configure it to keep partially downloaded data. + let download; + if (!gUseLegacySaver) { + let targetFilePath = getTempFile(TEST_TARGET_FILE_NAME).path; + download = await Downloads.createDownload({ + source: httpUrl("empty.txt"), + target: { path: targetFilePath, partFilePath: targetFilePath + ".part" }, + }); + download.tryToKeepPartialData = true; + download.start().catch(() => {}); + } else { + // Start a download using nsIExternalHelperAppService, that is configured + // to keep partially downloaded data by default. + download = await promiseStartExternalHelperAppServiceDownload( + httpUrl("empty.txt") + ); + } + await promiseDownloadStopped(download); + + // The target file should now have been created, and the ".part" file deleted. + Assert.equal((await IOUtils.stat(download.target.path)).size, 0); + Assert.ok(download.target.exists); + Assert.equal(download.target.size, 0); + + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); + Assert.equal(32, download.saver.getSha256Hash().length); +}); + +/** + * Downloads an empty file with no "Content-Length" and checks the progress. + */ +add_task(async function test_empty_noprogress() { + let sourcePath = "/test_empty_noprogress.txt"; + let sourceUrl = httpUrl("test_empty_noprogress.txt"); + let deferRequestReceived = PromiseUtils.defer(); + + // Register an interruptible handler that notifies us when the request occurs. + function cleanup() { + gHttpServer.registerPathHandler(sourcePath, null); + } + registerCleanupFunction(cleanup); + + registerInterruptibleHandler( + sourcePath, + function firstPart(aRequest, aResponse) { + aResponse.setHeader("Content-Type", "text/plain", false); + deferRequestReceived.resolve(); + }, + function secondPart(aRequest, aResponse) {} + ); + + // Start the download, without allowing the request to finish. + mustInterruptResponses(); + let download; + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we have control over the download, thus + // we can hook its onchange callback that will be notified when the + // download starts. + download = await promiseNewDownload(sourceUrl); + + download.onchange = function () { + if (!download.stopped) { + Assert.ok(!download.hasProgress); + Assert.equal(download.currentBytes, 0); + Assert.equal(download.totalBytes, 0); + } + }; + + download.start().catch(() => {}); + } else { + // When testing DownloadLegacySaver, the download is already started when it + // is created, and it may have already made all needed property change + // notifications, thus there is no point in checking the onchange callback. + download = await promiseStartLegacyDownload(sourceUrl); + } + + // Wait for the request to be received by the HTTP server, but don't allow the + // request to finish yet. Before checking the download state, wait for the + // events to be processed by the client. + await deferRequestReceived.promise; + await promiseExecuteSoon(); + + // Check that this download has no progress report. + Assert.ok(!download.stopped); + Assert.ok(!download.hasProgress); + Assert.equal(download.currentBytes, 0); + Assert.equal(download.totalBytes, 0); + + // Now allow the response to finish. + continueResponses(); + await promiseDownloadStopped(download); + + // We should have received the content type even if no progress is reported. + Assert.equal(download.contentType, "text/plain"); + + // Verify the state of the completed download. + Assert.ok(download.stopped); + Assert.ok(!download.hasProgress); + Assert.equal(download.progress, 100); + Assert.equal(download.currentBytes, 0); + Assert.equal(download.totalBytes, 0); + Assert.ok(download.target.exists); + Assert.equal(download.target.size, 0); + + Assert.equal((await IOUtils.stat(download.target.path)).size, 0); +}); + +/** + * Calls the "start" method two times before the download is finished. + */ +add_task(async function test_start_twice() { + mustInterruptResponses(); + + let download; + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we have control over the download, thus + // we can start the download later during the test. + download = await promiseNewDownload(httpUrl("interruptible.txt")); + } else { + // When testing DownloadLegacySaver, the download is already started when it + // is created. Effectively, we are starting the download three times. + download = await promiseStartLegacyDownload(httpUrl("interruptible.txt")); + } + + // Call the start method two times. + let promiseAttempt1 = download.start(); + let promiseAttempt2 = download.start(); + + // Allow the download to finish. + continueResponses(); + + // Both promises should now be resolved. + await promiseAttempt1; + await promiseAttempt2; + + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + + await promiseVerifyTarget(download.target, TEST_DATA_SHORT + TEST_DATA_SHORT); +}); + +/** + * Cancels a download and verifies that its state is reported correctly. + */ +add_task(async function test_cancel_midway() { + mustInterruptResponses(); + + // In this test case, we execute different checks that are only possible with + // DownloadCopySaver or DownloadLegacySaver respectively. + let download; + let options = {}; + if (!gUseLegacySaver) { + download = await promiseNewDownload(httpUrl("interruptible.txt")); + } else { + download = await promiseStartLegacyDownload( + httpUrl("interruptible.txt"), + options + ); + } + + // Cancel the download after receiving the first part of the response. + let deferCancel = PromiseUtils.defer(); + let onchange = function () { + if (!download.stopped && !download.canceled && download.progress == 50) { + // Cancel the download immediately during the notification. + deferCancel.resolve(download.cancel()); + + // The state change happens immediately after calling "cancel", but + // temporary files or part files may still exist at this point. + Assert.ok(download.canceled); + } + }; + + // Register for the notification, but also call the function directly in + // case the download already reached the expected progress. This may happen + // when using DownloadLegacySaver. + download.onchange = onchange; + onchange(); + + let promiseAttempt; + if (!gUseLegacySaver) { + promiseAttempt = download.start(); + } + + // Wait on the promise returned by the "cancel" method to ensure that the + // cancellation process finished and temporary files were removed. + await deferCancel.promise; + + if (gUseLegacySaver) { + // The nsIWebBrowserPersist instance should have been canceled now. + Assert.equal(options.outPersist.result, Cr.NS_ERROR_ABORT); + } + + Assert.ok(download.stopped); + Assert.ok(download.canceled); + Assert.ok(download.error === null); + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); + + Assert.equal(false, await IOUtils.exists(download.target.path)); + + // Progress properties are not reset by canceling. + Assert.equal(download.progress, 50); + Assert.equal(download.totalBytes, TEST_DATA_SHORT.length * 2); + Assert.equal(download.currentBytes, TEST_DATA_SHORT.length); + + if (!gUseLegacySaver) { + // The promise returned by "start" should have been rejected meanwhile. + try { + await promiseAttempt; + do_throw("The download should have been canceled."); + } catch (ex) { + if (!(ex instanceof Downloads.Error)) { + throw ex; + } + Assert.ok(!ex.becauseSourceFailed); + Assert.ok(!ex.becauseTargetFailed); + } + } +}); + +/** + * Cancels a download while keeping partially downloaded data, and verifies that + * both the target file and the ".part" file are deleted. + */ +add_task(async function test_cancel_midway_tryToKeepPartialData() { + let download = await promiseStartDownload_tryToKeepPartialData({ + useLegacySaver: gUseLegacySaver, + }); + + Assert.ok(await IOUtils.exists(download.target.path)); + Assert.ok(await IOUtils.exists(download.target.partFilePath)); + + await download.cancel(); + await download.removePartialData(); + + Assert.ok(download.stopped); + Assert.ok(download.canceled); + Assert.ok(download.error === null); + + Assert.equal(false, await IOUtils.exists(download.target.path)); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); +}); + +/** + * Cancels a download right after starting it. + */ +add_task(async function test_cancel_immediately() { + mustInterruptResponses(); + + let download = await promiseStartDownload(httpUrl("interruptible.txt")); + + let promiseAttempt = download.start(); + Assert.ok(!download.stopped); + + let promiseCancel = download.cancel(); + Assert.ok(download.canceled); + + // At this point, we don't know whether the download has already stopped or + // is still waiting for cancellation. We can wait on the promise returned + // by the "start" method to know for sure. + try { + await promiseAttempt; + do_throw("The download should have been canceled."); + } catch (ex) { + if (!(ex instanceof Downloads.Error)) { + throw ex; + } + Assert.ok(!ex.becauseSourceFailed); + Assert.ok(!ex.becauseTargetFailed); + } + + Assert.ok(download.stopped); + Assert.ok(download.canceled); + Assert.ok(download.error === null); + + Assert.equal(false, await IOUtils.exists(download.target.path)); + + // Check that the promise returned by the "cancel" method has been resolved. + await promiseCancel; +}); + +/** + * Cancels and restarts a download sequentially. + */ +add_task(async function test_cancel_midway_restart() { + mustInterruptResponses(); + + let download = await promiseStartDownload(httpUrl("interruptible.txt")); + + // The first time, cancel the download midway. + await promiseDownloadMidway(download); + await download.cancel(); + + Assert.ok(download.stopped); + + // The second time, we'll provide the entire interruptible response. + continueResponses(); + download.onchange = null; + let promiseAttempt = download.start(); + + // Download state should have already been reset. + Assert.ok(!download.stopped); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + + // For the following test, we rely on the network layer reporting its progress + // asynchronously. Otherwise, there is nothing stopping the restarted + // download from reaching the same progress as the first request already. + Assert.equal(download.progress, 0); + Assert.equal(download.totalBytes, 0); + Assert.equal(download.currentBytes, 0); + + await promiseAttempt; + + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + + await promiseVerifyTarget(download.target, TEST_DATA_SHORT + TEST_DATA_SHORT); +}); + +/** + * Cancels a download and restarts it from where it stopped. + */ +add_task(async function test_cancel_midway_restart_tryToKeepPartialData() { + let download = await promiseStartDownload_tryToKeepPartialData({ + useLegacySaver: gUseLegacySaver, + }); + await download.cancel(); + + Assert.ok(download.stopped); + Assert.ok(download.hasPartialData); + + // We should have kept the partial data and an empty target file placeholder. + Assert.ok(await IOUtils.exists(download.target.path)); + await promiseVerifyContents(download.target.partFilePath, TEST_DATA_SHORT); + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); + + // Verify that the server sent the response from the start. + Assert.equal(gMostRecentFirstBytePos, 0); + + // The second time, we'll request and obtain the second part of the response, + // but we still stop when half of the remaining progress is reached. + let deferMidway = PromiseUtils.defer(); + download.onchange = function () { + if ( + !download.stopped && + !download.canceled && + download.currentBytes == Math.floor((TEST_DATA_SHORT.length * 3) / 2) + ) { + download.onchange = null; + deferMidway.resolve(); + } + }; + + mustInterruptResponses(); + let promiseAttempt = download.start(); + + // Continue when the number of bytes we received is correct, then check that + // progress is at about 75 percent. The exact figure may vary because of + // rounding issues, since the total number of bytes in the response might not + // be a multiple of four. + await deferMidway.promise; + Assert.ok(download.progress > 72 && download.progress < 78); + + // Now we allow the download to finish. + continueResponses(); + await promiseAttempt; + + // Check that the server now sent the second part only. + Assert.equal(gMostRecentFirstBytePos, TEST_DATA_SHORT.length); + + // The target file should now have been created, and the ".part" file deleted. + await promiseVerifyTarget(download.target, TEST_DATA_SHORT + TEST_DATA_SHORT); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); +}); + +/** + * Cancels a download while keeping partially downloaded data, then removes the + * data and restarts the download from the beginning. + */ +add_task(async function test_cancel_midway_restart_removePartialData() { + let download = await promiseStartDownload_tryToKeepPartialData({ + useLegacySaver: gUseLegacySaver, + }); + await download.cancel(); + await download.removePartialData(); + + Assert.ok(!download.hasPartialData); + Assert.equal(false, await IOUtils.exists(download.target.path)); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); + + // The second time, we'll request and obtain the entire response again. + continueResponses(); + await download.start(); + + // Verify that the server sent the response from the start. + Assert.equal(gMostRecentFirstBytePos, 0); + + // The target file should now have been created, and the ".part" file deleted. + await promiseVerifyTarget(download.target, TEST_DATA_SHORT + TEST_DATA_SHORT); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); +}); + +/** + * Cancels a download while keeping partially downloaded data, then removes the + * data and restarts the download from the beginning without keeping the partial + * data anymore. + */ +add_task( + async function test_cancel_midway_restart_tryToKeepPartialData_false() { + let download = await promiseStartDownload_tryToKeepPartialData({ + useLegacySaver: gUseLegacySaver, + }); + await download.cancel(); + + download.tryToKeepPartialData = false; + + // The above property change does not affect existing partial data. + Assert.ok(download.hasPartialData); + await promiseVerifyContents(download.target.partFilePath, TEST_DATA_SHORT); + + await download.removePartialData(); + Assert.equal(false, await IOUtils.exists(download.target.path)); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); + + // Restart the download from the beginning. + mustInterruptResponses(); + download.start().catch(() => {}); + + await promiseDownloadMidway(download); + await promisePartFileReady(download); + + // While the download is in progress, we should still have a ".part" file. + Assert.ok(!download.hasPartialData); + Assert.ok(await IOUtils.exists(download.target.path)); + Assert.ok(await IOUtils.exists(download.target.partFilePath)); + + // On Unix, verify that the file with the partially downloaded data is not + // accessible by other users on the system. + if (Services.appinfo.OS == "Darwin" || Services.appinfo.OS == "Linux") { + Assert.equal( + (await IOUtils.stat(download.target.partFilePath)).permissions, + 0o600 + ); + } + + await download.cancel(); + + // The ".part" file should be deleted now that the download is canceled. + Assert.ok(!download.hasPartialData); + Assert.equal(false, await IOUtils.exists(download.target.path)); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); + + // The third time, we'll request and obtain the entire response again. + continueResponses(); + await download.start(); + + // Verify that the server sent the response from the start. + Assert.equal(gMostRecentFirstBytePos, 0); + + // The target file should now have been created, and the ".part" file deleted. + await promiseVerifyTarget( + download.target, + TEST_DATA_SHORT + TEST_DATA_SHORT + ); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); + } +); + +/** + * Cancels a download right after starting it, then restarts it immediately. + */ +add_task(async function test_cancel_immediately_restart_immediately() { + mustInterruptResponses(); + + let download = await promiseStartDownload(httpUrl("interruptible.txt")); + let promiseAttempt = download.start(); + + Assert.ok(!download.stopped); + + download.cancel(); + Assert.ok(download.canceled); + + let promiseRestarted = download.start(); + Assert.ok(!download.stopped); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + + // For the following test, we rely on the network layer reporting its progress + // asynchronously. Otherwise, there is nothing stopping the restarted + // download from reaching the same progress as the first request already. + Assert.equal(download.hasProgress, false); + Assert.equal(download.progress, 0); + Assert.equal(download.totalBytes, 0); + Assert.equal(download.currentBytes, 0); + + // Ensure the next request is now allowed to complete, regardless of whether + // the canceled request was received by the server or not. + continueResponses(); + try { + await promiseAttempt; + // If we get here, it means that the first attempt actually succeeded. In + // fact, this could be a valid outcome, because the cancellation request may + // not have been processed in time before the download finished. + info("The download should have been canceled."); + } catch (ex) { + if (!(ex instanceof Downloads.Error)) { + throw ex; + } + Assert.ok(!ex.becauseSourceFailed); + Assert.ok(!ex.becauseTargetFailed); + } + + await promiseRestarted; + + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + + await promiseVerifyTarget(download.target, TEST_DATA_SHORT + TEST_DATA_SHORT); +}); + +/** + * Cancels a download midway, then restarts it immediately. + */ +add_task(async function test_cancel_midway_restart_immediately() { + mustInterruptResponses(); + + let download = await promiseStartDownload(httpUrl("interruptible.txt")); + let promiseAttempt = download.start(); + + // The first time, cancel the download midway. + await promiseDownloadMidway(download); + download.cancel(); + Assert.ok(download.canceled); + + let promiseRestarted = download.start(); + Assert.ok(!download.stopped); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + + // For the following test, we rely on the network layer reporting its progress + // asynchronously. Otherwise, there is nothing stopping the restarted + // download from reaching the same progress as the first request already. + Assert.equal(download.hasProgress, false); + Assert.equal(download.progress, 0); + Assert.equal(download.totalBytes, 0); + Assert.equal(download.currentBytes, 0); + + // The second request is allowed to complete. + continueResponses(); + try { + await promiseAttempt; + do_throw("The download should have been canceled."); + } catch (ex) { + if (!(ex instanceof Downloads.Error)) { + throw ex; + } + Assert.ok(!ex.becauseSourceFailed); + Assert.ok(!ex.becauseTargetFailed); + } + + await promiseRestarted; + + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + + await promiseVerifyTarget(download.target, TEST_DATA_SHORT + TEST_DATA_SHORT); +}); + +/** + * Calls the "cancel" method on a successful download. + */ +add_task(async function test_cancel_successful() { + let download = await promiseStartDownload(); + await promiseDownloadStopped(download); + + // The cancel method should succeed with no effect. + await download.cancel(); + + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + + await promiseVerifyTarget(download.target, TEST_DATA_SHORT); +}); + +/** + * Calls the "cancel" method two times in a row. + */ +add_task(async function test_cancel_twice() { + mustInterruptResponses(); + + let download = await promiseStartDownload(httpUrl("interruptible.txt")); + + let promiseAttempt = download.start(); + Assert.ok(!download.stopped); + + let promiseCancel1 = download.cancel(); + Assert.ok(download.canceled); + let promiseCancel2 = download.cancel(); + + try { + await promiseAttempt; + do_throw("The download should have been canceled."); + } catch (ex) { + if (!(ex instanceof Downloads.Error)) { + throw ex; + } + Assert.ok(!ex.becauseSourceFailed); + Assert.ok(!ex.becauseTargetFailed); + } + + // Both promises should now be resolved. + await promiseCancel1; + await promiseCancel2; + + Assert.ok(download.stopped); + Assert.ok(!download.succeeded); + Assert.ok(download.canceled); + Assert.ok(download.error === null); + + Assert.equal(false, await IOUtils.exists(download.target.path)); +}); + +/** + * Checks the "refresh" method for succeeded downloads. + */ +add_task(async function test_refresh_succeeded() { + let download = await promiseStartDownload(); + await promiseDownloadStopped(download); + + // The DownloadTarget properties should be the same after calling "refresh". + await download.refresh(); + await promiseVerifyTarget(download.target, TEST_DATA_SHORT); + + // If the file is removed, only the "exists" property should change, and the + // "size" property should keep its previous value. + await IOUtils.move(download.target.path, `${download.target.path}.old`); + await download.refresh(); + Assert.ok(!download.target.exists); + Assert.equal( + await expectNonZeroDownloadTargetSize(download.target), + TEST_DATA_SHORT.length + ); + + // The DownloadTarget properties should be restored when the file is put back. + await IOUtils.move(`${download.target.path}.old`, download.target.path); + await download.refresh(); + await promiseVerifyTarget(download.target, TEST_DATA_SHORT); +}); + +/** + * Checks that a download cannot be restarted after the "finalize" method. + */ +add_task(async function test_finalize() { + mustInterruptResponses(); + + let download = await promiseStartDownload(httpUrl("interruptible.txt")); + + let promiseFinalized = download.finalize(); + + try { + await download.start(); + do_throw("It should not be possible to restart after finalization."); + } catch (ex) {} + + await promiseFinalized; + + Assert.ok(download.stopped); + Assert.ok(!download.succeeded); + Assert.ok(download.canceled); + Assert.ok(download.error === null); + + Assert.equal(false, await IOUtils.exists(download.target.path)); +}); + +/** + * Checks that the "finalize" method can remove partially downloaded data. + */ +add_task(async function test_finalize_tryToKeepPartialData() { + // Check finalization without removing partial data. + let download = await promiseStartDownload_tryToKeepPartialData({ + useLegacySaver: gUseLegacySaver, + }); + await download.finalize(); + + Assert.ok(download.hasPartialData); + Assert.ok(await IOUtils.exists(download.target.path)); + Assert.ok(await IOUtils.exists(download.target.partFilePath)); + + // Clean up. + await download.removePartialData(); + + // Check finalization while removing partial data. + download = await promiseStartDownload_tryToKeepPartialData({ + useLegacySaver: gUseLegacySaver, + }); + await download.finalize(true); + + Assert.ok(!download.hasPartialData); + Assert.equal(false, await IOUtils.exists(download.target.path)); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); +}); + +/** + * Checks that whenSucceeded returns a promise that is resolved after a restart. + */ +add_task(async function test_whenSucceeded_after_restart() { + mustInterruptResponses(); + + let promiseSucceeded; + + let download; + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we have control over the download, thus + // we can verify getting a reference before the first download attempt. + download = await promiseNewDownload(httpUrl("interruptible.txt")); + promiseSucceeded = download.whenSucceeded(); + download.start().catch(() => {}); + } else { + // When testing DownloadLegacySaver, the download is already started when it + // is created, thus we cannot get the reference before the first attempt. + download = await promiseStartLegacyDownload(httpUrl("interruptible.txt")); + promiseSucceeded = download.whenSucceeded(); + } + + // Cancel the first download attempt. + await download.cancel(); + + // The second request is allowed to complete. + continueResponses(); + download.start().catch(() => {}); + + // Wait for the download to finish by waiting on the whenSucceeded promise. + await promiseSucceeded; + + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + + await promiseVerifyTarget(download.target, TEST_DATA_SHORT + TEST_DATA_SHORT); +}); + +/** + * Ensures download error details are reported on network failures. + */ +add_task(async function test_error_source() { + let serverSocket = startFakeServer(); + try { + let sourceUrl = "http://localhost:" + serverSocket.port + "/source.txt"; + + let download; + try { + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we want to check that the promise + // returned by the "start" method is rejected. + download = await promiseNewDownload(sourceUrl); + + Assert.ok(download.error === null); + + await download.start(); + } else { + // When testing DownloadLegacySaver, we cannot be sure whether we are + // testing the promise returned by the "start" method or we are testing + // the "error" property checked by promiseDownloadStopped. This happens + // because we don't have control over when the download is started. + download = await promiseStartLegacyDownload(sourceUrl); + await promiseDownloadStopped(download); + } + do_throw("The download should have failed."); + } catch (ex) { + if (!(ex instanceof Downloads.Error) || !ex.becauseSourceFailed) { + throw ex; + } + // A specific error object is thrown when reading from the source fails. + } + + // Check the properties now that the download stopped. + Assert.ok(download.stopped); + Assert.ok(!download.canceled); + Assert.ok(download.error !== null); + Assert.ok(download.error.becauseSourceFailed); + Assert.ok(!download.error.becauseTargetFailed); + + Assert.equal(false, await IOUtils.exists(download.target.path)); + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); + } finally { + serverSocket.close(); + } +}); + +/** + * Ensures a download error is reported when receiving less bytes than what was + * specified in the Content-Length header. + */ +add_task(async function test_error_source_partial() { + let sourceUrl = httpUrl("shorter-than-content-length-http-1-1.txt"); + + let enforcePref = Services.prefs.getBoolPref( + "network.http.enforce-framing.http1" + ); + Services.prefs.setBoolPref("network.http.enforce-framing.http1", true); + + function cleanup() { + Services.prefs.setBoolPref( + "network.http.enforce-framing.http1", + enforcePref + ); + } + registerCleanupFunction(cleanup); + + let download; + try { + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we want to check that the promise + // returned by the "start" method is rejected. + download = await promiseNewDownload(sourceUrl); + + Assert.ok(download.error === null); + + await download.start(); + } else { + // When testing DownloadLegacySaver, we cannot be sure whether we are + // testing the promise returned by the "start" method or we are testing + // the "error" property checked by promiseDownloadStopped. This happens + // because we don't have control over when the download is started. + download = await promiseStartLegacyDownload(sourceUrl); + await promiseDownloadStopped(download); + } + do_throw("The download should have failed."); + } catch (ex) { + if (!(ex instanceof Downloads.Error) || !ex.becauseSourceFailed) { + throw ex; + } + // A specific error object is thrown when reading from the source fails. + } + + // Check the properties now that the download stopped. + Assert.ok(download.stopped); + Assert.ok(!download.canceled); + Assert.ok(download.error !== null); + Assert.ok(download.error.becauseSourceFailed); + Assert.ok(!download.error.becauseTargetFailed); + Assert.equal(download.error.result, Cr.NS_ERROR_NET_PARTIAL_TRANSFER); + + Assert.equal(false, await IOUtils.exists(download.target.path)); + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); +}); + +/** + * Ensures a download error is reported when an RST packet is received. + */ +add_task(async function test_error_source_netreset() { + if (AppConstants.platform == "win") { + return; + } + + let download; + try { + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we want to check that the promise + // returned by the "start" method is rejected. + download = await promiseNewDownload(httpUrl("netreset.txt")); + + Assert.ok(download.error === null); + + await download.start(); + } else { + // When testing DownloadLegacySaver, we cannot be sure whether we are + // testing the promise returned by the "start" method or we are testing + // the "error" property checked by promiseDownloadStopped. This happens + // because we don't have control over when the download is started. + download = await promiseStartLegacyDownload(httpUrl("netreset.txt")); + await promiseDownloadStopped(download); + } + do_throw("The download should have failed."); + } catch (ex) { + if (!(ex instanceof Downloads.Error) || !ex.becauseSourceFailed) { + throw ex; + } + // A specific error object is thrown when reading from the source fails. + } + + // Check the properties now that the download stopped. + Assert.ok(download.stopped); + Assert.ok(!download.canceled); + Assert.ok(download.error !== null); + Assert.ok(download.error.becauseSourceFailed); + Assert.ok(!download.error.becauseTargetFailed); + Assert.equal(download.error.result, Cr.NS_ERROR_NET_RESET); + + Assert.equal(false, await IOUtils.exists(download.target.path)); +}); + +/** + * Ensures download error details are reported on local writing failures. + */ +add_task(async function test_error_target() { + // Create a file without write access permissions before downloading. + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + targetFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0); + try { + let download; + try { + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we want to check that the promise + // returned by the "start" method is rejected. + download = await Downloads.createDownload({ + source: httpUrl("source.txt"), + target: targetFile, + }); + await download.start(); + } else { + // When testing DownloadLegacySaver, we cannot be sure whether we are + // testing the promise returned by the "start" method or we are testing + // the "error" property checked by promiseDownloadStopped. This happens + // because we don't have control over when the download is started. + download = await promiseStartLegacyDownload(null, { targetFile }); + await promiseDownloadStopped(download); + } + do_throw("The download should have failed."); + } catch (ex) { + if (!(ex instanceof Downloads.Error) || !ex.becauseTargetFailed) { + throw ex; + } + // A specific error object is thrown when writing to the target fails. + } + + // Check the properties now that the download stopped. + Assert.ok(download.stopped); + Assert.ok(!download.canceled); + Assert.ok(download.error !== null); + Assert.ok(download.error.becauseTargetFailed); + Assert.ok(!download.error.becauseSourceFailed); + + // Check unserializing a download with an errorObj and restarting it will + // clear the errorObj initially. + let serializable = download.toSerializable(); + Assert.ok(serializable.errorObj, "Ensure we have an errorObj initially"); + let reserialized = JSON.parse(JSON.stringify(serializable)); + download = await Downloads.createDownload(reserialized); + let promise = download.start().catch(() => {}); + serializable = download.toSerializable(); + Assert.ok(!serializable.errorObj, "Ensure we didn't persist the errorObj"); + await promise; + } finally { + // Restore the default permissions to allow deleting the file on Windows. + if (targetFile.exists()) { + targetFile.permissions = FileUtils.PERMS_FILE; + targetFile.remove(false); + } + } +}); + +/** + * Restarts a failed download. + */ +add_task(async function test_error_restart() { + let download; + + // Create a file without write access permissions before downloading. + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + targetFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0); + try { + // Use DownloadCopySaver or DownloadLegacySaver based on the test run, + // specifying the target file we created. + if (!gUseLegacySaver) { + download = await Downloads.createDownload({ + source: httpUrl("source.txt"), + target: targetFile, + }); + download.start().catch(() => {}); + } else { + download = await promiseStartLegacyDownload(null, { targetFile }); + } + await promiseDownloadStopped(download); + do_throw("The download should have failed."); + } catch (ex) { + if (!(ex instanceof Downloads.Error) || !ex.becauseTargetFailed) { + throw ex; + } + // A specific error object is thrown when writing to the target fails. + } finally { + // Restore the default permissions to allow deleting the file on Windows. + if (targetFile.exists()) { + targetFile.permissions = FileUtils.PERMS_FILE; + + // Also for Windows, rename the file before deleting. This makes the + // current file name available immediately for a new file, while deleting + // in place prevents creation of a file with the same name for some time. + targetFile.moveTo(null, targetFile.leafName + ".delete.tmp"); + targetFile.remove(false); + } + } + + // Restart the download and wait for completion. + await download.start(); + + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.canceled); + Assert.ok(download.error === null); + Assert.equal(download.progress, 100); + + await promiseVerifyTarget(download.target, TEST_DATA_SHORT); +}); + +/** + * Executes download in both public and private modes. + */ +add_task(async function test_public_and_private() { + let sourcePath = "/test_public_and_private.txt"; + let sourceUrl = httpUrl("test_public_and_private.txt"); + let testCount = 0; + + // Apply pref to allow all cookies. + Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); + + function cleanup() { + Services.prefs.clearUserPref("network.cookie.cookieBehavior"); + Services.cookies.removeAll(); + gHttpServer.registerPathHandler(sourcePath, null); + } + registerCleanupFunction(cleanup); + + gHttpServer.registerPathHandler(sourcePath, function (aRequest, aResponse) { + aResponse.setHeader("Content-Type", "text/plain", false); + + if (testCount == 0) { + // No cookies should exist for first public download. + Assert.ok(!aRequest.hasHeader("Cookie")); + aResponse.setHeader("Set-Cookie", "foobar=1", false); + testCount++; + } else if (testCount == 1) { + // The cookie should exists for second public download. + Assert.ok(aRequest.hasHeader("Cookie")); + Assert.equal(aRequest.getHeader("Cookie"), "foobar=1"); + testCount++; + } else if (testCount == 2) { + // No cookies should exist for first private download. + Assert.ok(!aRequest.hasHeader("Cookie")); + } + }); + + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + await Downloads.fetch(sourceUrl, targetFile); + await Downloads.fetch(sourceUrl, targetFile); + + if (!gUseLegacySaver) { + let download = await Downloads.createDownload({ + source: { url: sourceUrl, isPrivate: true }, + target: targetFile, + }); + await download.start(); + } else { + let download = await promiseStartLegacyDownload(sourceUrl, { + isPrivate: true, + }); + await promiseDownloadStopped(download); + } + + cleanup(); +}); + +/** + * Checks the startTime gets updated even after a restart. + */ +add_task(async function test_cancel_immediately_restart_and_check_startTime() { + let download = await promiseStartDownload(); + + let startTime = download.startTime; + Assert.ok(isValidDate(download.startTime)); + + await download.cancel(); + Assert.equal(download.startTime.getTime(), startTime.getTime()); + + // Wait for a timeout. + await promiseTimeout(10); + + await download.start(); + Assert.ok(download.startTime.getTime() > startTime.getTime()); +}); + +/** + * Executes download with content-encoding. + */ +add_task(async function test_with_content_encoding() { + let sourcePath = "/test_with_content_encoding.txt"; + let sourceUrl = httpUrl("test_with_content_encoding.txt"); + + function cleanup() { + gHttpServer.registerPathHandler(sourcePath, null); + } + registerCleanupFunction(cleanup); + + gHttpServer.registerPathHandler(sourcePath, function (aRequest, aResponse) { + aResponse.setHeader("Content-Type", "text/plain", false); + aResponse.setHeader("Content-Encoding", "gzip", false); + aResponse.setHeader( + "Content-Length", + "" + TEST_DATA_SHORT_GZIP_ENCODED.length, + false + ); + + let bos = new BinaryOutputStream(aResponse.bodyOutputStream); + bos.writeByteArray(TEST_DATA_SHORT_GZIP_ENCODED); + }); + + let download = await promiseStartDownload(sourceUrl); + await promiseDownloadStopped(download); + + Assert.equal(download.progress, 100); + Assert.equal(download.totalBytes, TEST_DATA_SHORT_GZIP_ENCODED.length); + + // Ensure the content matches the decoded test data. + await promiseVerifyTarget(download.target, TEST_DATA_SHORT); + + cleanup(); +}); + +/** + * Checks that the file is not decoded if the extension matches the encoding. + */ +add_task(async function test_with_content_encoding_ignore_extension() { + let sourcePath = "/test_with_content_encoding_ignore_extension.gz"; + let sourceUrl = httpUrl("test_with_content_encoding_ignore_extension.gz"); + + function cleanup() { + gHttpServer.registerPathHandler(sourcePath, null); + } + registerCleanupFunction(cleanup); + + gHttpServer.registerPathHandler(sourcePath, function (aRequest, aResponse) { + aResponse.setHeader("Content-Type", "text/plain", false); + aResponse.setHeader("Content-Encoding", "gzip", false); + aResponse.setHeader( + "Content-Length", + "" + TEST_DATA_SHORT_GZIP_ENCODED.length, + false + ); + + let bos = new BinaryOutputStream(aResponse.bodyOutputStream); + bos.writeByteArray(TEST_DATA_SHORT_GZIP_ENCODED); + }); + + let download = await promiseStartDownload(sourceUrl); + await promiseDownloadStopped(download); + + Assert.equal(download.progress, 100); + Assert.equal(download.totalBytes, TEST_DATA_SHORT_GZIP_ENCODED.length); + Assert.equal( + await expectNonZeroDownloadTargetSize(download.target), + TEST_DATA_SHORT_GZIP_ENCODED.length + ); + + // Ensure the content matches the encoded test data. We convert the data to a + // string before executing the content check. + await promiseVerifyTarget( + download.target, + String.fromCharCode.apply(String, TEST_DATA_SHORT_GZIP_ENCODED) + ); + + cleanup(); +}); + +/** + * Cancels and restarts a download sequentially with content-encoding. + */ +add_task(async function test_cancel_midway_restart_with_content_encoding() { + mustInterruptResponses(); + + let download = await promiseStartDownload(httpUrl("interruptible_gzip.txt")); + + // The first time, cancel the download midway. + await new Promise(resolve => { + let onchange = function () { + if ( + !download.stopped && + !download.canceled && + download.currentBytes == TEST_DATA_SHORT_GZIP_ENCODED_FIRST.length + ) { + resolve(download.cancel()); + } + }; + + // Register for the notification, but also call the function directly in + // case the download already reached the expected progress. + download.onchange = onchange; + onchange(); + }); + + Assert.ok(download.stopped); + + // The second time, we'll provide the entire interruptible response. + continueResponses(); + download.onchange = null; + await download.start(); + + Assert.equal(download.progress, 100); + Assert.equal(download.totalBytes, TEST_DATA_SHORT_GZIP_ENCODED.length); + + await promiseVerifyTarget(download.target, TEST_DATA_SHORT); +}); + +/** + * Download with parental controls enabled. + */ +add_task(async function test_blocked_parental_controls() { + let blockFn = base => ({ + shouldBlockForParentalControls: () => Promise.resolve(true), + }); + + Integration.downloads.register(blockFn); + function cleanup() { + Integration.downloads.unregister(blockFn); + } + registerCleanupFunction(cleanup); + + let download; + try { + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we want to check that the promise + // returned by the "start" method is rejected. + download = await promiseNewDownload(); + await download.start(); + } else { + // When testing DownloadLegacySaver, we cannot be sure whether we are + // testing the promise returned by the "start" method or we are testing + // the "error" property checked by promiseDownloadStopped. This happens + // because we don't have control over when the download is started. + download = await promiseStartLegacyDownload(); + await promiseDownloadStopped(download); + } + do_throw("The download should have blocked."); + } catch (ex) { + if (!(ex instanceof Downloads.Error) || !ex.becauseBlocked) { + throw ex; + } + Assert.ok(ex.becauseBlockedByParentalControls); + Assert.ok(download.error.becauseBlockedByParentalControls); + } + + // Now that the download stopped, the target file should not exist. + Assert.equal(false, await IOUtils.exists(download.target.path)); + + cleanup(); +}); + +/** + * Test a download that will be blocked by Windows parental controls by + * resulting in an HTTP status code of 450. + */ +add_task(async function test_blocked_parental_controls_httpstatus450() { + let download; + try { + if (!gUseLegacySaver) { + download = await promiseNewDownload(httpUrl("parentalblocked.zip")); + await download.start(); + } else { + download = await promiseStartLegacyDownload( + httpUrl("parentalblocked.zip") + ); + await promiseDownloadStopped(download); + } + do_throw("The download should have blocked."); + } catch (ex) { + if (!(ex instanceof Downloads.Error) || !ex.becauseBlocked) { + throw ex; + } + Assert.ok(ex.becauseBlockedByParentalControls); + Assert.ok(download.error.becauseBlockedByParentalControls); + Assert.ok(download.stopped); + } + + Assert.equal(false, await IOUtils.exists(download.target.path)); +}); + +/** + * Check that DownloadCopySaver can always retrieve the hash. + * DownloadLegacySaver can only retrieve the hash when + * nsIExternalHelperAppService is invoked. + */ +add_task(async function test_getSha256Hash() { + if (!gUseLegacySaver) { + let download = await promiseStartDownload(httpUrl("source.txt")); + await promiseDownloadStopped(download); + Assert.ok(download.stopped); + Assert.equal(32, download.saver.getSha256Hash().length); + } +}); + +/** + * Checks that application reputation blocks the download and the target file + * does not exist. + */ +add_task(async function test_blocked_applicationReputation() { + let download = await promiseBlockedDownload({ + keepPartialData: false, + keepBlockedData: false, + }); + + // Now that the download is blocked, the target file should not exist. + Assert.equal(false, await IOUtils.exists(download.target.path)); + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); + + // There should also be no blocked data in this case + Assert.ok(!download.hasBlockedData); +}); + +/** + * Checks that if a download restarts while processing an application reputation + * request, the status is handled correctly. + */ +add_task(async function test_blocked_applicationReputation_race() { + let isFirstShouldBlockCall = true; + + let blockFn = base => ({ + shouldBlockForReputationCheck(download) { + if (isFirstShouldBlockCall) { + isFirstShouldBlockCall = false; + + // 2. Cancel and restart the download before the first attempt has a + // chance to finish. + download.cancel(); + download.removePartialData(); + download.start(); + + // 3. Allow the first attempt to finish with a blocked response. + return Promise.resolve({ + shouldBlock: true, + verdict: Downloads.Error.BLOCK_VERDICT_UNCOMMON, + }); + } + + // 4/5. Don't block the download the second time. The race condition would + // occur with the first attempt regardless of whether the second one + // is blocked, but not blocking here makes the test simpler. + return Promise.resolve({ + shouldBlock: false, + verdict: "", + }); + }, + shouldKeepBlockedData: () => Promise.resolve(true), + }); + + Integration.downloads.register(blockFn); + function cleanup() { + Integration.downloads.unregister(blockFn); + } + registerCleanupFunction(cleanup); + + let download; + + try { + // 1. Start the download and get a reference to the promise asociated with + // the first attempt, before allowing the response to continue. + download = await promiseStartDownload_tryToKeepPartialData({ + useLegacySaver: gUseLegacySaver, + }); + let firstAttempt = promiseDownloadStopped(download); + continueResponses(); + + // 4/5. Wait for the first attempt to be completed. The result of this + // should appear as a cancellation. + await firstAttempt; + + do_throw("The first attempt should have been canceled."); + } catch (ex) { + // The "becauseBlocked" property should be false. + if (!(ex instanceof Downloads.Error) || ex.becauseBlocked) { + throw ex; + } + } + + // 6. Wait for the second attempt to be completed. + await promiseDownloadStopped(download); + + // 7. At this point, "hasBlockedData" should be false. + Assert.ok(!download.hasBlockedData); + + cleanup(); +}); + +/** + * Checks that application reputation blocks the download but maintains the + * blocked data, which will be deleted when the block is confirmed. + */ +add_task(async function test_blocked_applicationReputation_confirmBlock() { + let download = await promiseBlockedDownload({ + keepPartialData: true, + keepBlockedData: true, + }); + + Assert.ok(download.hasBlockedData); + Assert.equal((await IOUtils.stat(download.target.path)).size, 0); + Assert.ok(await IOUtils.exists(download.target.partFilePath)); + + await download.confirmBlock(); + + // After confirming the block the download should be in a failed state and + // have no downloaded data left on disk. + Assert.ok(download.stopped); + Assert.ok(!download.succeeded); + Assert.ok(!download.hasBlockedData); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); + Assert.equal(false, await IOUtils.exists(download.target.path)); + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); +}); + +/** + * Checks that application reputation blocks the download but maintains the + * blocked data, which will be used to complete the download when unblocking. + */ +add_task(async function test_blocked_applicationReputation_unblock() { + let download = await promiseBlockedDownload({ + keepPartialData: true, + keepBlockedData: true, + useLegacySaver: gUseLegacySaver, + }); + + Assert.ok(download.hasBlockedData); + Assert.equal((await IOUtils.stat(download.target.path)).size, 0); + Assert.ok(await IOUtils.exists(download.target.partFilePath)); + + await download.unblock(); + + // After unblocking the download should have succeeded and be + // present at the final path. + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.hasBlockedData); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); + await promiseVerifyTarget(download.target, TEST_DATA_SHORT + TEST_DATA_SHORT); + + // The only indication the download was previously blocked is the + // existence of the error, so we make sure it's still set. + Assert.ok(download.error instanceof Downloads.Error); + Assert.ok(download.error.becauseBlocked); + Assert.ok(download.error.becauseBlockedByReputationCheck); +}); + +/** + * Check that calling cancel on a blocked download will not cause errors + */ +add_task(async function test_blocked_applicationReputation_cancel() { + let download = await promiseBlockedDownload({ + keepPartialData: true, + keepBlockedData: true, + }); + + // This call should succeed on a blocked download. + await download.cancel(); + + // Calling cancel should not have changed the current state, the download + // should still be blocked. + Assert.ok(download.error.becauseBlockedByReputationCheck); + Assert.ok(download.stopped); + Assert.ok(!download.succeeded); + Assert.ok(download.hasBlockedData); +}); + +/** + * Checks that unblock and confirmBlock cannot race on a blocked download + */ +add_task(async function test_blocked_applicationReputation_decisionRace() { + let download = await promiseBlockedDownload({ + keepPartialData: true, + keepBlockedData: true, + }); + + let unblockPromise = download.unblock(); + let confirmBlockPromise = download.confirmBlock(); + + await confirmBlockPromise.then( + () => { + do_throw("confirmBlock should have failed."); + }, + () => {} + ); + + await unblockPromise; + + // After unblocking the download should have succeeded and be + // present at the final path. + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.hasBlockedData); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); + Assert.ok(await IOUtils.exists(download.target.path)); + + download = await promiseBlockedDownload({ + keepPartialData: true, + keepBlockedData: true, + }); + + confirmBlockPromise = download.confirmBlock(); + unblockPromise = download.unblock(); + + await unblockPromise.then( + () => { + do_throw("unblock should have failed."); + }, + () => {} + ); + + await confirmBlockPromise; + + // After confirming the block the download should be in a failed state and + // have no downloaded data left on disk. + Assert.ok(download.stopped); + Assert.ok(!download.succeeded); + Assert.ok(!download.hasBlockedData); + Assert.equal(false, await IOUtils.exists(download.target.partFilePath)); + Assert.equal(false, await IOUtils.exists(download.target.path)); + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); +}); + +/** + * Checks that unblocking a blocked download fails if the blocked data has been + * removed. + */ +add_task(async function test_blocked_applicationReputation_unblock() { + let download = await promiseBlockedDownload({ + keepPartialData: true, + keepBlockedData: true, + }); + + Assert.ok(download.hasBlockedData); + Assert.ok(await IOUtils.exists(download.target.partFilePath)); + + // Remove the blocked data without telling the download. + await IOUtils.remove(download.target.partFilePath); + + let unblockPromise = download.unblock(); + await unblockPromise.then( + () => { + do_throw("unblock should have failed."); + }, + () => {} + ); + + // Even though unblocking failed the download state should have been updated + // to reflect the lack of blocked data. + Assert.ok(!download.hasBlockedData); + Assert.ok(download.stopped); + Assert.ok(!download.succeeded); + Assert.ok(!download.target.exists); + Assert.equal(download.target.size, 0); +}); + +/** + * download.showContainingDirectory() action + */ +add_task(async function test_showContainingDirectory() { + let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path; + + let download = await Downloads.createDownload({ + source: { url: httpUrl("source.txt") }, + target: "", + }); + + let promiseDirectoryShown = waitForDirectoryShown(); + await download.showContainingDirectory(); + let path = await promiseDirectoryShown; + try { + new FileUtils.File(path); + do_throw("Should have failed because of an invalid path."); + } catch (ex) { + if (!(ex instanceof Components.Exception)) { + throw ex; + } + // Invalid paths on Windows are reported with NS_ERROR_FAILURE, + // but with NS_ERROR_FILE_UNRECOGNIZED_PATH on Mac/Linux + let validResult = + ex.result == Cr.NS_ERROR_FILE_UNRECOGNIZED_PATH || + ex.result == Cr.NS_ERROR_FAILURE; + Assert.ok(validResult); + } + + download = await Downloads.createDownload({ + source: { url: httpUrl("source.txt") }, + target: targetPath, + }); + + promiseDirectoryShown = waitForDirectoryShown(); + download.showContainingDirectory(); + await promiseDirectoryShown; +}); + +/** + * download.launch() action + */ +add_task(async function test_launch() { + let customLauncher = getTempFile("app-launcher"); + + // Test both with and without setting a custom application. + for (let launcherPath of [null, customLauncher.path]) { + let download; + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we have control over the download, thus + // we can test that file is not launched if download.succeeded is not set. + download = await Downloads.createDownload({ + source: httpUrl("source.txt"), + target: getTempFile(TEST_TARGET_FILE_NAME).path, + launcherPath, + launchWhenSucceeded: true, + }); + + try { + await download.launch(); + do_throw("Can't launch download file as it has not completed yet"); + } catch (ex) { + Assert.equal( + ex.message, + "launch can only be called if the download succeeded" + ); + } + + Assert.ok(download.launchWhenSucceeded); + await download.start(); + } else { + // When testing DownloadLegacySaver, the download is already started when + // it is created, thus we don't test calling "launch" before starting. + download = await promiseStartLegacyDownload(httpUrl("source.txt"), { + launcherPath, + launchWhenSucceeded: true, + }); + Assert.ok(download.launchWhenSucceeded); + await promiseDownloadStopped(download); + } + + let promiseFileLaunched = waitForFileLaunched(); + download.launch(); + let result = await promiseFileLaunched; + + // Verify that the results match the test case. + if (!launcherPath) { + // This indicates that the default handler has been chosen. + Assert.ok(result === null); + } else { + // Check the nsIMIMEInfo instance that would have been used for launching. + Assert.equal(result.preferredAction, Ci.nsIMIMEInfo.useHelperApp); + Assert.ok( + result.preferredApplicationHandler + .QueryInterface(Ci.nsILocalHandlerApp) + .executable.equals(customLauncher) + ); + } + } +}); + +/** + * Test passing an invalid path as the launcherPath property. + */ +add_task(async function test_launcherPath_invalid() { + let download = await Downloads.createDownload({ + source: { url: httpUrl("source.txt") }, + target: { path: getTempFile(TEST_TARGET_FILE_NAME).path }, + launcherPath: " ", + }); + + let promiseDownloadLaunched = new Promise(resolve => { + let waitFn = base => { + let launchOverride = { + launchDownload() { + Integration.downloads.unregister(waitFn); + let superPromise = super.launchDownload(...arguments); + resolve(superPromise); + return superPromise; + }, + }; + Object.setPrototypeOf(launchOverride, base); + return launchOverride; + }; + Integration.downloads.register(waitFn); + }); + + await download.start(); + try { + download.launch(); + await promiseDownloadLaunched; + do_throw("Can't launch file with invalid custom launcher"); + } catch (ex) { + if (!(ex instanceof Components.Exception)) { + throw ex; + } + // Invalid paths on Windows are reported with NS_ERROR_FAILURE, + // but with NS_ERROR_FILE_UNRECOGNIZED_PATH on Mac/Linux + let validResult = + ex.result == Cr.NS_ERROR_FILE_UNRECOGNIZED_PATH || + ex.result == Cr.NS_ERROR_FAILURE; + Assert.ok(validResult); + } +}); + +/** + * Tests that download.launch() is automatically called after + * the download finishes if download.launchWhenSucceeded = true + */ +add_task(async function test_launchWhenSucceeded() { + let customLauncher = getTempFile("app-launcher"); + + // Test both with and without setting a custom application. + for (let launcherPath of [null, customLauncher.path]) { + let promiseFileLaunched = waitForFileLaunched(); + + if (!gUseLegacySaver) { + let download = await Downloads.createDownload({ + source: httpUrl("source.txt"), + target: getTempFile(TEST_TARGET_FILE_NAME).path, + launchWhenSucceeded: true, + launcherPath, + }); + await download.start(); + } else { + let download = await promiseStartLegacyDownload(httpUrl("source.txt"), { + launcherPath, + launchWhenSucceeded: true, + }); + await promiseDownloadStopped(download); + } + + let result = await promiseFileLaunched; + + // Verify that the results match the test case. + if (!launcherPath) { + // This indicates that the default handler has been chosen. + Assert.ok(result === null); + } else { + // Check the nsIMIMEInfo instance that would have been used for launching. + Assert.equal(result.preferredAction, Ci.nsIMIMEInfo.useHelperApp); + Assert.ok( + result.preferredApplicationHandler + .QueryInterface(Ci.nsILocalHandlerApp) + .executable.equals(customLauncher) + ); + } + } +}); + +/** + * Tests that the proper content type is set for a normal download. + */ +add_task(async function test_contentType() { + let download = await promiseStartDownload(httpUrl("source.txt")); + await promiseDownloadStopped(download); + + Assert.equal("text/plain", download.contentType); +}); + +/** + * Tests that the serialization/deserialization of the startTime Date + * object works correctly. + */ +add_task(async function test_toSerializable_startTime() { + let download1 = await promiseStartDownload(httpUrl("source.txt")); + await promiseDownloadStopped(download1); + + let serializable = download1.toSerializable(); + let reserialized = JSON.parse(JSON.stringify(serializable)); + + let download2 = await Downloads.createDownload(reserialized); + + Assert.equal(download1.startTime.constructor.name, "Date"); + Assert.equal(download2.startTime.constructor.name, "Date"); + Assert.equal(download1.startTime.toJSON(), download2.startTime.toJSON()); +}); + +/** + * Checks that downloads are added to browsing history when they start. + */ +add_task(async function test_history() { + mustInterruptResponses(); + + let sourceUrl = httpUrl("interruptible.txt"); + + // We will wait for the visit to be notified during the download. + await PlacesUtils.history.clear(); + let promiseVisit = promiseWaitForVisit(sourceUrl); + + // Start a download that is not allowed to finish yet. + let download = await promiseStartDownload(sourceUrl); + let expectedFile = new FileUtils.File(download.target.path); + let expectedFileURI = Services.io.newFileURI(expectedFile); + let promiseAnnotation = waitForAnnotation( + sourceUrl, + "downloads/destinationFileURI", + expectedFileURI.spec + ); + + // The history and annotation notifications should be received before the download completes. + let [time, transitionType, lastKnownTitle] = await promiseVisit; + await promiseAnnotation; + + Assert.equal(time, download.startTime.getTime()); + Assert.equal(transitionType, Ci.nsINavHistoryService.TRANSITION_DOWNLOAD); + Assert.equal(lastKnownTitle, expectedFile.leafName); + + let pageInfo = await PlacesUtils.history.fetch(sourceUrl, { + includeAnnotations: true, + }); + Assert.equal( + pageInfo.annotations.get("downloads/destinationFileURI"), + expectedFileURI.spec, + "Should have saved the correct download target annotation." + ); + + // Restart and complete the download after clearing history. + await PlacesUtils.history.clear(); + download.cancel(); + continueResponses(); + await download.start(); + + // The restart should not have added a new history visit. + Assert.equal(false, await PlacesUtils.history.hasVisits(sourceUrl)); +}); + +/** + * Checks that downloads started by nsIHelperAppService are added to the + * browsing history when they start. + */ +add_task(async function test_history_tryToKeepPartialData() { + // We will wait for the visit to be notified during the download. + await PlacesUtils.history.clear(); + let promiseVisit = promiseWaitForVisit( + httpUrl("interruptible_resumable.txt") + ); + + // Start a download that is not allowed to finish yet. + let beforeStartTimeMs = Date.now(); + let download = await promiseStartDownload_tryToKeepPartialData({ + useLegacySaver: gUseLegacySaver, + }); + + // The history notifications should be received before the download completes. + let [time, transitionType] = await promiseVisit; + Assert.equal(transitionType, Ci.nsINavHistoryService.TRANSITION_DOWNLOAD); + + // The time set by nsIHelperAppService may be different than the start time in + // the download object, thus we only check that it is a meaningful time. Note + // that we subtract one second from the earliest time to account for rounding. + Assert.ok(time >= beforeStartTimeMs - 1000); + + // Complete the download before finishing the test. + continueResponses(); + await promiseDownloadStopped(download); +}); + +/** + * Checks that finished downloads are not removed. + */ +add_task(async function test_download_cancel_retry_finalize() { + // Start a download that is not allowed to finish yet. + let sourceUrl = httpUrl("interruptible.txt"); + let targetFilePath = getTempFile(TEST_TARGET_FILE_NAME).path; + mustInterruptResponses(); + let download1 = await Downloads.createDownload({ + source: sourceUrl, + target: { path: targetFilePath, partFilePath: targetFilePath + ".part" }, + }); + download1.start().catch(() => {}); + await promiseDownloadMidway(download1); + await promisePartFileReady(download1); + + // Cancel the download and make sure that the partial data do not exist. + await download1.cancel(); + Assert.equal(targetFilePath, download1.target.path); + Assert.equal(false, await IOUtils.exists(download1.target.path)); + Assert.equal(false, await IOUtils.exists(download1.target.partFilePath)); + continueResponses(); + + // Download the same file again with a different download session. + let download2 = await Downloads.createDownload({ + source: sourceUrl, + target: { path: targetFilePath, partFilePath: targetFilePath + ".part" }, + }); + download2.start().catch(() => {}); + + // Wait for download to be completed. + await promiseDownloadStopped(download2); + Assert.equal(targetFilePath, download2.target.path); + Assert.ok(await IOUtils.exists(download2.target.path)); + Assert.equal(false, await IOUtils.exists(download2.target.partFilePath)); + + // Finalize the first download session. + await download1.finalize(true); + + // The complete download should not have been removed. + Assert.ok(await IOUtils.exists(download2.target.path)); + Assert.equal(false, await IOUtils.exists(download2.target.partFilePath)); +}); + +/** + * Checks that confirmBlock does not clobber unrelated safe files. + */ +add_task(async function test_blocked_removeByHand_confirmBlock() { + let download1 = await promiseBlockedDownload({ + keepPartialData: true, + keepBlockedData: true, + }); + + Assert.ok(download1.hasBlockedData); + Assert.equal((await IOUtils.stat(download1.target.path)).size, 0); + Assert.ok(await IOUtils.exists(download1.target.partFilePath)); + + // Remove the placeholder without telling the download. + await IOUtils.remove(download1.target.path); + Assert.equal(false, await IOUtils.exists(download1.target.path)); + + // Download a file with the same name as the blocked download. + let download2 = await Downloads.createDownload({ + source: httpUrl("interruptible_resumable.txt"), + target: { + path: download1.target.path, + partFilePath: download1.target.path + ".part", + }, + }); + download2.start().catch(() => {}); + + // Wait for download to be completed. + await promiseDownloadStopped(download2); + Assert.equal(download1.target.path, download2.target.path); + Assert.ok(await IOUtils.exists(download2.target.path)); + + // Remove the blocked download. + await download1.confirmBlock(); + + // After confirming the complete download should not have been removed. + Assert.ok(await IOUtils.exists(download2.target.path)); +}); + +/** + * Tests that the temp download files are removed on exit and exiting private + * mode after they have been launched. + */ +add_task(async function test_launchWhenSucceeded_deleteTempFileOnExit() { + let customLauncherPath = getTempFile("app-launcher").path; + let autoDeleteTargetPathOne = getTempFile(TEST_TARGET_FILE_NAME).path; + let autoDeleteTargetPathTwo = getTempFile(TEST_TARGET_FILE_NAME).path; + let noAutoDeleteTargetPath = getTempFile(TEST_TARGET_FILE_NAME).path; + + let autoDeleteDownloadOne = await Downloads.createDownload({ + source: { url: httpUrl("source.txt"), isPrivate: true }, + target: autoDeleteTargetPathOne, + launchWhenSucceeded: true, + launcherPath: customLauncherPath, + }); + await autoDeleteDownloadOne.start(); + + Services.prefs.setBoolPref(kDeleteTempFileOnExit, true); + let autoDeleteDownloadTwo = await Downloads.createDownload({ + source: httpUrl("source.txt"), + target: autoDeleteTargetPathTwo, + launchWhenSucceeded: true, + launcherPath: customLauncherPath, + }); + await autoDeleteDownloadTwo.start(); + + Services.prefs.setBoolPref(kDeleteTempFileOnExit, false); + let noAutoDeleteDownload = await Downloads.createDownload({ + source: httpUrl("source.txt"), + target: noAutoDeleteTargetPath, + launchWhenSucceeded: true, + launcherPath: customLauncherPath, + }); + await noAutoDeleteDownload.start(); + + Services.prefs.clearUserPref(kDeleteTempFileOnExit); + + Assert.ok(await IOUtils.exists(autoDeleteTargetPathOne)); + Assert.ok(await IOUtils.exists(autoDeleteTargetPathTwo)); + Assert.ok(await IOUtils.exists(noAutoDeleteTargetPath)); + + // Simulate leaving private browsing mode + Services.obs.notifyObservers(null, "last-pb-context-exited"); + Assert.equal(false, await IOUtils.exists(autoDeleteTargetPathOne)); + + // Simulate browser shutdown + let expire = Cc[ + "@mozilla.org/uriloader/external-helper-app-service;1" + ].getService(Ci.nsIObserver); + expire.observe(null, "profile-before-change", null); + + // The file should still exist following the simulated shutdown. + Assert.ok(await IOUtils.exists(autoDeleteTargetPathTwo)); + Assert.ok(await IOUtils.exists(noAutoDeleteTargetPath)); +}); + +add_task(async function test_partitionKey() { + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + Services.prefs.setBoolPref("privacy.partition.network_state", true); + + function promiseVerifyDownloadChannel(url, partitionKey) { + return TestUtils.topicObserved( + "http-on-modify-request", + (subject, data) => { + let httpChannel = subject.QueryInterface(Ci.nsIHttpChannel); + if (httpChannel.URI.spec != url) { + return false; + } + + let reqLoadInfo = httpChannel.loadInfo; + let cookieJarSettings = reqLoadInfo.cookieJarSettings; + + // Check the partitionKey of the cookieJarSettings. + Assert.equal(cookieJarSettings.partitionKey, partitionKey); + + return true; + } + ); + } + + let test_url = httpUrl("source.txt"); + let uri = Services.io.newURI(test_url); + let cookieJarSettings = Cc["@mozilla.org/cookieJarSettings;1"].createInstance( + Ci.nsICookieJarSettings + ); + cookieJarSettings.initWithURI(uri, false); + let expectedPartitionKey = cookieJarSettings.partitionKey; + + let verifyPromise; + + let download; + if (!gUseLegacySaver) { + // When testing DownloadCopySaver, we have control over the download, thus + // we can check its basic properties before it starts. + download = await Downloads.createDownload({ + source: { url: test_url, cookieJarSettings }, + target: { path: targetFile.path }, + saver: { type: "copy" }, + }); + + Assert.equal(download.source.url, test_url); + Assert.equal(download.target.path, targetFile.path); + + verifyPromise = promiseVerifyDownloadChannel( + test_url, + expectedPartitionKey + ); + + await download.start(); + } else { + verifyPromise = promiseVerifyDownloadChannel( + test_url, + expectedPartitionKey + ); + + // When testing DownloadLegacySaver, the download is already started when it + // is created, thus we must check its basic properties while in progress. + download = await promiseStartLegacyDownload(null, { + targetFile, + cookieJarSettings, + }); + + Assert.equal(download.source.url, test_url); + Assert.equal(download.target.path, targetFile.path); + + await promiseDownloadStopped(download); + } + + await verifyPromise; + + Services.prefs.clearUserPref("privacy.partition.network_state"); +}); diff --git a/toolkit/components/downloads/test/unit/head.js b/toolkit/components/downloads/test/unit/head.js new file mode 100644 index 0000000000..44364c1cbf --- /dev/null +++ b/toolkit/components/downloads/test/unit/head.js @@ -0,0 +1,1187 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Provides infrastructure for automated download components tests. + */ + +"use strict"; + +var { Integration } = ChromeUtils.importESModule( + "resource://gre/modules/Integration.sys.mjs" +); +var { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); +const { AppConstants } = ChromeUtils.importESModule( + "resource://gre/modules/AppConstants.sys.mjs" +); + +ChromeUtils.defineESModuleGetters(this, { + DownloadPaths: "resource://gre/modules/DownloadPaths.sys.mjs", + Downloads: "resource://gre/modules/Downloads.sys.mjs", + E10SUtils: "resource://gre/modules/E10SUtils.sys.mjs", + FileTestUtils: "resource://testing-common/FileTestUtils.sys.mjs", + FileUtils: "resource://gre/modules/FileUtils.sys.mjs", + MockRegistrar: "resource://testing-common/MockRegistrar.sys.mjs", + PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs", + PromiseUtils: "resource://gre/modules/PromiseUtils.sys.mjs", + TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs", + TestUtils: "resource://testing-common/TestUtils.sys.mjs", +}); + +XPCOMUtils.defineLazyModuleGetters(this, { + HttpServer: "resource://testing-common/httpd.js", + NetUtil: "resource://gre/modules/NetUtil.jsm", +}); + +XPCOMUtils.defineLazyServiceGetter( + this, + "gExternalHelperAppService", + "@mozilla.org/uriloader/external-helper-app-service;1", + Ci.nsIExternalHelperAppService +); + +/* global DownloadIntegration */ +Integration.downloads.defineESModuleGetter( + this, + "DownloadIntegration", + "resource://gre/modules/DownloadIntegration.sys.mjs" +); + +const ServerSocket = Components.Constructor( + "@mozilla.org/network/server-socket;1", + "nsIServerSocket", + "init" +); +const BinaryOutputStream = Components.Constructor( + "@mozilla.org/binaryoutputstream;1", + "nsIBinaryOutputStream", + "setOutputStream" +); + +XPCOMUtils.defineLazyServiceGetter( + this, + "gMIMEService", + "@mozilla.org/mime;1", + "nsIMIMEService" +); + +const ReferrerInfo = Components.Constructor( + "@mozilla.org/referrer-info;1", + "nsIReferrerInfo", + "init" +); + +const TEST_TARGET_FILE_NAME = "test-download.txt"; +const TEST_STORE_FILE_NAME = "test-downloads.json"; + +// We are testing an HTTPS referrer with HTTP downloads in order to verify that +// the default policy will not prevent the referrer from being passed around. +const TEST_REFERRER_URL = "https://www.example.com/referrer.html"; + +const TEST_DATA_SHORT = "This test string is downloaded."; +// Generate using gzipCompressString in TelemetryController.sys.mjs. +const TEST_DATA_SHORT_GZIP_ENCODED_FIRST = [ + 31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 11, 201, 200, 44, 86, 40, 73, 45, 46, 81, 40, + 46, 41, 202, 204, +]; +const TEST_DATA_SHORT_GZIP_ENCODED_SECOND = [ + 75, 87, 0, 114, 83, 242, 203, 243, 114, 242, 19, 83, 82, 83, 244, 0, 151, 222, + 109, 43, 31, 0, 0, 0, +]; +const TEST_DATA_SHORT_GZIP_ENCODED = TEST_DATA_SHORT_GZIP_ENCODED_FIRST.concat( + TEST_DATA_SHORT_GZIP_ENCODED_SECOND +); + +/** + * All the tests are implemented with add_task, this starts them automatically. + */ +function run_test() { + do_get_profile(); + run_next_test(); +} + +// Support functions + +/** + * HttpServer object initialized before tests start. + */ +var gHttpServer; + +/** + * Given a file name, returns a string containing an URI that points to the file + * on the currently running instance of the test HTTP server. + */ +function httpUrl(aFileName) { + return ( + "http://www.example.com:" + + gHttpServer.identity.primaryPort + + "/" + + aFileName + ); +} + +/** + * Returns a reference to a temporary file that is guaranteed not to exist and + * is cleaned up later. See FileTestUtils.getTempFile for details. + */ +function getTempFile(leafName) { + return FileTestUtils.getTempFile(leafName); +} + +/** + * Check for file existence. + * @param {string} path The file path. + */ +async function fileExists(path) { + try { + return (await IOUtils.stat(path)).type == "regular"; + } catch (ex) { + return false; + } +} + +/** + * Waits for pending events to be processed. + * + * @return {Promise} + * @resolves When pending events have been processed. + * @rejects Never. + */ +function promiseExecuteSoon() { + return new Promise(resolve => { + executeSoon(resolve); + }); +} + +/** + * Waits for a pending events to be processed after a timeout. + * + * @return {Promise} + * @resolves When pending events have been processed. + * @rejects Never. + */ +function promiseTimeout(aTime) { + return new Promise(resolve => { + do_timeout(aTime, resolve); + }); +} + +/** + * Waits for a new history visit to be notified for the specified URI. + * + * @param aUrl + * String containing the URI that will be visited. + * + * @return {Promise} + * @resolves Array [aTime, aTransitionType] from page-visited places event. + * @rejects Never. + */ +function promiseWaitForVisit(aUrl) { + return new Promise(resolve => { + function listener(aEvents) { + Assert.equal(aEvents.length, 1); + let event = aEvents[0]; + Assert.equal(event.type, "page-visited"); + if (event.url == aUrl) { + PlacesObservers.removeListener(["page-visited"], listener); + resolve([event.visitTime, event.transitionType, event.lastKnownTitle]); + } + } + PlacesObservers.addListener(["page-visited"], listener); + }); +} + +/** + * Creates a new Download object, setting a temporary file as the target. + * + * @param aSourceUrl + * String containing the URI for the download source, or null to use + * httpUrl("source.txt"). + * + * @return {Promise} + * @resolves The newly created Download object. + * @rejects JavaScript exception. + */ +function promiseNewDownload(aSourceUrl) { + return Downloads.createDownload({ + source: aSourceUrl || httpUrl("source.txt"), + target: getTempFile(TEST_TARGET_FILE_NAME), + }); +} + +/** + * Starts a new download using the nsIWebBrowserPersist interface, and controls + * it using the legacy nsITransfer interface. + * + * @param aSourceUrl + * String containing the URI for the download source, or null to use + * httpUrl("source.txt"). + * @param aOptions + * An optional object used to control the behavior of this function. + * You may pass an object with a subset of the following fields: + * { + * isPrivate: Boolean indicating whether the download originated from a + * private window. + * referrerInfo: referrerInfo for the download source. + * cookieJarSettings: cookieJarSettings for the download source. + * targetFile: nsIFile for the target, or null to use a temporary file. + * outPersist: Receives a reference to the created nsIWebBrowserPersist + * instance. + * launchWhenSucceeded: Boolean indicating whether the target should + * be launched when it has completed successfully. + * launcherPath: String containing the path of the custom executable to + * use to launch the target of the download. + * } + * + * @return {Promise} + * @resolves The Download object created as a consequence of controlling the + * download through the legacy nsITransfer interface. + * @rejects Never. The current test fails in case of exceptions. + */ +function promiseStartLegacyDownload(aSourceUrl, aOptions) { + let sourceURI = NetUtil.newURI(aSourceUrl || httpUrl("source.txt")); + let targetFile = + (aOptions && aOptions.targetFile) || getTempFile(TEST_TARGET_FILE_NAME); + + let persist = Cc[ + "@mozilla.org/embedding/browser/nsWebBrowserPersist;1" + ].createInstance(Ci.nsIWebBrowserPersist); + if (aOptions) { + aOptions.outPersist = persist; + } + + let fileExtension = null, + mimeInfo = null; + let match = sourceURI.pathQueryRef.match(/\.([^.\/]+)$/); + if (match) { + fileExtension = match[1]; + } + + if (fileExtension) { + try { + mimeInfo = gMIMEService.getFromTypeAndExtension(null, fileExtension); + mimeInfo.preferredAction = Ci.nsIMIMEInfo.saveToDisk; + } catch (ex) {} + } + + if (aOptions && aOptions.launcherPath) { + Assert.ok(mimeInfo != null); + + let localHandlerApp = Cc[ + "@mozilla.org/uriloader/local-handler-app;1" + ].createInstance(Ci.nsILocalHandlerApp); + localHandlerApp.executable = new FileUtils.File(aOptions.launcherPath); + + mimeInfo.preferredApplicationHandler = localHandlerApp; + mimeInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp; + } + + if (aOptions && aOptions.launchWhenSucceeded) { + Assert.ok(mimeInfo != null); + + mimeInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp; + } + + // Apply decoding if required by the "Content-Encoding" header. + persist.persistFlags &= ~Ci.nsIWebBrowserPersist.PERSIST_FLAGS_NO_CONVERSION; + persist.persistFlags |= + Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION; + + let transfer = Cc["@mozilla.org/transfer;1"].createInstance(Ci.nsITransfer); + + return new Promise(resolve => { + Downloads.getList(Downloads.ALL) + .then(function (aList) { + // Temporarily register a view that will get notified when the download we + // are controlling becomes visible in the list of downloads. + aList + .addView({ + onDownloadAdded(aDownload) { + aList.removeView(this).catch(do_report_unexpected_exception); + + // Remove the download to keep the list empty for the next test. This + // also allows the caller to register the "onchange" event directly. + let promise = aList.remove(aDownload); + + // When the download object is ready, make it available to the caller. + promise.then( + () => resolve(aDownload), + do_report_unexpected_exception + ); + }, + }) + .catch(do_report_unexpected_exception); + + let isPrivate = aOptions && aOptions.isPrivate; + let referrerInfo = aOptions ? aOptions.referrerInfo : null; + let cookieJarSettings = aOptions ? aOptions.cookieJarSettings : null; + let classification = + aOptions?.downloadClassification ?? + Ci.nsITransfer.DOWNLOAD_ACCEPTABLE; + // Initialize the components so they reference each other. This will cause + // the Download object to be created and added to the public downloads. + transfer.init( + sourceURI, + null, + NetUtil.newURI(targetFile), + null, + mimeInfo, + null, + null, + persist, + isPrivate, + classification, + null + ); + persist.progressListener = transfer; + + // Start the actual download process. + persist.saveURI( + sourceURI, + Services.scriptSecurityManager.getSystemPrincipal(), + 0, + referrerInfo, + cookieJarSettings, + null, + null, + targetFile, + Ci.nsIContentPolicy.TYPE_SAVEAS_DOWNLOAD, + isPrivate + ); + }) + .catch(do_report_unexpected_exception); + }); +} + +/** + * Starts a new download using the nsIHelperAppService interface, and controls + * it using the legacy nsITransfer interface. The source of the download will + * be "interruptible_resumable.txt" and partially downloaded data will be kept. + * + * @param aSourceUrl + * String containing the URI for the download source, or null to use + * httpUrl("interruptible_resumable.txt"). + * + * @return {Promise} + * @resolves The Download object created as a consequence of controlling the + * download through the legacy nsITransfer interface. + * @rejects Never. The current test fails in case of exceptions. + */ +function promiseStartExternalHelperAppServiceDownload(aSourceUrl) { + let sourceURI = NetUtil.newURI( + aSourceUrl || httpUrl("interruptible_resumable.txt") + ); + + return new Promise(resolve => { + Downloads.getList(Downloads.PUBLIC) + .then(function (aList) { + // Temporarily register a view that will get notified when the download we + // are controlling becomes visible in the list of downloads. + aList + .addView({ + onDownloadAdded(aDownload) { + aList.removeView(this).catch(do_report_unexpected_exception); + + // Remove the download to keep the list empty for the next test. This + // also allows the caller to register the "onchange" event directly. + let promise = aList.remove(aDownload); + + // When the download object is ready, make it available to the caller. + promise.then( + () => resolve(aDownload), + do_report_unexpected_exception + ); + }, + }) + .catch(do_report_unexpected_exception); + + let channel = NetUtil.newChannel({ + uri: sourceURI, + loadUsingSystemPrincipal: true, + }); + + // Start the actual download process. + channel.asyncOpen({ + contentListener: null, + + onStartRequest(aRequest) { + let requestChannel = aRequest.QueryInterface(Ci.nsIChannel); + this.contentListener = gExternalHelperAppService.doContent( + requestChannel.contentType, + aRequest, + null, + true + ); + this.contentListener.onStartRequest(aRequest); + }, + + onStopRequest(aRequest, aStatusCode) { + this.contentListener.onStopRequest(aRequest, aStatusCode); + }, + + onDataAvailable(aRequest, aInputStream, aOffset, aCount) { + this.contentListener.onDataAvailable( + aRequest, + aInputStream, + aOffset, + aCount + ); + }, + }); + }) + .catch(do_report_unexpected_exception); + }); +} + +/** + * Waits for a download to reach half of its progress, in case it has not + * reached the expected progress already. + * + * @param aDownload + * The Download object to wait upon. + * + * @return {Promise} + * @resolves When the download has reached half of its progress. + * @rejects Never. + */ +function promiseDownloadMidway(aDownload) { + return new Promise(resolve => { + // Wait for the download to reach half of its progress. + let onchange = function () { + if ( + !aDownload.stopped && + !aDownload.canceled && + aDownload.progress == 50 + ) { + aDownload.onchange = null; + resolve(); + } + }; + + // Register for the notification, but also call the function directly in + // case the download already reached the expected progress. + aDownload.onchange = onchange; + onchange(); + }); +} + +/** + * Waits for a download to make any amount of progress. + * + * @param aDownload + * The Download object to wait upon. + * + * @return {Promise} + * @resolves When the download has transfered any number of bytes. + * @rejects Never. + */ +function promiseDownloadStarted(aDownload) { + return new Promise(resolve => { + // Wait for the download to transfer some amount of bytes. + let onchange = function () { + if ( + !aDownload.stopped && + !aDownload.canceled && + aDownload.currentBytes > 0 + ) { + aDownload.onchange = null; + resolve(); + } + }; + + // Register for the notification, but also call the function directly in + // case the download already reached the expected progress. + aDownload.onchange = onchange; + onchange(); + }); +} + +/** + * Waits for a download to finish. + * + * @param aDownload + * The Download object to wait upon. + * + * @return {Promise} + * @resolves When the download succeeded or errored. + * @rejects Never. + */ +function promiseDownloadFinished(aDownload) { + return new Promise(resolve => { + // Wait for the download to finish. + let onchange = function () { + if (aDownload.succeeded || aDownload.error) { + aDownload.onchange = null; + resolve(); + } + }; + + // Register for the notification, but also call the function directly in + // case the download already reached the expected progress. + aDownload.onchange = onchange; + onchange(); + }); +} + +/** + * Waits for a download to finish, in case it has not finished already. + * + * @param aDownload + * The Download object to wait upon. + * + * @return {Promise} + * @resolves When the download has finished successfully. + * @rejects JavaScript exception if the download failed. + */ +function promiseDownloadStopped(aDownload) { + if (!aDownload.stopped) { + // The download is in progress, wait for the current attempt to finish and + // report any errors that may occur. + return aDownload.start(); + } + + if (aDownload.succeeded) { + return Promise.resolve(); + } + + // The download failed or was canceled. + return Promise.reject(aDownload.error || new Error("Download canceled.")); +} + +/** + * Returns a new public or private DownloadList object. + * + * @param aIsPrivate + * True for the private list, false or undefined for the public list. + * + * @return {Promise} + * @resolves The newly created DownloadList object. + * @rejects JavaScript exception. + */ +function promiseNewList(aIsPrivate) { + // We need to clear all the internal state for the list and summary objects, + // since all the objects are interdependent internally. + Downloads._promiseListsInitialized = null; + Downloads._lists = {}; + Downloads._summaries = {}; + + return Downloads.getList(aIsPrivate ? Downloads.PRIVATE : Downloads.PUBLIC); +} + +/** + * Ensures that the given file contents are equal to the given string. + * + * @param aPath + * String containing the path of the file whose contents should be + * verified. + * @param aExpectedContents + * String containing the octets that are expected in the file. + * + * @return {Promise} + * @resolves When the operation completes. + * @rejects Never. + */ +async function promiseVerifyContents(aPath, aExpectedContents) { + let file = new FileUtils.File(aPath); + + if (!(await IOUtils.exists(aPath))) { + do_throw("File does not exist: " + aPath); + } + + if ((await IOUtils.stat(aPath)).size == 0) { + do_throw("File is empty: " + aPath); + } + + await new Promise(resolve => { + NetUtil.asyncFetch( + { uri: NetUtil.newURI(file), loadUsingSystemPrincipal: true }, + function (aInputStream, aStatus) { + Assert.ok(Components.isSuccessCode(aStatus)); + let contents = NetUtil.readInputStreamToString( + aInputStream, + aInputStream.available() + ); + if ( + contents.length > TEST_DATA_SHORT.length * 2 || + /[^\x20-\x7E]/.test(contents) + ) { + // Do not print the entire content string to the test log. + Assert.equal(contents.length, aExpectedContents.length); + Assert.ok(contents == aExpectedContents); + } else { + // Print the string if it is short and made of printable characters. + Assert.equal(contents, aExpectedContents); + } + resolve(); + } + ); + }); +} + +/** + * Creates and starts a new download, configured to keep partial data, and + * returns only when the first part of "interruptible_resumable.txt" has been + * saved to disk. You must call "continueResponses" to allow the interruptible + * request to continue. + * + * This function uses either DownloadCopySaver or DownloadLegacySaver based on + * the current test run. + * + * @param aOptions + * An optional object used to control the behavior of this function. + * You may pass an object with a subset of the following fields: + * { + * useLegacySaver: Boolean indicating whether to launch a legacy download. + * } + * + * @return {Promise} + * @resolves The newly created Download object, still in progress. + * @rejects JavaScript exception. + */ +async function promiseStartDownload_tryToKeepPartialData({ + useLegacySaver = false, +} = {}) { + mustInterruptResponses(); + + // Start a new download and configure it to keep partially downloaded data. + let download; + if (!useLegacySaver) { + let targetFilePath = getTempFile(TEST_TARGET_FILE_NAME).path; + download = await Downloads.createDownload({ + source: httpUrl("interruptible_resumable.txt"), + target: { + path: targetFilePath, + partFilePath: targetFilePath + ".part", + }, + }); + download.tryToKeepPartialData = true; + download.start().catch(() => {}); + } else { + // Start a download using nsIExternalHelperAppService, that is configured + // to keep partially downloaded data by default. + download = await promiseStartExternalHelperAppServiceDownload(); + } + + await promiseDownloadMidway(download); + await promisePartFileReady(download); + + return download; +} + +/** + * This function should be called after the progress notification for a download + * is received, and waits for the worker thread of BackgroundFileSaver to + * receive the data to be written to the ".part" file on disk. + * + * @return {Promise} + * @resolves When the ".part" file has been written to disk. + * @rejects JavaScript exception. + */ +async function promisePartFileReady(aDownload) { + // We don't have control over the file output code in BackgroundFileSaver. + // After we receive the download progress notification, we may only check + // that the ".part" file has been created, while its size cannot be + // determined because the file is currently open. + try { + do { + await promiseTimeout(50); + } while (!(await IOUtils.exists(aDownload.target.partFilePath))); + } catch (ex) { + if (!(ex instanceof IOUtils.Error)) { + throw ex; + } + // This indicates that the file has been created and cannot be accessed. + // The specific error might vary with the platform. + info("Expected exception while checking existence: " + ex.toString()); + // Wait some more time to allow the write to complete. + await promiseTimeout(100); + } +} + +/** + * Create a download which will be reputation blocked. + * + * @param options + * { + * keepPartialData: bool, + * keepBlockedData: bool, + * useLegacySaver: bool, + * verdict: string indicating the detailed reason for the block, + * } + * @return {Promise} + * @resolves The reputation blocked download. + * @rejects JavaScript exception. + */ +async function promiseBlockedDownload({ + keepPartialData, + keepBlockedData, + useLegacySaver, + verdict = Downloads.Error.BLOCK_VERDICT_UNCOMMON, +} = {}) { + let blockFn = base => ({ + shouldBlockForReputationCheck: () => + Promise.resolve({ + shouldBlock: true, + verdict, + }), + shouldKeepBlockedData: () => Promise.resolve(keepBlockedData), + }); + + Integration.downloads.register(blockFn); + function cleanup() { + Integration.downloads.unregister(blockFn); + } + registerCleanupFunction(cleanup); + + let download; + + try { + if (keepPartialData) { + download = await promiseStartDownload_tryToKeepPartialData({ + useLegacySaver, + }); + continueResponses(); + } else if (useLegacySaver) { + download = await promiseStartLegacyDownload(); + } else { + download = await promiseNewDownload(); + await download.start(); + do_throw("The download should have blocked."); + } + + await promiseDownloadStopped(download); + do_throw("The download should have blocked."); + } catch (ex) { + if (!(ex instanceof Downloads.Error) || !ex.becauseBlocked) { + throw ex; + } + Assert.ok(ex.becauseBlockedByReputationCheck); + Assert.equal(ex.reputationCheckVerdict, verdict); + Assert.ok(download.error.becauseBlockedByReputationCheck); + Assert.equal(download.error.reputationCheckVerdict, verdict); + } + + Assert.ok(download.stopped); + Assert.ok(!download.succeeded); + + cleanup(); + return download; +} + +/** + * Starts a socket listener that closes each incoming connection. + * + * @returns nsIServerSocket that listens for connections. Call its "close" + * method to stop listening and free the server port. + */ +function startFakeServer() { + let serverSocket = new ServerSocket(-1, true, -1); + serverSocket.asyncListen({ + onSocketAccepted(aServ, aTransport) { + aTransport.close(Cr.NS_BINDING_ABORTED); + }, + onStopListening() {}, + }); + return serverSocket; +} + +/** + * This is an internal reference that should not be used directly by tests. + */ +var _gDeferResponses = PromiseUtils.defer(); + +/** + * Ensures that all the interruptible requests started after this function is + * called won't complete until the continueResponses function is called. + * + * Normally, the internal HTTP server returns all the available data as soon as + * a request is received. In order for some requests to be served one part at a + * time, special interruptible handlers are registered on the HTTP server. This + * allows testing events or actions that need to happen in the middle of a + * download. + * + * For example, the handler accessible at the httpUri("interruptible.txt") + * address returns the TEST_DATA_SHORT text, then it may block until the + * continueResponses method is called. At this point, the handler sends the + * TEST_DATA_SHORT text again to complete the response. + * + * If an interruptible request is started before the function is called, it may + * or may not be blocked depending on the actual sequence of events. + */ +function mustInterruptResponses() { + // If there are pending blocked requests, allow them to complete. This is + // done to prevent requests from being blocked forever, but should not affect + // the test logic, since previously started requests should not be monitored + // on the client side anymore. + _gDeferResponses.resolve(); + + info("Interruptible responses will be blocked midway."); + _gDeferResponses = PromiseUtils.defer(); +} + +/** + * Allows all the current and future interruptible requests to complete. + */ +function continueResponses() { + info("Interruptible responses are now allowed to continue."); + _gDeferResponses.resolve(); +} + +/** + * Registers an interruptible response handler. + * + * @param aPath + * Path passed to nsIHttpServer.registerPathHandler. + * @param aFirstPartFn + * This function is called when the response is received, with the + * aRequest and aResponse arguments of the server. + * @param aSecondPartFn + * This function is called with the aRequest and aResponse arguments of + * the server, when the continueResponses function is called. + */ +function registerInterruptibleHandler(aPath, aFirstPartFn, aSecondPartFn) { + gHttpServer.registerPathHandler(aPath, function (aRequest, aResponse) { + info("Interruptible request started."); + + // Process the first part of the response. + aResponse.processAsync(); + aFirstPartFn(aRequest, aResponse); + + // Wait on the current deferred object, then finish the request. + _gDeferResponses.promise + .then(function RIH_onSuccess() { + aSecondPartFn(aRequest, aResponse); + aResponse.finish(); + info("Interruptible request finished."); + }) + .catch(console.error); + }); +} + +/** + * Ensure the given date object is valid. + * + * @param aDate + * The date object to be checked. This value can be null. + */ +function isValidDate(aDate) { + return aDate && aDate.getTime && !isNaN(aDate.getTime()); +} + +/** + * Check actual ReferrerInfo is the same as expected. + * Because the actual download's referrer info's computedReferrer is computed + * from referrerPolicy and originalReferrer and is non-null, and the expected + * referrer info was constructed in isolation and therefore the computedReferrer + * is null, it isn't possible to use equals here. */ +function checkEqualReferrerInfos(aActualInfo, aExpectedInfo) { + Assert.equal( + !!aExpectedInfo.originalReferrer, + !!aActualInfo.originalReferrer + ); + if (aExpectedInfo.originalReferrer && aActualInfo.originalReferrer) { + Assert.equal( + aExpectedInfo.originalReferrer.spec, + aActualInfo.originalReferrer.spec + ); + } + + Assert.equal(aExpectedInfo.sendReferrer, aActualInfo.sendReferrer); + Assert.equal(aExpectedInfo.referrerPolicy, aActualInfo.referrerPolicy); +} + +/** + * Waits for the download annotations to be set for the given page, required + * because the addDownload method will add these to the database asynchronously. + */ +function waitForAnnotation(sourceUriSpec, annotationName, optionalValue) { + return TestUtils.waitForCondition(async () => { + let pageInfo = await PlacesUtils.history.fetch(sourceUriSpec, { + includeAnnotations: true, + }); + if (optionalValue) { + return pageInfo?.annotations.get(annotationName) == optionalValue; + } + return pageInfo?.annotations.has(annotationName); + }, `Should have found annotation ${annotationName} for ${sourceUriSpec}`); +} + +/** + * Position of the first byte served by the "interruptible_resumable.txt" + * handler during the most recent response. + */ +var gMostRecentFirstBytePos; + +// Initialization functions common to all tests + +add_setup(function test_common_initialize() { + // Start the HTTP server. + gHttpServer = new HttpServer(); + gHttpServer.registerDirectory("/", do_get_file("../data")); + gHttpServer.start(-1); + registerCleanupFunction(() => { + return new Promise(resolve => { + // Ensure all the pending HTTP requests have a chance to finish. + continueResponses(); + // Stop the HTTP server, calling resolve when it's done. + gHttpServer.stop(resolve); + }); + }); + + // Serve the downloads from a domain located in the Internet zone on Windows. + gHttpServer.identity.setPrimary( + "http", + "www.example.com", + gHttpServer.identity.primaryPort + ); + Services.prefs.setCharPref("network.dns.localDomains", "www.example.com"); + registerCleanupFunction(function () { + Services.prefs.clearUserPref("network.dns.localDomains"); + }); + + // Cache locks might prevent concurrent requests to the same resource, and + // this may block tests that use the interruptible handlers. + Services.prefs.setBoolPref("browser.cache.disk.enable", false); + Services.prefs.setBoolPref("browser.cache.memory.enable", false); + registerCleanupFunction(function () { + Services.prefs.clearUserPref("browser.cache.disk.enable"); + Services.prefs.clearUserPref("browser.cache.memory.enable"); + }); + + // Allow relaxing default referrer. + Services.prefs.setBoolPref( + "network.http.referer.disallowCrossSiteRelaxingDefault", + false + ); + registerCleanupFunction(function () { + Services.prefs.clearUserPref( + "network.http.referer.disallowCrossSiteRelaxingDefault" + ); + }); + + registerInterruptibleHandler( + "/interruptible.txt", + function firstPart(aRequest, aResponse) { + aResponse.setHeader("Content-Type", "text/plain", false); + aResponse.setHeader( + "Content-Length", + "" + TEST_DATA_SHORT.length * 2, + false + ); + aResponse.write(TEST_DATA_SHORT); + }, + function secondPart(aRequest, aResponse) { + aResponse.write(TEST_DATA_SHORT); + } + ); + + registerInterruptibleHandler( + "/interruptible_nosize.txt", + function firstPart(aRequest, aResponse) { + aResponse.setHeader("Content-Type", "text/plain", false); + aResponse.write(TEST_DATA_SHORT); + }, + function secondPart(aRequest, aResponse) { + aResponse.write(TEST_DATA_SHORT); + } + ); + + registerInterruptibleHandler( + "/interruptible_resumable.txt", + function firstPart(aRequest, aResponse) { + aResponse.setHeader("Content-Type", "text/plain", false); + + // Determine if only part of the data should be sent. + let data = TEST_DATA_SHORT + TEST_DATA_SHORT; + if (aRequest.hasHeader("Range")) { + var matches = aRequest + .getHeader("Range") + .match(/^\s*bytes=(\d+)?-(\d+)?\s*$/); + var firstBytePos = matches[1] === undefined ? 0 : matches[1]; + var lastBytePos = + matches[2] === undefined ? data.length - 1 : matches[2]; + if (firstBytePos >= data.length) { + aResponse.setStatusLine( + aRequest.httpVersion, + 416, + "Requested Range Not Satisfiable" + ); + aResponse.setHeader("Content-Range", "*/" + data.length, false); + aResponse.finish(); + return; + } + + aResponse.setStatusLine(aRequest.httpVersion, 206, "Partial Content"); + aResponse.setHeader( + "Content-Range", + firstBytePos + "-" + lastBytePos + "/" + data.length, + false + ); + + data = data.substring(firstBytePos, lastBytePos + 1); + + gMostRecentFirstBytePos = firstBytePos; + } else { + gMostRecentFirstBytePos = 0; + } + + aResponse.setHeader("Content-Length", "" + data.length, false); + + aResponse.write(data.substring(0, data.length / 2)); + + // Store the second part of the data on the response object, so that it + // can be used by the secondPart function. + aResponse.secondPartData = data.substring(data.length / 2); + }, + function secondPart(aRequest, aResponse) { + aResponse.write(aResponse.secondPartData); + } + ); + + registerInterruptibleHandler( + "/interruptible_gzip.txt", + function firstPart(aRequest, aResponse) { + aResponse.setHeader("Content-Type", "text/plain", false); + aResponse.setHeader("Content-Encoding", "gzip", false); + aResponse.setHeader( + "Content-Length", + "" + TEST_DATA_SHORT_GZIP_ENCODED.length + ); + + let bos = new BinaryOutputStream(aResponse.bodyOutputStream); + bos.writeByteArray(TEST_DATA_SHORT_GZIP_ENCODED_FIRST); + }, + function secondPart(aRequest, aResponse) { + let bos = new BinaryOutputStream(aResponse.bodyOutputStream); + bos.writeByteArray(TEST_DATA_SHORT_GZIP_ENCODED_SECOND); + } + ); + + gHttpServer.registerPathHandler( + "/shorter-than-content-length-http-1-1.txt", + function (aRequest, aResponse) { + aResponse.processAsync(); + aResponse.setStatusLine("1.1", 200, "OK"); + aResponse.setHeader("Content-Type", "text/plain", false); + aResponse.setHeader( + "Content-Length", + "" + TEST_DATA_SHORT.length * 2, + false + ); + aResponse.write(TEST_DATA_SHORT); + aResponse.finish(); + } + ); + + gHttpServer.registerPathHandler("/busy.txt", function (aRequest, aResponse) { + aResponse.setStatusLine("1.1", 504, "Gateway Timeout"); + aResponse.setHeader("Content-Type", "text/plain", false); + aResponse.setHeader("Content-Length", "" + TEST_DATA_SHORT.length, false); + aResponse.write(TEST_DATA_SHORT); + }); + + gHttpServer.registerPathHandler("/redirect", function (aRequest, aResponse) { + aResponse.setStatusLine("1.1", 301, "Moved Permanently"); + aResponse.setHeader("Location", httpUrl("busy.txt"), false); + aResponse.setHeader("Content-Type", "text/javascript", false); + aResponse.setHeader("Content-Length", "0", false); + }); + + // This URL will emulate being blocked by Windows Parental controls + gHttpServer.registerPathHandler( + "/parentalblocked.zip", + function (aRequest, aResponse) { + aResponse.setStatusLine( + aRequest.httpVersion, + 450, + "Blocked by Windows Parental Controls" + ); + } + ); + + // This URL sends some data followed by an RST packet + gHttpServer.registerPathHandler( + "/netreset.txt", + function (aRequest, aResponse) { + info("Starting response that will be aborted."); + aResponse.processAsync(); + aResponse.setHeader("Content-Type", "text/plain", false); + aResponse.write(TEST_DATA_SHORT); + promiseExecuteSoon() + .then(() => { + aResponse.abort(null, true); + aResponse.finish(); + info("Aborting response with network reset."); + }) + .then(null, console.error); + } + ); + + // During unit tests, most of the functions that require profile access or + // operating system features will be disabled. Individual tests may override + // them again to check for specific behaviors. + Integration.downloads.register(base => { + let override = { + loadPublicDownloadListFromStore: () => Promise.resolve(), + shouldKeepBlockedData: () => Promise.resolve(false), + shouldBlockForParentalControls: () => Promise.resolve(false), + shouldBlockForReputationCheck: () => + Promise.resolve({ + shouldBlock: false, + verdict: "", + }), + confirmLaunchExecutable: () => Promise.resolve(), + launchFile: () => Promise.resolve(), + showContainingDirectory: () => Promise.resolve(), + // This flag allows re-enabling the default observers during their tests. + allowObservers: false, + addListObservers() { + return this.allowObservers + ? super.addListObservers(...arguments) + : Promise.resolve(); + }, + // This flag allows re-enabling the download directory logic for its tests. + _allowDirectories: false, + set allowDirectories(value) { + this._allowDirectories = value; + // We have to invalidate the previously computed directory path. + this._downloadsDirectory = null; + }, + _getDirectory(name) { + return super._getDirectory(this._allowDirectories ? name : "TmpD"); + }, + }; + Object.setPrototypeOf(override, base); + return override; + }); + + // Make sure that downloads started using nsIExternalHelperAppService are + // saved to disk without asking for a destination interactively. + let mock = { + QueryInterface: ChromeUtils.generateQI(["nsIHelperAppLauncherDialog"]), + promptForSaveToFileAsync( + aLauncher, + aWindowContext, + aDefaultFileName, + aSuggestedFileExtension, + aForcePrompt + ) { + // The dialog should create the empty placeholder file. + let file = getTempFile(TEST_TARGET_FILE_NAME); + file.create(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE); + aLauncher.saveDestinationAvailable(file); + }, + }; + + let cid = MockRegistrar.register( + "@mozilla.org/helperapplauncherdialog;1", + mock + ); + registerCleanupFunction(() => { + MockRegistrar.unregister(cid); + }); +}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadBlockedTelemetry.js b/toolkit/components/downloads/test/unit/test_DownloadBlockedTelemetry.js new file mode 100644 index 0000000000..2484e591a4 --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_DownloadBlockedTelemetry.js @@ -0,0 +1,113 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +Services.prefs.setBoolPref( + "toolkit.telemetry.testing.overrideProductsCheck", + true +); + +const errors = [ + Downloads.Error.BLOCK_VERDICT_MALWARE, + Downloads.Error.BLOCK_VERDICT_POTENTIALLY_UNWANTED, + Downloads.Error.BLOCK_VERDICT_INSECURE, + Downloads.Error.BLOCK_VERDICT_UNCOMMON, +]; + +add_task(async function test_confirm_block_download() { + for (let error of errors) { + info(`Testing block ${error} download`); + let histogram = TelemetryTestUtils.getAndClearKeyedHistogram( + "DOWNLOADS_USER_ACTION_ON_BLOCKED_DOWNLOAD" + ); + + let download; + try { + info(`Create ${error} download`); + if (error == Downloads.Error.BLOCK_VERDICT_INSECURE) { + download = await promiseStartLegacyDownload(null, { + downloadClassification: Ci.nsITransfer.DOWNLOAD_POTENTIALLY_UNSAFE, + }); + } else { + download = await promiseBlockedDownload({ + keepPartialData: true, + keepBlockedData: true, + useLegacySaver: false, + verdict: error, + }); + } + await download.start(); + do_throw("The download should have failed."); + } catch (ex) { + if (!(ex instanceof Downloads.Error)) { + throw ex; + } + } + + // Test blocked download is recorded + TelemetryTestUtils.assertKeyedHistogramValue(histogram, error, 0, 1); + + // Test confirm block + histogram = TelemetryTestUtils.getAndClearKeyedHistogram( + "DOWNLOADS_USER_ACTION_ON_BLOCKED_DOWNLOAD" + ); + info(`Block ${error} download`); + await download.confirmBlock(); + TelemetryTestUtils.assertKeyedHistogramValue(histogram, error, 1, 1); + } +}); + +add_task(async function test_confirm_unblock_download() { + for (let error of errors) { + info(`Testing unblock ${error} download`); + let histogram = TelemetryTestUtils.getAndClearKeyedHistogram( + "DOWNLOADS_USER_ACTION_ON_BLOCKED_DOWNLOAD" + ); + + let download; + try { + info(`Create ${error} download`); + if (error == Downloads.Error.BLOCK_VERDICT_INSECURE) { + download = await promiseStartLegacyDownload(null, { + downloadClassification: Ci.nsITransfer.DOWNLOAD_POTENTIALLY_UNSAFE, + }); + } else { + download = await promiseBlockedDownload({ + keepPartialData: true, + keepBlockedData: true, + useLegacySaver: false, + verdict: error, + }); + } + await download.start(); + do_throw("The download should have failed."); + } catch (ex) { + if (!(ex instanceof Downloads.Error)) { + throw ex; + } + } + + // Test blocked download is recorded + TelemetryTestUtils.assertKeyedHistogramValue(histogram, error, 0, 1); + + // Test unblock + histogram = TelemetryTestUtils.getAndClearKeyedHistogram( + "DOWNLOADS_USER_ACTION_ON_BLOCKED_DOWNLOAD" + ); + info(`Unblock ${error} download`); + let promise = new Promise(r => (download.onchange = r)); + await download.unblock(); + // The environment is not set up properly for performing a real download, cancel + // the unblocked download so it doesn't affect the next testcase. + await download.cancel(); + await promise; + if (error == Downloads.Error.BLOCK_VERDICT_INSECURE) { + Assert.ok(!download.error, "Ensure we didn't set download.error"); + } + + TelemetryTestUtils.assertKeyedHistogramValue(histogram, error, 2, 1); + } +}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadCore.js b/toolkit/components/downloads/test/unit/test_DownloadCore.js new file mode 100644 index 0000000000..41aeaac33b --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_DownloadCore.js @@ -0,0 +1,291 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests the main download interfaces using DownloadCopySaver. + */ + +"use strict"; + +ChromeUtils.defineESModuleGetters(this, { + DownloadError: "resource://gre/modules/DownloadCore.sys.mjs", +}); + +// Execution of common tests + +// This is used in common_test_Download.js +// eslint-disable-next-line no-unused-vars +var gUseLegacySaver = false; + +var scriptFile = do_get_file("common_test_Download.js"); +Services.scriptloader.loadSubScript(NetUtil.newURI(scriptFile).spec); + +// Tests + +/** + * The download should fail early if the source and the target are the same. + */ +add_task(async function test_error_target_downloadingToSameFile() { + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + targetFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE); + + let download = await Downloads.createDownload({ + source: NetUtil.newURI(targetFile), + target: targetFile, + }); + await Assert.rejects( + download.start(), + ex => ex instanceof Downloads.Error && ex.becauseTargetFailed + ); + + Assert.ok( + await IOUtils.exists(download.target.path), + "The file should not have been deleted." + ); +}); + +/** + * Tests allowHttpStatus allowing requests + */ +add_task(async function test_error_notfound() { + const targetFile = getTempFile(TEST_TARGET_FILE_NAME); + let called = false; + const download = await Downloads.createDownload({ + source: { + url: httpUrl("notfound.gone"), + allowHttpStatus(aDownload, aStatusCode) { + Assert.strictEqual(download, aDownload, "Check Download objects"); + Assert.strictEqual(aStatusCode, 404, "The status should be correct"); + called = true; + return true; + }, + }, + target: targetFile, + }); + await download.start(); + Assert.ok(called, "allowHttpStatus should have been called"); +}); + +/** + * Tests allowHttpStatus rejecting requests + */ +add_task(async function test_error_notfound_reject() { + const targetFile = getTempFile(TEST_TARGET_FILE_NAME); + let called = false; + const download = await Downloads.createDownload({ + source: { + url: httpUrl("notfound.gone"), + allowHttpStatus(aDownload, aStatusCode) { + Assert.strictEqual(download, aDownload, "Check Download objects"); + Assert.strictEqual(aStatusCode, 404, "The status should be correct"); + called = true; + return false; + }, + }, + target: targetFile, + }); + await Assert.rejects( + download.start(), + ex => ex instanceof Downloads.Error && ex.becauseSourceFailed, + "Download should have been rejected" + ); + Assert.ok(called, "allowHttpStatus should have been called"); +}); + +/** + * Tests allowHttpStatus rejecting requests other than 404 + */ +add_task(async function test_error_busy_reject() { + const targetFile = getTempFile(TEST_TARGET_FILE_NAME); + let called = false; + const download = await Downloads.createDownload({ + source: { + url: httpUrl("busy.txt"), + allowHttpStatus(aDownload, aStatusCode) { + Assert.strictEqual(download, aDownload, "Check Download objects"); + Assert.strictEqual(aStatusCode, 504, "The status should be correct"); + called = true; + return false; + }, + }, + target: targetFile, + }); + await Assert.rejects( + download.start(), + ex => ex instanceof Downloads.Error && ex.becauseSourceFailed, + "Download should have been rejected" + ); + Assert.ok(called, "allowHttpStatus should have been called"); +}); + +/** + * Tests redirects are followed correctly, and the meta data corresponds + * to the correct, final response + */ +add_task(async function test_redirects() { + const targetFile = getTempFile(TEST_TARGET_FILE_NAME); + let called = false; + const download = await Downloads.createDownload({ + source: { + url: httpUrl("redirect"), + allowHttpStatus(aDownload, aStatusCode) { + Assert.strictEqual(download, aDownload, "Check Download objects"); + Assert.strictEqual( + aStatusCode, + 504, + "The status should be correct after a redirect" + ); + called = true; + return true; + }, + }, + target: targetFile, + }); + await download.start(); + Assert.equal( + download.contentType, + "text/plain", + "Content-Type is correct after redirect" + ); + Assert.equal( + download.totalBytes, + TEST_DATA_SHORT.length, + "Content-Length is correct after redirect" + ); + Assert.equal(download.target.size, TEST_DATA_SHORT.length); + Assert.ok(called, "allowHttpStatus should have been called"); +}); + +/** + * Tests the DownloadError object. + */ +add_task(function test_DownloadError() { + let error = new DownloadError({ + result: Cr.NS_ERROR_NOT_RESUMABLE, + message: "Not resumable.", + }); + Assert.equal(error.result, Cr.NS_ERROR_NOT_RESUMABLE); + Assert.equal(error.message, "Not resumable."); + Assert.ok(!error.becauseSourceFailed); + Assert.ok(!error.becauseTargetFailed); + Assert.ok(!error.becauseBlocked); + Assert.ok(!error.becauseBlockedByParentalControls); + + error = new DownloadError({ message: "Unknown error." }); + Assert.equal(error.result, Cr.NS_ERROR_FAILURE); + Assert.equal(error.message, "Unknown error."); + + error = new DownloadError({ result: Cr.NS_ERROR_NOT_RESUMABLE }); + Assert.equal(error.result, Cr.NS_ERROR_NOT_RESUMABLE); + Assert.ok(error.message.indexOf("Exception") > 0); + + // becauseSourceFailed will be set, but not the unknown property. + error = new DownloadError({ + message: "Unknown error.", + becauseSourceFailed: true, + becauseUnknown: true, + }); + Assert.ok(error.becauseSourceFailed); + Assert.equal(false, "becauseUnknown" in error); + + error = new DownloadError({ + result: Cr.NS_ERROR_MALFORMED_URI, + inferCause: true, + }); + Assert.equal(error.result, Cr.NS_ERROR_MALFORMED_URI); + Assert.ok(error.becauseSourceFailed); + Assert.ok(!error.becauseTargetFailed); + Assert.ok(!error.becauseBlocked); + Assert.ok(!error.becauseBlockedByParentalControls); + + // This test does not set inferCause, so becauseSourceFailed will not be set. + error = new DownloadError({ result: Cr.NS_ERROR_MALFORMED_URI }); + Assert.equal(error.result, Cr.NS_ERROR_MALFORMED_URI); + Assert.ok(!error.becauseSourceFailed); + + error = new DownloadError({ + result: Cr.NS_ERROR_FILE_INVALID_PATH, + inferCause: true, + }); + Assert.equal(error.result, Cr.NS_ERROR_FILE_INVALID_PATH); + Assert.ok(!error.becauseSourceFailed); + Assert.ok(error.becauseTargetFailed); + Assert.ok(!error.becauseBlocked); + Assert.ok(!error.becauseBlockedByParentalControls); + + error = new DownloadError({ becauseBlocked: true }); + Assert.equal(error.message, "Download blocked."); + Assert.ok(!error.becauseSourceFailed); + Assert.ok(!error.becauseTargetFailed); + Assert.ok(error.becauseBlocked); + Assert.ok(!error.becauseBlockedByParentalControls); + + error = new DownloadError({ becauseBlockedByParentalControls: true }); + Assert.equal(error.message, "Download blocked."); + Assert.ok(!error.becauseSourceFailed); + Assert.ok(!error.becauseTargetFailed); + Assert.ok(error.becauseBlocked); + Assert.ok(error.becauseBlockedByParentalControls); +}); + +add_task(async function test_cancel_interrupted_download() { + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + + let download = await Downloads.createDownload({ + source: httpUrl("interruptible_resumable.txt"), + target: targetFile, + }); + + async function createAndCancelDownload() { + info("Create an interruptible download and cancel it midway"); + mustInterruptResponses(); + const promiseDownloaded = download.start(); + await promiseDownloadMidway(download); + await download.cancel(); + + info("Unblock the interruptible download and wait for its annotation"); + continueResponses(); + await waitForAnnotation( + httpUrl("interruptible_resumable.txt"), + "downloads/destinationFileURI" + ); + + await Assert.rejects( + promiseDownloaded, + /DownloadError: Download canceled/, + "Got a download error as expected" + ); + } + + await new Promise(resolve => { + const DONE = "=== download xpcshell test console listener done ==="; + const logDone = () => Services.console.logStringMessage(DONE); + const consoleListener = msg => { + if (msg == DONE) { + Services.console.unregisterListener(consoleListener); + resolve(); + } + }; + Services.console.reset(); + Services.console.registerListener(consoleListener); + + createAndCancelDownload().then(logDone); + }); + + info( + "Assert that nsIStreamListener.onDataAvailable has not been called after download.cancel" + ); + let found = Services.console + .getMessageArray() + .map(m => m.message) + .filter(message => { + return message.includes("nsIStreamListener.onDataAvailable"); + }); + Assert.deepEqual( + found, + [], + "Expect no nsIStreamListener.onDataAvaialable error" + ); +}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadHistory.js b/toolkit/components/downloads/test/unit/test_DownloadHistory.js new file mode 100644 index 0000000000..69eb1c4728 --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_DownloadHistory.js @@ -0,0 +1,273 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests the DownloadHistory module. + */ + +"use strict"; + +const { DownloadHistory } = ChromeUtils.importESModule( + "resource://gre/modules/DownloadHistory.sys.mjs" +); + +let baseDate = new Date("2000-01-01"); + +/** + * Non-fatal assertion used to test whether the downloads in the list already + * match the expected state. + */ +function areEqual(a, b) { + if (a === b) { + Assert.equal(a, b); + return true; + } + info(a + " !== " + b); + return false; +} + +/** + * This allows waiting for an expected list at various points during the test. + */ +class TestView { + constructor(expected) { + this.expected = [...expected]; + this.downloads = []; + this.resolveWhenExpected = () => {}; + } + onDownloadAdded(download, options = {}) { + if (options.insertBefore) { + let index = this.downloads.indexOf(options.insertBefore); + this.downloads.splice(index, 0, download); + } else { + this.downloads.push(download); + } + this.checkForExpectedDownloads(); + } + onDownloadChanged(download) { + this.checkForExpectedDownloads(); + } + onDownloadRemoved(download) { + let index = this.downloads.indexOf(download); + this.downloads.splice(index, 1); + this.checkForExpectedDownloads(); + } + checkForExpectedDownloads() { + // Wait for all the expected downloads to be added or removed before doing + // the detailed tests. This is done to avoid creating irrelevant output. + if (this.downloads.length != this.expected.length) { + return; + } + for (let i = 0; i < this.downloads.length; i++) { + if ( + this.downloads[i].source.url != this.expected[i].source.url || + this.downloads[i].target.path != this.expected[i].target.path + ) { + return; + } + } + // Check and report the actual state of the downloads. Even if the items + // are in the expected order, the metadata for history downloads might not + // have been updated to the final state yet. + for (let i = 0; i < this.downloads.length; i++) { + let download = this.downloads[i]; + let testDownload = this.expected[i]; + info( + "Checking download source " + + download.source.url + + " with target " + + download.target.path + ); + if ( + !areEqual(download.succeeded, !!testDownload.succeeded) || + !areEqual(download.canceled, !!testDownload.canceled) || + !areEqual(download.hasPartialData, !!testDownload.hasPartialData) || + !areEqual(!!download.error, !!testDownload.error) + ) { + return; + } + // If the above properties match, the error details should be correct. + if (download.error) { + if (testDownload.error.becauseSourceFailed) { + Assert.equal(download.error.message, "History download failed."); + } + Assert.equal( + download.error.becauseBlockedByParentalControls, + testDownload.error.becauseBlockedByParentalControls + ); + Assert.equal( + download.error.becauseBlockedByReputationCheck, + testDownload.error.becauseBlockedByReputationCheck + ); + } + } + this.resolveWhenExpected(); + } + async waitForExpected() { + let promise = new Promise(resolve => (this.resolveWhenExpected = resolve)); + this.checkForExpectedDownloads(); + await promise; + } +} + +/** + * Tests that various operations on session and history downloads are reflected + * by the DownloadHistoryList object, and that the order of results is correct. + */ +add_task(async function test_DownloadHistory() { + // Clean up at the beginning and at the end of the test. + async function cleanup() { + await PlacesUtils.history.clear(); + } + registerCleanupFunction(cleanup); + await cleanup(); + + let testDownloads = [ + // History downloads should appear in order at the beginning of the list. + { offset: 10, canceled: true }, + { offset: 20, succeeded: true }, + { offset: 30, error: { becauseSourceFailed: true } }, + { offset: 40, error: { becauseBlockedByParentalControls: true } }, + { offset: 50, error: { becauseBlockedByReputationCheck: true } }, + // Session downloads should show up after all the history download, in the + // same order as they were added. + { offset: 45, canceled: true, inSession: true }, + { offset: 35, canceled: true, hasPartialData: true, inSession: true }, + { offset: 55, succeeded: true, inSession: true }, + ]; + const NEXT_OFFSET = 60; + + let publicList = await promiseNewList(); + let allList = await Downloads.getList(Downloads.ALL); + + async function addTestDownload(properties) { + properties.source = { + url: httpUrl("source" + properties.offset), + isPrivate: properties.isPrivate, + }; + let targetFile = getTempFile(TEST_TARGET_FILE_NAME + properties.offset); + properties.target = { path: targetFile.path }; + properties.startTime = new Date(baseDate.getTime() + properties.offset); + + let download = await Downloads.createDownload(properties); + if (properties.inSession) { + await allList.add(download); + } + + if (properties.isPrivate) { + return; + } + + // Add the download to history using the XPCOM service, then use the + // DownloadHistory module to save the associated metadata. + let promiseFileAnnotation = waitForAnnotation( + properties.source.url, + "downloads/destinationFileURI" + ); + let promiseMetaAnnotation = waitForAnnotation( + properties.source.url, + "downloads/metaData" + ); + let promiseVisit = promiseWaitForVisit(properties.source.url); + await DownloadHistory.addDownloadToHistory(download); + await promiseVisit; + await DownloadHistory.updateMetaData(download); + await Promise.all([promiseFileAnnotation, promiseMetaAnnotation]); + } + + // Add all the test downloads to history. + for (let properties of testDownloads) { + await addTestDownload(properties); + } + + // Initialize DownloadHistoryList only after having added the history and + // session downloads, and check that they are loaded in the correct order. + let historyList = await DownloadHistory.getList(); + let view = new TestView(testDownloads); + await historyList.addView(view); + await view.waitForExpected(); + + // Remove a download from history and verify that the change is reflected. + let downloadToRemove = view.expected[1]; + view.expected.splice(1, 1); + await PlacesUtils.history.remove(downloadToRemove.source.url); + await view.waitForExpected(); + + // Add a download to history and verify it's placed before session downloads, + // even if the start date is more recent. + let downloadToAdd = { offset: NEXT_OFFSET, canceled: true }; + view.expected.splice( + view.expected.findIndex(d => d.inSession), + 0, + downloadToAdd + ); + await addTestDownload(downloadToAdd); + await view.waitForExpected(); + + // Add a session download and verify it's placed after all session downloads, + // even if the start date is less recent. + let sessionDownloadToAdd = { offset: 0, inSession: true, succeeded: true }; + view.expected.push(sessionDownloadToAdd); + await addTestDownload(sessionDownloadToAdd); + await view.waitForExpected(); + + // Add a session download for the same URI without a history entry, and verify + // it's visible and placed after all session downloads. + view.expected.push(sessionDownloadToAdd); + await publicList.add(await Downloads.createDownload(sessionDownloadToAdd)); + await view.waitForExpected(); + + // Create a new DownloadHistoryList that also shows private downloads. Since + // we only have public downloads, the two lists should contain the same items. + let allHistoryList = await DownloadHistory.getList({ type: Downloads.ALL }); + let allView = new TestView(view.expected); + await allHistoryList.addView(allView); + await allView.waitForExpected(); + + // Add a new private download and verify it appears only on the complete list. + let privateDownloadToAdd = { + offset: NEXT_OFFSET + 10, + inSession: true, + succeeded: true, + isPrivate: true, + }; + allView.expected.push(privateDownloadToAdd); + await addTestDownload(privateDownloadToAdd); + await view.waitForExpected(); + await allView.waitForExpected(); + + // Now test the maxHistoryResults parameter. + let allHistoryList2 = await DownloadHistory.getList({ + type: Downloads.ALL, + maxHistoryResults: 3, + }); + // Prepare the set of downloads to contain fewer history downloads by removing + // the oldest ones. + let allView2 = new TestView(allView.expected.slice(3)); + await allHistoryList2.addView(allView2); + await allView2.waitForExpected(); + + // Create a dummy list and view like the previous limited one to just add and + // remove its view to make sure it doesn't break other lists' updates. + let dummyList = await DownloadHistory.getList({ + type: Downloads.ALL, + maxHistoryResults: 3, + }); + let dummyView = new TestView([]); + await dummyList.addView(dummyView); + await dummyList.removeView(dummyView); + + // Clear history and check that session downloads with partial data remain. + // Private downloads are also not cleared when clearing history. + view.expected = view.expected.filter(d => d.hasPartialData); + allView.expected = allView.expected.filter( + d => d.hasPartialData || d.isPrivate + ); + await PlacesUtils.history.clear(); + await view.waitForExpected(); + await allView.waitForExpected(); + + // Check that the dummy view above did not prevent the limited from updating. + allView2.expected = allView.expected; + await allView2.waitForExpected(); +}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadHistory_initialization.js b/toolkit/components/downloads/test/unit/test_DownloadHistory_initialization.js new file mode 100644 index 0000000000..ea41038bf1 --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_DownloadHistory_initialization.js @@ -0,0 +1,108 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { DownloadHistory } = ChromeUtils.importESModule( + "resource://gre/modules/DownloadHistory.sys.mjs" +); +const { PlacesTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/PlacesTestUtils.sys.mjs" +); + +let baseDate = new Date("2000-01-01"); + +/** + * This test is designed to ensure the cache of download history is correctly + * loaded and initialized. We do this by having the test as the only test in + * this file, and injecting data into the places database before we start. + */ +add_task(async function test_DownloadHistory_initialization() { + // Clean up at the beginning and at the end of the test. + async function cleanup() { + await PlacesUtils.history.clear(); + } + registerCleanupFunction(cleanup); + await cleanup(); + + let testDownloads = []; + for (let i = 10; i <= 30; i += 10) { + let targetFile = getTempFile(`${TEST_TARGET_FILE_NAME}${i}`); + let download = { + source: { + url: httpUrl(`source${i}`), + isPrivate: false, + }, + target: { path: targetFile.path }, + endTime: baseDate.getTime() + i, + fileSize: 100 + i, + state: i / 10, + }; + + await PlacesTestUtils.addVisits([ + { + uri: download.source.url, + transition: PlacesUtils.history.TRANSITIONS.DOWNLOAD, + }, + ]); + + let targetUri = Services.io.newFileURI( + new FileUtils.File(download.target.path) + ); + + await PlacesUtils.history.update({ + annotations: new Map([ + ["downloads/destinationFileURI", targetUri.spec], + [ + "downloads/metaData", + JSON.stringify({ + state: download.state, + endTime: download.endTime, + fileSize: download.fileSize, + }), + ], + ]), + url: download.source.url, + }); + + testDownloads.push(download); + } + + // Initialize DownloadHistoryList only after having added the history and + // session downloads. + let historyList = await DownloadHistory.getList(); + let downloads = await historyList.getAll(); + Assert.equal(downloads.length, testDownloads.length); + + for (let expected of testDownloads) { + let download = downloads.find(d => d.source.url == expected.source.url); + + info(`Checking download ${expected.source.url}`); + Assert.ok(download, "Should have found the expected download"); + Assert.equal( + download.endTime, + expected.endTime, + "Should have the correct end time" + ); + Assert.equal( + download.target.size, + expected.fileSize, + "Should have the correct file size" + ); + Assert.equal( + download.succeeded, + expected.state == 1, + "Should have the correct succeeded value" + ); + Assert.equal( + download.canceled, + expected.state == 3, + "Should have the correct canceled value" + ); + Assert.equal( + download.target.path, + expected.target.path, + "Should have the correct target path" + ); + } +}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadHistory_initialization2.js b/toolkit/components/downloads/test/unit/test_DownloadHistory_initialization2.js new file mode 100644 index 0000000000..8b714a8fae --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_DownloadHistory_initialization2.js @@ -0,0 +1,61 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { DownloadHistory } = ChromeUtils.importESModule( + "resource://gre/modules/DownloadHistory.sys.mjs" +); + +/** + * This test is designed to ensure the cache of download history is correctly + * loaded and initialized via adding downloads. We do this by having the test as + * the only test in this file. + */ +add_task(async function test_initialization_via_addDownload() { + // Clean up at the beginning and at the end of the test. + async function cleanup() { + await PlacesUtils.history.clear(); + } + registerCleanupFunction(cleanup); + await cleanup(); + + const download1FileLocation = getTempFile(`${TEST_TARGET_FILE_NAME}1`).path; + const download2FileLocation = getTempFile(`${TEST_TARGET_FILE_NAME}2`).path; + const download = { + source: { + url: httpUrl(`source1`), + isPrivate: false, + }, + target: { path: download1FileLocation }, + }; + + await DownloadHistory.addDownloadToHistory(download); + + // Initialize DownloadHistoryList only after having added the history and + // session downloads. + let historyList = await DownloadHistory.getList(); + let downloads = await historyList.getAll(); + Assert.equal(downloads.length, 1, "Should have only one entry"); + + Assert.equal( + downloads[0].target.path, + download1FileLocation, + "Should have the correct target path" + ); + + // Now re-add the download but with a different target. + download.target.path = download2FileLocation; + + await DownloadHistory.addDownloadToHistory(download); + + historyList = await DownloadHistory.getList(); + downloads = await historyList.getAll(); + Assert.equal(downloads.length, 1, "Should still have only one entry"); + + Assert.equal( + downloads[0].target.path, + download2FileLocation, + "Should have the correct revised target path" + ); +}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadIntegration.js b/toolkit/components/downloads/test/unit/test_DownloadIntegration.js new file mode 100644 index 0000000000..486ff8a346 --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_DownloadIntegration.js @@ -0,0 +1,441 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests the DownloadIntegration object. + */ + +"use strict"; + +// Globals + +/** + * Notifies the prompt observers and verify the expected downloads count. + * + * @param aIsPrivate + * Flag to know is test private observers. + * @param aExpectedCount + * the expected downloads count for quit and offline observers. + * @param aExpectedPBCount + * the expected downloads count for private browsing observer. + */ +function notifyPromptObservers(aIsPrivate, aExpectedCount, aExpectedPBCount) { + let cancelQuit = Cc["@mozilla.org/supports-PRBool;1"].createInstance( + Ci.nsISupportsPRBool + ); + + // Notify quit application requested observer. + DownloadIntegration._testPromptDownloads = -1; + Services.obs.notifyObservers(cancelQuit, "quit-application-requested"); + Assert.equal(DownloadIntegration._testPromptDownloads, aExpectedCount); + + // Notify offline requested observer. + DownloadIntegration._testPromptDownloads = -1; + Services.obs.notifyObservers(cancelQuit, "offline-requested"); + Assert.equal(DownloadIntegration._testPromptDownloads, aExpectedCount); + + if (aIsPrivate) { + // Notify last private browsing requested observer. + DownloadIntegration._testPromptDownloads = -1; + Services.obs.notifyObservers(cancelQuit, "last-pb-context-exiting"); + Assert.equal(DownloadIntegration._testPromptDownloads, aExpectedPBCount); + } + + delete DownloadIntegration._testPromptDownloads; +} + +// Tests + +/** + * Allows re-enabling the real download directory logic during one test. + */ +function allowDirectoriesInTest() { + DownloadIntegration.allowDirectories = true; + function cleanup() { + DownloadIntegration.allowDirectories = false; + } + registerCleanupFunction(cleanup); + return cleanup; +} + +XPCOMUtils.defineLazyGetter(this, "gStringBundle", function () { + return Services.strings.createBundle( + "chrome://mozapps/locale/downloads/downloads.properties" + ); +}); + +/** + * Tests that getSystemDownloadsDirectory returns an existing directory or + * creates a new directory depending on the platform. Instead of the real + * directory, this test is executed in the temporary directory so we can safely + * delete the created folder to check whether it is created again. + */ +add_task(async function test_getSystemDownloadsDirectory_exists_or_creates() { + let tempDir = Services.dirsvc.get("TmpD", Ci.nsIFile); + let downloadDir; + + // OSX / Linux / Windows but not XP/2k + if ( + Services.appinfo.OS == "Darwin" || + Services.appinfo.OS == "Linux" || + (Services.appinfo.OS == "WINNT" && + parseFloat(Services.sysinfo.getProperty("version")) >= 6) + ) { + downloadDir = await DownloadIntegration.getSystemDownloadsDirectory(); + Assert.equal(downloadDir, tempDir.path); + Assert.ok(await IOUtils.exists(downloadDir)); + + let info = await IOUtils.stat(downloadDir); + Assert.equal(info.type, "directory"); + } else { + let targetPath = PathUtils.join( + tempDir.path, + gStringBundle.GetStringFromName("downloadsFolder") + ); + try { + await IOUtils.remove(targetPath); + } catch (e) {} + downloadDir = await DownloadIntegration.getSystemDownloadsDirectory(); + Assert.equal(downloadDir, targetPath); + Assert.ok(await IOUtils.exists(downloadDir)); + + let info = await IOUtils.stat(downloadDir); + Assert.equal(info.type, "directory"); + await IOUtils.remove(targetPath); + } +}); + +/** + * Tests that the real directory returned by getSystemDownloadsDirectory is not + * the one that is used during unit tests. Since this is the actual downloads + * directory of the operating system, we don't try to delete it afterwards. + */ +add_task(async function test_getSystemDownloadsDirectory_real() { + let fakeDownloadDir = await DownloadIntegration.getSystemDownloadsDirectory(); + + let cleanup = allowDirectoriesInTest(); + let realDownloadDir = await DownloadIntegration.getSystemDownloadsDirectory(); + cleanup(); + + Assert.notEqual(fakeDownloadDir, realDownloadDir); +}); + +/** + * Tests that the getPreferredDownloadsDirectory returns a valid download + * directory string path. + */ +add_task(async function test_getPreferredDownloadsDirectory() { + let cleanupDirectories = allowDirectoriesInTest(); + + let folderListPrefName = "browser.download.folderList"; + let dirPrefName = "browser.download.dir"; + function cleanupPrefs() { + Services.prefs.clearUserPref(folderListPrefName); + Services.prefs.clearUserPref(dirPrefName); + } + registerCleanupFunction(cleanupPrefs); + + // For legacy cloudstorage users with folderListPrefName as 3, + // Should return the system downloads directory because the dir preference + // is not set. + Services.prefs.setIntPref(folderListPrefName, 3); + let systemDir = await DownloadIntegration.getSystemDownloadsDirectory(); + let downloadDir = await DownloadIntegration.getPreferredDownloadsDirectory(); + Assert.notEqual(downloadDir, ""); + Assert.equal(downloadDir, systemDir); + + // Should return the system downloads directory. + Services.prefs.setIntPref(folderListPrefName, 1); + systemDir = await DownloadIntegration.getSystemDownloadsDirectory(); + downloadDir = await DownloadIntegration.getPreferredDownloadsDirectory(); + Assert.notEqual(downloadDir, ""); + Assert.equal(downloadDir, systemDir); + + // Should return the desktop directory. + Services.prefs.setIntPref(folderListPrefName, 0); + downloadDir = await DownloadIntegration.getPreferredDownloadsDirectory(); + Assert.notEqual(downloadDir, ""); + Assert.equal(downloadDir, Services.dirsvc.get("Desk", Ci.nsIFile).path); + + // Should return the system downloads directory because the dir preference + // is not set. + Services.prefs.setIntPref(folderListPrefName, 2); + downloadDir = await DownloadIntegration.getPreferredDownloadsDirectory(); + Assert.notEqual(downloadDir, ""); + Assert.equal(downloadDir, systemDir); + + // Should return the directory which is listed in the dir preference. + let time = new Date().getTime(); + let tempDir = Services.dirsvc.get("TmpD", Ci.nsIFile); + tempDir.append(time); + Services.prefs.setComplexValue("browser.download.dir", Ci.nsIFile, tempDir); + downloadDir = await DownloadIntegration.getPreferredDownloadsDirectory(); + Assert.notEqual(downloadDir, ""); + Assert.equal(downloadDir, tempDir.path); + Assert.ok(await IOUtils.exists(downloadDir)); + await IOUtils.remove(tempDir.path); + + // Should return the system downloads directory beacause the path is invalid + // in the dir preference. + tempDir = Services.dirsvc.get("TmpD", Ci.nsIFile); + tempDir.append("dir_not_exist"); + tempDir.append(time); + Services.prefs.setComplexValue("browser.download.dir", Ci.nsIFile, tempDir); + downloadDir = await DownloadIntegration.getPreferredDownloadsDirectory(); + Assert.equal(downloadDir, systemDir); + + // Should return the system downloads directory because the folderList + // preference is invalid + Services.prefs.setIntPref(folderListPrefName, 999); + downloadDir = await DownloadIntegration.getPreferredDownloadsDirectory(); + Assert.equal(downloadDir, systemDir); + + cleanupPrefs(); + cleanupDirectories(); +}); + +/** + * Tests that the getTemporaryDownloadsDirectory returns a valid download + * directory string path. + */ +add_task(async function test_getTemporaryDownloadsDirectory() { + let cleanup = allowDirectoriesInTest(); + + let downloadDir = await DownloadIntegration.getTemporaryDownloadsDirectory(); + Assert.notEqual(downloadDir, ""); + + if ("nsILocalFileMac" in Ci) { + let preferredDownloadDir = + await DownloadIntegration.getPreferredDownloadsDirectory(); + Assert.equal(downloadDir, preferredDownloadDir); + } else { + let tempDir = Services.dirsvc.get("TmpD", Ci.nsIFile); + Assert.equal(downloadDir, tempDir.path); + } + + cleanup(); +}); + +// Tests DownloadObserver + +/** + * Re-enables the default observers for the following tests. + * + * This takes effect the first time a DownloadList object is created, and lasts + * until this test file has completed. + */ +add_task(async function test_observers_setup() { + DownloadIntegration.allowObservers = true; + registerCleanupFunction(function () { + DownloadIntegration.allowObservers = false; + }); +}); + +/** + * Tests notifications prompts when observers are notified if there are public + * and private active downloads. + */ +add_task(async function test_notifications() { + for (let isPrivate of [false, true]) { + mustInterruptResponses(); + + let list = await promiseNewList(isPrivate); + let download1 = await promiseNewDownload(httpUrl("interruptible.txt")); + let download2 = await promiseNewDownload(httpUrl("interruptible.txt")); + let download3 = await promiseNewDownload(httpUrl("interruptible.txt")); + let promiseAttempt1 = download1.start(); + let promiseAttempt2 = download2.start(); + download3.start().catch(() => {}); + + // Add downloads to list. + await list.add(download1); + await list.add(download2); + await list.add(download3); + // Cancel third download + await download3.cancel(); + + notifyPromptObservers(isPrivate, 2, 2); + + // Allow the downloads to complete. + continueResponses(); + await promiseAttempt1; + await promiseAttempt2; + + // Clean up. + await list.remove(download1); + await list.remove(download2); + await list.remove(download3); + } +}); + +/** + * Tests that notifications prompts observers are not notified if there are no + * public or private active downloads. + */ +add_task(async function test_no_notifications() { + for (let isPrivate of [false, true]) { + let list = await promiseNewList(isPrivate); + let download1 = await promiseNewDownload(httpUrl("interruptible.txt")); + let download2 = await promiseNewDownload(httpUrl("interruptible.txt")); + download1.start().catch(() => {}); + download2.start().catch(() => {}); + + // Add downloads to list. + await list.add(download1); + await list.add(download2); + + await download1.cancel(); + await download2.cancel(); + + notifyPromptObservers(isPrivate, 0, 0); + + // Clean up. + await list.remove(download1); + await list.remove(download2); + } +}); + +/** + * Tests notifications prompts when observers are notified if there are public + * and private active downloads at the same time. + */ +add_task(async function test_mix_notifications() { + mustInterruptResponses(); + + let publicList = await promiseNewList(); + let privateList = await Downloads.getList(Downloads.PRIVATE); + let download1 = await promiseNewDownload(httpUrl("interruptible.txt")); + let download2 = await promiseNewDownload(httpUrl("interruptible.txt")); + let promiseAttempt1 = download1.start(); + let promiseAttempt2 = download2.start(); + + // Add downloads to lists. + await publicList.add(download1); + await privateList.add(download2); + + notifyPromptObservers(true, 2, 1); + + // Allow the downloads to complete. + continueResponses(); + await promiseAttempt1; + await promiseAttempt2; + + // Clean up. + await publicList.remove(download1); + await privateList.remove(download2); +}); + +/** + * Tests suspending and resuming as well as going offline and then online again. + * The downloads should stop when suspending and start again when resuming. + */ +add_task(async function test_suspend_resume() { + // The default wake delay is 10 seconds, so set the wake delay to be much + // faster for these tests. + Services.prefs.setIntPref("browser.download.manager.resumeOnWakeDelay", 5); + + let addDownload = function (list) { + return (async function () { + let download = await promiseNewDownload(httpUrl("interruptible.txt")); + download.start().catch(() => {}); + list.add(download); + return download; + })(); + }; + + let publicList = await promiseNewList(); + let privateList = await promiseNewList(true); + + let download1 = await addDownload(publicList); + let download2 = await addDownload(publicList); + let download3 = await addDownload(privateList); + let download4 = await addDownload(privateList); + let download5 = await addDownload(publicList); + + // First, check that the downloads are all canceled when going to sleep. + Services.obs.notifyObservers(null, "sleep_notification"); + Assert.ok(download1.canceled); + Assert.ok(download2.canceled); + Assert.ok(download3.canceled); + Assert.ok(download4.canceled); + Assert.ok(download5.canceled); + + // Remove a download. It should not be started again. + publicList.remove(download5); + Assert.ok(download5.canceled); + + // When waking up again, the downloads start again after the wake delay. To be + // more robust, don't check after a delay but instead just wait for the + // downloads to finish. + Services.obs.notifyObservers(null, "wake_notification"); + await download1.whenSucceeded(); + await download2.whenSucceeded(); + await download3.whenSucceeded(); + await download4.whenSucceeded(); + + // Downloads should no longer be canceled. However, as download5 was removed + // from the public list, it will not be restarted. + Assert.ok(!download1.canceled); + Assert.ok(download5.canceled); + + // Create four new downloads and check for going offline and then online again. + + download1 = await addDownload(publicList); + download2 = await addDownload(publicList); + download3 = await addDownload(privateList); + download4 = await addDownload(privateList); + + // Going offline should cancel the downloads. + Services.obs.notifyObservers(null, "network:offline-about-to-go-offline"); + Assert.ok(download1.canceled); + Assert.ok(download2.canceled); + Assert.ok(download3.canceled); + Assert.ok(download4.canceled); + + // Going back online should start the downloads again. + Services.obs.notifyObservers( + null, + "network:offline-status-changed", + "online" + ); + await download1.whenSucceeded(); + await download2.whenSucceeded(); + await download3.whenSucceeded(); + await download4.whenSucceeded(); + + Services.prefs.clearUserPref("browser.download.manager.resumeOnWakeDelay"); +}); + +/** + * Tests both the downloads list and the in-progress downloads are clear when + * private browsing observer is notified. + */ +add_task(async function test_exit_private_browsing() { + mustInterruptResponses(); + + let privateList = await promiseNewList(true); + let download1 = await promiseNewDownload(httpUrl("source.txt")); + let download2 = await promiseNewDownload(httpUrl("interruptible.txt")); + let promiseAttempt1 = download1.start(); + download2.start(); + + // Add downloads to list. + await privateList.add(download1); + await privateList.add(download2); + + // Complete the download. + await promiseAttempt1; + + Assert.equal((await privateList.getAll()).length, 2); + + // Simulate exiting the private browsing. + await new Promise(resolve => { + DownloadIntegration._testResolveClearPrivateList = resolve; + Services.obs.notifyObservers(null, "last-pb-context-exited"); + }); + delete DownloadIntegration._testResolveClearPrivateList; + + Assert.equal((await privateList.getAll()).length, 0); + + continueResponses(); +}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadLegacy.js b/toolkit/components/downloads/test/unit/test_DownloadLegacy.js new file mode 100644 index 0000000000..972820f29e --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_DownloadLegacy.js @@ -0,0 +1,100 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests the integration with legacy interfaces for downloads. + */ + +"use strict"; + +// Execution of common tests + +// This is used in common_test_Download.js +// eslint-disable-next-line no-unused-vars +var gUseLegacySaver = true; + +var scriptFile = do_get_file("common_test_Download.js"); +Services.scriptloader.loadSubScript(NetUtil.newURI(scriptFile).spec); + +/** + * Checks the referrer for restart downloads. + * If the legacy download is stopped and restarted, the saving method + * is changed from DownloadLegacySaver to the DownloadCopySaver. + * The referrer header should be passed correctly. + */ +add_task(async function test_referrer_restart() { + let sourcePath = "/test_referrer_restart.txt"; + let sourceUrl = httpUrl("test_referrer_restart.txt"); + + function cleanup() { + gHttpServer.registerPathHandler(sourcePath, null); + } + registerCleanupFunction(cleanup); + + registerInterruptibleHandler( + sourcePath, + function firstPart(aRequest, aResponse) { + aResponse.setHeader("Content-Type", "text/plain", false); + Assert.ok(aRequest.hasHeader("Referer")); + Assert.equal(aRequest.getHeader("Referer"), TEST_REFERRER_URL); + + aResponse.setHeader( + "Content-Length", + "" + TEST_DATA_SHORT.length * 2, + false + ); + aResponse.write(TEST_DATA_SHORT); + }, + function secondPart(aRequest, aResponse) { + Assert.ok(aRequest.hasHeader("Referer")); + Assert.equal(aRequest.getHeader("Referer"), TEST_REFERRER_URL); + + aResponse.write(TEST_DATA_SHORT); + } + ); + + let referrerInfo = new ReferrerInfo( + Ci.nsIReferrerInfo.UNSAFE_URL, + true, + NetUtil.newURI(TEST_REFERRER_URL) + ); + + async function restart_and_check_referrer(download) { + let promiseSucceeded = download.whenSucceeded(); + + // Cancel the first download attempt. + await promiseDownloadMidway(download); + await download.cancel(); + + // The second request is allowed to complete. + continueResponses(); + download.start().catch(() => {}); + + // Wait for the download to finish by waiting on the whenSucceeded promise. + await promiseSucceeded; + + Assert.ok(download.stopped); + Assert.ok(download.succeeded); + Assert.ok(!download.canceled); + + checkEqualReferrerInfos(download.source.referrerInfo, referrerInfo); + } + + mustInterruptResponses(); + + let download = await promiseStartLegacyDownload(sourceUrl, { + referrerInfo, + }); + await restart_and_check_referrer(download); + + mustInterruptResponses(); + download = await promiseStartLegacyDownload(sourceUrl, { + referrerInfo, + isPrivate: true, + }); + await restart_and_check_referrer(download); + + cleanup(); +}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadList.js b/toolkit/components/downloads/test/unit/test_DownloadList.js new file mode 100644 index 0000000000..17ac5ba13c --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_DownloadList.js @@ -0,0 +1,677 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests the DownloadList object. + */ + +"use strict"; + +// Globals + +const { TelemetryTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/TelemetryTestUtils.sys.mjs" +); + +Services.prefs.setBoolPref( + "toolkit.telemetry.testing.overrideProductsCheck", + true +); + +/** + * Returns a Date in the past usable to add expirable visits. + * + * @note Expiration ignores any visit added in the last 7 days, but it's + * better be safe against DST issues, by going back one day more. + */ +function getExpirableDate() { + let dateObj = new Date(); + // Normalize to midnight + dateObj.setHours(0); + dateObj.setMinutes(0); + dateObj.setSeconds(0); + dateObj.setMilliseconds(0); + return new Date(dateObj.getTime() - 8 * 86400000); +} + +/** + * Adds an expirable history visit for a download. + * + * @param aSourceUrl + * String containing the URI for the download source, or null to use + * httpUrl("source.txt"). + * + * @return {Promise} + * @rejects JavaScript exception. + */ +function promiseExpirableDownloadVisit(aSourceUrl) { + return PlacesUtils.history.insert({ + url: aSourceUrl || httpUrl("source.txt"), + visits: [ + { + transition: PlacesUtils.history.TRANSITIONS.DOWNLOAD, + date: getExpirableDate(), + }, + ], + }); +} + +// Tests + +/** + * Checks the testing mechanism used to build different download lists. + */ +add_task(async function test_construction() { + let downloadListOne = await promiseNewList(); + let downloadListTwo = await promiseNewList(); + let privateDownloadListOne = await promiseNewList(true); + let privateDownloadListTwo = await promiseNewList(true); + + Assert.notEqual(downloadListOne, downloadListTwo); + Assert.notEqual(privateDownloadListOne, privateDownloadListTwo); + Assert.notEqual(downloadListOne, privateDownloadListOne); +}); + +/** + * Checks the methods to add and retrieve items from the list. + */ +add_task(async function test_add_getAll() { + let list = await promiseNewList(); + + let downloadOne = await promiseNewDownload(); + await list.add(downloadOne); + + let itemsOne = await list.getAll(); + Assert.equal(itemsOne.length, 1); + Assert.equal(itemsOne[0], downloadOne); + + let downloadTwo = await promiseNewDownload(); + await list.add(downloadTwo); + + let itemsTwo = await list.getAll(); + Assert.equal(itemsTwo.length, 2); + Assert.equal(itemsTwo[0], downloadOne); + Assert.equal(itemsTwo[1], downloadTwo); + + // The first snapshot should not have been modified. + Assert.equal(itemsOne.length, 1); +}); + +/** + * Checks the method to remove items from the list. + */ +add_task(async function test_remove() { + let list = await promiseNewList(); + + await list.add(await promiseNewDownload()); + await list.add(await promiseNewDownload()); + + let items = await list.getAll(); + await list.remove(items[0]); + + // Removing an item that was never added should not raise an error. + await list.remove(await promiseNewDownload()); + + items = await list.getAll(); + Assert.equal(items.length, 1); +}); + +/** + * Tests that the "add", "remove", and "getAll" methods on the global + * DownloadCombinedList object combine the contents of the global DownloadList + * objects for public and private downloads. + */ +add_task(async function test_DownloadCombinedList_add_remove_getAll() { + let publicList = await promiseNewList(); + let privateList = await Downloads.getList(Downloads.PRIVATE); + let combinedList = await Downloads.getList(Downloads.ALL); + + let publicDownload = await promiseNewDownload(); + let privateDownload = await Downloads.createDownload({ + source: { url: httpUrl("source.txt"), isPrivate: true }, + target: getTempFile(TEST_TARGET_FILE_NAME).path, + }); + + await publicList.add(publicDownload); + await privateList.add(privateDownload); + + Assert.equal((await combinedList.getAll()).length, 2); + + await combinedList.remove(publicDownload); + await combinedList.remove(privateDownload); + + Assert.equal((await combinedList.getAll()).length, 0); + + await combinedList.add(publicDownload); + await combinedList.add(privateDownload); + + Assert.equal((await publicList.getAll()).length, 1); + Assert.equal((await privateList.getAll()).length, 1); + Assert.equal((await combinedList.getAll()).length, 2); + + await publicList.remove(publicDownload); + await privateList.remove(privateDownload); + + Assert.equal((await combinedList.getAll()).length, 0); +}); + +/** + * Checks that views receive the download add and remove notifications, and that + * adding and removing views works as expected, both for a normal and a combined + * list. + */ +add_task(async function test_notifications_add_remove() { + for (let isCombined of [false, true]) { + // Force creating a new list for both the public and combined cases. + let list = await promiseNewList(); + if (isCombined) { + list = await Downloads.getList(Downloads.ALL); + } + + let downloadOne = await promiseNewDownload(); + let downloadTwo = await Downloads.createDownload({ + source: { url: httpUrl("source.txt"), isPrivate: true }, + target: getTempFile(TEST_TARGET_FILE_NAME).path, + }); + await list.add(downloadOne); + await list.add(downloadTwo); + + // Check that we receive add notifications for existing elements. + let addNotifications = 0; + let viewOne = { + onDownloadAdded(aDownload) { + // The first download to be notified should be the first that was added. + if (addNotifications == 0) { + Assert.equal(aDownload, downloadOne); + } else if (addNotifications == 1) { + Assert.equal(aDownload, downloadTwo); + } + addNotifications++; + }, + }; + await list.addView(viewOne); + Assert.equal(addNotifications, 2); + + // Check that we receive add notifications for new elements. + await list.add(await promiseNewDownload()); + Assert.equal(addNotifications, 3); + + // Check that we receive remove notifications. + let removeNotifications = 0; + let viewTwo = { + onDownloadRemoved(aDownload) { + Assert.equal(aDownload, downloadOne); + removeNotifications++; + }, + }; + await list.addView(viewTwo); + await list.remove(downloadOne); + Assert.equal(removeNotifications, 1); + + // We should not receive remove notifications after the view is removed. + await list.removeView(viewTwo); + await list.remove(downloadTwo); + Assert.equal(removeNotifications, 1); + + // We should not receive add notifications after the view is removed. + await list.removeView(viewOne); + await list.add(await promiseNewDownload()); + Assert.equal(addNotifications, 3); + } +}); + +/** + * Checks that views receive the download change notifications, both for a + * normal and a combined list. + */ +add_task(async function test_notifications_change() { + for (let isCombined of [false, true]) { + // Force creating a new list for both the public and combined cases. + let list = await promiseNewList(); + if (isCombined) { + list = await Downloads.getList(Downloads.ALL); + } + + let downloadOne = await promiseNewDownload(); + let downloadTwo = await Downloads.createDownload({ + source: { url: httpUrl("source.txt"), isPrivate: true }, + target: getTempFile(TEST_TARGET_FILE_NAME).path, + }); + await list.add(downloadOne); + await list.add(downloadTwo); + + // Check that we receive change notifications. + let receivedOnDownloadChanged = false; + await list.addView({ + onDownloadChanged(aDownload) { + Assert.equal(aDownload, downloadOne); + receivedOnDownloadChanged = true; + }, + }); + await downloadOne.start(); + Assert.ok(receivedOnDownloadChanged); + + // We should not receive change notifications after a download is removed. + receivedOnDownloadChanged = false; + await list.remove(downloadTwo); + await downloadTwo.start(); + Assert.ok(!receivedOnDownloadChanged); + } +}); + +/** + * Checks that the reference to "this" is correct in the view callbacks. + */ +add_task(async function test_notifications_this() { + let list = await promiseNewList(); + + // Check that we receive change notifications. + let receivedOnDownloadAdded = false; + let receivedOnDownloadChanged = false; + let receivedOnDownloadRemoved = false; + let view = { + onDownloadAdded() { + Assert.equal(this, view); + receivedOnDownloadAdded = true; + }, + onDownloadChanged() { + // Only do this check once. + if (!receivedOnDownloadChanged) { + Assert.equal(this, view); + receivedOnDownloadChanged = true; + } + }, + onDownloadRemoved() { + Assert.equal(this, view); + receivedOnDownloadRemoved = true; + }, + }; + await list.addView(view); + + let download = await promiseNewDownload(); + await list.add(download); + await download.start(); + await list.remove(download); + + // Verify that we executed the checks. + Assert.ok(receivedOnDownloadAdded); + Assert.ok(receivedOnDownloadChanged); + Assert.ok(receivedOnDownloadRemoved); +}); + +/** + * Checks that download is removed on history expiration. + */ +add_task(async function test_history_expiration() { + mustInterruptResponses(); + + function cleanup() { + Services.prefs.clearUserPref("places.history.expiration.max_pages"); + } + registerCleanupFunction(cleanup); + + // Set max pages to 0 to make the download expire. + Services.prefs.setIntPref("places.history.expiration.max_pages", 0); + + let list = await promiseNewList(); + let downloadOne = await promiseNewDownload(); + let downloadTwo = await promiseNewDownload(httpUrl("interruptible.txt")); + + let deferred = PromiseUtils.defer(); + let removeNotifications = 0; + let downloadView = { + onDownloadRemoved(aDownload) { + if (++removeNotifications == 2) { + deferred.resolve(); + } + }, + }; + await list.addView(downloadView); + + // Work with one finished download and one canceled download. + await downloadOne.start(); + downloadTwo.start().catch(() => {}); + await downloadTwo.cancel(); + + // We must replace the visits added while executing the downloads with visits + // that are older than 7 days, otherwise they will not be expired. + await PlacesUtils.history.clear(); + await promiseExpirableDownloadVisit(); + await promiseExpirableDownloadVisit(httpUrl("interruptible.txt")); + + // After clearing history, we can add the downloads to be removed to the list. + await list.add(downloadOne); + await list.add(downloadTwo); + + // Force a history expiration. + Cc["@mozilla.org/places/expiration;1"] + .getService(Ci.nsIObserver) + .observe(null, "places-debug-start-expiration", -1); + + // Wait for both downloads to be removed. + await deferred.promise; + + cleanup(); +}); + +/** + * Checks all downloads are removed after clearing history. + */ +add_task(async function test_history_clear() { + let list = await promiseNewList(); + let downloadOne = await promiseNewDownload(); + let downloadTwo = await promiseNewDownload(); + await list.add(downloadOne); + await list.add(downloadTwo); + + let deferred = PromiseUtils.defer(); + let removeNotifications = 0; + let downloadView = { + onDownloadRemoved(aDownload) { + if (++removeNotifications == 2) { + deferred.resolve(); + } + }, + }; + await list.addView(downloadView); + + await downloadOne.start(); + await downloadTwo.start(); + + await PlacesUtils.history.clear(); + + // Wait for the removal notifications that may still be pending. + await deferred.promise; +}); + +/** + * Tests the removeFinished method to ensure that it only removes + * finished downloads. + */ +add_task(async function test_removeFinished() { + let list = await promiseNewList(); + let downloadOne = await promiseNewDownload(); + let downloadTwo = await promiseNewDownload(); + let downloadThree = await promiseNewDownload(); + let downloadFour = await promiseNewDownload(); + await list.add(downloadOne); + await list.add(downloadTwo); + await list.add(downloadThree); + await list.add(downloadFour); + + let deferred = PromiseUtils.defer(); + let removeNotifications = 0; + let downloadView = { + onDownloadRemoved(aDownload) { + Assert.ok( + aDownload == downloadOne || + aDownload == downloadTwo || + aDownload == downloadThree + ); + Assert.ok(removeNotifications < 3); + if (++removeNotifications == 3) { + deferred.resolve(); + } + }, + }; + await list.addView(downloadView); + + // Start three of the downloads, but don't start downloadTwo, then set + // downloadFour to have partial data. All downloads except downloadFour + // should be removed. + await downloadOne.start(); + await downloadThree.start(); + await downloadFour.start(); + downloadFour.hasPartialData = true; + + list.removeFinished(); + await deferred.promise; + + let downloads = await list.getAll(); + Assert.equal(downloads.length, 1); +}); + +/** + * Tests that removeFinished method keeps the file that is currently downloading, + * even if it needs to remove failed download of the same file. + */ +add_task(async function test_removeFinished_keepsDownloadingFile() { + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + + let oneDownload = await Downloads.createDownload({ + source: httpUrl("empty.txt"), + target: targetFile.path, + }); + + let otherDownload = await Downloads.createDownload({ + source: httpUrl("empty.txt"), + target: targetFile.path, + }); + + let list = await promiseNewList(); + await list.add(oneDownload); + await list.add(otherDownload); + + let deferred = PromiseUtils.defer(); + let downloadView = { + async onDownloadRemoved(aDownload) { + Assert.equal(aDownload, oneDownload); + await TestUtils.waitForCondition(() => oneDownload._finalizeExecuted); + deferred.resolve(); + }, + }; + await list.addView(downloadView); + + await oneDownload.start(); + await otherDownload.start(); + + oneDownload.hasPartialData = otherDownload.hasPartialData = true; + oneDownload.error = "Download failed"; + + list.removeFinished(); + await deferred.promise; + + let downloads = await list.getAll(); + Assert.equal( + downloads.length, + 1, + "Failed download should be removed, active download should be kept" + ); + + Assert.ok( + await IOUtils.exists(otherDownload.target.path), + "The file should not have been deleted." + ); +}); + +/** + * Tests the global DownloadSummary objects for the public, private, and + * combined download lists. + */ +add_task(async function test_DownloadSummary() { + mustInterruptResponses(); + + let publicList = await promiseNewList(); + let privateList = await Downloads.getList(Downloads.PRIVATE); + + let publicSummary = await Downloads.getSummary(Downloads.PUBLIC); + let privateSummary = await Downloads.getSummary(Downloads.PRIVATE); + let combinedSummary = await Downloads.getSummary(Downloads.ALL); + + // Add a public download that has succeeded. + let succeededPublicDownload = await promiseNewDownload(); + await succeededPublicDownload.start(); + await publicList.add(succeededPublicDownload); + + // Add a public download that has been canceled midway. + let canceledPublicDownload = await promiseNewDownload( + httpUrl("interruptible.txt") + ); + canceledPublicDownload.start().catch(() => {}); + await promiseDownloadMidway(canceledPublicDownload); + await canceledPublicDownload.cancel(); + await publicList.add(canceledPublicDownload); + + // Add a public download that is in progress. + let inProgressPublicDownload = await promiseNewDownload( + httpUrl("interruptible.txt") + ); + inProgressPublicDownload.start().catch(() => {}); + await promiseDownloadMidway(inProgressPublicDownload); + await publicList.add(inProgressPublicDownload); + + // Add a public download of unknown size that is in progress. + let inProgressSizelessPublicDownload = await promiseNewDownload( + httpUrl("interruptible_nosize.txt") + ); + inProgressSizelessPublicDownload.start().catch(() => {}); + await promiseDownloadStarted(inProgressSizelessPublicDownload); + await publicList.add(inProgressSizelessPublicDownload); + + // Add a private download that is in progress. + let inProgressPrivateDownload = await Downloads.createDownload({ + source: { url: httpUrl("interruptible.txt"), isPrivate: true }, + target: getTempFile(TEST_TARGET_FILE_NAME).path, + }); + inProgressPrivateDownload.start().catch(() => {}); + await promiseDownloadMidway(inProgressPrivateDownload); + await privateList.add(inProgressPrivateDownload); + + // Verify that the summary includes the total number of bytes and the + // currently transferred bytes only for the downloads that are not stopped. + // For simplicity, we assume that after a download is added to the list, its + // current state is immediately propagated to the summary object, which is + // true in the current implementation, though it is not guaranteed as all the + // download operations may happen asynchronously. + Assert.ok(!publicSummary.allHaveStopped); + Assert.ok(!publicSummary.allUnknownSize); + Assert.equal(publicSummary.progressTotalBytes, TEST_DATA_SHORT.length * 3); + Assert.equal(publicSummary.progressCurrentBytes, TEST_DATA_SHORT.length * 2); + + Assert.ok(!privateSummary.allHaveStopped); + Assert.ok(!privateSummary.allUnknownSize); + Assert.equal(privateSummary.progressTotalBytes, TEST_DATA_SHORT.length * 2); + Assert.equal(privateSummary.progressCurrentBytes, TEST_DATA_SHORT.length); + + Assert.ok(!combinedSummary.allHaveStopped); + Assert.ok(!combinedSummary.allUnknownSize); + Assert.equal(combinedSummary.progressTotalBytes, TEST_DATA_SHORT.length * 5); + Assert.equal( + combinedSummary.progressCurrentBytes, + TEST_DATA_SHORT.length * 3 + ); + + await inProgressPublicDownload.cancel(); + + // Stopping the download should have excluded it from the summary, but we + // should still have one public download (with unknown size) and also one + // private download remaining. + Assert.ok(!publicSummary.allHaveStopped); + Assert.ok(publicSummary.allUnknownSize); + Assert.equal(publicSummary.progressTotalBytes, TEST_DATA_SHORT.length); + Assert.equal(publicSummary.progressCurrentBytes, TEST_DATA_SHORT.length); + + Assert.ok(!privateSummary.allHaveStopped); + Assert.ok(!privateSummary.allUnknownSize); + Assert.equal(privateSummary.progressTotalBytes, TEST_DATA_SHORT.length * 2); + Assert.equal(privateSummary.progressCurrentBytes, TEST_DATA_SHORT.length); + + Assert.ok(!combinedSummary.allHaveStopped); + Assert.ok(!combinedSummary.allUnknownSize); + Assert.equal(combinedSummary.progressTotalBytes, TEST_DATA_SHORT.length * 3); + Assert.equal( + combinedSummary.progressCurrentBytes, + TEST_DATA_SHORT.length * 2 + ); + + await inProgressPrivateDownload.cancel(); + + // Stopping the private download should have excluded it from the summary, so + // now only the unknown size public download should remain. + Assert.ok(!publicSummary.allHaveStopped); + Assert.ok(publicSummary.allUnknownSize); + Assert.equal(publicSummary.progressTotalBytes, TEST_DATA_SHORT.length); + Assert.equal(publicSummary.progressCurrentBytes, TEST_DATA_SHORT.length); + + Assert.ok(privateSummary.allHaveStopped); + Assert.ok(privateSummary.allUnknownSize); + Assert.equal(privateSummary.progressTotalBytes, 0); + Assert.equal(privateSummary.progressCurrentBytes, 0); + + Assert.ok(!combinedSummary.allHaveStopped); + Assert.ok(combinedSummary.allUnknownSize); + Assert.equal(combinedSummary.progressTotalBytes, TEST_DATA_SHORT.length); + Assert.equal(combinedSummary.progressCurrentBytes, TEST_DATA_SHORT.length); + + await inProgressSizelessPublicDownload.cancel(); + + // All the downloads should be stopped now. + Assert.ok(publicSummary.allHaveStopped); + Assert.ok(publicSummary.allUnknownSize); + Assert.equal(publicSummary.progressTotalBytes, 0); + Assert.equal(publicSummary.progressCurrentBytes, 0); + + Assert.ok(privateSummary.allHaveStopped); + Assert.ok(privateSummary.allUnknownSize); + Assert.equal(privateSummary.progressTotalBytes, 0); + Assert.equal(privateSummary.progressCurrentBytes, 0); + + Assert.ok(combinedSummary.allHaveStopped); + Assert.ok(combinedSummary.allUnknownSize); + Assert.equal(combinedSummary.progressTotalBytes, 0); + Assert.equal(combinedSummary.progressCurrentBytes, 0); +}); + +/** + * Checks that views receive the summary change notification. This is tested on + * the combined summary when adding a public download, as we assume that if we + * pass the test in this case we will also pass it in the others. + */ +add_task(async function test_DownloadSummary_notifications() { + let list = await promiseNewList(); + let summary = await Downloads.getSummary(Downloads.ALL); + + let download = await promiseNewDownload(); + await list.add(download); + + // Check that we receive change notifications. + let receivedOnSummaryChanged = false; + await summary.addView({ + onSummaryChanged() { + receivedOnSummaryChanged = true; + }, + }); + await download.start(); + Assert.ok(receivedOnSummaryChanged); +}); + +/** + * Tests that if a new download is added, telemetry records the event and type of the file. + */ +add_task(async function test_downloadAddedTelemetry() { + Services.telemetry.clearEvents(); + Services.telemetry.setEventRecordingEnabled("downloads", true); + + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + + let download = await Downloads.createDownload({ + source: httpUrl("empty.txt"), + target: targetFile.path, + }); + + let list = await Downloads.getList(Downloads.ALL); + await list.add(download); + download.start(); + await promiseDownloadFinished(download); + + TelemetryTestUtils.assertEvents([ + { + category: "downloads", + method: "added", + object: "fileExtension", + value: "txt", + }, + ]); +}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadPaths.js b/toolkit/components/downloads/test/unit/test_DownloadPaths.js new file mode 100644 index 0000000000..00fb070669 --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_DownloadPaths.js @@ -0,0 +1,189 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests for the "DownloadPaths.sys.mjs" JavaScript module. + */ + +function testSanitize(leafName, expectedLeafName, options = {}) { + Assert.equal(DownloadPaths.sanitize(leafName, options), expectedLeafName); +} + +function testSplitBaseNameAndExtension(aLeafName, [aBase, aExt]) { + var [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName); + Assert.equal(base, aBase); + Assert.equal(ext, aExt); + + // If we modify the base name and concatenate it with the extension again, + // another roundtrip through the function should give a consistent result. + // The only exception is when we introduce an extension in a file name that + // didn't have one or that ended with one of the special cases like ".gz". If + // we avoid using a dot and we introduce at least another special character, + // the results are always consistent. + [base, ext] = DownloadPaths.splitBaseNameAndExtension("(" + base + ")" + ext); + Assert.equal(base, "(" + aBase + ")"); + Assert.equal(ext, aExt); +} + +function testCreateNiceUniqueFile(aTempFile, aExpectedLeafName) { + var createdFile = DownloadPaths.createNiceUniqueFile(aTempFile); + Assert.equal(createdFile.leafName, aExpectedLeafName); +} + +add_task(async function test_sanitize() { + // Platform-dependent conversion of special characters to spaces. + const kSpecialChars = 'A:*?|""<<>>;,+=[]B][=+,;>><<""|?*:C'; + if (AppConstants.platform == "android") { + testSanitize(kSpecialChars, "A B C"); + testSanitize(" :: Website :: ", "Website"); + testSanitize("* Website!", "Website!"); + testSanitize("Website | Page!", "Website Page!"); + testSanitize("Directory Listing: /a/b/", "Directory Listing _a_b_"); + } else if (AppConstants.platform == "win") { + testSanitize(kSpecialChars, "A ;,+=[]B][=+,; C"); + testSanitize(" :: Website :: ", "Website"); + testSanitize("* Website!", "Website!"); + testSanitize("Website | Page!", "Website Page!"); + testSanitize("Directory Listing: /a/b/", "Directory Listing _a_b_"); + } else if (AppConstants.platform == "macosx") { + testSanitize(kSpecialChars, "A ;,+=[]B][=+,; C"); + testSanitize(" :: Website :: ", "Website"); + testSanitize("* Website!", "Website!"); + testSanitize("Website | Page!", "Website Page!"); + testSanitize("Directory Listing: /a/b/", "Directory Listing _a_b_"); + } else { + testSanitize(kSpecialChars, "A ;,+=[]B][=+,; C"); + testSanitize(" :: Website :: ", "Website"); + testSanitize("* Website!", "Website!"); + testSanitize("Website | Page!", "Website Page!"); + testSanitize("Directory Listing: /a/b/", "Directory Listing _a_b_"); + } + + // Conversion of consecutive runs of slashes and backslashes to underscores. + testSanitize("\\ \\\\Website\\/Page// /", "_ __Website__Page__ _"); + + // Removal of leading and trailing whitespace and dots after conversion. + testSanitize(" Website ", "Website"); + testSanitize(". . Website . Page . .", "Website . Page"); + testSanitize(" File . txt ", "File . txt"); + testSanitize("\f\n\r\t\v\x00\x1f\x7f\x80\x9f\xa0 . txt", "txt"); + testSanitize("\u1680\u180e\u2000\u2008\u200a . txt", "txt"); + testSanitize("\u2028\u2029\u202f\u205f\u3000\ufeff . txt", "txt"); + + // Strings with whitespace and dots only. + testSanitize(".", ""); + testSanitize("..", ""); + testSanitize(" ", ""); + testSanitize(" . ", ""); + + // Stripping of BIDI formatting characters. + testSanitize("\u200e \u202b\u202c\u202d\u202etest\x7f\u200f", "_ ____test _"); + testSanitize("AB\x7f\u202a\x7f\u202a\x7fCD", "AB _ _ CD"); + + // Stripping of colons: + testSanitize("foo:bar", "foo bar"); + + // not compressing whitespaces. + testSanitize("foo : bar", "foo bar", { compressWhitespaces: false }); + + testSanitize("thing.lnk", "thing.lnk.download"); + testSanitize("thing.lnk\n", "thing.lnk.download"); + testSanitize("thing.lnk", "thing.lnk", { + allowInvalidFilenames: true, + }); + testSanitize("thing.lnk\n", "thing.lnk", { + allowInvalidFilenames: true, + }); + testSanitize("thing.URl", "thing.URl.download"); + testSanitize("thing.URl \n", "thing.URl", { + allowInvalidFilenames: true, + }); + + testSanitize("thing and more .URl", "thing and more .URl", { + allowInvalidFilenames: true, + }); + testSanitize("thing and more .URl ", "thing and more .URl", { + compressWhitespaces: false, + allowInvalidFilenames: true, + }); + + testSanitize("thing.local|", "thing.local.download"); + testSanitize("thing.lo|cal", "thing.lo cal"); + testSanitize('thing.local/*"', "thing.local_"); + + testSanitize("thing.desktoP", "thing.desktoP.download"); + testSanitize("thing.desktoP \n", "thing.desktoP", { + allowInvalidFilenames: true, + }); +}); + +add_task(async function test_splitBaseNameAndExtension() { + // Usual file names. + testSplitBaseNameAndExtension("base", ["base", ""]); + testSplitBaseNameAndExtension("base.ext", ["base", ".ext"]); + testSplitBaseNameAndExtension("base.application", ["base", ".application"]); + testSplitBaseNameAndExtension("base.x.Z", ["base", ".x.Z"]); + testSplitBaseNameAndExtension("base.ext.Z", ["base", ".ext.Z"]); + testSplitBaseNameAndExtension("base.ext.gz", ["base", ".ext.gz"]); + testSplitBaseNameAndExtension("base.ext.Bz2", ["base", ".ext.Bz2"]); + testSplitBaseNameAndExtension("base..ext", ["base.", ".ext"]); + testSplitBaseNameAndExtension("base..Z", ["base.", ".Z"]); + testSplitBaseNameAndExtension("base. .Z", ["base. ", ".Z"]); + testSplitBaseNameAndExtension("base.base.Bz2", ["base.base", ".Bz2"]); + testSplitBaseNameAndExtension("base .ext", ["base ", ".ext"]); + + // Corner cases. A name ending with a dot technically has no extension, but + // we consider the ending dot separately from the base name so that modifying + // the latter never results in an extension being introduced accidentally. + // Names beginning with a dot are hidden files on Unix-like platforms and if + // their name doesn't contain another dot they should have no extension, but + // on Windows the whole name is considered as an extension. + testSplitBaseNameAndExtension("base.", ["base", "."]); + testSplitBaseNameAndExtension(".ext", ["", ".ext"]); + + // Unusual file names (not recommended as input to the function). + testSplitBaseNameAndExtension("base. ", ["base", ". "]); + testSplitBaseNameAndExtension("base ", ["base ", ""]); + testSplitBaseNameAndExtension("", ["", ""]); + testSplitBaseNameAndExtension(" ", [" ", ""]); + testSplitBaseNameAndExtension(" . ", [" ", ". "]); + testSplitBaseNameAndExtension(" .. ", [" .", ". "]); + testSplitBaseNameAndExtension(" .ext", [" ", ".ext"]); + testSplitBaseNameAndExtension(" .ext. ", [" .ext", ". "]); + testSplitBaseNameAndExtension(" .ext.gz ", [" .ext", ".gz "]); +}); + +add_task(async function test_createNiceUniqueFile() { + var destDir = FileTestUtils.getTempFile("destdir"); + destDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY); + + // Single extension. + var tempFile = destDir.clone(); + tempFile.append("test.txt"); + testCreateNiceUniqueFile(tempFile, "test.txt"); + testCreateNiceUniqueFile(tempFile, "test(1).txt"); + testCreateNiceUniqueFile(tempFile, "test(2).txt"); + + // Double extension. + tempFile.leafName = "test.tar.gz"; + testCreateNiceUniqueFile(tempFile, "test.tar.gz"); + testCreateNiceUniqueFile(tempFile, "test(1).tar.gz"); + testCreateNiceUniqueFile(tempFile, "test(2).tar.gz"); + + // Test automatic shortening of long file names. We don't know exactly how + // many characters are removed, because it depends on the name of the folder + // where the file is located. + tempFile.leafName = new Array(256).join("T") + ".txt"; + var newFile = DownloadPaths.createNiceUniqueFile(tempFile); + Assert.ok(newFile.leafName.length < tempFile.leafName.length); + Assert.equal(newFile.leafName.slice(-4), ".txt"); + + // Creating a valid file name from an invalid one is not always possible. + tempFile.append("file-under-long-directory.txt"); + try { + DownloadPaths.createNiceUniqueFile(tempFile); + do_throw("Exception expected with a long parent directory name."); + } catch (e) { + // An exception is expected, but we don't know which one exactly. + } +}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadStore.js b/toolkit/components/downloads/test/unit/test_DownloadStore.js new file mode 100644 index 0000000000..9353b63060 --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_DownloadStore.js @@ -0,0 +1,458 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests the DownloadStore object. + */ + +"use strict"; + +// Globals + +ChromeUtils.defineESModuleGetters(this, { + DownloadError: "resource://gre/modules/DownloadCore.sys.mjs", + DownloadStore: "resource://gre/modules/DownloadStore.sys.mjs", +}); + +/** + * Returns a new DownloadList object with an associated DownloadStore. + * + * @param aStorePath + * String pointing to the file to be associated with the DownloadStore, + * or undefined to use a non-existing temporary file. In this case, the + * temporary file is deleted when the test file execution finishes. + * + * @return {Promise} + * @resolves Array [ Newly created DownloadList , associated DownloadStore ]. + * @rejects JavaScript exception. + */ +function promiseNewListAndStore(aStorePath) { + return promiseNewList().then(function (aList) { + let path = aStorePath || getTempFile(TEST_STORE_FILE_NAME).path; + let store = new DownloadStore(aList, path); + return [aList, store]; + }); +} + +// Tests + +/** + * Saves downloads to a file, then reloads them. + */ +add_task(async function test_save_reload() { + let [listForSave, storeForSave] = await promiseNewListAndStore(); + let [listForLoad, storeForLoad] = await promiseNewListAndStore( + storeForSave.path + ); + let referrerInfo = new ReferrerInfo( + Ci.nsIReferrerInfo.EMPTY, + true, + NetUtil.newURI(TEST_REFERRER_URL) + ); + + listForSave.add(await promiseNewDownload(httpUrl("source.txt"))); + listForSave.add( + await Downloads.createDownload({ + source: { url: httpUrl("empty.txt"), referrerInfo }, + target: getTempFile(TEST_TARGET_FILE_NAME), + }) + ); + + // If we used a callback to adjust the channel, the download should + // not be serialized because we can't recreate it across sessions. + let adjustedDownload = await Downloads.createDownload({ + source: { + url: httpUrl("empty.txt"), + adjustChannel: () => Promise.resolve(), + }, + target: getTempFile(TEST_TARGET_FILE_NAME), + }); + listForSave.add(adjustedDownload); + + let legacyDownload = await promiseStartLegacyDownload(); + await legacyDownload.cancel(); + listForSave.add(legacyDownload); + + await storeForSave.save(); + await storeForLoad.load(); + + // Remove the adjusted download because it should not appear here. + listForSave.remove(adjustedDownload); + + let itemsForSave = await listForSave.getAll(); + let itemsForLoad = await listForLoad.getAll(); + + Assert.equal(itemsForSave.length, itemsForLoad.length); + + // Downloads should be reloaded in the same order. + for (let i = 0; i < itemsForSave.length; i++) { + // The reloaded downloads are different objects. + Assert.notEqual(itemsForSave[i], itemsForLoad[i]); + + // The reloaded downloads have the same properties. + Assert.equal(itemsForSave[i].source.url, itemsForLoad[i].source.url); + Assert.equal( + !!itemsForSave[i].source.referrerInfo, + !!itemsForLoad[i].source.referrerInfo + ); + if ( + itemsForSave[i].source.referrerInfo && + itemsForLoad[i].source.referrerInfo + ) { + Assert.ok( + itemsForSave[i].source.referrerInfo.equals( + itemsForLoad[i].source.referrerInfo + ) + ); + } + Assert.equal(itemsForSave[i].target.path, itemsForLoad[i].target.path); + Assert.equal( + itemsForSave[i].saver.toSerializable(), + itemsForLoad[i].saver.toSerializable() + ); + } +}); + +/** + * Checks that saving an empty list deletes any existing file. + */ +add_task(async function test_save_empty() { + let [, store] = await promiseNewListAndStore(); + + await IOUtils.write(store.path, new Uint8Array()); + + await store.save(); + + let successful; + try { + await IOUtils.read(store.path); + successful = true; + } catch (ex) { + successful = ex.name != "NotFoundError"; + } + + ok(!successful, "File should not exist"); + + // If the file does not exist, saving should not generate exceptions. + await store.save(); +}); + +/** + * Checks that loading from a missing file results in an empty list. + */ +add_task(async function test_load_empty() { + let [list, store] = await promiseNewListAndStore(); + + let succeesful; + try { + await IOUtils.read(store.path); + succeesful = true; + } catch (ex) { + succeesful = ex.name != "NotFoundError"; + } + + ok(!succeesful, "File should not exist"); + + let items = await list.getAll(); + Assert.equal(items.length, 0); +}); + +/** + * Loads downloads from a string in a predefined format. The purpose of this + * test is to verify that the JSON format used in previous versions can be + * loaded, assuming the file is reloaded on the same platform. + */ +add_task(async function test_load_string_predefined() { + let [list, store] = await promiseNewListAndStore(); + + // The platform-dependent file name should be generated dynamically. + let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path; + let filePathLiteral = JSON.stringify(targetPath); + let sourceUriLiteral = JSON.stringify(httpUrl("source.txt")); + let emptyUriLiteral = JSON.stringify(httpUrl("empty.txt")); + let referrerInfo = new ReferrerInfo( + Ci.nsIReferrerInfo.EMPTY, + true, + NetUtil.newURI(TEST_REFERRER_URL) + ); + let referrerInfoLiteral = JSON.stringify( + E10SUtils.serializeReferrerInfo(referrerInfo) + ); + + let string = + '{"list":[{"source":' + + sourceUriLiteral + + "," + + '"target":' + + filePathLiteral + + "}," + + '{"source":{"url":' + + emptyUriLiteral + + "," + + '"referrerInfo":' + + referrerInfoLiteral + + "}," + + '"target":' + + filePathLiteral + + "}]}"; + + await IOUtils.write(store.path, new TextEncoder().encode(string), { + tmpPath: store.path + ".tmp", + }); + + await store.load(); + + let items = await list.getAll(); + + Assert.equal(items.length, 2); + + Assert.equal(items[0].source.url, httpUrl("source.txt")); + Assert.equal(items[0].target.path, targetPath); + + Assert.equal(items[1].source.url, httpUrl("empty.txt")); + + checkEqualReferrerInfos(items[1].source.referrerInfo, referrerInfo); + Assert.equal(items[1].target.path, targetPath); +}); + +/** + * Loads downloads from a well-formed JSON string containing unrecognized data. + */ +add_task(async function test_load_string_unrecognized() { + let [list, store] = await promiseNewListAndStore(); + + // The platform-dependent file name should be generated dynamically. + let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path; + let filePathLiteral = JSON.stringify(targetPath); + let sourceUriLiteral = JSON.stringify(httpUrl("source.txt")); + + let string = + '{"list":[{"source":null,' + + '"target":null},' + + '{"source":{"url":' + + sourceUriLiteral + + "}," + + '"target":{"path":' + + filePathLiteral + + "}," + + '"saver":{"type":"copy"}}]}'; + + await IOUtils.write(store.path, new TextEncoder().encode(string), { + tmpPath: store.path + ".tmp", + }); + + await store.load(); + + let items = await list.getAll(); + + Assert.equal(items.length, 1); + + Assert.equal(items[0].source.url, httpUrl("source.txt")); + Assert.equal(items[0].target.path, targetPath); +}); + +/** + * Loads downloads from a malformed JSON string. + */ +add_task(async function test_load_string_malformed() { + let [list, store] = await promiseNewListAndStore(); + + let string = + '{"list":[{"source":null,"target":null},' + + '{"source":{"url":"about:blank"}}}'; + + await IOUtils.write(store.path, new TextEncoder().encode(string), { + tmpPath: store.path + ".tmp", + }); + + try { + await store.load(); + do_throw("Exception expected when JSON data is malformed."); + } catch (ex) { + if (ex.name != "SyntaxError") { + throw ex; + } + info("The expected SyntaxError exception was thrown."); + } + + let items = await list.getAll(); + + Assert.equal(items.length, 0); +}); + +/** + * Saves downloads with unknown properties to a file and then reloads + * them to ensure that these properties are preserved. + */ +add_task(async function test_save_reload_unknownProperties() { + let [listForSave, storeForSave] = await promiseNewListAndStore(); + let [listForLoad, storeForLoad] = await promiseNewListAndStore( + storeForSave.path + ); + + let download1 = await promiseNewDownload(httpUrl("source.txt")); + // startTime should be ignored as it is a known property, and error + // is ignored by serialization + download1._unknownProperties = { + peanut: "butter", + orange: "marmalade", + startTime: 77, + error: { message: "Passed" }, + }; + listForSave.add(download1); + + let download2 = await promiseStartLegacyDownload(); + await download2.cancel(); + download2._unknownProperties = { number: 5, object: { test: "string" } }; + listForSave.add(download2); + + let referrerInfo = new ReferrerInfo( + Ci.nsIReferrerInfo.EMPTY, + true, + NetUtil.newURI(TEST_REFERRER_URL) + ); + let download3 = await Downloads.createDownload({ + source: { + url: httpUrl("empty.txt"), + referrerInfo, + source1: "download3source1", + source2: "download3source2", + }, + target: { + path: getTempFile(TEST_TARGET_FILE_NAME).path, + target1: "download3target1", + target2: "download3target2", + }, + saver: { + type: "copy", + saver1: "download3saver1", + saver2: "download3saver2", + }, + }); + listForSave.add(download3); + + await storeForSave.save(); + await storeForLoad.load(); + + let itemsForSave = await listForSave.getAll(); + let itemsForLoad = await listForLoad.getAll(); + + Assert.equal(itemsForSave.length, itemsForLoad.length); + + Assert.equal(Object.keys(itemsForLoad[0]._unknownProperties).length, 2); + Assert.equal(itemsForLoad[0]._unknownProperties.peanut, "butter"); + Assert.equal(itemsForLoad[0]._unknownProperties.orange, "marmalade"); + Assert.equal(false, "startTime" in itemsForLoad[0]._unknownProperties); + Assert.equal(false, "error" in itemsForLoad[0]._unknownProperties); + + Assert.equal(Object.keys(itemsForLoad[1]._unknownProperties).length, 2); + Assert.equal(itemsForLoad[1]._unknownProperties.number, 5); + Assert.equal(itemsForLoad[1]._unknownProperties.object.test, "string"); + + Assert.equal( + Object.keys(itemsForLoad[2].source._unknownProperties).length, + 2 + ); + Assert.equal( + itemsForLoad[2].source._unknownProperties.source1, + "download3source1" + ); + Assert.equal( + itemsForLoad[2].source._unknownProperties.source2, + "download3source2" + ); + + Assert.equal( + Object.keys(itemsForLoad[2].target._unknownProperties).length, + 2 + ); + Assert.equal( + itemsForLoad[2].target._unknownProperties.target1, + "download3target1" + ); + Assert.equal( + itemsForLoad[2].target._unknownProperties.target2, + "download3target2" + ); + + Assert.equal(Object.keys(itemsForLoad[2].saver._unknownProperties).length, 2); + Assert.equal( + itemsForLoad[2].saver._unknownProperties.saver1, + "download3saver1" + ); + Assert.equal( + itemsForLoad[2].saver._unknownProperties.saver2, + "download3saver2" + ); +}); + +/** + * Saves insecure downloads to a file, then reloads the file and checks if they + * are still there. + */ +add_task(async function test_insecure_download_deletion() { + let [listForSave, storeForSave] = await promiseNewListAndStore(); + let [listForLoad, storeForLoad] = await promiseNewListAndStore( + storeForSave.path + ); + let referrerInfo = new ReferrerInfo( + Ci.nsIReferrerInfo.EMPTY, + true, + NetUtil.newURI(TEST_REFERRER_URL) + ); + + const createTestDownload = async startTime => { + // Create a valid test download and start it so it creates a file + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + let download = await Downloads.createDownload({ + source: { url: httpUrl("empty.txt"), referrerInfo }, + target: targetFile.path, + startTime: new Date().toISOString(), + contentType: "application/zip", + }); + await download.start(); + + // Add an "Insecure Download" error and overwrite the start time for + // serialization + download.hasBlockedData = true; + download.error = DownloadError.fromSerializable({ + becauseBlockedByReputationCheck: true, + reputationCheckVerdict: "Insecure", + }); + download.startTime = startTime; + + let targetPath = download.target.path; + + // Add download to store, save, load and retrieve deserialized download list + listForSave.add(download); + await storeForSave.save(); + await storeForLoad.load(); + let loadedDownloadList = await listForLoad.getAll(); + + return [loadedDownloadList, targetPath]; + }; + + // Insecure downloads that are older than 5 minutes should get removed from + // the download-store and the file should get deleted. (360000 = 6 minutes) + let [loadedDownloadList1, targetPath1] = await createTestDownload( + new Date(Date.now() - 360000) + ); + + Assert.equal(loadedDownloadList1.length, 0, "Download should be removed"); + Assert.ok( + !(await IOUtils.exists(targetPath1)), + "The file should have been deleted." + ); + + // Insecure downloads that are newer than 5 minutes should stay in the + // download store and the file should remain. + let [loadedDownloadList2, targetPath2] = await createTestDownload(new Date()); + + Assert.equal(loadedDownloadList2.length, 1, "Download should be kept"); + Assert.ok( + await IOUtils.exists(targetPath2), + "The file should have not been deleted." + ); +}); diff --git a/toolkit/components/downloads/test/unit/test_Download_noext_win.js b/toolkit/components/downloads/test/unit/test_Download_noext_win.js new file mode 100644 index 0000000000..0e226229f6 --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_Download_noext_win.js @@ -0,0 +1,59 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function () { + info("Get a file without extension"); + let noExtFile = getTempFile("test_bug_1661365"); + Assert.ok(!noExtFile.leafName.includes("."), "Sanity check the filename"); + info("Create an exe file with the same name"); + await IOUtils.remove(noExtFile.path + ".exe", { ignoreAbsent: true }); + let exeFile = new FileUtils.File(noExtFile.path + ".exe"); + exeFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE); + Assert.ok(await fileExists(exeFile.path), "Sanity check the exe exists."); + Assert.equal( + exeFile.leafName, + noExtFile.leafName + ".exe", + "Sanity check the file names." + ); + registerCleanupFunction(async function () { + await IOUtils.remove(noExtFile.path, { ignoreAbsent: true }); + await IOUtils.remove(exeFile.path, { ignoreAbsent: true }); + }); + + info("Download to the no-extension file"); + let download = await Downloads.createDownload({ + source: httpUrl("source.txt"), + target: noExtFile, + }); + await download.start(); + + Assert.ok( + await fileExists(download.target.path), + "The file should have been created." + ); + Assert.ok(await fileExists(exeFile.path), "Sanity check the exe exists."); + + info("Launch should open the containing folder"); + let promiseShowInFolder = waitForDirectoryShown(); + download.launch(); + Assert.equal(await promiseShowInFolder, noExtFile.path); +}); + +/** + * Waits for an attempt to show the directory where a file is located, and + * returns the path of the file. + */ +function waitForDirectoryShown() { + return new Promise(resolve => { + let waitFn = base => ({ + showContainingDirectory(path) { + Integration.downloads.unregister(waitFn); + resolve(path); + return Promise.resolve(); + }, + }); + Integration.downloads.register(waitFn); + }); +} diff --git a/toolkit/components/downloads/test/unit/test_Downloads.js b/toolkit/components/downloads/test/unit/test_Downloads.js new file mode 100644 index 0000000000..b99f823008 --- /dev/null +++ b/toolkit/components/downloads/test/unit/test_Downloads.js @@ -0,0 +1,153 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests the functions located directly in the "Downloads" object. + */ + +"use strict"; + +// Tests + +/** + * Tests that the createDownload function exists and can be called. More + * detailed tests are implemented separately for the DownloadCore module. + */ +add_task(async function test_createDownload() { + // Creates a simple Download object without starting the download. + await Downloads.createDownload({ + source: { url: "about:blank" }, + target: { path: getTempFile(TEST_TARGET_FILE_NAME).path }, + saver: { type: "copy" }, + }); +}); + +/** + * Tests createDownload for private download. + */ +add_task(async function test_createDownload_private() { + let download = await Downloads.createDownload({ + source: { url: "about:blank", isPrivate: true }, + target: { path: getTempFile(TEST_TARGET_FILE_NAME).path }, + saver: { type: "copy" }, + }); + Assert.ok(download.source.isPrivate); +}); + +/** + * Tests createDownload for normal (public) download. + */ +add_task(async function test_createDownload_public() { + let tempPath = getTempFile(TEST_TARGET_FILE_NAME).path; + let download = await Downloads.createDownload({ + source: { url: "about:blank", isPrivate: false }, + target: { path: tempPath }, + saver: { type: "copy" }, + }); + Assert.ok(!download.source.isPrivate); + + download = await Downloads.createDownload({ + source: { url: "about:blank" }, + target: { path: tempPath }, + saver: { type: "copy" }, + }); + Assert.ok(!download.source.isPrivate); +}); + +/** + * Tests "fetch" with nsIURI and nsIFile as arguments. + */ +add_task(async function test_fetch_uri_file_arguments() { + let targetFile = getTempFile(TEST_TARGET_FILE_NAME); + await Downloads.fetch(NetUtil.newURI(httpUrl("source.txt")), targetFile); + await promiseVerifyContents(targetFile.path, TEST_DATA_SHORT); +}); + +/** + * Tests "fetch" with DownloadSource and DownloadTarget as arguments. + */ +add_task(async function test_fetch_object_arguments() { + let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path; + await Downloads.fetch({ url: httpUrl("source.txt") }, { path: targetPath }); + await promiseVerifyContents(targetPath, TEST_DATA_SHORT); +}); + +/** + * Tests "fetch" with string arguments. + */ +add_task(async function test_fetch_string_arguments() { + let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path; + await Downloads.fetch(httpUrl("source.txt"), targetPath); + await promiseVerifyContents(targetPath, TEST_DATA_SHORT); + + targetPath = getTempFile(TEST_TARGET_FILE_NAME).path; + await Downloads.fetch(httpUrl("source.txt"), targetPath); + await promiseVerifyContents(targetPath, TEST_DATA_SHORT); +}); + +/** + * Tests that the getList function returns the same list when called multiple + * times with the same argument, but returns different lists when called with + * different arguments. More detailed tests are implemented separately for the + * DownloadList module. + */ +add_task(async function test_getList() { + let publicListOne = await Downloads.getList(Downloads.PUBLIC); + let privateListOne = await Downloads.getList(Downloads.PRIVATE); + + let publicListTwo = await Downloads.getList(Downloads.PUBLIC); + let privateListTwo = await Downloads.getList(Downloads.PRIVATE); + + Assert.equal(publicListOne, publicListTwo); + Assert.equal(privateListOne, privateListTwo); + + Assert.notEqual(publicListOne, privateListOne); +}); + +/** + * Tests that the getSummary function returns the same summary when called + * multiple times with the same argument, but returns different summaries when + * called with different arguments. More detailed tests are implemented + * separately for the DownloadSummary object in the DownloadList module. + */ +add_task(async function test_getSummary() { + let publicSummaryOne = await Downloads.getSummary(Downloads.PUBLIC); + let privateSummaryOne = await Downloads.getSummary(Downloads.PRIVATE); + + let publicSummaryTwo = await Downloads.getSummary(Downloads.PUBLIC); + let privateSummaryTwo = await Downloads.getSummary(Downloads.PRIVATE); + + Assert.equal(publicSummaryOne, publicSummaryTwo); + Assert.equal(privateSummaryOne, privateSummaryTwo); + + Assert.notEqual(publicSummaryOne, privateSummaryOne); +}); + +/** + * Tests that the getSystemDownloadsDirectory returns a non-empty download + * directory string. + */ +add_task(async function test_getSystemDownloadsDirectory() { + let downloadDir = await Downloads.getSystemDownloadsDirectory(); + Assert.notEqual(downloadDir, ""); +}); + +/** + * Tests that the getPreferredDownloadsDirectory returns a non-empty download + * directory string. + */ +add_task(async function test_getPreferredDownloadsDirectory() { + let downloadDir = await Downloads.getPreferredDownloadsDirectory(); + Assert.notEqual(downloadDir, ""); +}); + +/** + * Tests that the getTemporaryDownloadsDirectory returns a non-empty download + * directory string. + */ +add_task(async function test_getTemporaryDownloadsDirectory() { + let downloadDir = await Downloads.getTemporaryDownloadsDirectory(); + Assert.notEqual(downloadDir, ""); +}); diff --git a/toolkit/components/downloads/test/unit/xpcshell.ini b/toolkit/components/downloads/test/unit/xpcshell.ini new file mode 100644 index 0000000000..a40f22d0f2 --- /dev/null +++ b/toolkit/components/downloads/test/unit/xpcshell.ini @@ -0,0 +1,23 @@ +[DEFAULT] +head = head.js +skip-if = toolkit == 'android' + +# Note: The "tail.js" file is not defined in the "tail" key because it calls +# the "add_test_task" function, that does not work properly in tail files. +support-files = + common_test_Download.js + +[test_Download_noext_win.js] +run-if = os == "win" # Windows-specific test. +[test_DownloadBlockedTelemetry.js] +[test_DownloadCore.js] +[test_DownloadHistory.js] +[test_DownloadHistory_initialization.js] +[test_DownloadHistory_initialization2.js] +[test_DownloadIntegration.js] +[test_DownloadLegacy.js] +run-sequentially = very high failure rate in parallel +[test_DownloadList.js] +[test_DownloadPaths.js] +[test_Downloads.js] +[test_DownloadStore.js]
\ No newline at end of file |