refactored device storage

This commit is contained in:
Tankred Hase 2013-06-10 23:22:57 +02:00
parent ccebe011cb
commit deb47fd4df
4 changed files with 103 additions and 98 deletions

View File

@ -4,16 +4,18 @@
* through transparent encryption. If not, the crypto API is
* used to encrypt data on the fly before persisting via a JSON store.
*/
app.dao.DeviceStorage = function(util, crypto, jsonDao, sqlcipherDao) {
define(['cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-dao'], function(util, crypto, jsonDao) {
'use strict';
var self = {};
/**
* Stores a list of encrypted items in the object store
* @param list [Array] The list of items to be persisted
* @param type [String] The type of item to be persisted e.g. 'email'
*/
this.storeEcryptedList = function(list, type, callback) {
var i, date, key, items = [];
self.storeEcryptedList = function(list, type, callback) {
var date, key, items = [];
// nothing to store
if (list.length === 0) {
@ -50,7 +52,7 @@ app.dao.DeviceStorage = function(util, crypto, jsonDao, sqlcipherDao) {
* @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)
*/
this.listEncryptedItems = function(type, offset, num, callback) {
self.listEncryptedItems = function(type, offset, num, callback) {
// fetch all items of a certain type from the data-store
jsonDao.list(type, offset, num, function(encryptedList) {
@ -61,8 +63,9 @@ app.dao.DeviceStorage = function(util, crypto, jsonDao, sqlcipherDao) {
/**
* Clear the whole device data-store
*/
this.clear = function(callback) {
self.clear = function(callback) {
jsonDao.clear(callback);
};
};
return self;
});

View File

@ -0,0 +1,92 @@
define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-dao', 'js/dao/devicestorage-dao', 'test/test-data'], function(_, util, crypto, jsonDao, storage, testData) {
'use strict';
module("DeviceStorage");
var devicestorageTest = {
user: 'devicestorage_test@example.com',
password: 'Password',
keySize: 128,
ivSize: 128,
rsaKeySize: 1024
};
asyncTest("Init", 3, function() {
// init dependencies
jsonDao.init(devicestorageTest.user);
ok(storage, 'DeviceStorageDAO');
// generate test data
devicestorageTest.list = testData.getEmailCollection(100).toJSON();
// init crypto
crypto.init({
emailAddress: devicestorageTest.user,
password: devicestorageTest.password,
keySize: devicestorageTest.keySize,
rsaKeySize: devicestorageTest.rsaKeySize
}, function(err, generatedKeypair) {
ok(!err && generatedKeypair, 'Init crypto');
devicestorageTest.generatedKeypair = generatedKeypair;
// clear db before tests
jsonDao.clear(function(err) {
ok(!err, 'DB cleared. Error status: ' + err);
start();
});
});
});
asyncTest("Encrypt list for user", 2, function() {
var receiverPubkeys = [devicestorageTest.generatedKeypair.publicKey];
crypto.encryptListForUser(devicestorageTest.list, receiverPubkeys, function(err, encryptedList) {
ok(!err);
equal(encryptedList.length, devicestorageTest.list.length, 'Encrypt list');
encryptedList.forEach(function(i) {
i.sentDate = _.findWhere(devicestorageTest.list, {
id: i.id
}).sentDate;
});
devicestorageTest.encryptedList = encryptedList;
start();
});
});
asyncTest("Store encrypted list", 1, function() {
storage.storeEcryptedList(devicestorageTest.encryptedList, 'email_inbox', function() {
ok(true, 'Store encrypted list');
start();
});
});
asyncTest("List items", 4, function() {
var senderPubkeys = [devicestorageTest.generatedKeypair.publicKey];
var offset = 2,
num = 6;
// list encrypted items from storage
storage.listEncryptedItems('email_inbox', offset, num, function(err, encryptedList) {
ok(!err);
// decrypt list
crypto.decryptListForUser(encryptedList, senderPubkeys, function(err, decryptedList) {
ok(!err);
equal(decryptedList.length, num, 'Found ' + decryptedList.length + ' items in store (and decrypted)');
var origSet = devicestorageTest.list.splice(92, num);
deepEqual(decryptedList, origSet, 'Messages decrypted correctly');
start();
});
});
});
});

View File

@ -1,91 +0,0 @@
module("DeviceStorage");
var devicestorage_test = {
user: 'devicestorage_test@example.com',
password: 'Password',
keySize: 128,
ivSize: 128,
rsaKeySize: 1024
};
asyncTest("Init", 3, function() {
// init dependencies
devicestorage_test.util = new cryptoLib.Util(window, uuid);
devicestorage_test.jsonDao = new app.dao.LawnchairDAO(Lawnchair);
devicestorage_test.jsonDao.init(devicestorage_test.user);
devicestorage_test.crypto = new app.crypto.Crypto(window, devicestorage_test.util);
devicestorage_test.storage = new app.dao.DeviceStorage(devicestorage_test.util, devicestorage_test.crypto, devicestorage_test.jsonDao, null);
ok(devicestorage_test.storage, 'DeviceStorageDAO');
// generate test data
devicestorage_test.list = new TestData().getEmailCollection(100).toJSON();
// init crypto
devicestorage_test.crypto.init({
emailAddress: devicestorage_test.user,
password: devicestorage_test.password,
keySize: devicestorage_test.keySize,
rsaKeySize: devicestorage_test.rsaKeySize
}, function(err, generatedKeypair) {
ok(!err && generatedKeypair, 'Init crypto');
devicestorage_test.generatedKeypair = generatedKeypair;
// clear db before tests
devicestorage_test.jsonDao.clear(function(err) {
ok(!err, 'DB cleared. Error status: ' + err);
start();
});
});
});
asyncTest("Encrypt list for user", 2, function() {
var receiverPubkeys = [devicestorage_test.generatedKeypair.publicKey];
devicestorage_test.crypto.encryptListForUser(devicestorage_test.list, receiverPubkeys, function(err, encryptedList) {
ok(!err);
equal(encryptedList.length, devicestorage_test.list.length, 'Encrypt list');
encryptedList.forEach(function(i) {
i.sentDate = _.findWhere(devicestorage_test.list, {
id: i.id
}).sentDate;
});
devicestorage_test.encryptedList = encryptedList;
start();
});
});
asyncTest("Store encrypted list", 1, function() {
devicestorage_test.storage.storeEcryptedList(devicestorage_test.encryptedList, 'email_inbox', function() {
ok(true, 'Store encrypted list');
start();
});
});
asyncTest("List items", 4, function() {
var senderPubkeys = [devicestorage_test.generatedKeypair.publicKey];
var offset = 2,
num = 6;
// list encrypted items from storage
devicestorage_test.storage.listEncryptedItems('email_inbox', offset, num, function(err, encryptedList) {
ok(!err);
// decrypt list
devicestorage_test.crypto.decryptListForUser(encryptedList, senderPubkeys, function(err, decryptedList) {
ok(!err);
equal(decryptedList.length, num, 'Found ' + decryptedList.length + ' items in store (and decrypted)');
var origSet = devicestorage_test.list.splice(92, num);
deepEqual(decryptedList, origSet, 'Messages decrypted correctly');
start();
});
});
});

View File

@ -26,7 +26,8 @@ function startTests() {
'test/unit/rsa-test',
'test/unit/lawnchair-dao-test',
'test/unit/keychain-dao-test',
'test/unit/crypto-test'
'test/unit/crypto-test',
'test/unit/devicestorage-dao-test'
], function() {
//Tests loaded, run tests
QUnit.start();