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:
parent
07325b8ed0
commit
e7185a8baa
@ -1,5 +1,4 @@
|
|||||||
{
|
{
|
||||||
"indent": 4,
|
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"globalstrict": true,
|
"globalstrict": true,
|
||||||
"node": true,
|
"node": true,
|
||||||
|
@ -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'
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
});
|
@ -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
|
||||||
|
@ -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;
|
||||||
|
@ -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);
|
||||||
|
@ -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);
|
||||||
|
@ -1,65 +1,69 @@
|
|||||||
module("Lawnchair DAO");
|
define(['js/dao/lawnchair-dao'], function(jsonDao) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var lawnchairdao_test = {
|
module("Lawnchair DAO");
|
||||||
user: 'lawnchair@test.com'
|
|
||||||
};
|
|
||||||
|
|
||||||
asyncTest("Init", 2, function() {
|
var lawnchairdaoTest = {
|
||||||
// init dependencies
|
user: 'lawnchair@test.com'
|
||||||
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'
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// create
|
asyncTest("Init", 2, function() {
|
||||||
lawnchairdao_test.jsonDao.persist(key, data, function() {
|
// init dependencies
|
||||||
|
jsonDao.init(lawnchairdaoTest.user);
|
||||||
|
ok(jsonDao, 'LanwchairDAO');
|
||||||
|
|
||||||
// read
|
// clear db before test
|
||||||
lawnchairdao_test.jsonDao.read(key, function(read) {
|
jsonDao.clear(function() {
|
||||||
equal(data.name, read.name, 'Create, Read');
|
ok(true, 'cleared db');
|
||||||
|
|
||||||
// list all
|
start();
|
||||||
lawnchairdao_test.jsonDao.list('type', 0, null, function(list) {
|
});
|
||||||
ok(list.length === 1, 'List');
|
});
|
||||||
|
|
||||||
// update
|
asyncTest("CRUD object literal", 4, function() {
|
||||||
var newName = 'updatedName';
|
|
||||||
read.name = newName;
|
|
||||||
lawnchairdao_test.jsonDao.persist(key, read, function() {
|
|
||||||
|
|
||||||
// read again
|
var key = 'type_asdf';
|
||||||
lawnchairdao_test.jsonDao.read(key, function(updated) {
|
var data = {
|
||||||
equal(updated.name, newName, 'Update');
|
name: 'testName',
|
||||||
|
type: 'testType'
|
||||||
|
};
|
||||||
|
|
||||||
// delete
|
// create
|
||||||
lawnchairdao_test.jsonDao.remove(key, function() {
|
jsonDao.persist(key, data, function() {
|
||||||
|
|
||||||
// should read empty
|
// read
|
||||||
lawnchairdao_test.jsonDao.read(key, function(lastRead) {
|
jsonDao.read(key, function(read) {
|
||||||
equal(lastRead, null, 'Delete');
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
@ -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
|
||||||
|
@ -1,50 +1,53 @@
|
|||||||
module("RSA Crypto");
|
define(['cryptoLib/rsa'], function(rsa) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var rsa_test = {
|
module("RSA Crypto");
|
||||||
keySize: 1024,
|
|
||||||
rsa: new cryptoLib.RSA(forge, new cryptoLib.Util(window, uuid)),
|
|
||||||
test_message: '06a9214036b8a15b512e03d534120006'
|
|
||||||
};
|
|
||||||
|
|
||||||
asyncTest("Generate keypair", 1, function() {
|
var rsaTest = {
|
||||||
rsa_test.rsa.generateKeypair(rsa_test.keySize, function(err) {
|
keySize: 1024,
|
||||||
ok(!err);
|
testMessage: '06a9214036b8a15b512e03d534120006'
|
||||||
|
};
|
||||||
|
|
||||||
start();
|
asyncTest("Generate keypair", 1, function() {
|
||||||
|
rsa.generateKeypair(rsaTest.keySize, function(err) {
|
||||||
|
ok(!err);
|
||||||
|
|
||||||
|
start();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Export keys", 2, function() {
|
||||||
|
rsaTest.keypair = rsa.exportKeys();
|
||||||
|
|
||||||
|
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.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);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Encrypt", 1, function() {
|
||||||
|
rsaTest.ct = rsa.encrypt(rsaTest.testMessage);
|
||||||
|
ok(rsaTest.ct);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Decrypt", 1, function() {
|
||||||
|
var pt = rsa.decrypt(rsaTest.ct);
|
||||||
|
equal(pt, rsaTest.testMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Sign", 1, function() {
|
||||||
|
rsaTest.sig = rsa.sign([btoa('iv'), btoa(rsaTest.testMessage)]);
|
||||||
|
ok(rsaTest.sig);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Verify", 1, function() {
|
||||||
|
var res = rsa.verify([btoa('iv'), btoa(rsaTest.testMessage)], rsaTest.sig);
|
||||||
|
ok(res);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Export keys", 2, function() {
|
|
||||||
rsa_test.keypair = rsa_test.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);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("Init", 2, function() {
|
|
||||||
rsa_test.rsa.init(rsa_test.keypair.pubkeyPem, rsa_test.keypair.privkeyPem);
|
|
||||||
var exported = rsa_test.rsa.exportKeys();
|
|
||||||
|
|
||||||
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("Decrypt", 1, function() {
|
|
||||||
var pt = rsa_test.rsa.decrypt(rsa_test.ct);
|
|
||||||
equal(pt, rsa_test.test_message);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("Sign", 1, function() {
|
|
||||||
rsa_test.sig = rsa_test.rsa.sign([btoa('iv'), btoa(rsa_test.test_message)]);
|
|
||||||
ok(rsa_test.sig);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("Verify", 1, function() {
|
|
||||||
var res = rsa_test.rsa.verify([btoa('iv'), btoa(rsa_test.test_message)], rsa_test.sig);
|
|
||||||
ok(res);
|
|
||||||
});
|
|
Loading…
Reference in New Issue
Block a user