From 9d3f32839349f26fa17a5984f52c5dee76785299 Mon Sep 17 00:00:00 2001 From: Philipp von Weitershausen Date: Thu, 14 Oct 2010 21:17:55 +0200 Subject: [PATCH] Bug 562431 - Move tests for WeaveCrypto to services/crypto/ [r=mconnor] --HG-- rename : services/sync/tests/unit/test_crypto_crypt.js => services/crypto/tests/unit/test_crypto_crypt.js rename : services/sync/tests/unit/test_crypto_keypair.js => services/crypto/tests/unit/test_crypto_keypair.js rename : services/sync/tests/unit/test_crypto_random.js => services/crypto/tests/unit/test_crypto_random.js rename : services/sync/tests/unit/test_crypto_rewrap.js => services/crypto/tests/unit/test_crypto_rewrap.js rename : services/sync/tests/unit/test_crypto_verify.js => services/crypto/tests/unit/test_crypto_verify.js --- services/crypto/tests/unit/head_helpers.js | 66 +++++++++++++++++++ .../tests/unit/test_crypto_crypt.js | 12 +++- .../tests/unit/test_crypto_keypair.js | 12 +++- .../tests/unit/test_crypto_random.js | 12 +++- .../tests/unit/test_crypto_rewrap.js | 12 +++- .../tests/unit/test_crypto_verify.js | 12 +++- 6 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 services/crypto/tests/unit/head_helpers.js rename services/{sync => crypto}/tests/unit/test_crypto_crypt.js (94%) rename services/{sync => crypto}/tests/unit/test_crypto_keypair.js (87%) rename services/{sync => crypto}/tests/unit/test_crypto_random.js (88%) rename services/{sync => crypto}/tests/unit/test_crypto_rewrap.js (81%) rename services/{sync => crypto}/tests/unit/test_crypto_verify.js (72%) diff --git a/services/crypto/tests/unit/head_helpers.js b/services/crypto/tests/unit/head_helpers.js new file mode 100644 index 000000000000..611554892004 --- /dev/null +++ b/services/crypto/tests/unit/head_helpers.js @@ -0,0 +1,66 @@ +const Cc = Components.classes; +const Ci = Components.interfaces; +const Cr = Components.results; +const Cu = Components.utils; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +try { + // In the context of xpcshell tests, there won't be a default AppInfo + Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo); +} +catch(ex) { + +// Make sure to provide the right OS so crypto loads the right binaries +let OS = "XPCShell"; +if ("@mozilla.org/windows-registry-key;1" in Cc) + OS = "WINNT"; +else if ("nsILocalFileMac" in Ci) + OS = "Darwin"; +else + OS = "Linux"; + +let XULAppInfo = { + vendor: "Mozilla", + name: "XPCShell", + ID: "{3e3ba16c-1675-4e88-b9c8-afef81b3d2ef}", + version: "1", + appBuildID: "20100621", + platformVersion: "", + platformBuildID: "20100621", + inSafeMode: false, + logConsoleErrors: true, + OS: OS, + XPCOMABI: "noarch-spidermonkey", + QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo, Ci.nsIXULRuntime]) +}; + +let XULAppInfoFactory = { + createInstance: function (outer, iid) { + if (outer != null) + throw Cr.NS_ERROR_NO_AGGREGATION; + return XULAppInfo.QueryInterface(iid); + } +}; + +let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); +registrar.registerFactory(Components.ID("{fbfae60b-64a4-44ef-a911-08ceb70b9f31}"), + "XULAppInfo", "@mozilla.org/xre/app-info;1", + XULAppInfoFactory); + +} + +// Provide resource://services-crypto if it isn't already available +let weaveService = Cc["@mozilla.org/weave/service;1"].getService(); +weaveService.wrappedJSObject.addResourceAlias(); + +/** + * Print some debug message to the console. All arguments will be printed, + * separated by spaces. + * + * @param [arg0, arg1, arg2, ...] + * Any number of arguments to print out + * @usage _("Hello World") -> prints "Hello World" + * @usage _(1, 2, 3) -> prints "1 2 3" + */ +let _ = function(some, debug, text, to) print(Array.slice(arguments).join(" ")); diff --git a/services/sync/tests/unit/test_crypto_crypt.js b/services/crypto/tests/unit/test_crypto_crypt.js similarity index 94% rename from services/sync/tests/unit/test_crypto_crypt.js rename to services/crypto/tests/unit/test_crypto_crypt.js index a42eb3aeccbf..dc53002dab7d 100644 --- a/services/sync/tests/unit/test_crypto_crypt.js +++ b/services/crypto/tests/unit/test_crypto_crypt.js @@ -1,8 +1,14 @@ -Cu.import("resource://services-sync/util.js"); +let cryptoSvc; +try { + Components.utils.import("resource://services-crypto/WeaveCrypto.js"); + cryptoSvc = new WeaveCrypto(); +} catch (ex) { + // Fallback to binary WeaveCrypto + cryptoSvc = Cc["@labs.mozilla.com/Weave/Crypto;1"] + .getService(Ci.IWeaveCrypto); +} function run_test() { - let cryptoSvc = Svc.Crypto; - // First, do a normal run with expected usage... Generate a random key and // iv, encrypt and decrypt a string. var iv = cryptoSvc.generateRandomIV(); diff --git a/services/sync/tests/unit/test_crypto_keypair.js b/services/crypto/tests/unit/test_crypto_keypair.js similarity index 87% rename from services/sync/tests/unit/test_crypto_keypair.js rename to services/crypto/tests/unit/test_crypto_keypair.js index 024e297a9d68..8ea7907f6370 100644 --- a/services/sync/tests/unit/test_crypto_keypair.js +++ b/services/crypto/tests/unit/test_crypto_keypair.js @@ -1,8 +1,14 @@ -Cu.import("resource://services-sync/util.js"); +let cryptoSvc; +try { + Components.utils.import("resource://services-crypto/WeaveCrypto.js"); + cryptoSvc = new WeaveCrypto(); +} catch (ex) { + // Fallback to binary WeaveCrypto + cryptoSvc = Cc["@labs.mozilla.com/Weave/Crypto;1"] + .getService(Ci.IWeaveCrypto); +} function run_test() { - let cryptoSvc = Svc.Crypto; - var salt = cryptoSvc.generateRandomBytes(16); do_check_eq(salt.length, 24); diff --git a/services/sync/tests/unit/test_crypto_random.js b/services/crypto/tests/unit/test_crypto_random.js similarity index 88% rename from services/sync/tests/unit/test_crypto_random.js rename to services/crypto/tests/unit/test_crypto_random.js index 4eeff02b7f81..ab85169be6b9 100644 --- a/services/sync/tests/unit/test_crypto_random.js +++ b/services/crypto/tests/unit/test_crypto_random.js @@ -1,8 +1,14 @@ -Cu.import("resource://services-sync/util.js"); +let cryptoSvc; +try { + Components.utils.import("resource://services-crypto/WeaveCrypto.js"); + cryptoSvc = new WeaveCrypto(); +} catch (ex) { + // Fallback to binary WeaveCrypto + cryptoSvc = Cc["@labs.mozilla.com/Weave/Crypto;1"] + .getService(Ci.IWeaveCrypto); +} function run_test() { - let cryptoSvc = Svc.Crypto; - // Test salt generation. var salt; diff --git a/services/sync/tests/unit/test_crypto_rewrap.js b/services/crypto/tests/unit/test_crypto_rewrap.js similarity index 81% rename from services/sync/tests/unit/test_crypto_rewrap.js rename to services/crypto/tests/unit/test_crypto_rewrap.js index bc2aa01d2192..d9568d6e66aa 100644 --- a/services/sync/tests/unit/test_crypto_rewrap.js +++ b/services/crypto/tests/unit/test_crypto_rewrap.js @@ -1,8 +1,14 @@ -Cu.import("resource://services-sync/util.js"); +let cryptoSvc; +try { + Components.utils.import("resource://services-crypto/WeaveCrypto.js"); + cryptoSvc = new WeaveCrypto(); +} catch (ex) { + // Fallback to binary WeaveCrypto + cryptoSvc = Cc["@labs.mozilla.com/Weave/Crypto;1"] + .getService(Ci.IWeaveCrypto); +} function run_test() { - let cryptoSvc = Svc.Crypto; - var salt = cryptoSvc.generateRandomBytes(16); var iv = cryptoSvc.generateRandomIV(); var symKey = cryptoSvc.generateRandomKey(); diff --git a/services/sync/tests/unit/test_crypto_verify.js b/services/crypto/tests/unit/test_crypto_verify.js similarity index 72% rename from services/sync/tests/unit/test_crypto_verify.js rename to services/crypto/tests/unit/test_crypto_verify.js index eefbd3a23ef0..5475503e69da 100644 --- a/services/sync/tests/unit/test_crypto_verify.js +++ b/services/crypto/tests/unit/test_crypto_verify.js @@ -1,8 +1,14 @@ -Cu.import("resource://services-sync/util.js"); +let cryptoSvc; +try { + Components.utils.import("resource://services-crypto/WeaveCrypto.js"); + cryptoSvc = new WeaveCrypto(); +} catch (ex) { + // Fallback to binary WeaveCrypto + cryptoSvc = Cc["@labs.mozilla.com/Weave/Crypto;1"] + .getService(Ci.IWeaveCrypto); +} function run_test() { - let cryptoSvc = Svc.Crypto; - var salt = cryptoSvc.generateRandomBytes(16); var iv = cryptoSvc.generateRandomIV();