diff options
author | Catatonic Prime <catatonicprime@gmail.com> | 2014-09-11 18:42:21 -0400 |
---|---|---|
committer | Catatonic Prime <catatonicprime@gmail.com> | 2014-09-11 18:42:21 -0400 |
commit | ef3b0cd9b7122effc16419b9ca1b4efbe688d02c (patch) | |
tree | 57834a04d0566308f07bb0c8dbe305d80a0df59f | |
parent | 95a26d6115e23e8055841911e0a101e717e980f7 (diff) | |
download | sjcl-ef3b0cd9b7122effc16419b9ca1b4efbe688d02c.zip sjcl-ef3b0cd9b7122effc16419b9ca1b4efbe688d02c.tar.gz sjcl-ef3b0cd9b7122effc16419b9ca1b4efbe688d02c.tar.bz2 |
Fixing build stuff, cause I was missing files.
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | test/json_test.js | 126 |
2 files changed, 129 insertions, 2 deletions
@@ -57,7 +57,7 @@ lint: core.js core/*.js test/*.js browserTest/*.js lint/coding_guidelines.pl TEST_COMMON= browserTest/nodeUtil.js test/test.js TEST_SCRIPTS= $(TEST_COMMON) \ - test/aes_vectors.js test/aes_test.js test/json_test.js \ + test/aes_vectors.js test/aes_test.js \ test/bitArray_vectors.js test/bitArray_test.js \ test/ocb2_vectors.js test/ocb2_test.js \ test/ccm_vectors.js test/ccm_test.js \ @@ -72,7 +72,8 @@ TEST_SCRIPTS= $(TEST_COMMON) \ test/pbkdf2_test.js \ test/bn_vectors.js test/bn_test.js \ test/ecdsa_test.js test/ecdsa_vectors.js test/ecdh_test.js \ - test/srp_vectors.js test/srp_test.js + test/srp_vectors.js test/srp_test.js \ + test/json_test.js test/ecc_conv.js # Run all tests in node.js. diff --git a/test/json_test.js b/test/json_test.js new file mode 100644 index 0000000..b2110e2 --- /dev/null +++ b/test/json_test.js @@ -0,0 +1,126 @@ +/* + * Asserts that an object can be encoded to an expected string. + */ +new sjcl.test.TestCase("JSON Encode Test", function (cb) { + if(!sjcl.json) { + this.unimplemented(); + cb && cb(); + return; + } + + //Build up a standard object for encoding, this includes a nice wide variety of properties. + var obj = new Object(); + obj.int = 4; + obj.nint = -5; + obj.str = 'string'; + obj.iv = [ -95577995, -949876189, 1443400017, 697058741 ]; + obj.truth = true; + obj.lie = false; + + try { + var str = sjcl.json.encode(obj); + this.require(!(!str)); //Check for non-'falsey' + } + catch (e) { + //The standard object should encode just fine, so this is out of place. Fail. + this.fail(e); + } + cb && cb(); +}); + + +/* + * Asserts that a JSON string can be decoded to an expected object. + */ +new sjcl.test.TestCase("JSON Decode Test", function (cb) { + if(!sjcl.json) { + this.unimplemented(); + cb && cb(); + return; + } + + var str = ''; var i; + str = '{"int":4,"nint":-5,"str":"string","iv":[0,1,2,3],"truth":true,"lie":false}'; + try { + var obj = sjcl.json.decode(str); + this.require(obj.int === 4); + this.require(obj.nint === -5); + this.require(obj.str === 'string'); + this.require(obj.truth === true); + this.require(obj.lie === false); + for(i in obj.iv) { + this.require(obj.iv[i] == i); + } + } catch (e) { this.fail(e); } + + str = '{ "int" : 4, "nint" : -5,"str":"string", "iv": [0,1 , 2 ,3],"truth": true,"lie": false }'; + try { + var obj = sjcl.json.decode(str); + this.require(obj.int === 4); + this.require(obj.nint === -5); + this.require(obj.str === 'string'); + this.require(obj.truth === true); + this.require(obj.lie === false); + for(i in obj.iv) { + this.require(obj.iv[i] == i); + } + } catch (e) { this.fail(e); } + + //Tests passed, return. + cb && cb(); +}); + + +/* + * Asserts that an Object can be Encoded to a string that can be decoded to an equivalent object + * as well as the converse. + */ +new sjcl.test.TestCase("JSON Commutative Test", function (cb) { + if(!sjcl.json) { + this.unimplemented(); + cb && cb(); + return; + } + + var obj1 = new Object(); + obj1.int = 4; + obj1.nint = -5; + obj1.str = 'string'; + obj1.iv = [ -95577995, -949876189, 1443400017, 697058741 ]; + obj1.truth = true; + obj1.lie = false; + obj1.udef = undefined; + + var str1 = ''; + var str2 = ''; + var obj2; + try { + str1 = sjcl.json.encode(obj1); + obj2 = sjcl.json.decode(str1); + str2 = sjcl.json.encode(obj2); + } + catch (e) { + this.fail(e); + } + + try { + this.require(str1 == str2); + this.require(obj1.int == obj2.int); + this.require(obj1.str == obj2.str); + this.require(obj1.lie == obj2.lie); + this.require(obj1.nint == obj2.nint); + this.require(obj1.truth == obj2.truth); + + var i; + for(i in obj1.iv) + { + this.require(obj1.iv[i] == obj2.iv[i]); + } + } + catch (e) { + this.fail(e); + } + + //Tests passed. + cb && cb(); +}); |