Bug 792546 - Part 2: Move fake services into testing-only JS module; r=rnewman
authorGregory Szorc <gps@mozilla.com>
Mon, 24 Sep 2012 13:42:54 -0700
changeset 111093 da562c88998fd83cd62eebecbfdd900d159db25e
parent 111092 234c791284378113405cf1dbb0744fe19e1b7f75
child 111094 6fc02deb63dad3f73af99b488db40bf2dccb5880
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersrnewman
bugs792546
milestone18.0a1
Bug 792546 - Part 2: Move fake services into testing-only JS module; r=rnewman We still import these functions in head.js. This will be cleaned up later once functionality from head.js that uses them is moved into a testing-only JS module.
services/sync/Makefile.in
services/sync/modules-testing/fakeservices.js
services/sync/tests/unit/head_helpers.js
services/sync/tests/unit/test_load_modules.js
--- a/services/sync/Makefile.in
+++ b/services/sync/Makefile.in
@@ -58,16 +58,17 @@ sync_engine_modules := \
   $(NULL)
 
 sync_stage_modules := \
   cluster.js \
   enginesync.js \
   $(NULL)
 
 sync_testing_modules := \
+  fakeservices.js \
   rotaryengine.js \
   $(NULL)
 
 DIRS += locales
 TEST_DIRS += tests
 
 EXTRA_COMPONENTS := \
   SyncComponents.manifest \
new file mode 100644
--- /dev/null
+++ b/services/sync/modules-testing/fakeservices.js
@@ -0,0 +1,103 @@
+/* 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/. */
+
+"use strict";
+
+const EXPORTED_SYMBOLS = [
+  "FakeCryptoService",
+  "FakeFilesystemService",
+  "FakeGUIDService",
+  "fakeSHA256HMAC",
+];
+
+const {utils: Cu} = Components;
+
+Cu.import("resource://services-sync/record.js");
+Cu.import("resource://services-sync/util.js");
+
+let btoa = Cu.import("resource://services-common/log4moz.js").btoa;
+
+function FakeFilesystemService(contents) {
+  this.fakeContents = contents;
+  let self = this;
+
+  Utils.jsonSave = function jsonSave(filePath, that, obj, callback) {
+    let json = typeof obj == "function" ? obj.call(that) : obj;
+    self.fakeContents["weave/" + filePath + ".json"] = JSON.stringify(json);
+    callback.call(that);
+  };
+
+  Utils.jsonLoad = function jsonLoad(filePath, that, cb) {
+    let obj;
+    let json = self.fakeContents["weave/" + filePath + ".json"];
+    if (json) {
+      obj = JSON.parse(json);
+    }
+    cb.call(that, obj);
+  };
+};
+
+function fakeSHA256HMAC(message) {
+   message = message.substr(0, 64);
+   while (message.length < 64) {
+     message += " ";
+   }
+   return message;
+}
+
+function FakeGUIDService() {
+  let latestGUID = 0;
+
+  Utils.makeGUID = function makeGUID() {
+    return "fake-guid-" + latestGUID++;
+  };
+}
+
+/*
+ * Mock implementation of WeaveCrypto. It does not encrypt or
+ * decrypt, merely returning the input verbatim.
+ */
+function FakeCryptoService() {
+  this.counter = 0;
+
+  delete Svc.Crypto;  // get rid of the getter first
+  Svc.Crypto = this;
+
+  CryptoWrapper.prototype.ciphertextHMAC = function ciphertextHMAC(keyBundle) {
+    return fakeSHA256HMAC(this.ciphertext);
+  };
+}
+FakeCryptoService.prototype = {
+
+  encrypt: function encrypt(clearText, symmetricKey, iv) {
+    return clearText;
+  },
+
+  decrypt: function decrypt(cipherText, symmetricKey, iv) {
+    return cipherText;
+  },
+
+  generateRandomKey: function generateRandomKey() {
+    return btoa("fake-symmetric-key-" + this.counter++);
+  },
+
+  generateRandomIV: function generateRandomIV() {
+    // A base64-encoded IV is 24 characters long
+    return btoa("fake-fake-fake-random-iv");
+  },
+
+  expandData: function expandData(data, len) {
+    return data;
+  },
+
+  deriveKeyFromPassphrase: function deriveKeyFromPassphrase(passphrase,
+                                                            salt, keyLength) {
+    return "some derived key string composed of bytes";
+  },
+
+  generateRandomBytes: function generateRandomBytes(byteCount) {
+    return "not-so-random-now-are-we-HA-HA-HA! >:)".slice(byteCount);
+  }
+};
+
--- a/services/sync/tests/unit/head_helpers.js
+++ b/services/sync/tests/unit/head_helpers.js
@@ -1,16 +1,18 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Cu.import("resource://services-common/utils.js");
 Cu.import("resource://services-common/async.js");
 Cu.import("resource://services-sync/util.js");
 Cu.import("resource://services-sync/record.js");
 Cu.import("resource://services-sync/engines.js");
