From 7bdbb58266d9e7231cb14c4e61f06f49e94a8c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Obernd=C3=B6rfer?= Date: Sun, 23 Feb 2014 20:52:52 +0100 Subject: [PATCH] Update keyring unit tests --- test/general/keyring.js | 259 +++++++++++++++++++++++++++++++++------- 1 file changed, 219 insertions(+), 40 deletions(-) diff --git a/test/general/keyring.js b/test/general/keyring.js index 6bf24fae..2dcff4ec 100644 --- a/test/general/keyring.js +++ b/test/general/keyring.js @@ -10,7 +10,8 @@ describe("Keyring", function() { var user = 'whiteout.test@t-online.de', passphrase = 'asdf', keySize = 512, - keyId = 'F6F60E9B42CDFF4C', + keyId = 'f6f60e9b42cdff4c', + keyFingerP = '5856cef789c3a307e8a1b976f6f60e9b42cdff4c', pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK-----\n' + 'Version: OpenPGP.js v.1.20131011\n' + 'Comment: http://openpgpjs.org\n' + @@ -37,65 +38,243 @@ describe("Keyring", function() { 'Af9C+kYW1AvNWmivdtr0M0iYCUjM9DNOQH1fcvXqIiN602mWrkd8jcEzLsW5\n' + 'IUNzVPLhrFIuKyBDTpLnC07Loce1\n' + '=ULta\n' + - '-----END PGP PRIVATE KEY BLOCK-----'; + '-----END PGP PRIVATE KEY BLOCK-----', + keyId2 = 'ba993fc2aee18a3a', + keyFingerP2 = '560b7a7f3f9ab516b233b299ba993fc2aee18a3a', + subkeyId2 = 'f47c5210a8cc2740', + subkeyFingerP2 = '2a20c371141e000833848d85f47c5210a8cc2740', + pubkey2 = + ['-----BEGIN PGP PUBLIC KEY BLOCK-----', + 'Version: GnuPG v2.0.22 (GNU/Linux)', + '', + 'mQMuBFLVgdQRCACOlpq0cd1IazNjOEpWPZvx/O3JMbdDs3B3iCG0Mo5OUZ8lpKU5', + 'EslVgTd8IcUU14ZMOO7y91dw0KP4q61b4OIy7oVxzfFfKCC1s0Dc7GTay+qo5afJ', + 'wbWcgTyCIahTRmi5UepU7xdRHRMlqAclO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heAAAoJELqZP8Ku4Yo6Aa0A/1Kz5S8d9czLiDbrhSa/', + 'C1rQ5qiWpFq9UNTFg2P/gASvAP92TzUMLK2my8ew1xXShtrfXked5fkSuFrPlZBs', + 'b4Ta67kCDQRS1YHUEAgAxOKx4y5QD78uPLlgNBHXrcncUNBIt4IXBGjQTxpFcn5j', + 'rSuj+ztvXJQ8wCkx+TTb2yuL5M+nXd7sx4s+M4KZ/MZfI6ZX4lhcoUdAbB9FWiV7', + 'uNntyeFo8qgGM5at/Q0EsyzMSqbeBxk4bpd5MfYGThn0Ae2xaw3X94KaZ3LjtHo2', + 'V27FD+jvmmoAj9b1+zcO/pJ8SuojQmcnS4VDVV+Ba5WPTav0LzDdQXyGMZI9PDxC', + 'jAI2f1HjTuxIt8X8rAQSQdoMIcQRYEjolsXS6iob1eVigyL86hLJjI3VPn6kBCv3', + 'Tb+WXX+9LgSAt9yvv4HMwBLK33k6IH7M72SqQulZywADBQgAt2xVTMjdVyMniMLj', + 'Ed4HbUgwyCPkVkcA4zTXqfKu+dAe4dK5tre0clkXZVtR1V8RDAD0zaVyM030e2zb', + 'zn4cGKDL2dmwk2ZBeXWZDgGKoKvGKYf8PRpTAYweFzol3OUdfXH5SngOylCD4OCL', + 's4RSVkSsllIWqLpnS5IJFgt6PDVcQgGXo2ZhVYkoLNhWTIEBuJWIyc4Vj20YpTms', + 'lgHnjeq5rP6781MwAJQnViyJ2SziGK4/+3CoDiQLO1zId42otXBvsbUuLSL5peX4', + 'v2XNVMLJMY5iSfzbBWczecyapiQ3fbVtWgucgrqlrqM3546v+GdATBhGOu8ppf5j', + '7d1A7ohhBBgRCAAJBQJS1YHUAhsMAAoJELqZP8Ku4Yo6SgoBAIVcZstwz4lyA2et', + 'y61IhKbJCOlQxyem+kepjNapkhKDAQDIDL38bZWU4Rm0nq82Xb4yaI0BCWDcFkHV', + 'og2umGfGng==', + '=v3+L', + '-----END PGP PUBLIC KEY BLOCK-----'].join('\n'); - it('Import key pair', function(done) { + it('Import key pair', function() { // clear any keys already in the keychain keyring.clear(); - keyring.importKey(privkey); - keyring.importKey(pubkey); - done(); + keyring.store(); + keyring.publicKeys.importKey(pubkey); + keyring.publicKeys.importKey(pubkey2); + keyring.privateKeys.importKey(privkey); }); - it('getPublicKeyForAddress() - unknown address', function(done) { - var key = keyring.getPublicKeyForAddress('nobody@example.com'); - expect(key).to.be.empty; - done(); + it('getKeysForId() - unknown id', function() { + var keys = keyring.getKeysForId('000102030405060708'); + expect(keys).to.be.null; }); - it('getPublicKeyForAddress() - valid address', function(done) { - var key = keyring.getPublicKeyForAddress(user); - expect(key).to.exist.and.have.length(1); - done(); + + it('getKeysForId() - valid id', function() { + var keys = keyring.getKeysForId(keyId); + // we get public and private key + expect(keys).to.exist.and.have.length(2); + expect(keys[0].primaryKey.getKeyId().toHex()).equals(keyId); }); - it('getPrivateKeyForAddress() - unknown address', function(done) { - var key = keyring.getPrivateKeyForAddress('nobody@example.com'); - expect(key).to.be.empty; - done(); + + it('publicKeys.getForId() - unknown id', function() { + var key = keyring.publicKeys.getForId('000102030405060708'); + expect(key).to.be.null; }); - it('getPrivateKeyForAddress() - valid address', function(done) { - var key = keyring.getPrivateKeyForAddress(user); - expect(key).to.exist.and.have.length(1); - done(); + + it('publicKeys.getForId() - valid id', function() { + var key = keyring.publicKeys.getForId(keyId); + expect(key).to.exist.and.be.an.instanceof(openpgp.key.Key); + expect(key.primaryKey.getKeyId().toHex()).equals(keyId); }); - it('getKeysForKeyId() - unknown id', function(done) { - var keys = keyring.getKeysForKeyId('000102030405060708'); + + it('privateKeys.getForId() - unknown id', function() { + var key = keyring.privateKeys.getForId('000102030405060708'); + expect(key).to.be.null; + }); + + it('privateKeys.getForId() - valid id', function() { + var key = keyring.privateKeys.getForId(keyId); + expect(key).to.exist.and.be.an.instanceof(openpgp.key.Key); + expect(key.primaryKey.getKeyId().toHex()).equals(keyId); + }); + + it('publicKeys.getForId() - subkey id', function() { + var key = keyring.publicKeys.getForId(subkeyId2); + expect(key).to.be.null; + }); + + it('publicKeys.getForId() - deep, including subkeys - subkey id', function() { + var key = keyring.publicKeys.getForId(subkeyId2, true); + expect(key).to.exist.and.be.an.instanceof(openpgp.key.Key); + expect(key.primaryKey.getKeyId().toHex()).equals(keyId2); + }); + + it('getKeysForLongId() - unknown long id', function() { + var keys = keyring.getKeysForLongId('000102030405060708'); + expect(keys).to.be.null; + }); + + it('getKeysForLongId() - valid long id', function() { + var keys = keyring.getKeysForLongId(keyFingerP2); + expect(keys).to.exist.and.have.length(1); + expect(keys[0].primaryKey.getKeyId().toHex()).equals(keyId2); + }); + + it('publicKeys.getForLongId() - unknown long id', function() { + var key = keyring.publicKeys.getForLongId('000102030405060708'); + expect(key).to.be.null; + }); + + it('publicKeys.getForLongId() - valid long id', function() { + var key = keyring.publicKeys.getForLongId(keyFingerP2); + expect(key).to.exist.and.be.an.instanceof(openpgp.key.Key); + expect(key.primaryKey.getKeyId().toHex()).equals(keyId2); + }); + + it('publicKeys.getForLongId() - subkey long id', function() { + var key = keyring.publicKeys.getForLongId(subkeyFingerP2); + expect(key).to.be.null; + }); + + it('publicKeys.getForLongId() - deep, including subkeys - subkey long id', function() { + var key = keyring.publicKeys.getForLongId(subkeyFingerP2, true); + expect(key).to.exist.and.be.an.instanceof(openpgp.key.Key); + expect(key.primaryKey.getKeyId().toHex()).equals(keyId2); + }); + + it('publicKeys.getForAddress() - unknown address', function() { + var keys = keyring.publicKeys.getForAddress('nobody@example.com'); expect(keys).to.be.empty; - done(); }); - it('getKeysForKeyId() - valid id', function(done) { - var keys = keyring.getKeysForKeyId(keyId.toLowerCase()); + + it('publicKeys.getForAddress() - valid address', function() { + var keys = keyring.publicKeys.getForAddress(user); expect(keys).to.exist.and.have.length(1); - done(); }); - it('store keys in localstorage', function(done){ + + it('privateKeys.getForAddress() - unknown address', function() { + var key = keyring.privateKeys.getForAddress('nobody@example.com'); + expect(key).to.be.empty; + }); + + it('privateKeys.getForAddress() - valid address', function() { + var key = keyring.privateKeys.getForAddress(user); + expect(key).to.exist.and.have.length(1); + }); + + it('store keys in localstorage', function(){ keyring.store(); - done(); }); - it('after loading from localstorage: getKeysForKeyId() - valid id', function(done) { + + it('after loading from localstorage: getKeysForKeyId() - valid id', function() { var keyring = new openpgp.Keyring(), - keys = keyring.getKeysForKeyId(keyId.toLowerCase()); - expect(keys).to.exist.and.have.length(1); - done(); + keys = keyring.getKeysForId(keyId); + // we expect public and private key + expect(keys).to.exist.and.have.length(2); }); + + it('publicKeys.removeForId() - unknown id', function() { + var key = keyring.publicKeys.removeForId('000102030405060708'); + expect(key).to.be.null; + }); + + it('publicKeys.removeForId() - valid id', function() { + var key = keyring.publicKeys.removeForId(keyId); + expect(key).to.exist.and.be.an.instanceof(openpgp.key.Key); + expect(key.primaryKey.getKeyId().toHex()).equals(keyId); + expect(keyring.publicKeys.keys).to.exist.and.have.length(1); + }); + + it('publicKeys.removeForLongId() - unknown id', function() { + var key = keyring.publicKeys.removeForLongId('000102030405060708'); + expect(key).to.be.null; + expect(keyring.publicKeys.keys).to.exist.and.have.length(1); + }); + + it('publicKeys.removeForLongId() - valid id', function() { + var key = keyring.publicKeys.removeForLongId(keyFingerP2); + expect(key).to.exist.and.be.an.instanceof(openpgp.key.Key); + expect(key.primaryKey.getKeyId().toHex()).equals(keyId2); + expect(keyring.publicKeys.keys).to.be.empty; + }); + it('customize localstorage itemname', function() { - var localstore1 = new openpgp.Keyring.localstore('my-custom-name'); - var localstore2 = new openpgp.Keyring.localstore('my-custom-name'); + var localstore1 = new openpgp.Keyring.localstore('my-custom-prefix-'); + var localstore2 = new openpgp.Keyring.localstore('my-custom-prefix-'); var localstore3 = new openpgp.Keyring.localstore(); - localstore3.store([]); + localstore3.storePublic([]); var key = openpgp.key.readArmored(pubkey).keys[0]; - localstore1.store([key]); - expect(localstore2.load()[0].primaryKey.getKeyId().equals(key.primaryKey.getKeyId())).to.be.true; - expect(localstore3.load()).to.have.length(0); + localstore1.storePublic([key]); + expect(localstore2.loadPublic()[0].primaryKey.getKeyId().equals(key.primaryKey.getKeyId())).to.be.true; + expect(localstore3.loadPublic()).to.have.length(0); }); + + it('removeKeysForId() - unknown id', function() { + keyring.publicKeys.importKey(pubkey); + keyring.publicKeys.importKey(pubkey2); + keyring.privateKeys.importKey(privkey); + expect(keyring.publicKeys.keys).to.have.length(2); + expect(keyring.privateKeys.keys).to.have.length(1); + var keys = keyring.removeKeysForId('000102030405060708'); + expect(keys).to.be.null; + expect(keyring.publicKeys.keys).to.have.length(2); + expect(keyring.privateKeys.keys).to.have.length(1); + }); + + it('removeKeysForId() - valid id', function() { + var keys = keyring.removeKeysForId(keyId); + expect(keys).to.have.length(2); + expect(keyring.publicKeys.keys).to.have.length(1); + expect(keyring.privateKeys.keys).to.have.length(0); + }); + + it('removeKeysForLongId() - unknown id', function() { + keyring.publicKeys.importKey(pubkey); + keyring.publicKeys.importKey(pubkey2); + keyring.privateKeys.importKey(privkey); + expect(keyring.publicKeys.keys).to.have.length(2); + expect(keyring.privateKeys.keys).to.have.length(1); + var keys = keyring.removeKeysForLongId('000102030405060708'); + expect(keys).to.be.null; + expect(keyring.publicKeys.keys).to.have.length(2); + expect(keyring.privateKeys.keys).to.have.length(1); + }); + + it('removeKeysForLongId() - valid id', function() { + var keys = keyring.removeKeysForLongId(keyFingerP); + expect(keys).to.have.length(2); + expect(keyring.publicKeys.keys).to.have.length(1); + expect(keyring.privateKeys.keys).to.have.length(0); + }); + });