From f00d3210aa7fa15203954d329c21ca0ad069584a Mon Sep 17 00:00:00 2001 From: Robert Nelson Date: Sun, 8 Dec 2013 19:35:01 -0800 Subject: [PATCH] Convert keyring to use key interface rather than the packetlist. --- src/keyring.js | 28 ++++++++++++++------------- test/ci-quick.js | 50 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/keyring.js b/src/keyring.js index e569756b..f9d0868c 100644 --- a/src/keyring.js +++ b/src/keyring.js @@ -56,7 +56,7 @@ var keyring = function() { } this.store = store; - function emailKeyCheck(key, email) { + function emailCheck(email, key) { email = email.toLowerCase(); var keyEmails = key.getUserIds(); for (var i; i < keyEmails.length; i++) { @@ -69,18 +69,20 @@ var keyring = function() { return false; } - function idKeyCheck(key, id) { - var keyid = key.getKeyId(); - if (keyid && keyid.write() == id) { - return true; + function idCheck(id, key) { + var keyids = key.getKeyIds(); + for (var i = 0; i < keyids.length; i++) { + if (openpgp.util.hexstrdump(keyids[i].write()) == id) { + return true; + } + return false; } - return false; } - function checkForIdentityAndPacketMatch(identityFunction, identityInput, keyType) { + function checkForIdentityAndKeyTypeMatch(keys, identityFunction, identityInput, keyType) { var results = []; - for (var p = 0; p < this.keys.length; p++) { - var key = this.keys[p]; + for (var p = 0; p < keys.length; p++) { + var key = keys[p]; switch (keyType) { case openpgp.enums.packet.public_key: if (key.isPublic() && identityFunction(identityInput, key)) { @@ -96,7 +98,7 @@ var keyring = function() { } return results; } - this.checkForIdentityAndPacketMatch = checkForIdentityAndPacketMatch; + this.checkForIdentityAndKeyTypeMatch = checkForIdentityAndKeyTypeMatch; /** * searches all public keys in the keyring matching the address or address part of the user ids @@ -104,7 +106,7 @@ var keyring = function() { * @return {openpgp.key.Key[]} The public keys associated with provided email address. */ function getPublicKeyForAddress(email) { - return checkForIdentityAndPacketMatch(emailPacketCheck, email, openpgp.enums.packet.public_key); + return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.public_key); } this.getPublicKeyForAddress = getPublicKeyForAddress; @@ -114,7 +116,7 @@ var keyring = function() { * @return {openpgp.key.Key[]} private keys found */ function getPrivateKeyForAddress(email) { - return checkForIdentityAndPacketMatch(emailPacketCheck, email, openpgp.enums.packet.secret_key); + return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.secret_key); } this.getPrivateKeyForAddress = getPrivateKeyForAddress; @@ -124,7 +126,7 @@ var keyring = function() { * @return {openpgp.key.Key[]} public keys found */ function getKeysForKeyId(keyId) { - return this.checkForIdentityAndPacketMatch(idPacketCheck, keyId); + return this.checkForIdentityAndKeyTypeMatch(this.keys, idCheck, keyId, openpgp.enums.packet.public_key); } this.getKeysForKeyId = getKeysForKeyId; diff --git a/test/ci-quick.js b/test/ci-quick.js index 5d84ef0d..cfd72838 100644 --- a/test/ci-quick.js +++ b/test/ci-quick.js @@ -61,15 +61,47 @@ describe('Openpgp integration tests', function() { }); }); - describe('Import key pair', function() { - it('should work', function(done) { - // clear any keypair already in the keychain - keyring.init(); - // import private key - keyring.importKey(privkey); - // import public key - keyring.importKey(pubkey); - done(); + describe('Keyring', function() { + describe('Import key pair', function() { + it('should work', function(done) { + // clear any keypair already in the keychain + keyring.init(); + keyring.importKey(privkey); + keyring.importKey(pubkey); + done(); + }); + }); + describe('Retrieve keys', function() { + it('getPublicKeyForAddress() - unknown address', function(done) { + var key = keyring.getPublicKeyForAddress('nobody@example.com'); + expect(key).to.be.empty; + done(); + }); + it('getPrivateKeyForAddress() - unknown address', function(done) { + var key = keyring.getPrivateKeyForAddress('nobody@example.com'); + expect(key).to.be.empty; + done(); + }); + it('getPublicKeyForAddress() - valid address', function(done) { + var key = keyring.getPublicKeyForAddress(user); + expect(key).to.exist; + done(); + }); + it('getPrivateKeyForAddress() - valid address', function(done) { + var key = keyring.getPrivateKeyForAddress(user); + expect(key).to.exist; + done(); + }); + it('getKeysForKeyId() - unknown id', function(done) { + var keys = keyring.getKeysForKeyId('000102030405060708'); + expect(keys).to.be.empty; + done(); + }); + it('getKeysForKeyId() - valid id', function(done) { + var keys = keyring.getKeysForKeyId(keyId.toLowerCase()); + expect(keys).to.exist.and.have.length(1); + done(); + }); }); });