mail/test/unit/crypto-test.js

114 lines
3.3 KiB
JavaScript
Raw Normal View History

2013-03-13 11:58:46 -04:00
module("Crypto Api");
var crypto_test = {
user: 'crypto_test@example.com',
password: 'Password',
keySize: 128,
ivSize: 104
};
2013-05-14 06:57:09 -04:00
asyncTest("Init", 2, function() {
2013-03-13 11:58:46 -04:00
// init dependencies
crypto_test.util = new app.crypto.Util(window, uuid);
crypto_test.crypto = new app.crypto.Crypto(window, crypto_test.util);
2013-05-14 06:57:09 -04:00
ok(crypto_test.crypto, 'Crypto');
2013-03-13 11:58:46 -04:00
crypto_test.crypto.init(crypto_test.user, crypto_test.password, crypto_test.keySize, crypto_test.ivSize, function() {
ok(true, 'Init crypto');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
start();
2013-05-14 06:57:09 -04:00
});
2013-03-13 11:58:46 -04:00
});
2013-05-14 06:57:09 -04:00
asyncTest("PBKDF2 (Async/Worker)", 1, function() {
2013-03-13 11:58:46 -04:00
crypto_test.crypto.deriveKey(crypto_test.password, crypto_test.keySize, function(key) {
2013-05-14 06:57:09 -04:00
equal(crypto_test.util.base642Str(key).length * 8, crypto_test.keySize, 'Keysize ' + crypto_test.keySize);
2013-03-13 11:58:46 -04:00
start();
});
});
2013-05-14 06:57:09 -04:00
asyncTest("En/Decrypt for User", 4, function() {
2013-03-13 11:58:46 -04:00
var secret = "Secret stuff";
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
var itemKey = crypto_test.util.random(crypto_test.keySize);
var itemIV = crypto_test.util.random(crypto_test.ivSize);
var keyIV = crypto_test.util.random(crypto_test.ivSize);
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
crypto_test.crypto.aesEncrypt(secret, itemKey, itemIV, function(ciphertext) {
ok(ciphertext, 'Encrypt item');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
crypto_test.crypto.aesEncryptForUser(itemKey, keyIV, function(encryptedKey) {
ok(encryptedKey, 'Encrypt item key');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
crypto_test.crypto.aesDecryptForUser(encryptedKey, keyIV, function(decryptedKey) {
equal(decryptedKey, itemKey, 'Decrypt item key');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
crypto_test.crypto.aesDecrypt(ciphertext, decryptedKey, itemIV, function(decrypted) {
equal(decrypted, secret, 'Decrypt item');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
start();
});
});
});
2013-05-14 06:57:09 -04:00
});
2013-03-13 11:58:46 -04:00
});
2013-05-14 06:57:09 -04:00
asyncTest("CCM mode (Async/Worker)", 2, function() {
2013-03-13 11:58:46 -04:00
var secret = 'Big secret';
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
var key = crypto_test.util.random(crypto_test.keySize);
var iv = crypto_test.util.random(crypto_test.ivSize);
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
crypto_test.crypto.aesEncrypt(secret, key, iv, function(ciphertext) {
ok(ciphertext, 'Encrypt item');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
crypto_test.crypto.aesDecrypt(ciphertext, key, iv, function(decrypted) {
equal(decrypted, secret, 'Decrypt item');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
start();
});
2013-05-14 06:57:09 -04:00
});
2013-03-13 11:58:46 -04:00
});
2013-05-14 06:57:09 -04:00
asyncTest("CCM batch mode (Async/Worker)", 5, function() {
2013-03-13 11:58:46 -04:00
// generate test data
var collection, list, td = new TestData();
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
collection = td.getEmailCollection(100);
list = td.packageCollectionForEncryption(collection, crypto_test.keySize, crypto_test.ivSize);
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
crypto_test.crypto.aesEncryptList(list, function(encryptedList) {
ok(encryptedList, 'Encrypt list');
equal(encryptedList.length, list.length, 'Length of list');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
crypto_test.crypto.aesDecryptList(encryptedList, function(decryptedList) {
ok(decryptedList, 'Decrypt list');
equal(decryptedList.length, list.length, 'Length of list');
deepEqual(decryptedList, list, 'Decrypted list is correct');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
start();
});
2013-05-14 06:57:09 -04:00
});
2013-03-13 11:58:46 -04:00
});
2013-05-14 06:57:09 -04:00
asyncTest("CCM batch mode for User (Async/Worker)", 5, function() {
2013-03-13 11:58:46 -04:00
// generate test data
var collection, list, td = new TestData();
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
collection = td.getEmailCollection(100);
list = collection.toJSON();
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
crypto_test.crypto.aesEncryptListForUser(list, function(encryptedList) {
ok(encryptedList, 'Encrypt list for user');
equal(encryptedList.length, list.length, 'Length of list');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
crypto_test.crypto.aesDecryptListForUser(encryptedList, function(decryptedList) {
ok(decryptedList, 'Decrypt list');
equal(decryptedList.length, list.length, 'Length of list');
deepEqual(decryptedList, list, 'Decrypted list is correct');
2013-05-14 06:57:09 -04:00
2013-03-13 11:58:46 -04:00
start();
});
2013-05-14 06:57:09 -04:00
});
2013-03-13 11:58:46 -04:00
});