Use requirejs to manage dependencies for core logic

This commit is contained in:
Martin Kleinschrodt 2013-11-11 17:15:16 +01:00
parent 35a139fa1e
commit a070cbeffd
8 changed files with 106 additions and 47 deletions

View File

@ -17,5 +17,8 @@
"dependencies": {
"polymer": "~0.0.20131107"
},
"private": true
"private": true,
"devDependencies": {
"requirejs": "~2.1.9"
}
}

View File

@ -1,14 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<!-- 1. Load Polymer before any code that touches the DOM. -->
<script src="bower_components/requirejs/require.js"></script>
<script src="src/config.js"></script>
<!-- // <script src="../../polymer/polymer.js"></script> -->
<script src="bower_components/polymer/polymer.min.js"></script>
<script src="lib/sjcl.js"></script>
<script src="src/safe.js"></script>
<script src="src/crypto.js"></script>
<script src="src/model.js"></script>
<!-- 2. Load a component -->
<link rel="import" href="src/components/app.html">
<link rel="import" href="src/components/record-item.html">
<link rel="import" href="src/components/record-view.html">

12
src/config.js Normal file
View File

@ -0,0 +1,12 @@
require.config({
baseUrl: "../",
paths: {
"safe": "src/",
"sjcl": "lib/sjcl"
},
shim: {
"sjcl": {
exports: "sjcl"
}
}
});

View File

@ -1,4 +1,4 @@
safe.crypto = (function(sjcl) {
define(["sjcl"], function(sjcl) {
// Available cipher algorithms
var ciphers = {
AES: "AES"
@ -89,4 +89,4 @@ safe.crypto = (function(sjcl) {
decrypt: decrypt,
encrypt: encrypt
};
})(sjcl);
});

View File

@ -1,5 +1,6 @@
safe.model = (function() {
define(["safe/crypto"], function(crypto) {
function insert(arr, rec, i) {
i = i || 0;
return arr.slice(0, i).concat(rec).concat(arr.slice(i));
}
@ -94,10 +95,12 @@ safe.model = (function() {
});
return {
_private: {
insert: insert,
remove: remove
},
record: record,
collection: collection,
store: store,
remove: remove,
insert: insert
store: store
};
})();
});

View File

@ -1 +0,0 @@
safe = {};

View File

@ -8,10 +8,8 @@
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="../lib/sjcl.js"></script>
<script src="../src/safe.js"></script>
<script src="../src/crypto.js"></script>
<script src="../src/model.js"></script>
<script src="../bower_components/requirejs/require.js"></script>
<script src="../src/config.js"></script>
<script src="qunit.js"></script>
<script src="tests.js"></script>
</body>

View File

@ -1,42 +1,90 @@
module("safe.crypto");
require(["safe/crypto", "safe/model"], function(crypto, model) {
module("safe/crypto");
test("key generation", function() {
var keyLength = 256, pwd = "password";
test("key generation", function() {
var keyLength = 256, pwd = "password";
var key = safe.crypto.genKey(pwd, null, keyLength);
var key = crypto.genKey(pwd, null, keyLength);
// Make sure key is the right size
equal(key.key.length, keyLength/32);
// Make sure key is the right size
equal(key.key.length, keyLength/32);
var newKey = safe.crypto.genKey(pwd, key.salt, keyLength);
var newKey = crypto.genKey(pwd, key.salt, keyLength);
// Using the same password and salt should result in the same key
deepEqual(key.key, newKey.key);
deepEqual(key.salt, newKey.salt);
// Using the same password and salt should result in the same key
deepEqual(key.key, newKey.key);
deepEqual(key.salt, newKey.salt);
newKey = safe.crypto.genKey(pwd, null, keyLength);
newKey = crypto.genKey(pwd, null, keyLength);
// A key generated with new salt should turn out differently.
notDeepEqual(newKey.key, key.key);
});
// A key generated with new salt should turn out differently.
notDeepEqual(newKey.key, key.key);
});
test("encrypt/decrypt roundtrip", function() {
var pwd = "password", pt = "Hello World!";
var key = safe.crypto.genKey(pwd);
test("encrypt/decrypt roundtrip", function() {
var pwd = "password", pt = "Hello World!";
var key = crypto.genKey(pwd);
var c = safe.crypto.encrypt(key.key, pt);
var c = crypto.encrypt(key.key, pt);
// We should get back a _safe.crypto.container_ object
ok(safe.crypto.container.isPrototypeOf(c));
// We should get back a _crypto.container_ object
ok(crypto.container.isPrototypeOf(c));
// Encrypting the same value twice with the same key should
// result in two different cipher texts, since a new iv is randomly
// generated each time
var newC = safe.crypto.encrypt(key.key, pt);
notEqual(newC.ct, c.ct);
// Encrypting the same value twice with the same key should
// result in two different cipher texts, since a new iv is randomly
// generated each time
var newC = crypto.encrypt(key.key, pt);
notEqual(newC.ct, c.ct);
// Decrypted value should be equal to the original value
var dec = safe.crypto.decrypt(key.key, c);
// Decrypted value should be equal to the original value
var dec = crypto.decrypt(key.key, c);
equal(dec, pt);
equal(dec, pt);
});
module("safe/model");
test("insert (private)", function() {
// Insert single element at the correct position
var a = model._private.insert([0, 1, 2, 3, 4, 5], "a", 2);
deepEqual(a, [0, 1, "a", 2, 3, 4, 5]);
// Insert mutliple elements at the correct position
var b = model._private.insert([0, 1, 2, 3, 4, 5], ["hello", "world"], 3);
deepEqual(b, [0, 1, 2, "hello", "world", 3, 4, 5]);
// For negative indexes, count from the end backwards
var c = model._private.insert([0, 1, 2, 3, 4, 5], "a", -2);
deepEqual(c, [0, 1, 2, 3, "a", 4, 5]);
// Index should default to 0
var d = model._private.insert([0, 1, 2, 3, 4, 5], "a");
deepEqual(d, ["a", 0, 1, 2, 3, 4, 5]);
// An out-of-range index should result in the value being inserted at the end
var e = model._private.insert([0, 1, 2, 3, 4, 5], "a", 9);
deepEqual(e, [0, 1, 2, 3, 4, 5, "a"]);
});
test("remove (private)", function() {
// Remove single element
var a = model._private.remove(["a", "b", "c", "d", "e"], 3);
deepEqual(a, ["a", "b", "c", "e"]);
// Remove a range of elements
var b = model._private.remove(["a", "b", "c", "d", "e"], 1, 3);
deepEqual(b, ["a", "e"]);
// If upper bound is smaller then lower bound, ignore it
var c = model._private.remove(["a", "b", "c", "d", "e"], 1, -1);
deepEqual(c, ["a", "c", "d", "e"]);
// If upper bound is bigger than the length of the list, remove everything up to the end
var d = model._private.remove(["a", "b", "c", "d", "e"], 1, 10);
deepEqual(d, ["a"]);
// If lower bound is out-of-range, return a simple copy
var e = model._private.remove(["a", "b", "c", "d", "e"], 10);
deepEqual(e, ["a", "b", "c", "d", "e"]);
});
});