bug 476539: use a XULRunner-based test harness for unit testing; r=thunder

This commit is contained in:
Myk Melez 2009-03-20 00:14:21 -07:00
parent 75994bc68f
commit e54cec2631
8 changed files with 57 additions and 281 deletions

View File

@ -254,7 +254,7 @@ endif
######################################################################
.PHONY: all build install test-install clean
.PHONY: all build install clean
all: build # default target
@ -266,14 +266,6 @@ install: build
cp $(idl_typelib) $(destdir)/components
cp $(so_target) $(platformdir)/components
# gross hack to get around component registration for xpcshell tests
test-install: install
ln -sf `pwd`/$(destdir)/defaults/preferences/sync.js $(sdkdir)/bin/defaults/pref/sync.js # fixme!!
ln -sf `pwd`/$(destdir)/components/$(idl_typelib) $(sdkdir)/bin/components/$(idl_typelib)
ln -sf `pwd`/$(platformdir)/components/$(so_target) $(sdkdir)/bin/components/$(so_target)
rm -f $(sdkdir)/bin/components/compreg.dat
rm -f $(sdkdir)/bin/components/xpti.dat
clean:
rm -f $(so_target) $(so_target_i386) $(so_target_ppc) \
$(cpp_objects) $(cpp_objects_i386) $(cpp_objects_ppc) \

View File

@ -1,261 +0,0 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Bookmarks Sync.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Dan Mills <thunder@mozilla.com>
* Justin Dolske <dolske@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
const EXPORTED_SYMBOLS = ['Crypto'];
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/constants.js");
Cu.import("resource://weave/util.js");
Cu.import("resource://weave/async.js");
Function.prototype.async = Async.sugar;
Utils.lazy(this, 'Crypto', CryptoSvc);
function CryptoSvc() {
this._init();
}
CryptoSvc.prototype = {
_logName: "Crypto",
get defaultAlgorithm() {
return Utils.prefs.getCharPref("encryption");
},
set defaultAlgorithm(value) {
if (value != Utils.prefs.getCharPref("encryption"))
Utils.prefs.setCharPref("encryption", value);
},
_init: function Crypto__init() {
this._log = Log4Moz.repository.getLogger("Service." + this._logName);
this._log.level =
Log4Moz.Level[Utils.prefs.getCharPref("log.logger.service.crypto")];
let branch = Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefBranch2);
branch.addObserver("extensions.weave.encryption", this, false);
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupports]),
// nsIObserver
observe: function Sync_observe(subject, topic, data) {
switch (topic) {
case "extensions.weave.encryption": {
if (Utils.pref.getCharPref("encryption") == data)
return;
switch (data) {
case "none":
this._log.info("Encryption disabled");
break;
default:
this._log.warn("Unknown encryption algorithm, resetting");
branch.clearUserPref("extensions.weave.encryption");
return; // otherwise we'll send the alg changed event twice
}
// FIXME: listen to this bad boy somewhere
Svc.Observer.notifyObservers(null, "weave:encryption:algorithm-changed", "");
} break;
default:
this._log.warn("Unknown encryption preference changed - ignoring");
}
},
checkModule: function Crypto_checkModule() {
let ok = false;
try {
let svc = Cc["@labs.mozilla.com/Weave/Crypto;1"].
createInstance(Ci.IWeaveCrypto);
let iv = svc.generateRandomIV();
if (iv.length == 24)
ok = true;
} catch (e) {}
return ok;
},
// Crypto
encryptData: function Crypto_encryptData(data, identity) {
let self = yield;
let ret;
this._log.trace("encrypt called. [id=" + identity.realm + "]");
if ("none" == this.defaultAlgorithm) {
this._log.debug("NOT encrypting data");
ret = data;
} else {
let symkey = identity.bulkKey;
let iv = identity.bulkIV;
ret = Svc.Crypto.encrypt(data, symkey, iv);
}
self.done(ret);
},
decryptData: function Crypto_decryptData(data, identity) {
let self = yield;
let ret;
this._log.trace("decrypt called. [id=" + identity.realm + "]");
if ("none" == this.defaultAlgorithm) {
this._log.debug("NOT decrypting data");
ret = data;
} else {
let symkey = identity.bulkKey;
let iv = identity.bulkIV;
ret = Svc.Crypto.decrypt(data, symkey, iv);
}
self.done(ret);
},
/*
* randomKeyGen
*
* Generates a random symmetric key and IV, and puts them in the specified
* identity.
*/
randomKeyGen: function Crypto_randomKeyGen(identity) {
let self = yield;
this._log.trace("randomKeyGen called. [id=" + identity.realm + "]");
let symkey = Svc.Crypto.generateRandomKey();
let iv = Svc.Crypto.generateRandomIV();
identity.bulkKey = symkey;
identity.bulkIV = iv;
},
/*
* RSAkeygen
*
* Generates a new RSA keypair, as well as the salt/IV used for protecting
* the private key, and puts them in the specified identity.
*/
RSAkeygen: function Crypto_RSAkeygen(identity) {
let self = yield;
this._log.trace("RSAkeygen called. [id=" + identity.realm + "]");
let privOut = {};
let pubOut = {};
// Generate a blob of random data for salting the passphrase used to
// encrypt the private key.
let salt = Svc.Crypto.generateRandomBytes(32);
let iv = Svc.Crypto.generateRandomIV();
Svc.Crypto.generateKeypair(identity.password, salt, iv, pubOut, privOut);
identity.keypairAlg = "RSA";
identity.pubkey = pubOut.value;
identity.privkey = privOut.value;
identity.passphraseSalt = salt;
identity.privkeyWrapIV = iv;
},
wrapKey : function Crypto_wrapKey(data, identity) {
let self = yield;
this._log.trace("wrapKey called. [id=" + identity.realm + "]");
let ret = Svc.Crypto.wrapSymmetricKey(data, identity.pubkey);
self.done(ret);
},
unwrapKey: function Crypto_unwrapKey(data, identity) {
let self = yield;
this._log.trace("upwrapKey called. [id=" + identity.realm + "]");
let ret = Svc.Crypto.unwrapSymmetricKey(data, identity.privkey,
identity.password, identity.passphraseSalt, identity.privkeyWrapIV);
self.done(ret);
},
// This function tests to see if the passphrase which encrypts
// the private key for the given identity is valid.
isPassphraseValid: function Crypto_isPassphraseValid(identity) {
var self = yield;
// We do this in a somewhat roundabout way; an alternative is
// to use a hard-coded symmetric key, but even that is still
// roundabout--ideally, the IWeaveCrypto interface should
// expose some sort of functionality to make this easier,
// or perhaps it should just offer this very function. -AV
// Generate a temporary fake identity.
var idTemp = {realm: "Temporary passphrase validation"};
// Generate a random symmetric key.
this.randomKeyGen.async(Crypto, self.cb, idTemp);
yield;
// Encrypt the symmetric key with the user's public key.
this.wrapKey.async(Crypto, self.cb, idTemp.bulkKey, identity);
let wrappedKey = yield;
let unwrappedKey;
// Decrypt the symmetric key with the user's private key.
try {
this.unwrapKey.async(Crypto, self.cb, wrappedKey, identity);
unwrappedKey = yield;
} catch (e) {
self.done(false);
return;
}
// Ensure that the original symmetric key is identical to
// the decrypted version.
if (unwrappedKey != idTemp.bulkKey)
throw new Error("Unwrapped key is not identical to original key.");
self.done(true);
}
};

