Test lawnchair dao

Test pgp promise api
This commit is contained in:
Tankred Hase 2014-12-10 14:16:53 +01:00
parent 502c6b7467
commit 7aa0d2cf4a
6 changed files with 182 additions and 202 deletions

View File

@ -43,7 +43,8 @@
"forge", "forge",
"Lawnchair", "Lawnchair",
"_", "_",
"openpgp" "openpgp",
"qMock"
], ],
"globals": { "globals": {

View File

@ -19,25 +19,32 @@ function PGP($q) {
/** /**
* Generate a key pair for the user * Generate a key pair for the user
* @return {Promise}
*/ */
PGP.prototype.generateKeys = function(options) { PGP.prototype.generateKeys = function(options) {
var userId, passphrase; return this._q(function(resolve) {
var userId, passphrase;
if (!util.emailRegEx.test(options.emailAddress) || !options.keySize) { if (!util.emailRegEx.test(options.emailAddress) || !options.keySize) {
return this._q(function(resolve, reject) { throw new Error('Crypto init failed. Not all options set!');
reject(new Error('Crypto init failed. Not all options set!')); }
// generate keypair
userId = 'Whiteout User <' + options.emailAddress + '>';
passphrase = (options.passphrase) ? options.passphrase : undefined;
resolve({
userId: userId,
passphrase: passphrase
}); });
}
// generate keypair }).then(function(res) {
userId = 'Whiteout User <' + options.emailAddress + '>'; return openpgp.generateKeyPair({
passphrase = (options.passphrase) ? options.passphrase : undefined; keyType: 1, // (keytype 1=RSA)
numBits: options.keySize,
return openpgp.generateKeyPair({ userId: res.userId,
keyType: 1, // (keytype 1=RSA) passphrase: res.passphrase
numBits: options.keySize, });
userId: userId,
passphrase: passphrase
}).then(function(keys) { }).then(function(keys) {
return { return {
keyId: keys.key.primaryKey.getKeyId().toHex().toUpperCase(), keyId: keys.key.primaryKey.getKeyId().toHex().toUpperCase(),
@ -145,16 +152,16 @@ PGP.prototype.extractPublicKey = function(privateKeyArmored) {
/** /**
* Import the user's key pair * Import the user's key pair
* @return {Promise}
*/ */
PGP.prototype.importKeys = function(options) { PGP.prototype.importKeys = function(options) {
var self = this; var self = this;
return self._q(function(resolve, reject) { return self._q(function(resolve) {
var pubKeyId, privKeyId; var pubKeyId, privKeyId;
// check options // check options
if (!options.privateKeyArmored || !options.publicKeyArmored) { if (!options.privateKeyArmored || !options.publicKeyArmored) {
reject(new Error('Importing keys failed. Not all options set!')); throw new Error('Importing keys failed. Not all options set!');
return;
} }
function resetKeys() { function resetKeys() {
@ -168,15 +175,13 @@ PGP.prototype.importKeys = function(options) {
self._privateKey = openpgp.key.readArmored(options.privateKeyArmored).keys[0]; self._privateKey = openpgp.key.readArmored(options.privateKeyArmored).keys[0];
} catch (e) { } catch (e) {
resetKeys(); resetKeys();
reject(new Error('Importing keys failed. Parsing error!')); throw new Error('Importing keys failed. Parsing error!');
return;
} }
// decrypt private key with passphrase // decrypt private key with passphrase
if (!self._privateKey.decrypt(options.passphrase)) { if (!self._privateKey.decrypt(options.passphrase)) {
resetKeys(); resetKeys();
reject(new Error('Incorrect passphrase!')); throw new Error('Incorrect passphrase!');
return;
} }
// check if keys have the same id // check if keys have the same id
@ -184,8 +189,7 @@ PGP.prototype.importKeys = function(options) {
privKeyId = self._privateKey.primaryKey.getKeyId().toHex(); privKeyId = self._privateKey.primaryKey.getKeyId().toHex();
if (!pubKeyId || !privKeyId || pubKeyId !== privKeyId) { if (!pubKeyId || !privKeyId || pubKeyId !== privKeyId) {
resetKeys(); resetKeys();
reject(new Error('Key IDs dont match!')); throw new Error('Key IDs dont match!');
return;
} }
resolve(); resolve();
@ -194,13 +198,13 @@ PGP.prototype.importKeys = function(options) {
/** /**
* Export the user's key pair * Export the user's key pair
* @return {Promise}
*/ */
PGP.prototype.exportKeys = function() { PGP.prototype.exportKeys = function() {
var self = this; var self = this;
return self._q(function(resolve, reject) { return self._q(function(resolve) {
if (!self._publicKey || !self._privateKey) { if (!self._publicKey || !self._privateKey) {
reject(new Error('Could not export keys!')); throw new Error('Could not export keys!');
return;
} }
resolve({ resolve({
@ -213,37 +217,34 @@ PGP.prototype.exportKeys = function() {
/** /**
* Change the passphrase of an ascii armored private key. * Change the passphrase of an ascii armored private key.
* @return {Promise}
*/ */
PGP.prototype.changePassphrase = function(options) { PGP.prototype.changePassphrase = function(options) {
return this._q(function(resolve, reject) { return this._q(function(resolve) {
var privKey, packets, newPassphrase, newKeyArmored; var privKey, packets, newPassphrase, newKeyArmored;
// set undefined instead of empty string as passphrase // set undefined instead of empty string as passphrase
newPassphrase = (options.newPassphrase) ? options.newPassphrase : undefined; newPassphrase = (options.newPassphrase) ? options.newPassphrase : undefined;
if (!options.privateKeyArmored) { if (!options.privateKeyArmored) {
reject(new Error('Private key must be specified to change passphrase!')); throw new Error('Private key must be specified to change passphrase!');
return;
} }
if (options.oldPassphrase === newPassphrase || if (options.oldPassphrase === newPassphrase ||
(!options.oldPassphrase && !newPassphrase)) { (!options.oldPassphrase && !newPassphrase)) {
reject(new Error('New and old passphrase are the same!')); throw new Error('New and old passphrase are the same!');
return;
} }
// read armored key // read armored key
try { try {
privKey = openpgp.key.readArmored(options.privateKeyArmored).keys[0]; privKey = openpgp.key.readArmored(options.privateKeyArmored).keys[0];
} catch (e) { } catch (e) {
reject(new Error('Importing key failed. Parsing error!')); throw new Error('Importing key failed. Parsing error!');
return;
} }
// decrypt private key with passphrase // decrypt private key with passphrase
if (!privKey.decrypt(options.oldPassphrase)) { if (!privKey.decrypt(options.oldPassphrase)) {
reject(new Error('Old passphrase incorrect!')); throw new Error('Old passphrase incorrect!');
return;
} }
// encrypt key with new passphrase // encrypt key with new passphrase
@ -254,14 +255,12 @@ PGP.prototype.changePassphrase = function(options) {
} }
newKeyArmored = privKey.armor(); newKeyArmored = privKey.armor();
} catch (e) { } catch (e) {
reject(new Error('Setting new passphrase failed!')); throw new Error('Setting new passphrase failed!');
return;
} }
// check if new passphrase really works // check if new passphrase really works
if (!privKey.decrypt(newPassphrase)) { if (!privKey.decrypt(newPassphrase)) {
reject(new Error('Decrypting key with new passphrase failed!')); throw new Error('Decrypting key with new passphrase failed!');
return;
} }
resolve(newKeyArmored); resolve(newKeyArmored);
@ -270,16 +269,16 @@ PGP.prototype.changePassphrase = function(options) {
/** /**
* Encrypt and sign a pgp message for a list of receivers * Encrypt and sign a pgp message for a list of receivers
* @return {Promise}
*/ */
PGP.prototype.encrypt = function(plaintext, publicKeysArmored) { PGP.prototype.encrypt = function(plaintext, publicKeysArmored) {
var self = this; var self = this;
return self._q(function(resolve, reject) { return self._q(function(resolve) {
var publicKeys; var publicKeys;
// check keys // check keys
if (!self._privateKey) { if (!self._privateKey) {
reject(new Error('Error encrypting. Keys must be set!')); throw new Error('Error encrypting. Keys must be set!');
return;
} }
// parse armored public keys // parse armored public keys
try { try {
@ -290,8 +289,7 @@ PGP.prototype.encrypt = function(plaintext, publicKeysArmored) {
}); });
} }
} catch (err) { } catch (err) {
reject(new Error('Error encrypting plaintext!')); throw new Error('Error encrypting plaintext!');
return;
} }
resolve(publicKeys); resolve(publicKeys);
@ -311,16 +309,16 @@ PGP.prototype.encrypt = function(plaintext, publicKeysArmored) {
* @param {String} ciphertext The encrypted PGP message block * @param {String} ciphertext The encrypted PGP message block
* @param {String} publicKeyArmored The public key used to sign the message * @param {String} publicKeyArmored The public key used to sign the message
* @param {Function} callback(error, plaintext, signaturesValid) signaturesValid is undefined in case there are no signature, null in case there are signatures but the wrong public key or no key was used to verify, true if the signature was successfully verified, or false if the signataure verification failed. * @param {Function} callback(error, plaintext, signaturesValid) signaturesValid is undefined in case there are no signature, null in case there are signatures but the wrong public key or no key was used to verify, true if the signature was successfully verified, or false if the signataure verification failed.
* @return {Promise}
*/ */
PGP.prototype.decrypt = function(ciphertext, publicKeyArmored) { PGP.prototype.decrypt = function(ciphertext, publicKeyArmored) {
var self = this; var self = this;
return self._q(function(resolve, reject) { return self._q(function(resolve) {
var publicKeys, message; var publicKeys, message;
// check keys // check keys
if (!self._privateKey) { if (!self._privateKey) {
reject(new Error('Error decrypting. Keys must be set!')); throw new Error('Error decrypting. Keys must be set!');
return;
} }
// read keys and ciphertext message // read keys and ciphertext message
try { try {
@ -333,14 +331,16 @@ PGP.prototype.decrypt = function(ciphertext, publicKeyArmored) {
} }
message = openpgp.message.readArmored(ciphertext); message = openpgp.message.readArmored(ciphertext);
} catch (err) { } catch (err) {
reject(new Error('Error parsing encrypted PGP message!')); throw new Error('Error parsing encrypted PGP message!');
return;
} }
resolve(publicKeys, message); resolve({
publicKeys: publicKeys,
message: message
});
}).then(function(publicKeys, message) { }).then(function(res) {
// decrypt and verify pgp message // decrypt and verify pgp message
return openpgp.decryptAndVerifyMessage(self._privateKey, publicKeys, message); return openpgp.decryptAndVerifyMessage(self._privateKey, res.publicKeys, res.message);
}).then(function(decrypted) { }).then(function(decrypted) {
// return decrypted plaintext // return decrypted plaintext
return { return {
@ -355,16 +355,16 @@ PGP.prototype.decrypt = function(ciphertext, publicKeyArmored) {
* @param {String} clearSignedText The clearsigned text, usually from a signed pgp/inline message * @param {String} clearSignedText The clearsigned text, usually from a signed pgp/inline message
* @param {String} publicKeyArmored The public key used to signed the message * @param {String} publicKeyArmored The public key used to signed the message
* @param {Function} callback(error, signaturesValid) signaturesValid is undefined in case there are no signature, null in case there are signatures but the wrong public key or no key was used to verify, true if the signature was successfully verified, or false if the signataure verification failed. * @param {Function} callback(error, signaturesValid) signaturesValid is undefined in case there are no signature, null in case there are signatures but the wrong public key or no key was used to verify, true if the signature was successfully verified, or false if the signataure verification failed.
* @return {Promise}
*/ */
PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmored) { PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmored) {
var self = this; var self = this;
return self._q(function(resolve, reject) { return self._q(function(resolve) {
var publicKeys, message; var publicKeys, message;
// check keys // check keys
if (!self._privateKey) { if (!self._privateKey) {
reject(new Error('Error verifying signed PGP message. Keys must be set!')); throw new Error('Error verifying signed PGP message. Keys must be set!');
return;
} }
// read keys and ciphertext message // read keys and ciphertext message
try { try {
@ -377,13 +377,15 @@ PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmo
} }
message = openpgp.cleartext.readArmored(clearSignedText); message = openpgp.cleartext.readArmored(clearSignedText);
} catch (err) { } catch (err) {
reject(new Error('Error verifying signed PGP message!')); throw new Error('Error verifying signed PGP message!');
return;
} }
resolve(publicKeys, message); resolve({
publicKeys: publicKeys,
message: message
});
}).then(function(publicKeys, message) { }).then(function(res) {
return openpgp.verifyClearSignedMessage(publicKeys, message); return openpgp.verifyClearSignedMessage(res.publicKeys, res.message);
}).then(function(result) { }).then(function(result) {
return checkSignatureValidity(result.signatures); return checkSignatureValidity(result.signatures);
}); });
@ -395,16 +397,16 @@ PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmo
* @param {String} pgpSignature The detached signature, usually from a signed pgp/mime message * @param {String} pgpSignature The detached signature, usually from a signed pgp/mime message
* @param {String} publicKeyArmored The public key used to signed the message * @param {String} publicKeyArmored The public key used to signed the message
* @param {Function} callback(error, signaturesValid) signaturesValid is undefined in case there are no signature, null in case there are signatures but the wrong public key or no key was used to verify, true if the signature was successfully verified, or false if the signataure verification failed. * @param {Function} callback(error, signaturesValid) signaturesValid is undefined in case there are no signature, null in case there are signatures but the wrong public key or no key was used to verify, true if the signature was successfully verified, or false if the signataure verification failed.
* @return {Promise}
*/ */
PGP.prototype.verifySignedMessage = function(message, pgpSignature, publicKeyArmored) { PGP.prototype.verifySignedMessage = function(message, pgpSignature, publicKeyArmored) {
var self = this; var self = this;
return self._q(function(resolve, reject) { return self._q(function(resolve) {
var publicKeys, signatures; var publicKeys, signatures;
// check keys // check keys
if (!self._privateKey) { if (!self._privateKey) {
reject(new Error('Error verifying signed PGP message. Keys must be set!')); throw new Error('Error verifying signed PGP message. Keys must be set!');
return;
} }
// read keys and ciphertext message // read keys and ciphertext message
try { try {
@ -416,16 +418,14 @@ PGP.prototype.verifySignedMessage = function(message, pgpSignature, publicKeyArm
publicKeys = [self._publicKey]; publicKeys = [self._publicKey];
} }
} catch (err) { } catch (err) {
reject(new Error('Error verifying signed PGP message!')); throw new Error('Error verifying signed PGP message!');
return;
} }
// check signatures // check signatures
try { try {
var msg = openpgp.message.readSignedContent(message, pgpSignature); var msg = openpgp.message.readSignedContent(message, pgpSignature);
signatures = msg.verify(publicKeys); signatures = msg.verify(publicKeys);
} catch (err) { } catch (err) {
reject(new Error('Error verifying signed PGP message!')); throw new Error('Error verifying signed PGP message!');
return;
} }
resolve(checkSignatureValidity(signatures)); resolve(checkSignatureValidity(signatures));

View File

@ -15,23 +15,30 @@ function LawnchairDAO($q) {
/** /**
* Initialize the lawnchair database * Initialize the lawnchair database
* @param {String} dbName The name of the database * @param {String} dbName The name of the database
* @return {Promise}
*/ */
LawnchairDAO.prototype.init = function(dbName, callback) { LawnchairDAO.prototype.init = function(dbName) {
var self = this; var self = this;
return self._q(function(resolve, reject) {
if (!dbName) {
reject(new Error('Lawnchair DB name must be specified!'));
}
if (!dbName) { self._db = new Lawnchair({
return callback(new Error('Lawnchair DB name must be specified!')); name: dbName
} }, function(success) {
if (success) {
self._db = new Lawnchair({ resolve();
name: dbName } else {
}, function(success) { reject(new Error('Lawnchair initialization ' + dbName + ' failed!'));
callback(success ? undefined : new Error('Lawnchair initialization ' + dbName + ' failed!')); }
});
}); });
}; };
/** /**
* Create or update an object * Create or update an object
* @return {Promise}
*/ */
LawnchairDAO.prototype.persist = function(key, object) { LawnchairDAO.prototype.persist = function(key, object) {
var self = this; var self = this;
@ -57,6 +64,7 @@ LawnchairDAO.prototype.persist = function(key, object) {
/** /**
* Persist a bunch of items at once * Persist a bunch of items at once
* @return {Promise}
*/ */
LawnchairDAO.prototype.batch = function(list) { LawnchairDAO.prototype.batch = function(list) {
var self = this; var self = this;
@ -79,6 +87,7 @@ LawnchairDAO.prototype.batch = function(list) {
/** /**
* Read a single item by its key * Read a single item by its key
* @return {Promise}
*/ */
LawnchairDAO.prototype.read = function(key) { LawnchairDAO.prototype.read = function(key) {
var self = this; var self = this;
@ -103,6 +112,7 @@ LawnchairDAO.prototype.read = function(key) {
* @param type [String] The type of item e.g. 'email' * @param type [String] The type of item e.g. 'email'
* @param offset [Number] The offset of items to fetch (0 is the last stored item) * @param offset [Number] The offset of items to fetch (0 is the last stored item)
* @param num [Number] The number of items to fetch (null means fetch all) * @param num [Number] The number of items to fetch (null means fetch all)
* @return {Promise}
*/ */
LawnchairDAO.prototype.list = function(type, offset, num) { LawnchairDAO.prototype.list = function(type, offset, num) {
var self = this; var self = this;
@ -162,6 +172,7 @@ LawnchairDAO.prototype.list = function(type, offset, num) {
/** /**
* Removes an object liter from local storage by its key (delete) * Removes an object liter from local storage by its key (delete)
* @return {Promise}
*/ */
LawnchairDAO.prototype.remove = function(key) { LawnchairDAO.prototype.remove = function(key) {
var self = this; var self = this;
@ -178,6 +189,7 @@ LawnchairDAO.prototype.remove = function(key) {
/** /**
* Removes an object liter from local storage by its key (delete) * Removes an object liter from local storage by its key (delete)
* @return {Promise}
*/ */
LawnchairDAO.prototype.removeList = function(type) { LawnchairDAO.prototype.removeList = function(type) {
var self = this; var self = this;
@ -216,6 +228,7 @@ LawnchairDAO.prototype.removeList = function(type) {
/** /**
* Clears the whole local storage cache * Clears the whole local storage cache
* @return {Promise}
*/ */
LawnchairDAO.prototype.clear = function() { LawnchairDAO.prototype.clear = function() {
var self = this; var self = this;

View File

@ -43,4 +43,12 @@ require('../src/js/app-config');
require('../src/js/util'); require('../src/js/util');
require('../src/js/crypto'); require('../src/js/crypto');
require('../src/js/service'); require('../src/js/service');
require('../src/js/email'); require('../src/js/email');
//
// Global mocks
//
window.qMock = function(res, rej) {
return new Promise(res, rej);
};

View File

@ -39,7 +39,7 @@ describe('PGP Crypto Api unit tests', function() {
'-----END PGP PRIVATE KEY BLOCK-----\r\n'; '-----END PGP PRIVATE KEY BLOCK-----\r\n';
beforeEach(function() { beforeEach(function() {
pgp = new PGP(); pgp = new PGP(qMock);
}); });
afterEach(function() {}); afterEach(function() {});
@ -50,9 +50,8 @@ describe('PGP Crypto Api unit tests', function() {
emailAddress: 'whiteout.test@t-onlinede', emailAddress: 'whiteout.test@t-onlinede',
keySize: keySize, keySize: keySize,
passphrase: passphrase passphrase: passphrase
}, function(err, keys) { }).catch(function(err) {
expect(err).to.exist; expect(err.message).to.match(/options/);
expect(keys).to.not.exist;
done(); done();
}); });
}); });
@ -61,76 +60,69 @@ describe('PGP Crypto Api unit tests', function() {
emailAddress: 'whiteout.testt-online.de', emailAddress: 'whiteout.testt-online.de',
keySize: keySize, keySize: keySize,
passphrase: passphrase passphrase: passphrase
}, function(err, keys) { }).catch(function(err) {
expect(err).to.exist; expect(err.message).to.match(/options/);
expect(keys).to.not.exist;
done(); done();
}); });
}); });
it('should work with passphrase', function(done) { it('should work with passphrase', function(done) {
var keyObject;
pgp.generateKeys({ pgp.generateKeys({
emailAddress: user, emailAddress: user,
keySize: keySize, keySize: keySize,
passphrase: passphrase passphrase: passphrase
}, function(err, keys) { }).then(function(keys) {
expect(err).to.not.exist;
expect(keys.keyId).to.exist; expect(keys.keyId).to.exist;
expect(keys.privateKeyArmored).to.exist; expect(keys.privateKeyArmored).to.exist;
expect(keys.publicKeyArmored).to.exist; expect(keys.publicKeyArmored).to.exist;
keyObject = keys;
// test encrypt/decrypt // test encrypt/decrypt
pgp.importKeys({ return pgp.importKeys({
passphrase: passphrase, passphrase: passphrase,
privateKeyArmored: keys.privateKeyArmored, privateKeyArmored: keys.privateKeyArmored,
publicKeyArmored: keys.publicKeyArmored publicKeyArmored: keys.publicKeyArmored
}, function(err) {
expect(err).to.not.exist;
pgp.encrypt('secret', [keys.publicKeyArmored], function(err, ct) {
expect(err).to.not.exist;
expect(ct).to.exist;
pgp.decrypt(ct, keys.publicKeyArmored, function(err, pt, signValid) {
expect(err).to.not.exist;
expect(pt).to.equal('secret');
expect(signValid).to.be.true;
done();
});
});
}); });
}).then(function() {
return pgp.encrypt('secret', [keyObject.publicKeyArmored]);
}).then(function(ct) {
expect(ct).to.exist;
return pgp.decrypt(ct, keyObject.publicKeyArmored);
}).then(function(pt) {
expect(pt.decrypted).to.equal('secret');
expect(pt.signaturesValid).to.be.true;
done();
}); });
}); });
it('should work without passphrase', function(done) { it('should work without passphrase', function(done) {
var keyObject;
pgp.generateKeys({ pgp.generateKeys({
emailAddress: user, emailAddress: user,
keySize: keySize, keySize: keySize,
passphrase: '' passphrase: ''
}, function(err, keys) { }).then(function(keys) {
expect(err).to.not.exist;
expect(keys.keyId).to.exist; expect(keys.keyId).to.exist;
expect(keys.privateKeyArmored).to.exist; expect(keys.privateKeyArmored).to.exist;
expect(keys.publicKeyArmored).to.exist; expect(keys.publicKeyArmored).to.exist;
keyObject = keys;
// test encrypt/decrypt // test encrypt/decrypt
pgp.importKeys({ return pgp.importKeys({
passphrase: undefined, passphrase: passphrase,
privateKeyArmored: keys.privateKeyArmored, privateKeyArmored: keys.privateKeyArmored,
publicKeyArmored: keys.publicKeyArmored publicKeyArmored: keys.publicKeyArmored
}, function(err) {
expect(err).to.not.exist;
pgp.encrypt('secret', [keys.publicKeyArmored], function(err, ct) {
expect(err).to.not.exist;
expect(ct).to.exist;
pgp.decrypt(ct, keys.publicKeyArmored, function(err, pt, signValid) {
expect(err).to.not.exist;
expect(pt).to.equal('secret');
expect(signValid).to.be.true;
done();
});
});
}); });
}).then(function() {
return pgp.encrypt('secret', [keyObject.publicKeyArmored]);
}).then(function(ct) {
expect(ct).to.exist;
return pgp.decrypt(ct, keyObject.publicKeyArmored);
}).then(function(pt) {
expect(pt.decrypted).to.equal('secret');
expect(pt.signaturesValid).to.be.true;
done();
}); });
}); });
}); });
@ -141,15 +133,13 @@ describe('PGP Crypto Api unit tests', function() {
passphrase: 'asd', passphrase: 'asd',
privateKeyArmored: privkey, privateKeyArmored: privkey,
publicKeyArmored: pubkey publicKeyArmored: pubkey
}, function(err) { }).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(err.message).to.equal('Incorrect passphrase!'); expect(err.message).to.equal('Incorrect passphrase!');
return pgp.exportKeys();
pgp.exportKeys(function(err, keys) { }).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(keys).to.not.exist; done();
done();
});
}); });
}); });
it('should work', function(done) { it('should work', function(done) {
@ -157,16 +147,13 @@ describe('PGP Crypto Api unit tests', function() {
passphrase: passphrase, passphrase: passphrase,
privateKeyArmored: privkey, privateKeyArmored: privkey,
publicKeyArmored: pubkey publicKeyArmored: pubkey
}, function(err) { }).then(function() {
expect(err).to.not.exist; return pgp.exportKeys();
}).then(function(keys) {
pgp.exportKeys(function(err, keys) { expect(keys.keyId).to.equal(keyId);
expect(err).to.not.exist; expect(keys.privateKeyArmored.replace(/\r/g, '')).to.equal(privkey.replace(/\r/g, ''));
expect(keys.keyId).to.equal(keyId); expect(keys.publicKeyArmored.replace(/\r/g, '')).to.equal(pubkey.replace(/\r/g, ''));
expect(keys.privateKeyArmored.replace(/\r/g, '')).to.equal(privkey.replace(/\r/g, '')); done();
expect(keys.publicKeyArmored.replace(/\r/g, '')).to.equal(pubkey.replace(/\r/g, ''));
done();
});
}); });
}); });
}); });
@ -177,47 +164,38 @@ describe('PGP Crypto Api unit tests', function() {
privateKeyArmored: privkey, privateKeyArmored: privkey,
oldPassphrase: passphrase, oldPassphrase: passphrase,
newPassphrase: 'yxcv' newPassphrase: 'yxcv'
}, function(err, reEncryptedKey) { }).then(function(reEncryptedKey) {
expect(err).to.not.exist;
expect(reEncryptedKey).to.exist; expect(reEncryptedKey).to.exist;
pgp.importKeys({ return pgp.importKeys({
passphrase: 'yxcv', passphrase: 'yxcv',
privateKeyArmored: reEncryptedKey, privateKeyArmored: reEncryptedKey,
publicKeyArmored: pubkey publicKeyArmored: pubkey
}, function(err) {
expect(err).to.not.exist;
done();
}); });
}); }).then(done);
}); });
it('should work with empty passphrase', function(done) { it('should work with empty passphrase', function(done) {
pgp.changePassphrase({ pgp.changePassphrase({
privateKeyArmored: privkey, privateKeyArmored: privkey,
oldPassphrase: passphrase, oldPassphrase: passphrase,
newPassphrase: undefined newPassphrase: undefined
}, function(err, reEncryptedKey) { }).then(function(reEncryptedKey) {
expect(err).to.not.exist;
expect(reEncryptedKey).to.exist; expect(reEncryptedKey).to.exist;
pgp.importKeys({ return pgp.importKeys({
passphrase: undefined, passphrase: undefined,
privateKeyArmored: reEncryptedKey, privateKeyArmored: reEncryptedKey,
publicKeyArmored: pubkey publicKeyArmored: pubkey
}, function(err) {
expect(err).to.not.exist;
done();
}); });
}); }).then(done);
}); });
it('should fail when passphrases are equal', function(done) { it('should fail when passphrases are equal', function(done) {
pgp.changePassphrase({ pgp.changePassphrase({
privateKeyArmored: privkey, privateKeyArmored: privkey,
oldPassphrase: passphrase, oldPassphrase: passphrase,
newPassphrase: passphrase newPassphrase: passphrase
}, function(err, reEncryptedKey) { }).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(reEncryptedKey).to.not.exist;
done(); done();
}); });
}); });
@ -226,9 +204,8 @@ describe('PGP Crypto Api unit tests', function() {
privateKeyArmored: privkey, privateKeyArmored: privkey,
oldPassphrase: 'asd', oldPassphrase: 'asd',
newPassphrase: 'yxcv' newPassphrase: 'yxcv'
}, function(err, reEncryptedKey) { }).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(reEncryptedKey).to.not.exist;
done(); done();
}); });
}); });
@ -247,10 +224,7 @@ describe('PGP Crypto Api unit tests', function() {
passphrase: passphrase, passphrase: passphrase,
privateKeyArmored: privkey, privateKeyArmored: privkey,
publicKeyArmored: pubkey publicKeyArmored: pubkey
}, function(err) { }).then(done);
expect(err).to.not.exist;
done();
});
}); });
describe('Get KeyId', function() { describe('Get KeyId', function() {
@ -311,22 +285,19 @@ describe('PGP Crypto Api unit tests', function() {
it('should fail', function(done) { it('should fail', function(done) {
var input = null; var input = null;
pgp.encrypt(input, [pubkey], function(err, ct) { pgp.encrypt(input, [pubkey]).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(ct).to.not.exist;
done(); done();
}); });
}); });
it('should work', function(done) { it('should work', function(done) {
pgp.encrypt(message, [pubkey], function(err, ct) { pgp.encrypt(message, [pubkey]).then(function(ct) {
expect(err).to.not.exist;
expect(ct).to.exist; expect(ct).to.exist;
done(); done();
}); });
}); });
it('should encrypt to myself if public keys are empty', function(done) { it('should encrypt to myself if public keys are empty', function(done) {
pgp.encrypt(message, undefined, function(err, ct) { pgp.encrypt(message, undefined).then(function(ct) {
expect(err).to.not.exist;
expect(ct).to.exist; expect(ct).to.exist;
done(); done();
}); });
@ -337,8 +308,7 @@ describe('PGP Crypto Api unit tests', function() {
var ciphertext; var ciphertext;
beforeEach(function(done) { beforeEach(function(done) {
pgp.encrypt(message, [pubkey], function(err, ct) { pgp.encrypt(message, [pubkey]).then(function(ct) {
expect(err).to.not.exist;
expect(ct).to.exist; expect(ct).to.exist;
ciphertext = ct; ciphertext = ct;
done(); done();
@ -348,45 +318,40 @@ describe('PGP Crypto Api unit tests', function() {
it('should fail', function(done) { it('should fail', function(done) {
var input = 'asdfa\rsdf'; var input = 'asdfa\rsdf';
pgp.decrypt(input, pubkey, function(err, pt) { pgp.decrypt(input, pubkey).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(pt).to.not.exist;
done(); done();
}); });
}); });
it('should work', function(done) { it('should work', function(done) {
pgp.decrypt(ciphertext, pubkey, function(err, pt, signValid) { pgp.decrypt(ciphertext, pubkey).then(function(pt) {
expect(err).to.not.exist; expect(pt.decrypted).to.equal(message);
expect(pt).to.equal(message); expect(pt.signaturesValid).to.be.true;
expect(signValid).to.be.true;
done(); done();
}); });
}); });
it('should work without signature', function(done) { it('should work without signature', function(done) {
openpgp.encryptMessage([pgp._publicKey], message).then(function(ct) { openpgp.encryptMessage([pgp._publicKey], message).then(function(ct) {
pgp.decrypt(ct, undefined, function(err, pt, signValid) { return pgp.decrypt(ct, undefined);
expect(err).to.not.exist; }).then(function(pt) {
expect(pt).to.equal(message); expect(pt.decrypted).to.equal(message);
expect(signValid).to.be.undefined; expect(pt.signaturesValid).to.be.undefined;
done(); done();
});
}); });
}); });
it('should fail to verify if public keys are empty', function(done) { it('should fail to verify if public keys are empty', function(done) {
// setup another public key so that signature verification fails // setup another public key so that signature verification fails
pgp._publicKey = openpgp.key.readArmored(wrongPubkey).keys[0]; pgp._publicKey = openpgp.key.readArmored(wrongPubkey).keys[0];
pgp.decrypt(ciphertext, undefined, function(err, pt, signValid) { pgp.decrypt(ciphertext, undefined).then(function(pt) {
expect(err).to.not.exist; expect(pt.decrypted).to.equal(message);
expect(pt).to.equal(message); expect(pt.signaturesValid).to.be.null;
expect(signValid).to.be.null;
done(); done();
}); });
}); });
it('should decrypt but signValid should be null for wrong public key', function(done) { it('should decrypt but signValid should be null for wrong public key', function(done) {
pgp.decrypt(ciphertext, wrongPubkey, function(err, pt, signValid) { pgp.decrypt(ciphertext, wrongPubkey).then(function(pt) {
expect(err).to.not.exist; expect(pt.decrypted).to.equal(message);
expect(pt).to.equal(message); expect(pt.signaturesValid).to.be.null;
expect(signValid).to.be.null;
done(); done();
}); });
}); });
@ -403,23 +368,20 @@ describe('PGP Crypto Api unit tests', function() {
}); });
it('should work', function(done) { it('should work', function(done) {
pgp.verifyClearSignedMessage(clearsigned, pubkey, function(err, signaturesValid) { pgp.verifyClearSignedMessage(clearsigned, pubkey).then(function(signaturesValid) {
expect(err).to.not.exist;
expect(signaturesValid).to.be.true; expect(signaturesValid).to.be.true;
done(); done();
}); });
}); });
it('should fail', function(done) { it('should fail', function(done) {
pgp.verifyClearSignedMessage(clearsigned.replace('clearsigned', 'invalid'), pubkey, function(err, signaturesValid) { pgp.verifyClearSignedMessage(clearsigned.replace('clearsigned', 'invalid'), pubkey).then(function(signaturesValid) {
expect(err).to.not.exist;
expect(signaturesValid).to.be.false; expect(signaturesValid).to.be.false;
done(); done();
}); });
}); });
it('should be null for wrong public key', function(done) { it('should be null for wrong public key', function(done) {
pgp.verifyClearSignedMessage(clearsigned, wrongPubkey, function(err, signaturesValid) { pgp.verifyClearSignedMessage(clearsigned, wrongPubkey).then(function(signaturesValid) {
expect(err).to.not.exist;
expect(signaturesValid).to.be.null; expect(signaturesValid).to.be.null;
done(); done();
}); });
@ -439,23 +401,20 @@ describe('PGP Crypto Api unit tests', function() {
}); });
it('should work', function(done) { it('should work', function(done) {
pgp.verifySignedMessage(signedMessage, signature, pubkey, function(err, signaturesValid) { pgp.verifySignedMessage(signedMessage, signature, pubkey).then(function(signaturesValid) {
expect(err).to.not.exist;
expect(signaturesValid).to.be.true; expect(signaturesValid).to.be.true;
done(); done();
}); });
}); });
it('should fail', function(done) { it('should fail', function(done) {
pgp.verifySignedMessage(signedMessage.replace('signed', 'invalid'), signature, pubkey, function(err, signaturesValid) { pgp.verifySignedMessage(signedMessage.replace('signed', 'invalid'), signature, pubkey).then(function(signaturesValid) {
expect(err).to.not.exist;
expect(signaturesValid).to.be.false; expect(signaturesValid).to.be.false;
done(); done();
}); });
}); });
it('should be null for wrong public key', function(done) { it('should be null for wrong public key', function(done) {
pgp.verifySignedMessage(signedMessage, signature, wrongPubkey, function(err, signaturesValid) { pgp.verifySignedMessage(signedMessage, signature, wrongPubkey).then(function(signaturesValid) {
expect(err).to.not.exist;
expect(signaturesValid).to.be.null; expect(signaturesValid).to.be.null;
done(); done();
}); });

View File

@ -21,9 +21,8 @@ describe('Lawnchair DAO unit tests', function() {
var lawnchairDao; var lawnchairDao;
beforeEach(function(done) { beforeEach(function(done) {
lawnchairDao = new LawnchairDAO(); lawnchairDao = new LawnchairDAO(qMock);
lawnchairDao.init(dbName, function(err) { lawnchairDao.init(dbName).then(function() {
expect(err).to.not.exist;
expect(lawnchairDao._db).to.exist; expect(lawnchairDao._db).to.exist;
done(); done();
}); });