From 5449ad7bff9c52ba7c30ad5a392b80208c1048b5 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 2 Jan 2015 19:15:20 -0500 Subject: [PATCH 1/3] Add test case that incorrectly handles hexa string as json --- test/privatekey.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/privatekey.js b/test/privatekey.js index 7cae44bb1..3212bd4df 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -16,6 +16,7 @@ var invalidbase58 = require('./data/bitcoind/base58_keys_invalid.json'); describe('PrivateKey', function() { var hex = '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a'; + var hex2 = '8080808080808080808080808080808080808080808080808080808080808080'; var buf = new Buffer(hex, 'hex'); var wifTestnet = 'cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG'; var wifTestnetUncompressed = '92jJzK4tbURm1C7udQXxeCBvXHoHJstDXRxAMouPG1k1XUaXdsu'; @@ -31,6 +32,12 @@ describe('PrivateKey', function() { should.exist(b.bn); }); + it('should create a privatkey from hexa string', function() { + var a = new PrivateKey(hex2); + should.exist(a); + should.exist(a.bn); + }); + it('should create a new random testnet private key with only one argument', function() { var a = new PrivateKey(Networks.testnet); should.exist(a); From c20a0eabed129a13039040af1997aca384e33177 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 2 Jan 2015 19:32:49 -0500 Subject: [PATCH 2/3] Check type of parsed JSON to determine it's an object and not a number. --- lib/util/js.js | 8 +++++++- test/privatekey.js | 2 +- test/util/js.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 test/util/js.js diff --git a/lib/util/js.js b/lib/util/js.js index 85c128428..331606205 100644 --- a/lib/util/js.js +++ b/lib/util/js.js @@ -28,9 +28,15 @@ module.exports = { * @return {Object|boolean} false if the argument is not a JSON string. */ isValidJSON: function isValidJSON(arg) { + var parsed; try { - return JSON.parse(arg); + parsed = JSON.parse(arg); } catch (e) { + parsed = false; + } + if (typeof(parsed) === 'object') { + return true; + } else { return false; } }, diff --git a/test/privatekey.js b/test/privatekey.js index 3212bd4df..dcaab92b9 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -32,7 +32,7 @@ describe('PrivateKey', function() { should.exist(b.bn); }); - it('should create a privatkey from hexa string', function() { + it('should create a privatekey from hexa string', function() { var a = new PrivateKey(hex2); should.exist(a); should.exist(a.bn); diff --git a/test/util/js.js b/test/util/js.js new file mode 100644 index 000000000..197ccdf20 --- /dev/null +++ b/test/util/js.js @@ -0,0 +1,29 @@ +'use strict'; +/* jshint unused: false */ + +var should = require('chai').should(); +var expect = require('chai').expect; + +var bitcore = require('../..'); +var JSUtil = bitcore.util.js; + +describe('js utils', function() { + + describe('isValidJSON', function() { + + var hexa = '8080808080808080808080808080808080808080808080808080808080808080'; + var json = '{"key": ["value", "value2"]}'; + + it('does not mistake an integer as valid json object', function() { + var valid = JSUtil.isValidJSON(hexa); + valid.should.equal(false); + }); + + it('correctly validates a json object', function() { + var valid = JSUtil.isValidJSON(json); + valid.should.equal(true); + }); + + }); + +}); From a1ee393dce56539df8dbe14d11a855e503311dae Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 2 Jan 2015 19:38:22 -0500 Subject: [PATCH 3/3] JSUtil: Return result quicker for isValidJSON and add additional tests. --- lib/util/js.js | 5 ++--- test/util/js.js | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/util/js.js b/lib/util/js.js index 331606205..fffd33631 100644 --- a/lib/util/js.js +++ b/lib/util/js.js @@ -32,13 +32,12 @@ module.exports = { try { parsed = JSON.parse(arg); } catch (e) { - parsed = false; + return false; } if (typeof(parsed) === 'object') { return true; - } else { - return false; } + return false; }, isHexa: isHexa, isHexaString: isHexa, diff --git a/test/util/js.js b/test/util/js.js index 197ccdf20..b39230067 100644 --- a/test/util/js.js +++ b/test/util/js.js @@ -13,6 +13,7 @@ describe('js utils', function() { var hexa = '8080808080808080808080808080808080808080808080808080808080808080'; var json = '{"key": ["value", "value2"]}'; + var json2 = '["value", "value2", {"key": "value"}]'; it('does not mistake an integer as valid json object', function() { var valid = JSUtil.isValidJSON(hexa); @@ -24,6 +25,11 @@ describe('js utils', function() { valid.should.equal(true); }); + it('correctly validates an array json object', function() { + var valid = JSUtil.isValidJSON(json); + valid.should.equal(true); + }); + }); });