1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-25 18:32:20 -05:00

rsa and lawnchair tests refactored

This commit is contained in:
Tankred Hase 2013-06-10 22:02:29 +02:00
parent 07325b8ed0
commit e7185a8baa
10 changed files with 132 additions and 116 deletions

View File

@ -1,5 +1,4 @@
{
"indent": 4,
"strict": true,
"globalstrict": true,
"node": true,

View File

@ -42,7 +42,7 @@ module.exports = function(grunt) {
},
jshint: {
all: ['Gruntfile.js', 'src/js/**/*.js'],
all: ['Gruntfile.js', 'src/*.js', 'src/js/**/*.js', 'test/**/*.js'],
options: {
jshintrc: '.jshintrc'
}

View File

@ -1,12 +1,14 @@
/**
* Handles generic caching of JSON objects in a lawnchair adapter
*/
app.dao.LawnchairDAO = function(Lawnchair) {
define(['lawnchair', 'lawnchairSQL', 'lawnchairIDB'], function(Lawnchair) {
'use strict';
var self = {};
var db;
this.init = function(dbName) {
self.init = function(dbName) {
if (!dbName) {
throw new Error('Lawnchair DB name must be specified!');
}
@ -23,7 +25,7 @@ app.dao.LawnchairDAO = function(Lawnchair) {
/**
* Create or update an object
*/
this.persist = function(key, object, callback) {
self.persist = function(key, object, callback) {
db.save({
key: key,
object: object
@ -33,14 +35,14 @@ app.dao.LawnchairDAO = function(Lawnchair) {
/**
* Persist a bunch of items at once
*/
this.batch = function(list, callback) {
self.batch = function(list, callback) {
db.batch(list, callback);
};
/**
* Read a single item by its key
*/
this.read = function(key, callback) {
self.read = function(key, callback) {
db.get(key, function(o) {
if (o) {
callback(o.object);
@ -56,7 +58,7 @@ app.dao.LawnchairDAO = function(Lawnchair) {
* @param offset [Number] The offset of items to fetch (0 is the last stored item)
* @param num [Number] The number of items to fetch (null means fetch all)
*/
this.list = function(type, offset, num, callback) {
self.list = function(type, offset, num, callback) {
var i, from, to,
matchingKeys = [],
intervalKeys = [],
@ -108,15 +110,16 @@ app.dao.LawnchairDAO = function(Lawnchair) {
/**
* Removes an object liter from local storage by its key (delete)
*/
this.remove = function(key, callback) {
self.remove = function(key, callback) {
db.remove(key, callback);
};
/**
* Clears the whole local storage cache
*/
this.clear = function(callback) {
self.clear = function(callback) {
db.nuke(callback);
};
};
return self;
});

View File

@ -17,9 +17,14 @@
},
shim: {
lawnchair: {
deps: ['lawnchairSQL', 'lawnchairIDB'],
exports: 'Lawnchair'
},
lawnchairSQL: {
deps: ['lawnchair']
},
lawnchairIDB: {
deps: ['lawnchair', 'lawnchairSQL']
},
backbone: {
//These script dependencies should be loaded before loading
//backbone.js

View File

@ -43,13 +43,13 @@ define(['cryptoLib/util'], function(util) {
};
self.generateBigString = function(iterations) {
var test_message = '';
var testMessage = '';
// generate test data
for (var i = 0; i < iterations; i++) {
test_message += 'aslghaksfdhsakjzfgasjfdgsauk';
testMessage += 'aslghaksfdhsakjzfgasjfdgsauk';
}
return test_message;
return testMessage;
};
return self;

View File

@ -3,17 +3,17 @@ define(['cryptoLib/aes-cbc', 'cryptoLib/util', 'test/test-data'], function(aes,
module("AES Crypto");
var aes_test = {
var aesTest = {
keySize: 128,
test_message: testData.generateBigString(1000)
testMessage: testData.generateBigString(1000)
};
test("CBC mode", 4, function() {
var plaintext = aes_test.test_message;
var key = util.random(aes_test.keySize);
var iv = util.random(aes_test.keySize);
var plaintext = aesTest.testMessage;
var key = util.random(aesTest.keySize);
var iv = util.random(aesTest.keySize);
ok(key, 'Key: ' + key);
equal(util.base642Str(key).length * 8, aes_test.keySize, 'Keysize ' + aes_test.keySize);
equal(util.base642Str(key).length * 8, aesTest.keySize, 'Keysize ' + aesTest.keySize);
var ciphertext = aes.encrypt(plaintext, key, iv);
ok(ciphertext, 'Ciphertext lenght: ' + ciphertext.length);

View File

@ -3,38 +3,38 @@ define(['forge', 'cryptoLib/util', 'test/test-data'], function(forge, util, test
module("Forge Crypto");
var forge_rsa_test = {
var forgeRsaTest = {
keySize: 1024,
test_message: '06a9214036b8a15b512e03d534120006'
testMessage: '06a9214036b8a15b512e03d534120006'
};
var forge_aes_test = {
var forgeAesTest = {
keySize: 128,
test_message: testData.generateBigString(1000)
testMessage: testData.generateBigString(1000)
};
test("SHA-1 Hash", 1, function() {
var sha1 = forge.md.sha1.create();
sha1.update(forge_aes_test.test_message);
sha1.update(forgeAesTest.testMessage);
var digest = sha1.digest().toHex();
ok(digest, digest);
});
test("SHA-256 Hash", 1, function() {
forge_rsa_test.md = forge.md.sha256.create();
forge_rsa_test.md.update(forge_aes_test.test_message);
var digest = forge_rsa_test.md.digest().toHex();
forgeRsaTest.md = forge.md.sha256.create();
forgeRsaTest.md.update(forgeAesTest.testMessage);
var digest = forgeRsaTest.md.digest().toHex();
ok(digest, digest);
});
test("HMAC SHA-256", 1, function() {
var key = util.base642Str(util.random(forge_aes_test.keySize));
var iv = util.base642Str(util.random(forge_aes_test.keySize));
var key = util.base642Str(util.random(forgeAesTest.keySize));
var iv = util.base642Str(util.random(forgeAesTest.keySize));
var hmac = forge.hmac.create();
hmac.start('sha256', key);
hmac.update(iv);
hmac.update(forge_aes_test.test_message);
hmac.update(forgeAesTest.testMessage);
var digest = hmac.digest().toHex();
ok(digest, digest);

View File

@ -1,65 +1,69 @@
module("Lawnchair DAO");
define(['js/dao/lawnchair-dao'], function(jsonDao) {
'use strict';
var lawnchairdao_test = {
user: 'lawnchair@test.com'
};
module("Lawnchair DAO");
asyncTest("Init", 2, function() {
// init dependencies
lawnchairdao_test.jsonDao = new app.dao.LawnchairDAO(Lawnchair);
lawnchairdao_test.jsonDao.init(lawnchairdao_test.user);
ok(lawnchairdao_test.jsonDao, 'LanwchairDAO');
// clear db before test
lawnchairdao_test.jsonDao.clear(function() {
ok(true, 'cleared db');
start();
});
});
asyncTest("CRUD object literal", 4, function() {
var key = 'type_asdf';
var data = {
name: 'testName',
type: 'testType'
var lawnchairdaoTest = {
user: 'lawnchair@test.com'
};
// create
lawnchairdao_test.jsonDao.persist(key, data, function() {
asyncTest("Init", 2, function() {
// init dependencies
jsonDao.init(lawnchairdaoTest.user);
ok(jsonDao, 'LanwchairDAO');
// read
lawnchairdao_test.jsonDao.read(key, function(read) {
equal(data.name, read.name, 'Create, Read');
// clear db before test
jsonDao.clear(function() {
ok(true, 'cleared db');
// list all
lawnchairdao_test.jsonDao.list('type', 0, null, function(list) {
ok(list.length === 1, 'List');
start();
});
});
// update
var newName = 'updatedName';
read.name = newName;
lawnchairdao_test.jsonDao.persist(key, read, function() {
asyncTest("CRUD object literal", 4, function() {
// read again
lawnchairdao_test.jsonDao.read(key, function(updated) {
equal(updated.name, newName, 'Update');
var key = 'type_asdf';
var data = {
name: 'testName',
type: 'testType'
};
// delete
lawnchairdao_test.jsonDao.remove(key, function() {
// create
jsonDao.persist(key, data, function() {
// should read empty
lawnchairdao_test.jsonDao.read(key, function(lastRead) {
equal(lastRead, null, 'Delete');
// read
jsonDao.read(key, function(read) {
equal(data.name, read.name, 'Create, Read');
start();
// list all
jsonDao.list('type', 0, null, function(list) {
ok(list.length === 1, 'List');
// update
var newName = 'updatedName';
read.name = newName;
jsonDao.persist(key, read, function() {
// read again
jsonDao.read(key, function(updated) {
equal(updated.name, newName, 'Update');
// delete
jsonDao.remove(key, function() {
// should read empty
jsonDao.read(key, function(lastRead) {
equal(lastRead, null, 'Delete');
start();
});
});
});
});
});
});
});
});
});
});

View File

@ -22,7 +22,9 @@ require(['../../src/require-config'], function() {
function startTests() {
require([
'test/unit/forge-test',
'test/unit/aes-test'
'test/unit/aes-test',
'test/unit/rsa-test',
'test/unit/lawnchair-dao-test'
], function() {
QUnit.start(); //Tests loaded, run tests

View File

@ -1,50 +1,53 @@
module("RSA Crypto");
define(['cryptoLib/rsa'], function(rsa) {
'use strict';
var rsa_test = {
keySize: 1024,
rsa: new cryptoLib.RSA(forge, new cryptoLib.Util(window, uuid)),
test_message: '06a9214036b8a15b512e03d534120006'
};
module("RSA Crypto");
asyncTest("Generate keypair", 1, function() {
rsa_test.rsa.generateKeypair(rsa_test.keySize, function(err) {
ok(!err);
var rsaTest = {
keySize: 1024,
testMessage: '06a9214036b8a15b512e03d534120006'
};
start();
asyncTest("Generate keypair", 1, function() {
rsa.generateKeypair(rsaTest.keySize, function(err) {
ok(!err);
start();
});
});
});
test("Export keys", 2, function() {
rsa_test.keypair = rsa_test.rsa.exportKeys();
test("Export keys", 2, function() {
rsaTest.keypair = rsa.exportKeys();
ok(rsa_test.keypair.pubkeyPem.indexOf('-----BEGIN PUBLIC KEY-----') === 0, rsa_test.keypair.pubkeyPem);
ok(rsa_test.keypair.privkeyPem.indexOf('-----BEGIN RSA PRIVATE KEY-----') === 0, rsa_test.keypair.privkeyPem);
});
ok(rsaTest.keypair.pubkeyPem.indexOf('-----BEGIN PUBLIC KEY-----') === 0, rsaTest.keypair.pubkeyPem);
ok(rsaTest.keypair.privkeyPem.indexOf('-----BEGIN RSA PRIVATE KEY-----') === 0, rsaTest.keypair.privkeyPem);
});
test("Init", 2, function() {
rsa_test.rsa.init(rsa_test.keypair.pubkeyPem, rsa_test.keypair.privkeyPem);
var exported = rsa_test.rsa.exportKeys();
test("Init", 2, function() {
rsa.init(rsaTest.keypair.pubkeyPem, rsaTest.keypair.privkeyPem);
var exported = rsa.exportKeys();
ok(exported.pubkeyPem.indexOf('-----BEGIN PUBLIC KEY-----') === 0);
ok(exported.privkeyPem.indexOf('-----BEGIN RSA PRIVATE KEY-----') === 0);
});
ok(exported.pubkeyPem.indexOf('-----BEGIN PUBLIC KEY-----') === 0);
ok(exported.privkeyPem.indexOf('-----BEGIN RSA PRIVATE KEY-----') === 0);
});
test("Encrypt", 1, function() {
rsa_test.ct = rsa_test.rsa.encrypt(rsa_test.test_message);
ok(rsa_test.ct);
});
test("Encrypt", 1, function() {
rsaTest.ct = rsa.encrypt(rsaTest.testMessage);
ok(rsaTest.ct);
});
test("Decrypt", 1, function() {
var pt = rsa_test.rsa.decrypt(rsa_test.ct);
equal(pt, rsa_test.test_message);
});
test("Decrypt", 1, function() {
var pt = rsa.decrypt(rsaTest.ct);
equal(pt, rsaTest.testMessage);
});
test("Sign", 1, function() {
rsa_test.sig = rsa_test.rsa.sign([btoa('iv'), btoa(rsa_test.test_message)]);
ok(rsa_test.sig);
});
test("Sign", 1, function() {
rsaTest.sig = rsa.sign([btoa('iv'), btoa(rsaTest.testMessage)]);
ok(rsaTest.sig);
});
test("Verify", 1, function() {
var res = rsa_test.rsa.verify([btoa('iv'), btoa(rsa_test.test_message)], rsa_test.sig);
ok(res);
test("Verify", 1, function() {
var res = rsa.verify([btoa('iv'), btoa(rsaTest.testMessage)], rsaTest.sig);
ok(res);
});
});