From e7185a8baa3f84ed64d4b828eb139ebac238188c Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Mon, 10 Jun 2013 22:02:29 +0200 Subject: [PATCH] rsa and lawnchair tests refactored --- .jshintrc | 1 - Gruntfile.js | 2 +- src/js/dao/lawnchair-dao.js | 21 ++++---- src/require-config.js | 7 ++- test/test-data.js | 6 +-- test/unit/aes-test.js | 12 ++--- test/unit/forge-test.js | 22 ++++---- test/unit/lawnchair-dao-test.js | 96 +++++++++++++++++---------------- test/unit/main.js | 4 +- test/unit/rsa-test.js | 77 +++++++++++++------------- 10 files changed, 132 insertions(+), 116 deletions(-) diff --git a/.jshintrc b/.jshintrc index d03208e..a690bc2 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,5 +1,4 @@ { - "indent": 4, "strict": true, "globalstrict": true, "node": true, diff --git a/Gruntfile.js b/Gruntfile.js index 3803ba3..9a2a8ee 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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' } diff --git a/src/js/dao/lawnchair-dao.js b/src/js/dao/lawnchair-dao.js index b2bd8b8..cabc42d 100644 --- a/src/js/dao/lawnchair-dao.js +++ b/src/js/dao/lawnchair-dao.js @@ -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); }; -}; \ No newline at end of file + return self; +}); \ No newline at end of file diff --git a/src/require-config.js b/src/require-config.js index 31cdad7..a28fe0e 100644 --- a/src/require-config.js +++ b/src/require-config.js @@ -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 diff --git a/test/test-data.js b/test/test-data.js index 6869ece..bbdabdc 100644 --- a/test/test-data.js +++ b/test/test-data.js @@ -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; diff --git a/test/unit/aes-test.js b/test/unit/aes-test.js index 52faacf..130546f 100644 --- a/test/unit/aes-test.js +++ b/test/unit/aes-test.js @@ -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); diff --git a/test/unit/forge-test.js b/test/unit/forge-test.js index 6b6db3e..e1b6692 100644 --- a/test/unit/forge-test.js +++ b/test/unit/forge-test.js @@ -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); diff --git a/test/unit/lawnchair-dao-test.js b/test/unit/lawnchair-dao-test.js index 0d1576b..ea9a918 100644 --- a/test/unit/lawnchair-dao-test.js +++ b/test/unit/lawnchair-dao-test.js @@ -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(); + }); }); - }); + }); }); }); }); }); }); + }); \ No newline at end of file diff --git a/test/unit/main.js b/test/unit/main.js index f5321f4..300b96a 100644 --- a/test/unit/main.js +++ b/test/unit/main.js @@ -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 diff --git a/test/unit/rsa-test.js b/test/unit/rsa-test.js index 95a7ae7..8f0fad5 100644 --- a/test/unit/rsa-test.js +++ b/test/unit/rsa-test.js @@ -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); + }); }); \ No newline at end of file