1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-16 06:05:07 -05:00
mail/test/unit/devicestorage-dao-test.js

106 lines
3.4 KiB
JavaScript
Raw Normal View History

define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/devicestorage-dao', 'test/test-data', 'js/dao/lawnchair-dao'], function(_, util, Crypto, DeviceStorageDAO, testData, LawnchairDAO) {
2013-09-26 07:26:57 -04:00
'use strict';
module("DeviceStorage");
var devicestorageTest = {
user: 'devicestorage_test@example.com',
password: 'Password',
keySize: 128,
ivSize: 128,
rsaKeySize: 1024
};
var crypto, storage;
asyncTest("Init", 3, function() {
// init dependencies
storage = new DeviceStorageDAO(new LawnchairDAO());
2013-09-26 07:26:57 -04:00
storage.init(devicestorageTest.user, function() {
ok(storage, 'DeviceStorageDAO');
// generate test data
devicestorageTest.list = testData.getEmailCollection(100);
// init crypto
crypto = new Crypto();
crypto.init({
emailAddress: devicestorageTest.user,
password: devicestorageTest.password,
salt: util.random(devicestorageTest.keySize),
2013-09-26 07:26:57 -04:00
keySize: devicestorageTest.keySize,
rsaKeySize: devicestorageTest.rsaKeySize
}, function(err, generatedKeypair) {
ok(!err && generatedKeypair, 'Init crypto');
devicestorageTest.generatedKeypair = generatedKeypair;
// clear db before tests
storage.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() {
2013-09-26 09:48:32 -04:00
storage.storeList(devicestorageTest.encryptedList, 'email_inbox', function() {
2013-09-26 07:26:57 -04:00
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
2013-09-26 09:48:32 -04:00
storage.listItems('email_inbox', offset, num, function(err, encryptedList) {
2013-09-26 07:26:57 -04:00
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();
});
});
});
2013-06-10 17:22:57 -04:00
asyncTest("Delete List items", 1, function() {
// list encrypted items from storage
storage.removeList('email_inbox', function(err) {
ok(!err);
start();
});
});
2013-06-10 17:22:57 -04:00
});