1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-15 05:35:05 -05:00
mail/test/unit/crypto-test.js

103 lines
3.0 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
};
asyncTest("Init without keypair", 4, function() {
2013-03-13 11:58:46 -04:00
// init dependencies
crypto_test.util = new cryptoLib.Util(window, uuid);
2013-03-13 11:58:46 -04:00
crypto_test.crypto = new app.crypto.Crypto(window, crypto_test.util);
2013-05-14 06:57:09 -04:00
ok(crypto_test.crypto, 'Crypto');
// test without passing keys
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
}, function(err, generatedKeypair) {
ok(!err && generatedKeypair, 'Init crypto without keypair input');
var pk = generatedKeypair.publicKey;
ok(pk._id && pk.userId, 'Key ID: ' + pk._id);
ok(pk.publicKey.indexOf('-----BEGIN PUBLIC KEY-----') === 0, pk.publicKey);
crypto_test.generatedKeypair = generatedKeypair;
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("Init with keypair", 1, function() {
// test with passing keypair
crypto_test.crypto.init({
emailAddress: crypto_test.user,
password: crypto_test.password,
keySize: crypto_test.keySize,
rsaKeySize: crypto_test.rsaKeySize,
storedKeypair: crypto_test.generatedKeypair
}, function(err, generatedKeypair) {
ok(!err, 'Init crypto with keypair input');
2013-05-18 22:00:53 -04:00
start();
});
2013-05-18 22:00:53 -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();
});
});
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/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
var receiverPubkeys = [crypto_test.generatedKeypair.publicKey];
crypto_test.crypto.encryptListForUser(crypto_test.list, receiverPubkeys, 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() {
var senderPubkeys = [crypto_test.generatedKeypair.publicKey];
crypto_test.crypto.decryptListForUser(crypto_test.encryptedList, senderPubkeys, 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
});