From afe8c1329b9087cffb1fd37910369d5146e82340 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Sat, 18 May 2013 22:33:10 +0200 Subject: [PATCH] integrated new crypto into email dao unit test --- src/js/crypto/crypto.js | 13 ++++++++++++ src/js/dao/email-dao.js | 42 ++++++++++++++++++------------------- test/unit/crypto-test.js | 6 ++++++ test/unit/email-dao-test.js | 19 ++++++++++------- test/unit/index.html | 3 --- 5 files changed, 51 insertions(+), 32 deletions(-) diff --git a/src/js/crypto/crypto.js b/src/js/crypto/crypto.js index b942a86..6766072 100644 --- a/src/js/crypto/crypto.js +++ b/src/js/crypto/crypto.js @@ -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 */ diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 3199cba..c141687 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -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); } }; diff --git a/test/unit/crypto-test.js b/test/unit/crypto-test.js index dab0dc2..ca74c37 100644 --- a/test/unit/crypto-test.js +++ b/test/unit/crypto-test.js @@ -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); diff --git a/test/unit/email-dao-test.js b/test/unit/email-dao-test.js index 15a4ea5..c0018d7 100644 --- a/test/unit/email-dao-test.js +++ b/test/unit/email-dao-test.js @@ -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(); diff --git a/test/unit/index.html b/test/unit/index.html index b57cd42..f3ee304 100644 --- a/test/unit/index.html +++ b/test/unit/index.html @@ -25,7 +25,6 @@ - @@ -43,7 +42,6 @@ - @@ -59,7 +57,6 @@ -