+Cu.import("resource://testing-common/services/sync/fakeservices.js");
+
 let btoa;
 let atob;
 
 let provider = {
   getFile: function(prop, persistent) {
     persistent.value = true;
     switch (prop) {
       case "ExtPrefDL":
@@ -139,99 +141,16 @@ function uninstallAddon(addon) {
     }
   }};
 
   AddonManager.addAddonListener(listener);
   addon.uninstall();
   Async.waitForSyncCallback(cb);
 }
 
-function FakeFilesystemService(contents) {
-  this.fakeContents = contents;
-  let self = this;
-
-  Utils.jsonSave = function jsonSave(filePath, that, obj, callback) {
-    let json = typeof obj == "function" ? obj.call(that) : obj;
-    self.fakeContents["weave/" + filePath + ".json"] = JSON.stringify(json);
-    callback.call(that);
-  };
-
-  Utils.jsonLoad = function jsonLoad(filePath, that, callback) {
-    let obj;
-    let json = self.fakeContents["weave/" + filePath + ".json"];
-    if (json) {
-      obj = JSON.parse(json);
-    }
-    callback.call(that, obj);
-  };
-};
-
-function FakeGUIDService() {
-  let latestGUID = 0;
-
-  Utils.makeGUID = function fake_makeGUID() {
-    return "fake-guid-" + latestGUID++;
-  };
-}
-
-
-function fakeSHA256HMAC(message) {
-   message = message.substr(0, 64);
-   while (message.length < 64) {
-     message += " ";
-   }
-   return message;
-}
-
-/*
- * Mock implementation of WeaveCrypto. It does not encrypt or
- * decrypt, merely returning the input verbatim.
- */
-function FakeCryptoService() {
-  this.counter = 0;
-
-  delete Svc.Crypto;  // get rid of the getter first
-  Svc.Crypto = this;
-
-  CryptoWrapper.prototype.ciphertextHMAC = function ciphertextHMAC(keyBundle) {
-    return fakeSHA256HMAC(this.ciphertext);
-  };
-}
-FakeCryptoService.prototype = {
-
-  encrypt: function(aClearText, aSymmetricKey, aIV) {
-    return aClearText;
-  },
-
-  decrypt: function(aCipherText, aSymmetricKey, aIV) {
-    return aCipherText;
-  },
-
-  generateRandomKey: function() {
-    return btoa("fake-symmetric-key-" + this.counter++);
-  },
-
-  generateRandomIV: function() {
-    // A base64-encoded IV is 24 characters long
-    return btoa("fake-fake-fake-random-iv");
-  },
-
-  expandData : function expandData(data, len) {
-    return data;
-  },
-
-  deriveKeyFromPassphrase : function (passphrase, salt, keyLength) {
-    return "some derived key string composed of bytes";
-  },
-
-  generateRandomBytes: function(aByteCount) {
-    return "not-so-random-now-are-we-HA-HA-HA! >:)".slice(aByteCount);
-  }
-};
-
 function setBasicCredentials(username, password, syncKey) {
   let ns = {};
   Cu.import("resource://services-sync/service.js", ns);
 
   let auth = ns.Service.identity;
   auth.username = username;
   auth.basicPassword = password;
   auth.syncKey = syncKey;
--- a/services/sync/tests/unit/test_load_modules.js
+++ b/services/sync/tests/unit/test_load_modules.js
@@ -27,16 +27,17 @@ const modules = [
   "stages/cluster.js",
   "stages/enginesync.js",
   "status.js",
   "userapi.js",
   "util.js",
 ];
 
 const testingModules = [
+  "fakeservices.js",
   "rotaryengine.js",
 ];
 
 function run_test() {
   for (let m of modules) {
     let res = "resource://services-sync/" + m;
     _("Attempting to load " + res);
     Cu.import(res, {});