some small key record fixes; avoid causing indirect login manager queries in the Identity constructor; fix wbo, keys, crypto record unit tests
authorDan Mills <thunder@mozilla.com>
Tue, 02 Dec 2008 14:26:18 -0800
changeset 45074 e14daee67de057d552e0a51d79713b770c45c168
parent 45073 aae29a193600db71ae0ed6e07234cec128cde3b5
child 45075 d3f6e2cf4fc7b4bc1458175208ecdcde46571caa
push idunknown
push userunknown
push dateunknown
some small key record fixes; avoid causing indirect login manager queries in the Identity constructor; fix wbo, keys, crypto record unit tests
services/sync/modules/base_records/keys.js
services/sync/modules/identity.js
services/sync/tests/unit/test_records_crypto.js
services/sync/tests/unit/test_records_keys.js
services/sync/tests/unit/test_records_wbo.js
--- a/services/sync/modules/base_records/keys.js
+++ b/services/sync/modules/base_records/keys.js
@@ -60,76 +60,76 @@ function PubKey(uri, authenticator) {
   this._PubKey_init(uri, authenticator);
 }
 PubKey.prototype = {
   __proto__: WBORecord.prototype,
   _logName: "Record.PubKey",
 
   _PubKey_init: function PubKey_init(uri, authenticator) {
     this._WBORec_init(uri, authenticator);
-    this.data.payload = {
+    this.payload = {
       type: "pubkey",
       key_data: null,
       private_key: null
     };
   },
 
-  get keyData() this.data.payload.key_data,
+  get keyData() this.payload.key_data,
   set keyData(value) {
-    this.data.payload.key_data = value;
+    this.payload.key_data = value;
   },
-  get _privateKeyUri() this.data.payload.private_key,
+  get _privateKeyUri() this.payload.private_key,
   get privateKeyUri() {
     if (!this.data)
       return null;
     // resolve
     let uri = this.uri.resolve(this._privateKeyUri);
     if (uri)
       return Utils.makeURI(uri);
     // does not resolve, return raw (this uri type might not be able to resolve)
     return Utils.makeURI(this._privateKeyUri);
   },
   set privateKeyUri(value) {
-    this.data.payload.private_key = value;
+    this.payload.private_key = value;
   },
 
   get publicKeyUri() {
     throw "attempted to get public key url from a public key!";
   }
 };
 
 function PrivKey(uri, authenticator) {
   this._PrivKey_init(uri, authenticator);
 }
 PrivKey.prototype = {
   __proto__: WBORecord.prototype,
   _logName: "Record.PrivKey",
 
   _PrivKey_init: function PrivKey_init(uri, authenticator) {
     this._WBORec_init(uri, authenticator);
-    this.data.payload = {
+    this.payload = {
       type: "privkey",
       salt: null,
       iv: null,
       key_data: null,
       public_key: null
     };
   },
 
-  get salt() this.data.payload.salt,
+  get salt() this.payload.salt,
   set salt(value) {
-    this.data.payload.salt = value;
+    this.payload.salt = value;
   },
-  get iv() this.data.payload.iv,
+  get iv() this.payload.iv,
   set iv(value) {
-    this.data.payload.iv = value;
+    this.payload.iv = value;
   },
-  get keyData() this.data.payload.key_data,
+  get keyData() this.payload.key_data,
   set keyData(value) {
-    this.data.payload.key_data = value;
+    this.payload.key_data = value;
   },
 
   get publicKeyUri() {
     if (!this.data)
       return null;
     // resolve
     let uri = this.uri.resolve(this.payload.public_key);
     if (uri)
--- a/services/sync/modules/identity.js
+++ b/services/sync/modules/identity.js
@@ -85,18 +85,18 @@ IDManager.prototype = {
  * Identity
  * These objects hold a realm, username, and password
  * They can hold a password in memory, but will try to fetch it from
  * the password manager if it's not set.
  * FIXME: need to rethink this stuff as part of a bigger identity mgmt framework
  */
 
 function Identity(realm, username, password) {
-  this.realm     = realm;
-  this.username  = username;
+  this._realm = realm;
+  this._username = username;
   this._password = password;
 }
 Identity.prototype = {
   get realm() this._realm,
   set realm(value) {
     let current = Utils.findPassword(this.realm, this.username);
     if (current)
       this.password = null;
--- a/services/sync/tests/unit/test_records_crypto.js
+++ b/services/sync/tests/unit/test_records_crypto.js
@@ -6,82 +6,103 @@ try {
   Cu.import("resource://weave/identity.js");
   Cu.import("resource://weave/base_records/keys.js");
   Cu.import("resource://weave/base_records/crypto.js");
 } catch (e) {
   do_throw(e);
 }
 Function.prototype.async = Async.sugar;
 
-let jsonSvc = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
-let cryptoSvc = Cc["@labs.mozilla.com/Weave/Crypto;1"].
+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) {
-  return httpd_basic_auth_handler(jsonSvc.encode(keys.pubkey.data),
-                                  metadata, response);
+  let obj = {id: "ignore-me",
+             modified: keys.pubkey.modified,
+             payload: json.encode(keys.pubkey.payload)};
+  return httpd_basic_auth_handler(json.encode(obj), metadata, response);
 }
 
 function privkey_handler(metadata, response) {
-  return httpd_basic_auth_handler(jsonSvc.encode(keys.privkey.data),
-                                  metadata, response);
+  let obj = {id: "ignore-me-2",
+             modified: keys.privkey.modified,
+             payload: json.encode(keys.privkey.payload)};
+  return httpd_basic_auth_handler(json.encode(obj), metadata, response);
 }
 
 function crypted_resource_handler(metadata, response) {
-  return httpd_basic_auth_handler(jsonSvc.encode(cryptoWrap.data),
-                                  metadata, response);
+  let obj = {id: "ignore-me-3",
+             modified: cryptoWrap.modified,
+             payload: json.encode(cryptoWrap.payload)};
+  return httpd_basic_auth_handler(json.encode(obj), metadata, response);
 }
 
 function crypto_meta_handler(metadata, response) {
-  return httpd_basic_auth_handler(jsonSvc.encode(cryptoMeta.data),
-                                  metadata, response);
+  let obj = {id: "ignore-me-4",
+             modified: cryptoMeta.modified,
+             payload: json.encode(cryptoMeta.payload)};
+  return httpd_basic_auth_handler(json.encode(obj), metadata, response);
 }
 
 function async_test() {
   let self = yield;
   let server;
 
   try {
     let log = Log4Moz.repository.getLogger();
     Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender());
 
-    let auth = new BasicAuthenticator(new Identity("secret", "guest", "guest"));
-    Auth.defaultAuthenticator = auth;
+    log.info("Setting up server and authenticator");
 
     server = httpd_setup({"/pubkey": pubkey_handler,
                           "/privkey": privkey_handler,
                           "/crypted-resource": crypted_resource_handler,
                           "/crypto-meta": crypto_meta_handler});
 
+    let auth = new BasicAuthenticator(new Identity("secret", "guest", "guest"));
+    Auth.defaultAuthenticator = auth;
+
+    log.info("Generating keypair + symmetric key");
+
     PubKeys.defaultKeyUri = "http://localhost:8080/pubkey";
     keys = PubKeys.createKeypair("my passphrase",
                                  "http://localhost:8080/pubkey",
                                  "http://localhost:8080/privkey");
-    keys.symkey = cryptoSvc.generateRandomKey();
-    keys.wrappedkey = cryptoSvc.wrapSymmetricKey(keys.symkey, keys.pubkey.keyData);
+    keys.symkey = crypto.generateRandomKey();
+    keys.wrappedkey = crypto.wrapSymmetricKey(keys.symkey, keys.pubkey.keyData);
+
+    log.info("Setting up keyring");
 
     cryptoMeta = new CryptoMeta("http://localhost:8080/crypto-meta", auth);
     cryptoMeta.generateIV();
     yield cryptoMeta.addUnwrappedKey(self.cb, keys.pubkey, keys.symkey);
 
+    log.info("Creating and encrypting a record");
+
     cryptoWrap = new CryptoWrapper("http://localhost:8080/crypted-resource", auth);
     cryptoWrap.encryption = "http://localhost:8080/crypto-meta";
     cryptoWrap.cleartext = "my payload here";
     yield cryptoWrap.encrypt(self.cb, "my passphrase");
 
+    log.info("Decrypting the record");
+
     let payload = yield cryptoWrap.decrypt(self.cb, "my passphrase");
     do_check_eq(payload, "my payload here");
     do_check_neq(payload, cryptoWrap.payload); // wrap.data.payload is the encrypted one
 
+    log.info("Re-encrypting the record with alternate payload");
+
     cryptoWrap.cleartext = "another payload";
     yield cryptoWrap.encrypt(self.cb, "my passphrase");
     payload = yield cryptoWrap.decrypt(self.cb, "my passphrase");
     do_check_eq(payload, "another payload");
 
+    log.info("Done!");
     do_test_finished();
   }
   catch (e) { do_throw(e); }
   finally { server.stop(); }
 
   self.done();
 }
 
--- a/services/sync/tests/unit/test_records_keys.js
+++ b/services/sync/tests/unit/test_records_keys.js
@@ -1,60 +1,68 @@
 try {
   Cu.import("resource://weave/log4moz.js");
   Cu.import("resource://weave/util.js");
   Cu.import("resource://weave/async.js");
   Cu.import("resource://weave/auth.js");
   Cu.import("resource://weave/identity.js");
   Cu.import("resource://weave/base_records/keys.js");
-} catch (e) {
-  do_throw(e);
-}
+} catch (e) { do_throw(e); }
+
 Function.prototype.async = Async.sugar;
 
 let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
 
 function pubkey_handler(metadata, response) {
-  let obj = {modified: "2454725.98283",
-             payload: {type: "pubkey",
-                       private_key: "http://localhost:8080/privkey",
-                       key_data: "asdfasdfasf..."}};
+  let obj = {id: "asdf-1234-asdf-1234",
+             modified: "2454725.98283",
+             payload: json.encode({type: "pubkey",
+                                   private_key: "http://localhost:8080/privkey",
+                                   key_data: "asdfasdfasf..."})};
   return httpd_basic_auth_handler(json.encode(obj), metadata, response);
 }
 
 function privkey_handler(metadata, response) {
-  let obj = {modified: "2454725.98283",
-             payload: {type: "privkey",
-                       public_key: "http://localhost:8080/pubkey",
-                       key_data: "asdfasdfasf..."}};
-  let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
+  let obj = {id: "asdf-1234-asdf-1234-2",
+             modified: "2454725.98283",
+             payload: json.encode({type: "privkey",
+                                   public_key: "http://localhost:8080/pubkey",
+                                   key_data: "asdfasdfasf..."})};
   return httpd_basic_auth_handler(json.encode(obj), metadata, response);
 }
 
 function async_test() {
   let self = yield;
   let server;
 
   try {
     let log = Log4Moz.repository.getLogger();
     Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender());
 
+    log.info("Setting up server and authenticator");
+
+    server = httpd_setup({"/pubkey": pubkey_handler,
+                          "/privkey": privkey_handler});
+
     let auth = new BasicAuthenticator(new Identity("secret", "guest", "guest"));
     Auth.defaultAuthenticator = auth;
-    server = httpd_setup({"/pubkey": pubkey_handler,
-                          "/privkey": privkey_handler});
+
+    log.info("Getting a public key");
 
     let pubkey = yield PubKeys.get(self.cb, "http://localhost:8080/pubkey");
     do_check_eq(pubkey.data.payload.type, "pubkey");
     do_check_eq(pubkey.lastRequest.status, 200);
 
+    log.info("Getting a private key");
+
     let privkey = yield PrivKeys.get(self.cb, pubkey.privateKeyUri);
     do_check_eq(privkey.data.payload.type, "privkey");
     do_check_eq(privkey.lastRequest.status, 200);
 
+    log.info("Done!");
     do_test_finished();
   }
   catch (e) { do_throw(e); }
   finally { server.stop(); }
 
   self.done();
 }
 
--- a/services/sync/tests/unit/test_records_wbo.js
+++ b/services/sync/tests/unit/test_records_wbo.js
@@ -1,44 +1,58 @@
-Cu.import("resource://weave/log4moz.js");
-Cu.import("resource://weave/async.js");
-Cu.import("resource://weave/auth.js");
-Cu.import("resource://weave/identity.js");
-Cu.import("resource://weave/base_records/wbo.js");
+try {
+  Cu.import("resource://weave/log4moz.js");
+  Cu.import("resource://weave/util.js");
+  Cu.import("resource://weave/async.js");
+  Cu.import("resource://weave/auth.js");
+  Cu.import("resource://weave/identity.js");
+  Cu.import("resource://weave/base_records/wbo.js");
+} catch (e) { do_throw(e); }
 
 Function.prototype.async = Async.sugar;
 
-let logger;
-let Httpd = {};
-Cu.import("resource://tests/lib/httpd.js", Httpd);
+let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
 
-function server_handler(metadata, response) {
-  let body = '{"guid": "asdf-1234-asdf-1234", "type": ["object"]}';
-  response.setStatusLine(metadata.httpVersion, 200, "OK");
-  response.bodyOutputStream.write(body, body.length);
+function record_handler(metadata, response) {
+  let obj = {id: "asdf-1234-asdf-1234",
+             modified: "2454725.98283",
+             payload: json.encode({cheese: "roquefort"})};
+  return httpd_basic_auth_handler(json.encode(obj), metadata, response);
 }
 
 function async_test() {
   let self = yield;
+  let server;
 
-  logger = Log4Moz.repository.getLogger('Test');
-  Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender());
+  try {
+    let log = Log4Moz.repository.getLogger('Test');
+    Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender());
 
-  let server = new Httpd.nsHttpServer();
-  server.registerPathHandler("/record", server_handler);
-  server.start(8080);
+    log.info("Setting up server and authenticator");
+
+    server = httpd_setup({"/record": record_handler});
+
+    let auth = new BasicAuthenticator(new Identity("secret", "guest", "guest"));
+    Auth.defaultAuthenticator = auth;
 
-  let res = new WBORecord("http://localhost:8080/record");
-  let rec = yield res.get(self.cb);
-  do_check_eq(rec.guid, "asdf-1234-asdf-1234");
-  do_check_eq(rec.type[0], "object");
-  do_check_eq(res.lastRequest.status, 200);
+    log.info("Getting a WBO record");
 
-  do_test_finished();
-  server.stop();
+    let res = new WBORecord("http://localhost:8080/record");
+    let rec = yield res.get(self.cb);
+    do_check_eq(rec.id, "record"); // NOT "asdf-1234-asdf-1234"!
+    do_check_eq(rec.modified, 2454725.98283);
+    do_check_eq(typeof(rec.payload), "object");
+    do_check_eq(rec.payload.cheese, "roquefort");
+    do_check_eq(res.lastRequest.status, 200);
+
+    log.info("Done!");
+    do_test_finished();
+  }
+  catch (e) { do_throw(e); }
+  finally { server.stop(); }
     
   self.done();
 }
 
 function run_test() {
   async_test.async(this);
   do_test_pending();
 }