Merge bug 601973 from fx-sync. a=blocking-beta7
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Tue, 05 Oct 2010 21:13:00 +0200
changeset 54941 13b0effff79db87c4745b5325b22f0e7fbacb44c
parent 54939 beb044c602a72bc694d72f8ff08b6e961e5ac727 (current diff)
parent 54940 4f041eff33bdeb3274554383dcffb8cbb6adbf44 (diff)
child 54942 dee1e01fd8ed0fb92be81765b4132709ccff80e2
child 55513 01dbfd987f73995092031551f15432a2714cfecc
push id16093
push userpweitershausen@mozilla.com
push dateTue, 05 Oct 2010 19:26:44 +0000
treeherdermozilla-central@13b0effff79d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblocking-beta7
bugs601973
milestone2.0b7pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Merge bug 601973 from fx-sync. a=blocking-beta7
services/sync/modules/service.js
services/sync/tests/unit/test_syncengine_sync.js
--- a/services/sync/modules/engines.js
+++ b/services/sync/modules/engines.js
@@ -754,27 +754,28 @@ SyncEngine.prototype = {
       this._syncFinish();
     }
     catch (e) {
       this._log.warn("Sync failed");
       throw e;
     }
   },
 
-  _testDecrypt: function _testDecrypt() {
+  canDecrypt: function canDecrypt() {
     // Report failure even if there's nothing to decrypt
     let canDecrypt = false;
 
     // Fetch the most recently uploaded record and try to decrypt it
     let test = new Collection(this.engineURL, this._recordObj);
     test.limit = 1;
     test.sort = "newest";
     test.full = true;
+    let self = this;
     test.recordHandler = function(record) {
-      record.decrypt(ID.get("WeaveCryptoID"), this.cryptoMetaURL);
+      record.decrypt(ID.get("WeaveCryptoID"), self.cryptoMetaURL);
       canDecrypt = true;
     };
 
     // Any failure fetching/decrypting will just result in false
     try {
       this._log.trace("Trying to decrypt a record from the server..");
       test.get();
     }
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -1676,17 +1676,17 @@ WeaveSvc.prototype = {
         engines = [Clients].concat(Engines.getAll());
       }
       // Convert the array of names into engines
       else
         engines = Engines.get(engines);
 
       // Fully wipe each engine if it's able to decrypt data
       for each (let engine in engines)
-        if (engine._testDecrypt())
+        if (engine.canDecrypt())
           engine.wipeClient();
 
       // Save the password/passphrase just in-case they aren't restored by sync
       this.persistLogin();
     }))(),
 
   /**
    * Wipe all remote user data by wiping the server then telling each remote
new file mode 100644
--- /dev/null
+++ b/services/sync/tests/unit/test_service_wipeClient.js
@@ -0,0 +1,64 @@
+Cu.import("resource://services-sync/service.js");
+Cu.import("resource://services-sync/engines.js");
+
+
+function CanDecryptEngine() {
+  SyncEngine.call(this, "CanDecrypt");
+}
+CanDecryptEngine.prototype = {
+  __proto__: SyncEngine.prototype,
+
+  // Override these methods with mocks for the test
+  canDecrypt: function canDecrypt() {
+    return true;
+  },
+
+  wasWiped: false,
+  wipeClient: function wipeClient() {
+    this.wasWiped = true;
+  }
+};
+Engines.register(CanDecryptEngine);
+
+
+function CannotDecryptEngine() {
+  SyncEngine.call(this, "CannotDecrypt");
+}
+CannotDecryptEngine.prototype = {
+  __proto__: SyncEngine.prototype,
+
+  // Override these methods with mocks for the test
+  canDecrypt: function canDecrypt() {
+    return false;
+  },
+
+  wasWiped: false,
+  wipeClient: function wipeClient() {
+    this.wasWiped = true;
+  }
+};
+Engines.register(CannotDecryptEngine);
+
+
+function test_withEngineList() {
+  try {
+    _("Ensure initial scenario.");
+    do_check_false(Engines.get("candecrypt").wasWiped);
+    do_check_false(Engines.get("cannotdecrypt").wasWiped);
+    
+    _("Wipe local engine data.");
+    Service.wipeClient(["candecrypt", "cannotdecrypt"]);
+
+    _("Ensure only the engine that can decrypt was wiped.");
+    do_check_true(Engines.get("candecrypt").wasWiped);
+    do_check_false(Engines.get("cannotdecrypt").wasWiped);
+  } finally {
+    Engines.get("candecrypt").wasWiped = false;
+    Engines.get("cannotdecrypt").wasWiped = false;
+    Service.startOver();
+  }
+}
+
+function run_test() {
+  test_withEngineList();
+}
--- a/services/sync/tests/unit/test_syncengine_sync.js
+++ b/services/sync/tests/unit/test_syncengine_sync.js
@@ -1082,16 +1082,78 @@ function test_syncFinish_deleteLotsInBat
   } finally {
     server.stop(do_test_finished);
     Svc.Prefs.resetBranch("");
     Records.clearCache();
     syncTesting = new SyncTestingInfrastructure(makeSteamEngine);
   }
 }
 
+function test_canDecrypt_noCryptoMeta() {
+  _("SyncEngine.canDecrypt returns false if the engine fails to decrypt items on the server, e.g. due to a missing crypto key.");
+  Svc.Prefs.set("clusterURL", "http://localhost:8080/");
+  Svc.Prefs.set("username", "foo");
+
+  let collection = new ServerCollection();
+  collection.wbos.flying = new ServerWBO(
+      'flying', encryptPayload({id: 'flying',
+                                denomination: "LNER Class A3 4472"}));
+
+  let server = sync_httpd_setup({
+      "/1.0/foo/storage/steam": collection.handler()
+  });
+  do_test_pending();
+  createAndUploadKeypair();
+
+  let engine = makeSteamEngine();
+  try {
+
+    do_check_false(engine.canDecrypt());
+
+  } finally {
+    server.stop(do_test_finished);
+    Svc.Prefs.resetBranch("");
+    Records.clearCache();
+    syncTesting = new SyncTestingInfrastructure(makeSteamEngine);
+  }
+}
+
+function test_canDecrypt_true() {
+  _("SyncEngine.canDecrypt returns true if the engine can decrypt the items on the server.");
+  Svc.Prefs.set("clusterURL", "http://localhost:8080/");
+  Svc.Prefs.set("username", "foo");
+
+  let crypto_steam = new ServerWBO('steam');
+  let collection = new ServerCollection();
+  collection.wbos.flying = new ServerWBO(
+      'flying', encryptPayload({id: 'flying',
+                                denomination: "LNER Class A3 4472"}));
+
+  let server = sync_httpd_setup({
+      "/1.0/foo/storage/crypto/steam": crypto_steam.handler(),
+      "/1.0/foo/storage/steam": collection.handler()
+  });
+  do_test_pending();
+  createAndUploadKeypair();
+  createAndUploadSymKey("http://localhost:8080/1.0/foo/storage/crypto/steam");
+
+  let engine = makeSteamEngine();
+  try {
+
+    do_check_true(engine.canDecrypt());
+
+  } finally {
+    server.stop(do_test_finished);
+    Svc.Prefs.resetBranch("");
+    Records.clearCache();
+    syncTesting = new SyncTestingInfrastructure(makeSteamEngine);
+  }
+}
+
+
 function run_test() {
   test_syncStartup_emptyOrOutdatedGlobalsResetsSync();
   test_syncStartup_metaGet404();
   test_syncStartup_failedMetaGet();
   test_syncStartup_serverHasNewerVersion();
   test_syncStartup_syncIDMismatchResetsClient();
   test_syncStartup_badKeyWipesServerData();
   test_processIncoming_emptyServer();
@@ -1099,9 +1161,11 @@ function run_test() {
   test_processIncoming_reconcile();
   test_processIncoming_fetchNum();
   test_uploadOutgoing_toEmptyServer();
   test_uploadOutgoing_failed();
   test_uploadOutgoing_MAX_UPLOAD_RECORDS();
   test_syncFinish_noDelete();
   test_syncFinish_deleteByIds();
   test_syncFinish_deleteLotsInBatches();
+  test_canDecrypt_noCryptoMeta();
+  test_canDecrypt_true();
 }