2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
var Auth = require('../../../src/js/service/auth'),
|
|
|
|
PrivateKey = require('../../../src/js/service/privatekey'),
|
|
|
|
PGP = require('../../../src/js/crypto/pgp'),
|
|
|
|
Crypto = require('../../../src/js/crypto/crypto'),
|
|
|
|
axe = require('axe-logger'),
|
|
|
|
appConfig = require('../../../src/js/app-config'),
|
|
|
|
util = require('crypto-lib').util,
|
|
|
|
Mailbuild = require('mailbuild'),
|
|
|
|
mailreader = require('mailreader'),
|
|
|
|
ImapClient = require('imap-client');
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('Private Key DAO unit tests', function() {
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
var privkeyDao, authStub, pgpStub, cryptoStub, imapClientStub,
|
2014-10-07 14:32:23 -04:00
|
|
|
emailAddress = 'test@example.com',
|
2015-02-27 14:04:44 -05:00
|
|
|
keyId = '12345',
|
|
|
|
salt = util.random(appConfig.config.symKeySize),
|
|
|
|
iv = util.random(appConfig.config.symIvSize),
|
|
|
|
encryptedPrivateKey = util.random(1024 * 8);
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
2015-02-27 14:04:44 -05:00
|
|
|
authStub = sinon.createStubInstance(Auth);
|
|
|
|
authStub.emailAddress = emailAddress;
|
|
|
|
pgpStub = sinon.createStubInstance(PGP);
|
|
|
|
cryptoStub = sinon.createStubInstance(Crypto);
|
|
|
|
privkeyDao = new PrivateKey(authStub, Mailbuild, mailreader, appConfig, pgpStub, cryptoStub, axe);
|
|
|
|
imapClientStub = sinon.createStubInstance(ImapClient);
|
|
|
|
privkeyDao._imap = imapClientStub;
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
afterEach(function() {});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
describe('destroy', function() {
|
|
|
|
it('should work', function(done) {
|
|
|
|
privkeyDao.destroy().then(function() {
|
2015-04-01 08:24:46 -04:00
|
|
|
expect(imapClientStub.logout.calledOnce).to.be.true;
|
2015-02-27 14:04:44 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('encrypt', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to invalid args', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.encrypt().catch(function(err) {
|
|
|
|
expect(err.message).to.match(/Incomplete/);
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
pgpStub.exportKeys.returns(resolves({
|
|
|
|
keyId: keyId,
|
|
|
|
privateKeyArmored: 'PGP BLOCK'
|
2014-12-11 13:07:04 -05:00
|
|
|
}));
|
2015-02-27 14:04:44 -05:00
|
|
|
cryptoStub.encrypt.returns(resolves(encryptedPrivateKey));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.encrypt('asdf').then(function(encryptedKey) {
|
|
|
|
expect(encryptedKey._id).to.equal(keyId);
|
|
|
|
expect(encryptedKey.encryptedPrivateKey).to.equal(encryptedPrivateKey);
|
|
|
|
expect(encryptedKey.salt).to.exist;
|
|
|
|
expect(encryptedKey.iv).to.exist;
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
describe('upload', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to invalid args', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.upload({}).catch(function(err) {
|
|
|
|
expect(err.message).to.match(/Incomplete/);
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
imapClientStub.createFolder.returns(resolves());
|
|
|
|
imapClientStub.uploadMessage.returns(resolves());
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.upload({
|
|
|
|
_id: keyId,
|
2014-10-07 14:32:23 -04:00
|
|
|
userId: emailAddress,
|
2015-02-27 14:04:44 -05:00
|
|
|
encryptedPrivateKey: encryptedPrivateKey,
|
|
|
|
salt: salt,
|
|
|
|
iv: iv
|
|
|
|
}).then(function() {
|
|
|
|
expect(imapClientStub.uploadMessage.calledOnce).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2015-02-27 14:04:44 -05:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
describe('isSynced', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
sinon.stub(privkeyDao, '_fetchMessage');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
privkeyDao._fetchMessage.restore();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
it('should be synced', function(done) {
|
|
|
|
privkeyDao._fetchMessage.returns(resolves({}));
|
|
|
|
|
|
|
|
privkeyDao.isSynced().then(function(synced) {
|
|
|
|
expect(synced).to.be.true;
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
it('should not be synced', function(done) {
|
|
|
|
privkeyDao._fetchMessage.returns(resolves());
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.isSynced().then(function(synced) {
|
|
|
|
expect(synced).to.be.false;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
it('should not be synced in case of error', function(done) {
|
|
|
|
privkeyDao._fetchMessage.returns(rejects(new Error()));
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.isSynced().then(function(synced) {
|
|
|
|
expect(synced).to.be.false;
|
|
|
|
done();
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
describe('download', function() {
|
|
|
|
var base64Content = 'AYzsvV+hGMMT4BIl/XFjbl60BaM5DpDYVNyKPnoZ4ZyW1qy1udkQR7VUeNKJw5v2gWOqc3y6KHkZIqybOVro6e8tzhK1Fvpz+rgmME0tbrrh/Dd6QMBXb9c6ZAzgbLdq0sxftqXO9GoxINAVcfGN/MkcOIhonEjIsLSaYY2WLuGOLp8ZNdgO0tPxfcdd/f1hVXH2JRYmkOwStH3y2uYDmUhEWWeLfP2vF57F4NgtK2Ln4Ypn4VDx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eCMOz0bULRyVTe0lzSXBcCgu5gK+KEo8p38trTSJ/S95sKQnyNbrnz2QOIXvzxLrL6/nnC/4pwxXKZ6XqB/2zLVfiJRjUQ1NUC0xDXA==';
|
|
|
|
var root = [{
|
|
|
|
type: 'attachment',
|
|
|
|
content: util.binStr2Uint8Arr(util.base642Str(base64Content))
|
|
|
|
}];
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
sinon.stub(privkeyDao, '_fetchMessage');
|
|
|
|
sinon.stub(privkeyDao, '_parse');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
privkeyDao._fetchMessage.restore();
|
|
|
|
privkeyDao._parse.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail if key not synced', function(done) {
|
|
|
|
privkeyDao._fetchMessage.returns(resolves());
|
|
|
|
|
|
|
|
privkeyDao.download({
|
|
|
|
userId: emailAddress,
|
|
|
|
keyId: keyId
|
|
|
|
}).catch(function(err) {
|
|
|
|
expect(err.message).to.match(/not synced/);
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao._fetchMessage.returns(resolves({}));
|
|
|
|
imapClientStub.getBodyParts.returns(resolves());
|
|
|
|
privkeyDao._parse.returns(resolves(root));
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.download({
|
|
|
|
userId: emailAddress,
|
|
|
|
keyId: keyId
|
|
|
|
}).then(function(privkey) {
|
|
|
|
expect(privkey._id).to.equal(keyId);
|
|
|
|
expect(privkey.userId).to.equal(emailAddress);
|
|
|
|
expect(privkey.encryptedPrivateKey).to.exist;
|
|
|
|
done();
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
describe('decrypt', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to invalid args', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.decrypt({}).catch(function(err) {
|
|
|
|
expect(err.message).to.match(/Incomplete/);
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
it('should fail for invalid code', function(done) {
|
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
cryptoStub.decrypt.returns(rejects(new Error()));
|
2014-12-11 13:07:04 -05:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.decrypt({
|
|
|
|
_id: keyId,
|
|
|
|
userId: emailAddress,
|
|
|
|
code: 'asdf',
|
|
|
|
encryptedPrivateKey: encryptedPrivateKey,
|
|
|
|
salt: salt,
|
|
|
|
iv: iv
|
|
|
|
}).catch(function(err) {
|
|
|
|
expect(err.message).to.match(/Invalid/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail for invalid key params', function(done) {
|
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
cryptoStub.decrypt.returns(resolves('PGP BLOCK'));
|
|
|
|
pgpStub.getKeyParams.returns({
|
|
|
|
_id: '7890',
|
|
|
|
userId: emailAddress
|
|
|
|
});
|
2014-12-11 13:07:04 -05:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.decrypt({
|
|
|
|
_id: keyId,
|
2014-12-11 13:07:04 -05:00
|
|
|
userId: emailAddress,
|
2015-02-27 14:04:44 -05:00
|
|
|
code: 'asdf',
|
|
|
|
encryptedPrivateKey: encryptedPrivateKey,
|
|
|
|
salt: salt,
|
|
|
|
iv: iv
|
|
|
|
}).catch(function(err) {
|
|
|
|
expect(err.message).to.match(/key parameters/);
|
2014-12-11 13:07:04 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
cryptoStub.deriveKey.returns(resolves('derivedKey'));
|
|
|
|
cryptoStub.decrypt.returns(resolves('PGP BLOCK'));
|
|
|
|
pgpStub.getKeyParams.returns({
|
|
|
|
_id: keyId,
|
|
|
|
userId: emailAddress
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao.decrypt({
|
|
|
|
_id: keyId,
|
2014-10-07 14:32:23 -04:00
|
|
|
userId: emailAddress,
|
2015-02-27 14:04:44 -05:00
|
|
|
code: 'asdf',
|
|
|
|
encryptedPrivateKey: encryptedPrivateKey,
|
|
|
|
salt: salt,
|
|
|
|
iv: iv
|
|
|
|
}).then(function(privkey) {
|
|
|
|
expect(privkey._id).to.equal(keyId);
|
|
|
|
expect(privkey.userId).to.equal(emailAddress);
|
|
|
|
expect(privkey.encryptedKey).to.equal('PGP BLOCK');
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-08-28 12:17:34 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-08-28 12:17:34 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
describe('_fetchMessage', function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should fail due to invalid args', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao._fetchMessage({}).catch(function(err) {
|
|
|
|
expect(err.message).to.match(/Incomplete/);
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-08-28 12:17:34 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-08-28 12:17:34 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
it('should fail if imap folder does not exist', function(done) {
|
|
|
|
imapClientStub.listMessages.returns(rejects(new Error()));
|
2014-12-11 13:07:04 -05:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao._fetchMessage({
|
2014-12-11 13:07:04 -05:00
|
|
|
userId: emailAddress,
|
|
|
|
keyId: keyId
|
2015-02-27 14:04:44 -05:00
|
|
|
}).catch(function(err) {
|
|
|
|
expect(err.message).to.match(/Imap folder/);
|
2014-12-11 13:07:04 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
imapClientStub.listMessages.returns(resolves([{
|
|
|
|
subject: keyId
|
|
|
|
}]));
|
2014-08-28 12:17:34 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao._fetchMessage({
|
|
|
|
userId: emailAddress,
|
|
|
|
keyId: keyId
|
|
|
|
}).then(function(msg) {
|
|
|
|
expect(msg.subject).to.equal(keyId);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work for not matching message', function(done) {
|
|
|
|
imapClientStub.listMessages.returns(resolves([{
|
|
|
|
subject: '7890'
|
|
|
|
}]));
|
2014-08-28 12:17:34 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao._fetchMessage({
|
2014-10-07 14:32:23 -04:00
|
|
|
userId: emailAddress,
|
|
|
|
keyId: keyId
|
2015-02-27 14:04:44 -05:00
|
|
|
}).then(function(msg) {
|
|
|
|
expect(msg).to.not.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work for no messages', function(done) {
|
|
|
|
imapClientStub.listMessages.returns(resolves([]));
|
|
|
|
|
|
|
|
privkeyDao._fetchMessage({
|
|
|
|
userId: emailAddress,
|
|
|
|
keyId: keyId
|
|
|
|
}).then(function(msg) {
|
|
|
|
expect(msg).to.not.exist;
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
describe('_parse', function() {
|
|
|
|
var root = {
|
|
|
|
foo: 'bar'
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
sinon.stub(mailreader, 'parse');
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
mailreader.parse.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail', function(done) {
|
|
|
|
mailreader.parse.yields(new Error('asdf'));
|
|
|
|
|
|
|
|
privkeyDao._parse().catch(function(err) {
|
|
|
|
expect(err.message).to.match(/asdf/);
|
2014-10-07 14:32:23 -04:00
|
|
|
done();
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2015-02-27 14:04:44 -05:00
|
|
|
mailreader.parse.yields(null, root);
|
2014-06-06 12:36:23 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
privkeyDao._parse().then(function(res) {
|
|
|
|
expect(res).to.equal(root);
|
|
|
|
done();
|
|
|
|
});
|
2014-06-06 12:36:23 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|