some small key record fixes; avoid causing indirect login manager queries in the Identity constructor; fix wbo, keys, crypto record unit tests

This commit is contained in:
Dan Mills 2008-12-02 14:26:18 -08:00
parent c5ed82885f
commit 497743cb82
5 changed files with 109 additions and 66 deletions

View File

@ -65,18 +65,18 @@ PubKey.prototype = {
_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;
@ -88,7 +88,7 @@ PubKey.prototype = {
return Utils.makeURI(this._privateKeyUri);
},
set privateKeyUri(value) {
this.data.payload.private_key = value;
this.payload.private_key = value;
},
get publicKeyUri() {
@ -105,7 +105,7 @@ PrivKey.prototype = {
_PrivKey_init: function PrivKey_init(uri, authenticator) {
this._WBORec_init(uri, authenticator);
this.data.payload = {
this.payload = {
type: "privkey",
salt: null,
iv: null,
@ -114,17 +114,17 @@ PrivKey.prototype = {
};
},
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() {

View File

@ -90,8 +90,8 @@ IDManager.prototype = {
*/
function Identity(realm, username, password) {
this.realm = realm;
this.username = username;
this._realm = realm;
this._username = username;
this._password = password;
}
Identity.prototype = {

View File

@ -11,29 +11,37 @@ try {
}
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() {
@ -44,39 +52,52 @@ function async_test() {
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); }

View File

@ -5,27 +5,27 @@ try {
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);
}
@ -37,19 +37,27 @@ function async_test() {
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});
let auth = new BasicAuthenticator(new Identity("secret", "guest", "guest"));
Auth.defaultAuthenticator = auth;
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); }

View File

@ -1,39 +1,53 @@
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");
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);
server = httpd_setup({"/record": record_handler});
do_test_finished();
server.stop();
let auth = new BasicAuthenticator(new Identity("secret", "guest", "guest"));
Auth.defaultAuthenticator = auth;
log.info("Getting a WBO record");
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();
}