Merge bug 618389 from fx-sync. a=blocking-beta8
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Fri, 10 Dec 2010 21:44:17 -0800
changeset 59112 7aedd1d023d0ba4eb9b3483f6742bd11691a1e2d
parent 59107 d0a4697b4b6a587ca6cb22dac54d5576d2d65b34 (current diff)
parent 59111 9cf74b4d9f21ce36182e72a3bd9f73c2fcd872cc (diff)
child 59113 e42d81b3e0ccc79066cd3514446ae4f881b4bff2
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersblocking-beta8
bugs618389
milestone2.0b8pre
Merge bug 618389 from fx-sync. a=blocking-beta8
services/sync/modules/service.js
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -774,20 +774,23 @@ WeaveSvc.prototype = {
         }
         else {
           this._log.info("... 'crypto' is not a reported collection. Generating new keys.");
         }
 
         if (!cryptoKeys) {
           // Must have got a 404, or no reported collection.
           // Better make some and upload them.
+          // 
+          // Reset the client so we reupload.
+          this.resetClient();
+          
+          // Generate the new keys.
           this.generateNewSymmetricKeys();
           
-          // Oh, and reset the client so we reupload, too.
-          this.resetClient();
           return true;
         }
         
         // Last-ditch case.
         return false;
       }
       else {
         // No update needed: we're good!
@@ -937,36 +940,40 @@ WeaveSvc.prototype = {
       this.wipeServer();
 
       this.logout();
 
       /* Set this so UI is updated on next run. */
       this.passphrase = newphrase;
       this.persistLogin();
 
+      /* We need to re-encrypt everything, so reset. */
+      this.resetClient();
+      CollectionKeys.clear();
+
       /* Login and sync. This also generates new keys. */
       this.login();
       this.sync(true);
       return true;
     }))(),
 
   startOver: function() {
     // Set a username error so the status message shows "set up..."
     Status.login = LOGIN_FAILED_NO_USERNAME;
     this.logout();
-    // Reset all engines.
+    
+    // Reset all engines and clear keys.
     this.resetClient();
+    CollectionKeys.clear();
+    
     // Reset Weave prefs.
     this._ignorePrefObserver = true;
     Svc.Prefs.resetBranch("");
     this._ignorePrefObserver = false;
     
-    // Clear keys.
-    CollectionKeys.clear();
-    
     Svc.Prefs.set("lastversion", WEAVE_VERSION);
     // Find weave logins and remove them.
     this.password = "";
     this.passphrase = "";
     Svc.Login.findLogins({}, PWDMGR_HOST, "", "").map(function(login) {
       Svc.Login.removeLogin(login);
     });
     Svc.Obs.notify("weave:service:start-over");
@@ -1264,16 +1271,17 @@ WeaveSvc.prototype = {
       Status.sync = VERSION_OUT_OF_DATE;
       this._log.warn("Upgrade required to access newer storage version.");
       return false;
     }
     else if (meta.payload.syncID != this.syncID) {
       
       this._log.info("Sync IDs differ. Local is " + this.syncID + ", remote is " + meta.payload.syncID);
       this.resetClient();
+      CollectionKeys.clear();
       this.syncID = meta.payload.syncID;
       this._log.debug("Clear cached values and take syncId: " + this.syncID);
 
       if (!this.upgradeSyncKey(meta.payload.syncID)) {
         this._log.warn("Failed to upgrade sync key. Failing remote setup.");
         return false;
       }
       
@@ -1799,16 +1807,17 @@ WeaveSvc.prototype = {
     this.persistLogin();
     this._log.info("Done saving.");
     return true;
   },
 
   _freshStart: function WeaveSvc__freshStart() {
     this._log.info("Fresh start. Resetting client and considering key upgrade.");
     this.resetClient();
+    CollectionKeys.clear();
     this.upgradeSyncKey(this.syncID);
 
     let meta = new WBORecord("meta", "global");
     meta.payload.syncID = this.syncID;
     meta.payload.storageVersion = STORAGE_VERSION;
     meta.isNew = true;
 
     this._log.debug("New metadata record: " + JSON.stringify(meta.payload));
--- a/services/sync/tests/unit/test_service_detect_upgrade.js
+++ b/services/sync/tests/unit/test_service_detect_upgrade.js
@@ -4,16 +4,18 @@ Cu.import("resource://services-sync/engi
 Cu.import("resource://services-sync/util.js");
 Cu.import("resource://services-sync/status.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/base_records/wbo.js");      // For Records.
 Cu.import("resource://services-sync/base_records/crypto.js");   // For CollectionKeys.
 Cu.import("resource://services-sync/engines/tabs.js");
 Cu.import("resource://services-sync/log4moz.js");
   
+Engines.register(TabEngine);
+
 function v4_upgrade() {
   let passphrase = "abcdeabcdeabcdeabcdeabcdea";
 
   let clients = new ServerCollection();
   let meta_global = new ServerWBO('global');
 
   // Tracking info/collections.
   let collectionsHelper = track_collections_helper();
@@ -37,17 +39,16 @@ function v4_upgrade() {
     "/1.0/johndoe/storage/forms": new ServerCollection().handler(),
     "/1.0/johndoe/storage/history": new ServerCollection().handler(),
     "/1.0/johndoe/storage/passwords": new ServerCollection().handler(),
     "/1.0/johndoe/storage/prefs": new ServerCollection().handler()
   });
 
   try {
     
-    Svc.Prefs.set("registerEngines", "Tab");
     _("Set up some tabs.");
     let myTabs = 
       {windows: [{tabs: [{index: 1,
                           entries: [{
                             url: "http://foo.com/",
                             title: "Title"
                           }],
                           attributes: {
@@ -212,17 +213,16 @@ function v5_upgrade() {
       
     // Track modified times.
     "/1.0/johndoe/storage/clients": upd("clients", clients.handler()),
     "/1.0/johndoe/storage/tabs": upd("tabs", new ServerCollection().handler()),
   });
 
   try {
     
-    Svc.Prefs.set("registerEngines", "Tab");
     _("Set up some tabs.");
     let myTabs = 
       {windows: [{tabs: [{index: 1,
                           entries: [{
                             url: "http://foo.com/",
                             title: "Title"
                           }],
                           attributes: {
--- a/services/sync/tests/unit/test_service_sync_remoteSetup.js
+++ b/services/sync/tests/unit/test_service_sync_remoteSetup.js
@@ -24,21 +24,26 @@ function run_test() {
   function wasCalledHandler(wbo) {
     let handler = wbo.handler();
     return function() {
       wbo.wasCalled = true;
       handler.apply(this, arguments);
     };
   }
 
+  let keysWBO = new ServerWBO("keys");
+  let cryptoColl = new ServerCollection({keys: keysWBO});
+  let metaColl = new ServerCollection({global: meta_global});
   do_test_pending();
   let server = httpd_setup({
-    "/1.0/johndoe/storage/crypto/keys": upd("crypto", new ServerWBO("keys").handler()),
+    "/1.0/johndoe/storage/crypto/keys": upd("crypto", keysWBO.handler()),
+    "/1.0/johndoe/storage/crypto": upd("crypto", cryptoColl.handler()),
     "/1.0/johndoe/storage/clients": upd("clients", clients.handler()),
     "/1.0/johndoe/storage/meta/global": upd("meta", wasCalledHandler(meta_global)),
+    "/1.0/johndoe/storage/meta": upd("meta", wasCalledHandler(metaColl)),
     "/1.0/johndoe/info/collections": collectionsHelper.handler
   });
 
   try {
     _("Log in.");
     Weave.Service.serverURL = "http://localhost:8080/";
     Weave.Service.clusterURL = "http://localhost:8080/";
     
@@ -91,16 +96,32 @@ function run_test() {
     let pp = Weave.Service.passphrase;
     Weave.Service.passphrase = "notvalid";
     do_check_false(Weave.Service.verifyAndFetchSymmetricKeys());
     do_check_eq(Status.sync, CREDENTIALS_CHANGED);
     do_check_eq(Status.login, LOGIN_FAILED_INVALID_PASSPHRASE);
     Weave.Service.passphrase = pp;
     do_check_true(Weave.Service.verifyAndFetchSymmetricKeys());
     
+    // changePassphrase wipes our keys, and they're regenerated on next sync.
+    _("Checking changed passphrase.");
+    let existingDefault = CollectionKeys.keyForCollection();
+    let existingKeysPayload = keysWBO.payload;
+    let newPassphrase = "bbbbbabcdeabcdeabcdeabcdea";
+    Weave.Service.changePassphrase(newPassphrase);
+    
+    _("Local key cache is full, but different.");
+    do_check_true(!!CollectionKeys._default);
+    do_check_false(CollectionKeys._default.equals(existingDefault));
+    
+    _("Server has new keys.");
+    do_check_true(!!keysWBO.payload);
+    do_check_true(!!keysWBO.modified);
+    do_check_neq(keysWBO.payload, existingKeysPayload);
+
     // Try to screw up HMAC calculation.
     // Re-encrypt keys with a new random keybundle, and upload them to the
     // server, just as might happen with a second client.
     _("Attempting to screw up HMAC by re-encrypting keys.");
     let keys = CollectionKeys.asWBO();
     let b = new BulkKeyBundle("hmacerror", "hmacerror");
     b.generateRandom();
     collections.crypto = keys.modified = 100 + (Date.now()/1000);  // Future modification time.