View File

@ -1 +1,50 @@
# profiledir needs to be an absolute path on Mac OS X (FIXME: file bug).
profiledir = $(abspath ../profile)
sys := $(shell uname -s)
# OS detection
ifeq ($(sys), Darwin)
os = Darwin
else
ifeq ($(sys), Linux)
os = Linux
else
ifeq ($(sys), MINGW32_NT-6.0)
os = WINNT
else
ifeq ($(sys), MINGW32_NT-5.1)
os = WINNT
else
$(error Sorry, your os is unknown/unsupported: $(sys))
endif
endif
endif
endif
ifeq ($(topsrcdir),)
topsrcdir = ../..
endif
ifeq ($(native_topsrcdir),)
ifeq ($(os), WINNT)
native_topsrcdir = ..\..
else
native_topsrcdir = ../..
endif
endif
# The path to the extension, in the native format, as required by the extension
# manager when it installs an extension via a file in the <profile>/extensions/
# directory that contains the path to the extension.
ifeq ($(os), WINNT)
extensiondir = $(subst /,\,$(shell pwd -W))\$(native_topsrcdir)
else
extensiondir = `pwd`/$(topsrcdir)
endif
# A command that installs the extension into the profile when the harness
# creates a new profile.
configure_profile = echo "$(extensiondir)" > $(profiledir)/extensions/\{340c2bbc-ce74-4362-90b5-7c26312808ef\};
include ../harness/Makefile

View File

@ -1,5 +1,3 @@
version(180);
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
@ -31,9 +29,6 @@ let provider = {
};
ds.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider);
do_bind_resource(do_get_file("modules"), "weave");
do_bind_resource(do_get_file("tests"), "tests");
function loadInSandbox(aUri) {
var sandbox = Components.utils.Sandbox(this);
var request = Components.
@ -41,8 +36,9 @@ function loadInSandbox(aUri) {
createInstance();
request.open("GET", aUri, false);
request.overrideMimeType("application/javascript");
request.send(null);
Components.utils.evalInSandbox(request.responseText, sandbox);
Components.utils.evalInSandbox(request.responseText, sandbox, "1.8");
return sandbox;
}

View File

@ -1,5 +1,5 @@
let Httpd = {};
Cu.import("resource://tests/lib/httpd.js", Httpd);
Cu.import("resource://harness/modules/httpd.js", Httpd);
function httpd_setup (handlers) {
let server = new Httpd.nsHttpServer();

View File

@ -9,7 +9,7 @@ Function.prototype.async = Async.sugar;
let logger;
let Httpd = {};
Cu.import("resource://tests/lib/httpd.js", Httpd);
Cu.import("resource://harness/modules/httpd.js", Httpd);
function server_handler(metadata, response) {
let body;

View File

@ -12,8 +12,6 @@ try {
Function.prototype.async = Async.sugar;
let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
let crypto = Cc["@labs.mozilla.com/Weave/Crypto;1"].
getService(Ci.IWeaveCrypto);
let keys, cryptoMeta, cryptoWrap;
function pubkey_handler(metadata, response) {
@ -68,6 +66,8 @@ function async_test() {
keys = PubKeys.createKeypair("my passphrase",
"http://localhost:8080/pubkey",
"http://localhost:8080/privkey");
let crypto = Cc["@labs.mozilla.com/Weave/Crypto;1"].
getService(Ci.IWeaveCrypto);
keys.symkey = crypto.generateRandomKey();
keys.wrappedkey = crypto.wrapSymmetricKey(keys.symkey, keys.pubkey.keyData);

View File

@ -9,7 +9,7 @@ Function.prototype.async = Async.sugar;
let logger;
let Httpd = {};
Cu.import("resource://tests/lib/httpd.js", Httpd);
Cu.import("resource://harness/modules/httpd.js", Httpd);
function server_open(metadata, response) {
let body = "This path exists";