1
0
mirror of https://github.com/moparisthebest/mail synced 2024-10-31 15:25:01 -04: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, "strict": true,
"globalstrict": true, "globalstrict": true,
"node": true, "node": true,

View File

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

View File

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

View File

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

View File

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

View File

@ -3,17 +3,17 @@ define(['cryptoLib/aes-cbc', 'cryptoLib/util', 'test/test-data'], function(aes,
module("AES Crypto"); module("AES Crypto");
var aes_test = { var aesTest = {
keySize: 128, keySize: 128,
test_message: testData.generateBigString(1000) testMessage: testData.generateBigString(1000)
}; };
test("CBC mode", 4, function() { test("CBC mode", 4, function() {
var plaintext = aes_test.test_message; var plaintext = aesTest.testMessage;
var key = util.random(aes_test.keySize); var key = util.random(aesTest.keySize);
var iv = util.random(aes_test.keySize); var iv = util.random(aesTest.keySize);
ok(key, 'Key: ' + key); 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); var ciphertext = aes.encrypt(plaintext, key, iv);
ok(ciphertext, 'Ciphertext lenght: ' + ciphertext.length); 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"); module("Forge Crypto");
var forge_rsa_test = { var forgeRsaTest = {
keySize: 1024, keySize: 1024,
test_message: '06a9214036b8a15b512e03d534120006' testMessage: '06a9214036b8a15b512e03d534120006'
}; };
var forge_aes_test = { var forgeAesTest = {
keySize: 128, keySize: 128,
test_message: testData.generateBigString(1000) testMessage: testData.generateBigString(1000)
}; };
test("SHA-1 Hash", 1, function() { test("SHA-1 Hash", 1, function() {
var sha1 = forge.md.sha1.create(); var sha1 = forge.md.sha1.create();
sha1.update(forge_aes_test.test_message); sha1.update(forgeAesTest.testMessage);
var digest = sha1.digest().toHex(); var digest = sha1.digest().toHex();
ok(digest, digest); ok(digest, digest);
}); });
test("SHA-256 Hash", 1, function() { test("SHA-256 Hash", 1, function() {
forge_rsa_test.md = forge.md.sha256.create(); forgeRsaTest.md = forge.md.sha256.create();
forge_rsa_test.md.update(forge_aes_test.test_message); forgeRsaTest.md.update(forgeAesTest.testMessage);
var digest = forge_rsa_test.md.digest().toHex(); var digest = forgeRsaTest.md.digest().toHex();
ok(digest, digest); ok(digest, digest);
}); });
test("HMAC SHA-256", 1, function() { test("HMAC SHA-256", 1, function() {
var key = util.base642Str(util.random(forge_aes_test.keySize)); var key = util.base642Str(util.random(forgeAesTest.keySize));
var iv = util.base642Str(util.random(forge_aes_test.keySize)); var iv = util.base642Str(util.random(forgeAesTest.keySize));
var hmac = forge.hmac.create(); var hmac = forge.hmac.create();
hmac.start('sha256', key); hmac.start('sha256', key);
hmac.update(iv); hmac.update(iv);
hmac.update(forge_aes_test.test_message); hmac.update(forgeAesTest.testMessage);
var digest = hmac.digest().toHex(); var digest = hmac.digest().toHex();
ok(digest, digest); ok(digest, digest);

View File

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

View File

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

View File

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