1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 08:52:15 -05:00

integrated new crypto into email dao unit test

This commit is contained in:
Tankred Hase 2013-05-18 22:33:10 +02:00
parent 2c1458b663
commit afe8c1329b
5 changed files with 51 additions and 32 deletions

View File

@ -84,6 +84,19 @@ app.crypto.Crypto = function(window, util) {
}
};
/**
* Return a Public Key object containing the Public Key PEM
*/
this.getPublicKey = function() {
var keypair = rsa.exportKeys();
return {
_id: keypair._id,
userId: this.emailAddress,
publicKey: keypair.pubkeyPem
};
};
/**
* Do PBKDF2 key derivation in a WebWorker thread
*/

View File

@ -2,11 +2,9 @@
* A high-level Data-Access Api for handling Email synchronization
* between the cloud service and the device's local storage
*/
app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, naclCrypto, util) {
app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) {
'use strict';
var keypair; // the user's keypair
/**
* Inits all dependencies
*/
@ -29,30 +27,27 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, naclCrypto,
});
function initCrypto() {
crypto.init(account.get('emailAddress'), password, account.get('symKeySize'), account.get('symIvSize'), function(err) {
crypto.init({
emailAddress: account.get('emailAddress'),
password: password,
keySize: account.get('symKeySize')
}, function(err) {
if (err) {
callback(err);
return;
}
initNaclCrypto();
publishPublicKey();
});
}
function initNaclCrypto() {
// derive keypair from user's secret key
crypto.deriveKeyPair(naclCrypto, function(generated) {
keypair = generated;
function publishPublicKey() {
// get public key from crypto
var pubkey = crypto.getPublicKey();
//publish public key to cloud service
var pubkey = new app.model.PublicKey({
_id: keypair.id,
userId: account.get('emailAddress'),
publicKey: keypair.boxPk
});
cloudstorage.putPublicKey(pubkey.toJSON(), function(err) {
callback(err);
});
//publish public key to cloud service
cloudstorage.putPublicKey(pubkey, function(err) {
callback(err);
});
}
};
@ -85,7 +80,12 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, naclCrypto,
if (!folder) {
// get items from storage
devicestorage.listItems('email_' + folderName, offset, num, function(decryptedList) {
devicestorage.listItems('email_' + folderName, offset, num, function(err, decryptedList) {
if (err) {
callback(err);
return;
}
// parse to backbone model collection
collection = new app.model.EmailCollection(decryptedList);
@ -98,13 +98,13 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, naclCrypto,
self.account.get('folders').add(folder);
}
callback(collection);
callback(null, collection);
});
} else {
// read items from memory
collection = folder.get('items');
callback(collection);
callback(null, collection);
}
};

View File

@ -24,6 +24,12 @@ asyncTest("Init", 2, function() {
});
});
test("Get Public Key PEM", 2, function() {
var pk = crypto_test.crypto.getPublicKey();
ok(pk._id && pk.userId, 'Key ID: ' + pk._id);
ok(pk.publicKey.indexOf('-----BEGIN PUBLIC KEY-----') === 0, pk.publicKey);
});
asyncTest("PBKDF2 (Async/Worker)", 1, function() {
crypto_test.crypto.deriveKey(crypto_test.password, crypto_test.keySize, function(key) {
equal(crypto_test.util.base642Str(key).length * 8, crypto_test.keySize, 'Keysize ' + crypto_test.keySize);

View File

@ -7,12 +7,11 @@ var emaildao_test = {
ivSize: 128
};
asyncTest("Init", 2, function() {
asyncTest("Init", 3, function() {
// init dependencies
var util = new app.crypto.Util(window, uuid);
var jsonDao = new app.dao.LawnchairDAO(window);
emaildao_test.crypto = new app.crypto.Crypto(window, util);
var naclCrypto = new app.crypto.NaclCrypto(nacl, util);
emaildao_test.storage = new app.dao.DeviceStorage(util, emaildao_test.crypto, jsonDao, null);
// cloud storage stub
var cloudstorageStub = {
@ -23,7 +22,7 @@ asyncTest("Init", 2, function() {
callback();
}
};
emaildao_test.emailDao = new app.dao.EmailDAO(_, emaildao_test.crypto, emaildao_test.storage, cloudstorageStub, naclCrypto);
emaildao_test.emailDao = new app.dao.EmailDAO(_, emaildao_test.crypto, emaildao_test.storage, cloudstorageStub);
// generate test data
emaildao_test.list = new TestData().getEmailCollection(100);
@ -34,7 +33,8 @@ asyncTest("Init", 2, function() {
symIvSize: emaildao_test.ivSize
});
emaildao_test.emailDao.init(account, emaildao_test.password, function() {
emaildao_test.emailDao.init(account, emaildao_test.password, function(err) {
ok(!err);
equal(emaildao_test.emailDao.account.get('emailAddress'), emaildao_test.user, 'Email DAO Account');
// clear db before tests
@ -46,8 +46,9 @@ asyncTest("Init", 2, function() {
});
});
asyncTest("Persist test emails", 2, function() {
emaildao_test.crypto.aesEncryptListForUser(emaildao_test.list.toJSON(), function(encryptedList) {
asyncTest("Persist test emails", 3, function() {
emaildao_test.crypto.encryptListForUser(emaildao_test.list.toJSON(), null, function(err, encryptedList) {
ok(!err);
equal(encryptedList.length, emaildao_test.list.length, 'Encrypt list');
// add sent date to encrypted items
@ -63,8 +64,10 @@ asyncTest("Persist test emails", 2, function() {
});
});
asyncTest("List Email models", 1, function() {
emaildao_test.emailDao.listItems('inbox', 0, emaildao_test.list.length, function(collection) {
asyncTest("List Email models", 2, function() {
emaildao_test.emailDao.listItems('inbox', 0, emaildao_test.list.length, function(err, collection) {
ok(!err);
var gotten = collection.toJSON(),
reference = emaildao_test.list.toJSON();

View File

@ -25,7 +25,6 @@
<script src="../lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js"></script>
<script src="../lib/forge/forge.rsa.bundle.js"></script>
<script src="../lib/nacl.js"></script>
<script src="../lib/uuid.js"></script>
<script src="../js/app-config.js"></script>
@ -43,7 +42,6 @@
<script src="../js/crypto/pbkdf2.js"></script>
<script src="../js/crypto/aes-cbc.js"></script>
<script src="../js/crypto/rsa.js"></script>
<script src="../js/crypto/nacl-crypto.js"></script>
<script src="../js/crypto/crypto-batch.js"></script>
<script src="../js/crypto/crypto.js"></script>
@ -59,7 +57,6 @@
<script src="forge-test.js"></script>
<script src="aes-test.js"></script>
<script src="rsa-test.js"></script>
<script src="nacl-crypto-test.js"></script>
<script src="crypto-test.js"></script>
<script src="localstorage-dao-test.js"></script>
<script src="lawnchair-dao-test.js"></script>