mail/test/unit/crypto-test.js

105 lines
3.1 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,
2013-05-18 19:33:59 -04:00
ivSize: 128,
rsaKeySize: 1024
2013-03-13 11:58:46 -04:00
};
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-05-14 14:28:12 -04:00
crypto_test.crypto.init({
emailAddress: crypto_test.user,
password: crypto_test.password,
2013-05-18 19:33:59 -04:00
keySize: crypto_test.keySize,
rsaKeySize: crypto_test.rsaKeySize
2013-05-17 11:44:34 -04:00
}, function(err) {
ok(!err, '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
});
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);
});
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();
});
});
asyncTest("AES en/decrypt (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
});
asyncTest("AES en/decrypt batch (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
});
asyncTest("AES/RSA encrypt batch for User (Async/Worker)", 2, function() {
2013-03-13 11:58:46 -04:00
// generate test data
var collection, td = new TestData();
2013-05-14 06:57:09 -04:00
collection = td.getEmailCollection(10);
crypto_test.list = collection.toJSON();
2013-05-14 06:57:09 -04:00
crypto_test.crypto.encryptListForUser(crypto_test.list, null, function(err, encryptedList) {
ok(!err && encryptedList, 'Encrypt list for user');
equal(encryptedList.length, crypto_test.list.length, 'Length of list');
crypto_test.encryptedList = encryptedList;
2013-05-14 06:57:09 -04:00
start();
});
});
2013-05-14 06:57:09 -04:00
asyncTest("AES/RSA decrypt batch for User (Async/Worker)", 3, function() {
crypto_test.crypto.decryptListForUser(crypto_test.encryptedList, null, function(err, decryptedList) {
ok(!err && decryptedList, 'Decrypt list');
equal(decryptedList.length, crypto_test.list.length, 'Length of list');
deepEqual(decryptedList, crypto_test.list, 'Decrypted list is correct');
start();
2013-05-14 06:57:09 -04:00
});
2013-03-13 11:58:46 -04:00
});