1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-28 20:02:16 -05:00
mail/test/unit/service/privatekey-dao-test.js

419 lines
15 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
2014-06-06 12:36:23 -04: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
var privkeyDao, authStub, pgpStub, cryptoStub, imapClientStub,
2014-10-07 14:32:23 -04:00
emailAddress = 'test@example.com',
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() {
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
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;
done();
});
});
});
describe('encrypt', function() {
2014-10-07 14:32:23 -04:00
it('should fail due to invalid args', function(done) {
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) {
cryptoStub.deriveKey.returns(resolves('derivedKey'));
pgpStub.exportKeys.returns(resolves({
keyId: keyId,
privateKeyArmored: 'PGP BLOCK'
2014-12-11 13:07:04 -05:00
}));
cryptoStub.encrypt.returns(resolves(encryptedPrivateKey));
2014-06-06 12:36:23 -04: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
describe('upload', function() {
2014-10-07 14:32:23 -04:00
it('should fail due to invalid args', function(done) {
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) {
var IMAP_KEYS_FOLDER = 'openpgp_keys';
var fullPath = 'INBOX.' + IMAP_KEYS_FOLDER;
imapClientStub.createFolder.withArgs({
path: IMAP_KEYS_FOLDER
}).returns(resolves(fullPath));
imapClientStub.uploadMessage.withArgs(sinon.match(function(arg) {
expect(arg.path).to.equal(fullPath);
expect(arg.message).to.exist;
return true;
})).returns(resolves());
2014-06-06 12:36:23 -04:00
privkeyDao.upload({
_id: keyId,
2014-10-07 14:32:23 -04:00
userId: emailAddress,
encryptedPrivateKey: encryptedPrivateKey,
salt: salt,
iv: iv
}).then(function() {
expect(imapClientStub.createFolder.calledOnce).to.be.true;
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
});
});
2014-06-06 12:36:23 -04:00
describe('isSynced', function() {
beforeEach(function() {
sinon.stub(privkeyDao, '_getFolder');
sinon.stub(privkeyDao, '_fetchMessage');
});
afterEach(function() {
privkeyDao._getFolder.restore();
privkeyDao._fetchMessage.restore();
2014-06-06 12:36:23 -04:00
});
it('should be synced', function(done) {
privkeyDao._getFolder.returns(resolves('foo'));
privkeyDao._fetchMessage.returns(resolves({}));
privkeyDao.isSynced().then(function(synced) {
expect(synced).to.be.true;
expect(privkeyDao._getFolder.calledOnce).to.be.true;
expect(privkeyDao._fetchMessage.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
});
2014-06-06 12:36:23 -04:00
it('should not be synced', function(done) {
privkeyDao._getFolder.returns(resolves());
privkeyDao._fetchMessage.returns(resolves());
2014-06-06 12:36:23 -04:00
privkeyDao.isSynced().then(function(synced) {
expect(synced).to.be.false;
expect(privkeyDao._getFolder.calledOnce).to.be.true;
expect(privkeyDao._fetchMessage.calledOnce).to.be.true;
done();
});
});
it('should not be synced in case of error', function(done) {
privkeyDao._getFolder.returns(rejects(new Error()));
privkeyDao.isSynced().then(function(synced) {
expect(synced).to.be.false;
expect(privkeyDao._getFolder.calledOnce).to.be.true;
done();
});
});
it('should not be synced in case of error', function(done) {
privkeyDao._getFolder.returns(resolves('foo'));
privkeyDao._fetchMessage.returns(rejects(new Error()));
privkeyDao.isSynced().then(function(synced) {
expect(synced).to.be.false;
expect(privkeyDao._getFolder.calledOnce).to.be.true;
expect(privkeyDao._fetchMessage.calledOnce).to.be.true;
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
describe('download', function() {
var base64Content = 'AYzsvV+hGMMT4BIl/XFjbl60BaM5DpDYVNyKPnoZ4ZyW1qy1udkQR7VUeNKJw5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ClIt+bxTyrjZ7MV0mkDuaWUaeEq2xuaU5cKlG1Aam6vSb3jmURgEzOk1onlkGrCfVUTne18W8V6KL7iG+lX+331baiZVGoMUXT++0T05KYBdTRYL0OZ0P3OPRqilPpxaZCY0NG5rJxC5ij0vnu9ECAvN3xSdiRF7SobVSVFIdc32aY24nLKv8/gSnROgmQbAqeCMOz0bULRyVTe0lzSXBcCgu5gK+KEo8p38trTSJ/S95sKQnyNbrnz2QOIXvzxLrL6/nnC/4pwxXKZ6XqB/2zLVfiJRjUQ1NUC0xDXA==';
var root = [{
type: 'attachment',
content: util.binStr2Uint8Arr(util.base642Str(base64Content))
}];
beforeEach(function() {
sinon.stub(privkeyDao, '_getFolder');
sinon.stub(privkeyDao, '_fetchMessage');
sinon.stub(privkeyDao, '_parse');
});
afterEach(function() {
privkeyDao._getFolder.restore();
privkeyDao._fetchMessage.restore();
privkeyDao._parse.restore();
});
it('should fail if key not synced', function(done) {
privkeyDao._getFolder.returns(resolves('foo'));
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) {
privkeyDao._getFolder.returns(resolves('foo'));
privkeyDao._fetchMessage.returns(resolves({}));
imapClientStub.getBodyParts.returns(resolves());
privkeyDao._parse.returns(resolves(root));
2014-06-06 12:36:23 -04: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
describe('decrypt', function() {
2014-10-07 14:32:23 -04:00
it('should fail due to invalid args', function(done) {
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
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
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
privkeyDao.decrypt({
_id: keyId,
2014-12-11 13:07:04 -05:00
userId: emailAddress,
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) {
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
privkeyDao.decrypt({
_id: keyId,
2014-10-07 14:32:23 -04:00
userId: emailAddress,
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-10-07 14:32:23 -04:00
});
describe('_getFolder', function() {
it('should fail if imap folder does not exist', function(done) {
imapClientStub.listWellKnownFolders.returns(resolves({
Inbox: [{
path: 'INBOX'
}],
Other: [{
path: 'foo'
}]
}));
2014-12-11 13:07:04 -05:00
privkeyDao._getFolder().catch(function(err) {
expect(err.message).to.match(/Imap folder/);
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
2014-12-11 13:07:04 -05:00
done();
});
});
2014-10-07 14:32:23 -04:00
it('should work', function(done) {
imapClientStub.listWellKnownFolders.returns(resolves({
Inbox: [{
path: 'INBOX'
}],
Other: [{
path: 'openpgp_keys'
}]
}));
privkeyDao._getFolder().then(function(path) {
expect(path).to.equal('openpgp_keys');
expect(imapClientStub.listWellKnownFolders.calledOnce).to.be.true;
done();
});
});
});
describe('_fetchMessage', function() {
it('should fail due to invalid args', function(done) {
privkeyDao._fetchMessage({}).catch(function(err) {
expect(err.message).to.match(/Incomplete/);
done();
});
});
it('should work', function(done) {
imapClientStub.listMessages.returns(resolves([{
subject: keyId
}]));
privkeyDao._fetchMessage({
userId: emailAddress,
keyId: keyId
}).then(function(msg) {
expect(msg.subject).to.equal(keyId);
expect(imapClientStub.listMessages.calledOnce).to.be.true;
done();
});
});
it('should work with path prefix', function(done) {
imapClientStub.listMessages.returns(resolves([{
subject: keyId
}]));
privkeyDao._fetchMessage({
userId: emailAddress,
keyId: keyId
}).then(function(msg) {
expect(msg.subject).to.equal(keyId);
expect(imapClientStub.listMessages.calledOnce).to.be.true;
done();
});
});
it('should work for not matching message', function(done) {
imapClientStub.listMessages.returns(resolves([{
subject: '7890'
}]));
privkeyDao._fetchMessage({
2014-10-07 14:32:23 -04:00
userId: emailAddress,
keyId: keyId
}).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
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) {
mailreader.parse.yields(null, root);
2014-06-06 12:36:23 -04:00
privkeyDao._parse().then(function(res) {
expect(res).to.equal(root);
done();
});
2014-06-06 12:36:23 -04:00
});
});
});