diff options
Diffstat (limited to 'toolkit/components/downloads/test/unit')
10 files changed, 0 insertions, 1154 deletions
diff --git a/toolkit/components/downloads/test/unit/.eslintrc.js b/toolkit/components/downloads/test/unit/.eslintrc.js deleted file mode 100644 index d35787cd2..000000000 --- a/toolkit/components/downloads/test/unit/.eslintrc.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -module.exports = { - "extends": [ - "../../../../../testing/xpcshell/xpcshell.eslintrc.js" - ] -}; diff --git a/toolkit/components/downloads/test/unit/data/block_digest.chunk b/toolkit/components/downloads/test/unit/data/block_digest.chunk deleted file mode 100644 index 34c47c4bb..000000000 --- a/toolkit/components/downloads/test/unit/data/block_digest.chunk +++ /dev/null @@ -1,2 +0,0 @@ -a:5:32:37 -,AJ,AJ8Wbb_e;OτCV
\ No newline at end of file diff --git a/toolkit/components/downloads/test/unit/data/digest.chunk b/toolkit/components/downloads/test/unit/data/digest.chunk deleted file mode 100644 index b1fbb4667..000000000 --- a/toolkit/components/downloads/test/unit/data/digest.chunk +++ /dev/null @@ -1,3 +0,0 @@ -a:5:32:64 -_H^a7]=#nmnoQ -@.R0D7Y4ퟆS$8
\ No newline at end of file diff --git a/toolkit/components/downloads/test/unit/data/signed_win.exe b/toolkit/components/downloads/test/unit/data/signed_win.exe Binary files differdeleted file mode 100644 index de3bb40e8..000000000 --- a/toolkit/components/downloads/test/unit/data/signed_win.exe +++ /dev/null diff --git a/toolkit/components/downloads/test/unit/head_download_manager.js b/toolkit/components/downloads/test/unit/head_download_manager.js deleted file mode 100644 index 1e8248071..000000000 --- a/toolkit/components/downloads/test/unit/head_download_manager.js +++ /dev/null @@ -1,26 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -// This file tests the download manager backend - -var Cc = Components.classes; -var Ci = Components.interfaces; -var Cu = Components.utils; -var Cr = Components.results; - -do_get_profile(); - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); -Cu.import("resource://gre/modules/Services.jsm"); -Cu.import("resource://testing-common/httpd.js"); -XPCOMUtils.defineLazyModuleGetter(this, "Promise", - "resource://gre/modules/Promise.jsm"); - -function createURI(aObj) -{ - var ios = Cc["@mozilla.org/network/io-service;1"]. - getService(Ci.nsIIOService); - return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) : - ios.newURI(aObj, null, null); -} diff --git a/toolkit/components/downloads/test/unit/tail_download_manager.js b/toolkit/components/downloads/test/unit/tail_download_manager.js deleted file mode 100644 index 4043f31b9..000000000 --- a/toolkit/components/downloads/test/unit/tail_download_manager.js +++ /dev/null @@ -1,23 +0,0 @@ -/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ -/* vim: set ts=2 et sw=2 tw=80: */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/** - * Provides infrastructure for automated download components tests. - */ - -"use strict"; - -// Termination functions common to all tests - -add_task(function* test_common_terminate() -{ - // Stop the HTTP server. We must do this inside a task in "tail.js" until the - // xpcshell testing framework supports asynchronous termination functions. - let deferred = Promise.defer(); - gHttpServer.stop(deferred.resolve); - yield deferred.promise; -}); - diff --git a/toolkit/components/downloads/test/unit/test_app_rep.js b/toolkit/components/downloads/test/unit/test_app_rep.js deleted file mode 100644 index 636a71e78..000000000 --- a/toolkit/components/downloads/test/unit/test_app_rep.js +++ /dev/null @@ -1,342 +0,0 @@ -/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ -/* vim: set ts=2 et sw=2 tw=80: */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -Cu.import('resource://gre/modules/NetUtil.jsm'); -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); - -const gAppRep = Cc["@mozilla.org/downloads/application-reputation-service;1"]. - getService(Ci.nsIApplicationReputationService); -var gHttpServ = null; -var gTables = {}; - -var ALLOW_LIST = 0; -var BLOCK_LIST = 1; -var NO_LIST = 2; - -var whitelistedURI = createURI("http://foo:bar@whitelisted.com/index.htm#junk"); -var exampleURI = createURI("http://user:password@example.com/i.html?foo=bar"); -var blocklistedURI = createURI("http://baz:qux@blocklisted.com?xyzzy"); - -const appRepURLPref = "browser.safebrowsing.downloads.remote.url"; - -function readFileToString(aFilename) { - let f = do_get_file(aFilename); - let stream = Cc["@mozilla.org/network/file-input-stream;1"] - .createInstance(Ci.nsIFileInputStream); - stream.init(f, -1, 0, 0); - let buf = NetUtil.readInputStreamToString(stream, stream.available()); - return buf; -} - -// Registers a table for which to serve update chunks. Returns a promise that -// resolves when that chunk has been downloaded. -function registerTableUpdate(aTable, aFilename) { - // If we haven't been given an update for this table yet, add it to the map - if (!(aTable in gTables)) { - gTables[aTable] = []; - } - - // The number of chunks associated with this table. - let numChunks = gTables[aTable].length + 1; - let redirectPath = "/" + aTable + "-" + numChunks; - let redirectUrl = "localhost:4444" + redirectPath; - - // Store redirect url for that table so we can return it later when we - // process an update request. - gTables[aTable].push(redirectUrl); - - gHttpServ.registerPathHandler(redirectPath, function(request, response) { - do_print("Mock safebrowsing server handling request for " + redirectPath); - let contents = readFileToString(aFilename); - do_print("Length of " + aFilename + ": " + contents.length); - response.setHeader("Content-Type", - "application/vnd.google.safebrowsing-update", false); - response.setStatusLine(request.httpVersion, 200, "OK"); - response.bodyOutputStream.write(contents, contents.length); - }); -} - -add_task(function* test_setup() { - // Set up a local HTTP server to return bad verdicts. - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - // Ensure safebrowsing is enabled for this test, even if the app - // doesn't have it enabled. - Services.prefs.setBoolPref("browser.safebrowsing.malware.enabled", true); - Services.prefs.setBoolPref("browser.safebrowsing.downloads.enabled", true); - do_register_cleanup(function() { - Services.prefs.clearUserPref("browser.safebrowsing.malware.enabled"); - Services.prefs.clearUserPref("browser.safebrowsing.downloads.enabled"); - }); - - // Set block and allow tables explicitly, since the allowlist is normally - // disabled on non-Windows platforms. - Services.prefs.setCharPref("urlclassifier.downloadBlockTable", - "goog-badbinurl-shavar"); - Services.prefs.setCharPref("urlclassifier.downloadAllowTable", - "goog-downloadwhite-digest256"); - do_register_cleanup(function() { - Services.prefs.clearUserPref("urlclassifier.downloadBlockTable"); - Services.prefs.clearUserPref("urlclassifier.downloadAllowTable"); - }); - - gHttpServ = new HttpServer(); - gHttpServ.registerDirectory("/", do_get_cwd()); - gHttpServ.registerPathHandler("/download", function(request, response) { - do_throw("This test should never make a remote lookup"); - }); - gHttpServ.start(4444); -}); - -function run_test() { - run_next_test(); -} - -function check_telemetry(aCount, - aShouldBlockCount, - aListCounts) { - let count = Cc["@mozilla.org/base/telemetry;1"] - .getService(Ci.nsITelemetry) - .getHistogramById("APPLICATION_REPUTATION_COUNT") - .snapshot(); - do_check_eq(count.counts[1], aCount); - let local = Cc["@mozilla.org/base/telemetry;1"] - .getService(Ci.nsITelemetry) - .getHistogramById("APPLICATION_REPUTATION_LOCAL") - .snapshot(); - do_check_eq(local.counts[ALLOW_LIST], aListCounts[ALLOW_LIST], - "Allow list counts don't match"); - do_check_eq(local.counts[BLOCK_LIST], aListCounts[BLOCK_LIST], - "Block list counts don't match"); - do_check_eq(local.counts[NO_LIST], aListCounts[NO_LIST], - "No list counts don't match"); - - let shouldBlock = Cc["@mozilla.org/base/telemetry;1"] - .getService(Ci.nsITelemetry) - .getHistogramById("APPLICATION_REPUTATION_SHOULD_BLOCK") - .snapshot(); - // SHOULD_BLOCK = true - do_check_eq(shouldBlock.counts[1], aShouldBlockCount); - // Sanity check that SHOULD_BLOCK total adds up to the COUNT. - do_check_eq(shouldBlock.counts[0] + shouldBlock.counts[1], aCount); -} - -function get_telemetry_counts() { - let count = Cc["@mozilla.org/base/telemetry;1"] - .getService(Ci.nsITelemetry) - .getHistogramById("APPLICATION_REPUTATION_COUNT") - .snapshot(); - let local = Cc["@mozilla.org/base/telemetry;1"] - .getService(Ci.nsITelemetry) - .getHistogramById("APPLICATION_REPUTATION_LOCAL") - .snapshot(); - let shouldBlock = Cc["@mozilla.org/base/telemetry;1"] - .getService(Ci.nsITelemetry) - .getHistogramById("APPLICATION_REPUTATION_SHOULD_BLOCK") - .snapshot(); - return { total: count.counts[1], - shouldBlock: shouldBlock.counts[1], - listCounts: local.counts }; -} - -add_test(function test_nullSourceURI() { - let counts = get_telemetry_counts(); - gAppRep.queryReputation({ - // No source URI - fileSize: 12, - }, function onComplete(aShouldBlock, aStatus) { - do_check_eq(Cr.NS_ERROR_UNEXPECTED, aStatus); - do_check_false(aShouldBlock); - check_telemetry(counts.total + 1, counts.shouldBlock, counts.listCounts); - run_next_test(); - }); -}); - -add_test(function test_nullCallback() { - let counts = get_telemetry_counts(); - try { - gAppRep.queryReputation({ - sourceURI: createURI("http://example.com"), - fileSize: 12, - }, null); - do_throw("Callback cannot be null"); - } catch (ex) { - if (ex.result != Cr.NS_ERROR_INVALID_POINTER) - throw ex; - // We don't even increment the count here, because there's no callback. - check_telemetry(counts.total, counts.shouldBlock, counts.listCounts); - run_next_test(); - } -}); - -// Set up the local whitelist. -add_test(function test_local_list() { - // Construct a response with redirect urls. - function processUpdateRequest() { - let response = "n:1000\n"; - for (let table in gTables) { - response += "i:" + table + "\n"; - for (let i = 0; i < gTables[table].length; ++i) { - response += "u:" + gTables[table][i] + "\n"; - } - } - do_print("Returning update response: " + response); - return response; - } - gHttpServ.registerPathHandler("/downloads", function(request, response) { - let blob = processUpdateRequest(); - response.setHeader("Content-Type", - "application/vnd.google.safebrowsing-update", false); - response.setStatusLine(request.httpVersion, 200, "OK"); - response.bodyOutputStream.write(blob, blob.length); - }); - - let streamUpdater = Cc["@mozilla.org/url-classifier/streamupdater;1"] - .getService(Ci.nsIUrlClassifierStreamUpdater); - - // Load up some update chunks for the safebrowsing server to serve. - // This chunk contains the hash of blocklisted.com/. - registerTableUpdate("goog-badbinurl-shavar", "data/block_digest.chunk"); - // This chunk contains the hash of whitelisted.com/. - registerTableUpdate("goog-downloadwhite-digest256", "data/digest.chunk"); - - // Download some updates, and don't continue until the downloads are done. - function updateSuccess(aEvent) { - // Timeout of n:1000 is constructed in processUpdateRequest above and - // passed back in the callback in nsIUrlClassifierStreamUpdater on success. - do_check_eq("1000", aEvent); - do_print("All data processed"); - run_next_test(); - } - // Just throw if we ever get an update or download error. - function handleError(aEvent) { - do_throw("We didn't download or update correctly: " + aEvent); - } - streamUpdater.downloadUpdates( - "goog-downloadwhite-digest256,goog-badbinurl-shavar", - "goog-downloadwhite-digest256,goog-badbinurl-shavar;\n", - true, // isPostRequest. - "http://localhost:4444/downloads", - updateSuccess, handleError, handleError); -}); - -add_test(function test_unlisted() { - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - let counts = get_telemetry_counts(); - let listCounts = counts.listCounts; - listCounts[NO_LIST]++; - gAppRep.queryReputation({ - sourceURI: exampleURI, - fileSize: 12, - }, function onComplete(aShouldBlock, aStatus) { - do_check_eq(Cr.NS_OK, aStatus); - do_check_false(aShouldBlock); - check_telemetry(counts.total + 1, counts.shouldBlock, listCounts); - run_next_test(); - }); -}); - -add_test(function test_non_uri() { - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - let counts = get_telemetry_counts(); - let listCounts = counts.listCounts; - // No listcount is incremented, since the sourceURI is not an nsIURL - let source = NetUtil.newURI("data:application/octet-stream,ABC"); - do_check_false(source instanceof Ci.nsIURL); - gAppRep.queryReputation({ - sourceURI: source, - fileSize: 12, - }, function onComplete(aShouldBlock, aStatus) { - do_check_eq(Cr.NS_OK, aStatus); - do_check_false(aShouldBlock); - check_telemetry(counts.total + 1, counts.shouldBlock, listCounts); - run_next_test(); - }); -}); - -add_test(function test_local_blacklist() { - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - let counts = get_telemetry_counts(); - let listCounts = counts.listCounts; - listCounts[BLOCK_LIST]++; - gAppRep.queryReputation({ - sourceURI: blocklistedURI, - fileSize: 12, - }, function onComplete(aShouldBlock, aStatus) { - do_check_eq(Cr.NS_OK, aStatus); - do_check_true(aShouldBlock); - check_telemetry(counts.total + 1, counts.shouldBlock + 1, listCounts); - run_next_test(); - }); -}); - -add_test(function test_referer_blacklist() { - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - let counts = get_telemetry_counts(); - let listCounts = counts.listCounts; - listCounts[BLOCK_LIST]++; - gAppRep.queryReputation({ - sourceURI: exampleURI, - referrerURI: blocklistedURI, - fileSize: 12, - }, function onComplete(aShouldBlock, aStatus) { - do_check_eq(Cr.NS_OK, aStatus); - do_check_true(aShouldBlock); - check_telemetry(counts.total + 1, counts.shouldBlock + 1, listCounts); - run_next_test(); - }); -}); - -add_test(function test_blocklist_trumps_allowlist() { - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - let counts = get_telemetry_counts(); - let listCounts = counts.listCounts; - listCounts[BLOCK_LIST]++; - gAppRep.queryReputation({ - sourceURI: whitelistedURI, - referrerURI: blocklistedURI, - fileSize: 12, - }, function onComplete(aShouldBlock, aStatus) { - do_check_eq(Cr.NS_OK, aStatus); - do_check_true(aShouldBlock); - check_telemetry(counts.total + 1, counts.shouldBlock + 1, listCounts); - run_next_test(); - }); -}); - -add_test(function test_redirect_on_blocklist() { - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - let counts = get_telemetry_counts(); - let listCounts = counts.listCounts; - listCounts[BLOCK_LIST]++; - listCounts[ALLOW_LIST]++; - let secman = Services.scriptSecurityManager; - let badRedirects = Cc["@mozilla.org/array;1"] - .createInstance(Ci.nsIMutableArray); - badRedirects.appendElement(secman.createCodebasePrincipal(exampleURI, {}), - false); - badRedirects.appendElement(secman.createCodebasePrincipal(blocklistedURI, {}), - false); - badRedirects.appendElement(secman.createCodebasePrincipal(whitelistedURI, {}), - false); - gAppRep.queryReputation({ - sourceURI: whitelistedURI, - referrerURI: exampleURI, - redirects: badRedirects, - fileSize: 12, - }, function onComplete(aShouldBlock, aStatus) { - do_check_eq(Cr.NS_OK, aStatus); - do_check_true(aShouldBlock); - check_telemetry(counts.total + 1, counts.shouldBlock + 1, listCounts); - run_next_test(); - }); -}); diff --git a/toolkit/components/downloads/test/unit/test_app_rep_maclinux.js b/toolkit/components/downloads/test/unit/test_app_rep_maclinux.js deleted file mode 100644 index 7f94d1520..000000000 --- a/toolkit/components/downloads/test/unit/test_app_rep_maclinux.js +++ /dev/null @@ -1,303 +0,0 @@ -/* -*- 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 file tests signature extraction using Windows Authenticode APIs of - * downloaded files. - */ - -// Globals - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); - -XPCOMUtils.defineLazyModuleGetter(this, "NetUtil", - "resource://gre/modules/NetUtil.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "Promise", - "resource://gre/modules/Promise.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "Task", - "resource://gre/modules/Task.jsm"); - -const gAppRep = Cc["@mozilla.org/downloads/application-reputation-service;1"]. - getService(Ci.nsIApplicationReputationService); -var gStillRunning = true; -var gTables = {}; -var gHttpServer = null; - -const appRepURLPref = "browser.safebrowsing.downloads.remote.url"; -const remoteEnabledPref = "browser.safebrowsing.downloads.remote.enabled"; - -function readFileToString(aFilename) { - let f = do_get_file(aFilename); - let stream = Cc["@mozilla.org/network/file-input-stream;1"] - .createInstance(Ci.nsIFileInputStream); - stream.init(f, -1, 0, 0); - let buf = NetUtil.readInputStreamToString(stream, stream.available()); - return buf; -} - -function registerTableUpdate(aTable, aFilename) { - // If we haven't been given an update for this table yet, add it to the map - if (!(aTable in gTables)) { - gTables[aTable] = []; - } - - // The number of chunks associated with this table. - let numChunks = gTables[aTable].length + 1; - let redirectPath = "/" + aTable + "-" + numChunks; - let redirectUrl = "localhost:4444" + redirectPath; - - // Store redirect url for that table so we can return it later when we - // process an update request. - gTables[aTable].push(redirectUrl); - - gHttpServer.registerPathHandler(redirectPath, function(request, response) { - do_print("Mock safebrowsing server handling request for " + redirectPath); - let contents = readFileToString(aFilename); - do_print("Length of " + aFilename + ": " + contents.length); - response.setHeader("Content-Type", - "application/vnd.google.safebrowsing-update", false); - response.setStatusLine(request.httpVersion, 200, "OK"); - response.bodyOutputStream.write(contents, contents.length); - }); -} - -// Tests - -function run_test() -{ - run_next_test(); -} - -add_task(function test_setup() -{ - // Wait 10 minutes, that is half of the external xpcshell timeout. - do_timeout(10 * 60 * 1000, function() { - if (gStillRunning) { - do_throw("Test timed out."); - } - }); - // Set up a local HTTP server to return bad verdicts. - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - // Ensure safebrowsing is enabled for this test, even if the app - // doesn't have it enabled. - Services.prefs.setBoolPref("browser.safebrowsing.malware.enabled", true); - Services.prefs.setBoolPref("browser.safebrowsing.downloads.enabled", true); - // Set block table explicitly, no need for the allow table though - Services.prefs.setCharPref("urlclassifier.downloadBlockTable", - "goog-badbinurl-shavar"); - // SendRemoteQueryInternal needs locale preference. - let locale = Services.prefs.getCharPref("general.useragent.locale"); - Services.prefs.setCharPref("general.useragent.locale", "en-US"); - - do_register_cleanup(function() { - Services.prefs.clearUserPref("browser.safebrowsing.malware.enabled"); - Services.prefs.clearUserPref("browser.safebrowsing.downloads.enabled"); - Services.prefs.clearUserPref("urlclassifier.downloadBlockTable"); - Services.prefs.setCharPref("general.useragent.locale", locale); - }); - - gHttpServer = new HttpServer(); - gHttpServer.registerDirectory("/", do_get_cwd()); - - function createVerdict(aShouldBlock) { - // We can't programmatically create a protocol buffer here, so just - // hardcode some already serialized ones. - let blob = String.fromCharCode(parseInt(0x08, 16)); - if (aShouldBlock) { - // A safe_browsing::ClientDownloadRequest with a DANGEROUS verdict - blob += String.fromCharCode(parseInt(0x01, 16)); - } else { - // A safe_browsing::ClientDownloadRequest with a SAFE verdict - blob += String.fromCharCode(parseInt(0x00, 16)); - } - return blob; - } - - gHttpServer.registerPathHandler("/throw", function(request, response) { - do_throw("We shouldn't be getting here"); - }); - - gHttpServer.registerPathHandler("/download", function(request, response) { - do_print("Querying remote server for verdict"); - response.setHeader("Content-Type", "application/octet-stream", false); - let buf = NetUtil.readInputStreamToString( - request.bodyInputStream, - request.bodyInputStream.available()); - do_print("Request length: " + buf.length); - // A garbage response. By default this produces NS_CANNOT_CONVERT_DATA as - // the callback status. - let blob = "this is not a serialized protocol buffer (the length doesn't match our hard-coded values)"; - // We can't actually parse the protocol buffer here, so just switch on the - // length instead of inspecting the contents. - if (buf.length == 67) { - // evil.com - blob = createVerdict(true); - } else if (buf.length == 73) { - // mozilla.com - blob = createVerdict(false); - } - response.bodyOutputStream.write(blob, blob.length); - }); - - gHttpServer.start(4444); -}); - -// Construct a response with redirect urls. -function processUpdateRequest() { - let response = "n:1000\n"; - for (let table in gTables) { - response += "i:" + table + "\n"; - for (let i = 0; i < gTables[table].length; ++i) { - response += "u:" + gTables[table][i] + "\n"; - } - } - do_print("Returning update response: " + response); - return response; -} - -// Set up the local whitelist. -function waitForUpdates() { - let deferred = Promise.defer(); - gHttpServer.registerPathHandler("/downloads", function(request, response) { - let blob = processUpdateRequest(); - response.setHeader("Content-Type", - "application/vnd.google.safebrowsing-update", false); - response.setStatusLine(request.httpVersion, 200, "OK"); - response.bodyOutputStream.write(blob, blob.length); - }); - - let streamUpdater = Cc["@mozilla.org/url-classifier/streamupdater;1"] - .getService(Ci.nsIUrlClassifierStreamUpdater); - - // Load up some update chunks for the safebrowsing server to serve. This - // particular chunk contains the hash of whitelisted.com/ and - // sb-ssl.google.com/safebrowsing/csd/certificate/. - registerTableUpdate("goog-downloadwhite-digest256", "data/digest.chunk"); - - // Resolve the promise once processing the updates is complete. - function updateSuccess(aEvent) { - // Timeout of n:1000 is constructed in processUpdateRequest above and - // passed back in the callback in nsIUrlClassifierStreamUpdater on success. - do_check_eq("1000", aEvent); - do_print("All data processed"); - deferred.resolve(true); - } - // Just throw if we ever get an update or download error. - function handleError(aEvent) { - do_throw("We didn't download or update correctly: " + aEvent); - deferred.reject(); - } - streamUpdater.downloadUpdates( - "goog-downloadwhite-digest256", - "goog-downloadwhite-digest256;\n", - true, - "http://localhost:4444/downloads", - updateSuccess, handleError, handleError); - return deferred.promise; -} - -function promiseQueryReputation(query, expectedShouldBlock) { - let deferred = Promise.defer(); - function onComplete(aShouldBlock, aStatus) { - do_check_eq(Cr.NS_OK, aStatus); - do_check_eq(aShouldBlock, expectedShouldBlock); - deferred.resolve(true); - } - gAppRep.queryReputation(query, onComplete); - return deferred.promise; -} - -add_task(function* () -{ - // Wait for Safebrowsing local list updates to complete. - yield waitForUpdates(); -}); - -add_task(function* test_blocked_binary() -{ - // We should reach the remote server for a verdict. - Services.prefs.setBoolPref(remoteEnabledPref, - true); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - // evil.com should return a malware verdict from the remote server. - yield promiseQueryReputation({sourceURI: createURI("http://evil.com"), - suggestedFileName: "noop.bat", - fileSize: 12}, true); -}); - -add_task(function* test_non_binary() -{ - // We should not reach the remote server for a verdict for non-binary files. - Services.prefs.setBoolPref(remoteEnabledPref, - true); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/throw"); - yield promiseQueryReputation({sourceURI: createURI("http://evil.com"), - suggestedFileName: "noop.txt", - fileSize: 12}, false); -}); - -add_task(function* test_good_binary() -{ - // We should reach the remote server for a verdict. - Services.prefs.setBoolPref(remoteEnabledPref, - true); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - // mozilla.com should return a not-guilty verdict from the remote server. - yield promiseQueryReputation({sourceURI: createURI("http://mozilla.com"), - suggestedFileName: "noop.bat", - fileSize: 12}, false); -}); - -add_task(function* test_disabled() -{ - // Explicitly disable remote checks - Services.prefs.setBoolPref(remoteEnabledPref, - false); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/throw"); - let query = {sourceURI: createURI("http://example.com"), - suggestedFileName: "noop.bat", - fileSize: 12}; - let deferred = Promise.defer(); - gAppRep.queryReputation(query, - function onComplete(aShouldBlock, aStatus) { - // We should be getting NS_ERROR_NOT_AVAILABLE if the service is disabled - do_check_eq(Cr.NS_ERROR_NOT_AVAILABLE, aStatus); - do_check_false(aShouldBlock); - deferred.resolve(true); - } - ); - yield deferred.promise; -}); - -add_task(function* test_disabled_through_lists() -{ - Services.prefs.setBoolPref(remoteEnabledPref, - false); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - Services.prefs.setCharPref("urlclassifier.downloadBlockTable", ""); - let query = {sourceURI: createURI("http://example.com"), - suggestedFileName: "noop.bat", - fileSize: 12}; - let deferred = Promise.defer(); - gAppRep.queryReputation(query, - function onComplete(aShouldBlock, aStatus) { - // We should be getting NS_ERROR_NOT_AVAILABLE if the service is disabled - do_check_eq(Cr.NS_ERROR_NOT_AVAILABLE, aStatus); - do_check_false(aShouldBlock); - deferred.resolve(true); - } - ); - yield deferred.promise; -}); -add_task(function* test_teardown() -{ - gStillRunning = false; -}); diff --git a/toolkit/components/downloads/test/unit/test_app_rep_windows.js b/toolkit/components/downloads/test/unit/test_app_rep_windows.js deleted file mode 100644 index 4ff772e61..000000000 --- a/toolkit/components/downloads/test/unit/test_app_rep_windows.js +++ /dev/null @@ -1,434 +0,0 @@ -/* -*- 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 file tests signature extraction using Windows Authenticode APIs of - * downloaded files. - */ - -// Globals - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); - -XPCOMUtils.defineLazyModuleGetter(this, "FileUtils", - "resource://gre/modules/FileUtils.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "NetUtil", - "resource://gre/modules/NetUtil.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "Promise", - "resource://gre/modules/Promise.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "Task", - "resource://gre/modules/Task.jsm"); - -const BackgroundFileSaverOutputStream = Components.Constructor( - "@mozilla.org/network/background-file-saver;1?mode=outputstream", - "nsIBackgroundFileSaver"); - -const StringInputStream = Components.Constructor( - "@mozilla.org/io/string-input-stream;1", - "nsIStringInputStream", - "setData"); - -const TEST_FILE_NAME_1 = "test-backgroundfilesaver-1.txt"; - -const gAppRep = Cc["@mozilla.org/downloads/application-reputation-service;1"]. - getService(Ci.nsIApplicationReputationService); -var gStillRunning = true; -var gTables = {}; -var gHttpServer = null; - -const appRepURLPref = "browser.safebrowsing.downloads.remote.url"; -const remoteEnabledPref = "browser.safebrowsing.downloads.remote.enabled"; - -/** - * Returns a reference to a temporary file. If the file is then created, it - * will be removed when tests in this file finish. - */ -function getTempFile(aLeafName) { - let file = FileUtils.getFile("TmpD", [aLeafName]); - do_register_cleanup(function GTF_cleanup() { - if (file.exists()) { - file.remove(false); - } - }); - return file; -} - -function readFileToString(aFilename) { - let f = do_get_file(aFilename); - let stream = Cc["@mozilla.org/network/file-input-stream;1"] - .createInstance(Ci.nsIFileInputStream); - stream.init(f, -1, 0, 0); - let buf = NetUtil.readInputStreamToString(stream, stream.available()); - return buf; -} - -/** - * Waits for the given saver object to complete. - * - * @param aSaver - * The saver, with the output stream or a stream listener implementation. - * @param aOnTargetChangeFn - * Optional callback invoked with the target file name when it changes. - * - * @return {Promise} - * @resolves When onSaveComplete is called with a success code. - * @rejects With an exception, if onSaveComplete is called with a failure code. - */ -function promiseSaverComplete(aSaver, aOnTargetChangeFn) { - let deferred = Promise.defer(); - aSaver.observer = { - onTargetChange: function BFSO_onSaveComplete(unused, aTarget) - { - if (aOnTargetChangeFn) { - aOnTargetChangeFn(aTarget); - } - }, - onSaveComplete: function BFSO_onSaveComplete(unused, aStatus) - { - if (Components.isSuccessCode(aStatus)) { - deferred.resolve(); - } else { - deferred.reject(new Components.Exception("Saver failed.", aStatus)); - } - }, - }; - return deferred.promise; -} - -/** - * Feeds a string to a BackgroundFileSaverOutputStream. - * - * @param aSourceString - * The source data to copy. - * @param aSaverOutputStream - * The BackgroundFileSaverOutputStream to feed. - * @param aCloseWhenDone - * If true, the output stream will be closed when the copy finishes. - * - * @return {Promise} - * @resolves When the copy completes with a success code. - * @rejects With an exception, if the copy fails. - */ -function promiseCopyToSaver(aSourceString, aSaverOutputStream, aCloseWhenDone) { - let deferred = Promise.defer(); - let inputStream = new StringInputStream(aSourceString, aSourceString.length); - let copier = Cc["@mozilla.org/network/async-stream-copier;1"] - .createInstance(Ci.nsIAsyncStreamCopier); - copier.init(inputStream, aSaverOutputStream, null, false, true, 0x8000, true, - aCloseWhenDone); - copier.asyncCopy({ - onStartRequest: function () { }, - onStopRequest: function (aRequest, aContext, aStatusCode) - { - if (Components.isSuccessCode(aStatusCode)) { - deferred.resolve(); - } else { - deferred.reject(new Components.Exception(aResult)); - } - }, - }, null); - return deferred.promise; -} - -// Registers a table for which to serve update chunks. -function registerTableUpdate(aTable, aFilename) { - // If we haven't been given an update for this table yet, add it to the map - if (!(aTable in gTables)) { - gTables[aTable] = []; - } - - // The number of chunks associated with this table. - let numChunks = gTables[aTable].length + 1; - let redirectPath = "/" + aTable + "-" + numChunks; - let redirectUrl = "localhost:4444" + redirectPath; - - // Store redirect url for that table so we can return it later when we - // process an update request. - gTables[aTable].push(redirectUrl); - - gHttpServer.registerPathHandler(redirectPath, function(request, response) { - do_print("Mock safebrowsing server handling request for " + redirectPath); - let contents = readFileToString(aFilename); - do_print("Length of " + aFilename + ": " + contents.length); - response.setHeader("Content-Type", - "application/vnd.google.safebrowsing-update", false); - response.setStatusLine(request.httpVersion, 200, "OK"); - response.bodyOutputStream.write(contents, contents.length); - }); -} - -// Tests - -function run_test() -{ - run_next_test(); -} - -add_task(function* test_setup() -{ - // Wait 10 minutes, that is half of the external xpcshell timeout. - do_timeout(10 * 60 * 1000, function() { - if (gStillRunning) { - do_throw("Test timed out."); - } - }); - // Set up a local HTTP server to return bad verdicts. - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - // Ensure safebrowsing is enabled for this test, even if the app - // doesn't have it enabled. - Services.prefs.setBoolPref("browser.safebrowsing.malware.enabled", true); - Services.prefs.setBoolPref("browser.safebrowsing.downloads.enabled", true); - // Set block and allow tables explicitly, since the allowlist is normally - // disabled on comm-central. - Services.prefs.setCharPref("urlclassifier.downloadBlockTable", - "goog-badbinurl-shavar"); - Services.prefs.setCharPref("urlclassifier.downloadAllowTable", - "goog-downloadwhite-digest256"); - // SendRemoteQueryInternal needs locale preference. - let locale = Services.prefs.getCharPref("general.useragent.locale"); - Services.prefs.setCharPref("general.useragent.locale", "en-US"); - - do_register_cleanup(function() { - Services.prefs.clearUserPref("browser.safebrowsing.malware.enabled"); - Services.prefs.clearUserPref("browser.safebrowsing.downloads.enabled"); - Services.prefs.clearUserPref("urlclassifier.downloadBlockTable"); - Services.prefs.clearUserPref("urlclassifier.downloadAllowTable"); - Services.prefs.setCharPref("general.useragent.locale", locale); - }); - - gHttpServer = new HttpServer(); - gHttpServer.registerDirectory("/", do_get_cwd()); - - function createVerdict(aShouldBlock) { - // We can't programmatically create a protocol buffer here, so just - // hardcode some already serialized ones. - let blob = String.fromCharCode(parseInt(0x08, 16)); - if (aShouldBlock) { - // A safe_browsing::ClientDownloadRequest with a DANGEROUS verdict - blob += String.fromCharCode(parseInt(0x01, 16)); - } else { - // A safe_browsing::ClientDownloadRequest with a SAFE verdict - blob += String.fromCharCode(parseInt(0x00, 16)); - } - return blob; - } - - gHttpServer.registerPathHandler("/throw", function(request, response) { - do_throw("We shouldn't be getting here"); - }); - - gHttpServer.registerPathHandler("/download", function(request, response) { - do_print("Querying remote server for verdict"); - response.setHeader("Content-Type", "application/octet-stream", false); - let buf = NetUtil.readInputStreamToString( - request.bodyInputStream, - request.bodyInputStream.available()); - do_print("Request length: " + buf.length); - // A garbage response. By default this produces NS_CANNOT_CONVERT_DATA as - // the callback status. - let blob = "this is not a serialized protocol buffer (the length doesn't match our hard-coded values)"; - // We can't actually parse the protocol buffer here, so just switch on the - // length instead of inspecting the contents. - if (buf.length == 67) { - // evil.com - blob = createVerdict(true); - } else if (buf.length == 73) { - // mozilla.com - blob = createVerdict(false); - } - response.bodyOutputStream.write(blob, blob.length); - }); - - gHttpServer.start(4444); -}); - -// Construct a response with redirect urls. -function processUpdateRequest() { - let response = "n:1000\n"; - for (let table in gTables) { - response += "i:" + table + "\n"; - for (let i = 0; i < gTables[table].length; ++i) { - response += "u:" + gTables[table][i] + "\n"; - } - } - do_print("Returning update response: " + response); - return response; -} - -// Set up the local whitelist. -function waitForUpdates() { - let deferred = Promise.defer(); - gHttpServer.registerPathHandler("/downloads", function(request, response) { - let blob = processUpdateRequest(); - response.setHeader("Content-Type", - "application/vnd.google.safebrowsing-update", false); - response.setStatusLine(request.httpVersion, 200, "OK"); - response.bodyOutputStream.write(blob, blob.length); - }); - - let streamUpdater = Cc["@mozilla.org/url-classifier/streamupdater;1"] - .getService(Ci.nsIUrlClassifierStreamUpdater); - - // Load up some update chunks for the safebrowsing server to serve. This - // particular chunk contains the hash of whitelisted.com/ and - // sb-ssl.google.com/safebrowsing/csd/certificate/. - registerTableUpdate("goog-downloadwhite-digest256", "data/digest.chunk"); - - // Resolve the promise once processing the updates is complete. - function updateSuccess(aEvent) { - // Timeout of n:1000 is constructed in processUpdateRequest above and - // passed back in the callback in nsIUrlClassifierStreamUpdater on success. - do_check_eq("1000", aEvent); - do_print("All data processed"); - deferred.resolve(true); - } - // Just throw if we ever get an update or download error. - function handleError(aEvent) { - do_throw("We didn't download or update correctly: " + aEvent); - deferred.reject(); - } - streamUpdater.downloadUpdates( - "goog-downloadwhite-digest256", - "goog-downloadwhite-digest256;\n", - true, - "http://localhost:4444/downloads", - updateSuccess, handleError, handleError); - return deferred.promise; -} - -function promiseQueryReputation(query, expectedShouldBlock) { - let deferred = Promise.defer(); - function onComplete(aShouldBlock, aStatus) { - do_check_eq(Cr.NS_OK, aStatus); - do_check_eq(aShouldBlock, expectedShouldBlock); - deferred.resolve(true); - } - gAppRep.queryReputation(query, onComplete); - return deferred.promise; -} - -add_task(function* () -{ - // Wait for Safebrowsing local list updates to complete. - yield waitForUpdates(); -}); - -add_task(function* test_signature_whitelists() -{ - // We should never get to the remote server. - Services.prefs.setBoolPref(remoteEnabledPref, - true); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/throw"); - - // Use BackgroundFileSaver to extract the signature on Windows. - let destFile = getTempFile(TEST_FILE_NAME_1); - - let data = readFileToString("data/signed_win.exe"); - let saver = new BackgroundFileSaverOutputStream(); - let completionPromise = promiseSaverComplete(saver); - saver.enableSignatureInfo(); - saver.setTarget(destFile, false); - yield promiseCopyToSaver(data, saver, true); - - saver.finish(Cr.NS_OK); - yield completionPromise; - - // Clean up. - destFile.remove(false); - - // evil.com is not on the allowlist, but this binary is signed by an entity - // whose certificate information is on the allowlist. - yield promiseQueryReputation({sourceURI: createURI("http://evil.com"), - signatureInfo: saver.signatureInfo, - fileSize: 12}, false); -}); - -add_task(function* test_blocked_binary() -{ - // We should reach the remote server for a verdict. - Services.prefs.setBoolPref(remoteEnabledPref, - true); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - // evil.com should return a malware verdict from the remote server. - yield promiseQueryReputation({sourceURI: createURI("http://evil.com"), - suggestedFileName: "noop.bat", - fileSize: 12}, true); -}); - -add_task(function* test_non_binary() -{ - // We should not reach the remote server for a verdict for non-binary files. - Services.prefs.setBoolPref(remoteEnabledPref, - true); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/throw"); - yield promiseQueryReputation({sourceURI: createURI("http://evil.com"), - suggestedFileName: "noop.txt", - fileSize: 12}, false); -}); - -add_task(function* test_good_binary() -{ - // We should reach the remote server for a verdict. - Services.prefs.setBoolPref(remoteEnabledPref, - true); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - // mozilla.com should return a not-guilty verdict from the remote server. - yield promiseQueryReputation({sourceURI: createURI("http://mozilla.com"), - suggestedFileName: "noop.bat", - fileSize: 12}, false); -}); - -add_task(function* test_disabled() -{ - // Explicitly disable remote checks - Services.prefs.setBoolPref(remoteEnabledPref, - false); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/throw"); - let query = {sourceURI: createURI("http://example.com"), - suggestedFileName: "noop.bat", - fileSize: 12}; - let deferred = Promise.defer(); - gAppRep.queryReputation(query, - function onComplete(aShouldBlock, aStatus) { - // We should be getting NS_ERROR_NOT_AVAILABLE if the service is disabled - do_check_eq(Cr.NS_ERROR_NOT_AVAILABLE, aStatus); - do_check_false(aShouldBlock); - deferred.resolve(true); - } - ); - yield deferred.promise; -}); - -add_task(function* test_disabled_through_lists() -{ - Services.prefs.setBoolPref(remoteEnabledPref, - false); - Services.prefs.setCharPref(appRepURLPref, - "http://localhost:4444/download"); - Services.prefs.setCharPref("urlclassifier.downloadBlockTable", ""); - let query = {sourceURI: createURI("http://example.com"), - suggestedFileName: "noop.bat", - fileSize: 12}; - let deferred = Promise.defer(); - gAppRep.queryReputation(query, - function onComplete(aShouldBlock, aStatus) { - // We should be getting NS_ERROR_NOT_AVAILABLE if the service is disabled - do_check_eq(Cr.NS_ERROR_NOT_AVAILABLE, aStatus); - do_check_false(aShouldBlock); - deferred.resolve(true); - } - ); - yield deferred.promise; -}); -add_task(function* test_teardown() -{ - gStillRunning = false; -}); diff --git a/toolkit/components/downloads/test/unit/xpcshell.ini b/toolkit/components/downloads/test/unit/xpcshell.ini deleted file mode 100644 index 68b6e1fc3..000000000 --- a/toolkit/components/downloads/test/unit/xpcshell.ini +++ /dev/null @@ -1,14 +0,0 @@ -[DEFAULT] -head = head_download_manager.js -tail = tail_download_manager.js -skip-if = toolkit == 'android' -support-files = - data/digest.chunk - data/block_digest.chunk - data/signed_win.exe - -[test_app_rep.js] -[test_app_rep_windows.js] -skip-if = os != "win" -[test_app_rep_maclinux.js] -skip-if = os == "win" |