mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-11-11 13:48:23 +02:00
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8
This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:
ChromeUtils.import("resource://gre/modules/Services.jsm");
is approximately the same as the following, in the new model:
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs
This was done using the followng script:
https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8
Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16750
--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
200 lines
6.3 KiB
JavaScript
200 lines
6.3 KiB
JavaScript
/* -*- 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
|
|
|
|
ChromeUtils.defineModuleGetter(this, "FileUtils",
|
|
"resource://gre/modules/FileUtils.jsm");
|
|
ChromeUtils.defineModuleGetter(this, "NetUtil",
|
|
"resource://gre/modules/NetUtil.jsm");
|
|
ChromeUtils.defineModuleGetter(this, "FileTestUtils",
|
|
"resource://testing-common/FileTestUtils.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";
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
return new Promise((resolve, reject) => {
|
|
aSaver.observer = {
|
|
onTargetChange: function BFSO_onSaveComplete(aSaver, aTarget)
|
|
{
|
|
if (aOnTargetChangeFn) {
|
|
aOnTargetChangeFn(aTarget);
|
|
}
|
|
},
|
|
onSaveComplete: function BFSO_onSaveComplete(aSaver, aStatus)
|
|
{
|
|
if (Components.isSuccessCode(aStatus)) {
|
|
resolve();
|
|
} else {
|
|
reject(new Components.Exception("Saver failed.", aStatus));
|
|
}
|
|
},
|
|
};
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
return new Promise((resolve, reject) => {
|
|
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)) {
|
|
resolve();
|
|
} else {
|
|
reject(new Components.Exception(aResult));
|
|
}
|
|
},
|
|
}, null);
|
|
});
|
|
}
|
|
|
|
var gStillRunning = true;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
//// 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.");
|
|
}
|
|
})
|
|
});
|
|
|
|
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;
|
|
}
|
|
|
|
add_task(async function test_signature()
|
|
{
|
|
// Check that we get a signature if the saver is finished on Windows.
|
|
let destFile = getTempFile(TEST_FILE_NAME_1);
|
|
|
|
let data = readFileToString("data/signed_win.exe");
|
|
let saver = new BackgroundFileSaverOutputStream();
|
|
let completionPromise = promiseSaverComplete(saver);
|
|
|
|
try {
|
|
let signatureInfo = saver.signatureInfo;
|
|
do_throw("Can't get signature before saver is complete.");
|
|
} catch (ex) {
|
|
if (ex.result != Cr.NS_ERROR_NOT_AVAILABLE) {
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
saver.enableSignatureInfo();
|
|
saver.setTarget(destFile, false);
|
|
await promiseCopyToSaver(data, saver, true);
|
|
|
|
saver.finish(Cr.NS_OK);
|
|
await completionPromise;
|
|
|
|
// There's only one nsIX509CertList in the signature array.
|
|
Assert.equal(1, saver.signatureInfo.length);
|
|
let certLists = saver.signatureInfo.enumerate();
|
|
Assert.ok(certLists.hasMoreElements());
|
|
let certList = certLists.getNext().QueryInterface(Ci.nsIX509CertList);
|
|
Assert.ok(!certLists.hasMoreElements());
|
|
|
|
// Check that it has 3 certs.
|
|
let certs = certList.getEnumerator();
|
|
Assert.ok(certs.hasMoreElements());
|
|
let signer = certs.getNext().QueryInterface(Ci.nsIX509Cert);
|
|
Assert.ok(certs.hasMoreElements());
|
|
let issuer = certs.getNext().QueryInterface(Ci.nsIX509Cert);
|
|
Assert.ok(certs.hasMoreElements());
|
|
let root = certs.getNext().QueryInterface(Ci.nsIX509Cert);
|
|
Assert.ok(!certs.hasMoreElements());
|
|
|
|
// Check that the certs have expected strings attached.
|
|
let organization = "Microsoft Corporation";
|
|
Assert.equal("Microsoft Corporation", signer.commonName);
|
|
Assert.equal(organization, signer.organization);
|
|
Assert.equal("Copyright (c) 2002 Microsoft Corp.", signer.organizationalUnit);
|
|
|
|
Assert.equal("Microsoft Code Signing PCA", issuer.commonName);
|
|
Assert.equal(organization, issuer.organization);
|
|
Assert.equal("Copyright (c) 2000 Microsoft Corp.", issuer.organizationalUnit);
|
|
|
|
Assert.equal("Microsoft Root Authority", root.commonName);
|
|
Assert.ok(!root.organization);
|
|
Assert.equal("Copyright (c) 1997 Microsoft Corp.", root.organizationalUnit);
|
|
|
|
// Clean up.
|
|
destFile.remove(false);
|
|
});
|
|
|
|
add_task(function test_teardown()
|
|
{
|
|
gStillRunning = false;
|
|
});
|