2013-03-13 11:58:46 -04:00
|
|
|
module("DeviceStorage");
|
|
|
|
|
|
|
|
var devicestorage_test = {
|
|
|
|
user: 'devicestorage_test@example.com',
|
|
|
|
password: 'Password',
|
|
|
|
keySize: 128,
|
2013-05-18 22:00:53 -04:00
|
|
|
ivSize: 128,
|
|
|
|
rsaKeySize: 1024
|
2013-03-13 11:58:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
asyncTest("Init", 3, function() {
|
|
|
|
// init dependencies
|
2013-05-22 11:03:54 -04:00
|
|
|
devicestorage_test.util = new cryptoLib.Util(window, uuid);
|
2013-05-31 07:09:49 -04:00
|
|
|
devicestorage_test.jsonDao = new app.dao.LawnchairDAO(Lawnchair);
|
2013-03-13 11:58:46 -04:00
|
|
|
devicestorage_test.crypto = new app.crypto.Crypto(window, devicestorage_test.util);
|
2013-03-22 07:46:56 -04:00
|
|
|
devicestorage_test.storage = new app.dao.DeviceStorage(devicestorage_test.util, devicestorage_test.crypto, devicestorage_test.jsonDao, null);
|
2013-03-13 11:58:46 -04:00
|
|
|
ok(devicestorage_test.storage, 'DeviceStorageDAO');
|
2013-05-17 11:44:34 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
// generate test data
|
|
|
|
devicestorage_test.list = new TestData().getEmailCollection(100).toJSON();
|
2013-05-17 11:44:34 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
// init crypto
|
2013-05-17 11:44:34 -04:00
|
|
|
devicestorage_test.crypto.init({
|
|
|
|
emailAddress: devicestorage_test.user,
|
|
|
|
password: devicestorage_test.password,
|
2013-05-18 22:00:53 -04:00
|
|
|
keySize: devicestorage_test.keySize,
|
|
|
|
rsaKeySize: devicestorage_test.rsaKeySize
|
2013-05-31 07:00:54 -04:00
|
|
|
}, function(err, generatedKeypair) {
|
|
|
|
ok(!err && generatedKeypair, 'Init crypto');
|
|
|
|
devicestorage_test.generatedKeypair = generatedKeypair;
|
2013-05-17 11:44:34 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
// clear db before tests
|
|
|
|
devicestorage_test.jsonDao.clear(function(err) {
|
|
|
|
ok(!err, 'DB cleared. Error status: ' + err);
|
2013-05-17 11:44:34 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
start();
|
|
|
|
});
|
2013-05-17 11:44:34 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-05-17 11:44:34 -04:00
|
|
|
asyncTest("Encrypt list for user", 2, function() {
|
2013-05-31 07:00:54 -04:00
|
|
|
var receiverPubkeys = [devicestorage_test.generatedKeypair.publicKey];
|
2013-05-23 16:33:44 -04:00
|
|
|
|
|
|
|
devicestorage_test.crypto.encryptListForUser(devicestorage_test.list, receiverPubkeys, function(err, encryptedList) {
|
2013-05-17 11:44:34 -04:00
|
|
|
ok(!err);
|
2013-03-13 11:58:46 -04:00
|
|
|
equal(encryptedList.length, devicestorage_test.list.length, 'Encrypt list');
|
2013-05-17 11:44:34 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
devicestorage_test.encryptedList = encryptedList;
|
|
|
|
start();
|
2013-05-17 11:44:34 -04:00
|
|
|
});
|
2013-03-13 11:58:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
asyncTest("Store encrypted list", 1, function() {
|
|
|
|
devicestorage_test.storage.storeEcryptedList(devicestorage_test.encryptedList, 'email_inbox', function() {
|
|
|
|
ok(true, 'Store encrypted list');
|
|
|
|
|
|
|
|
start();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-05-31 09:51:34 -04:00
|
|
|
asyncTest("List items", 4, function() {
|
2013-05-17 11:44:34 -04:00
|
|
|
|
2013-05-31 07:00:54 -04:00
|
|
|
var senderPubkeys = [devicestorage_test.generatedKeypair.publicKey];
|
2013-05-23 16:33:44 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
var offset = 2,
|
|
|
|
num = 6;
|
2013-05-17 11:44:34 -04:00
|
|
|
|
2013-05-31 09:51:34 -04:00
|
|
|
// list encrypted items from storage
|
|
|
|
devicestorage_test.storage.listEncryptedItems('email_inbox_5', offset, num, function(err, encryptedList) {
|
2013-05-17 11:44:34 -04:00
|
|
|
ok(!err);
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-05-31 09:51:34 -04:00
|
|
|
// 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)');
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-05-31 09:51:34 -04:00
|
|
|
var decrypted, orig = devicestorage_test.list[54];
|
|
|
|
|
|
|
|
// check ids
|
|
|
|
for (var i = 0; i < decryptedList.length; i++) {
|
|
|
|
if (decryptedList[i].id === orig.id && decryptedList[i].from === orig.from) {
|
|
|
|
deepEqual(decryptedList[i], orig, 'Messages decrypted correctly');
|
|
|
|
break;
|
|
|
|
}
|
2013-05-17 11:44:34 -04:00
|
|
|
}
|
|
|
|
|
2013-05-31 09:51:34 -04:00
|
|
|
start();
|
|
|
|
});
|
2013-03-13 11:58:46 -04:00
|
|
|
});
|
|
|
|
});
|