From 4b8d7598c89e28bc3f7d38c8ca935aa5cd5d88ab Mon Sep 17 00:00:00 2001 From: Mickael van der Beek Date: Tue, 18 Nov 2014 23:51:26 +0100 Subject: [PATCH] Refactoring of the module. The validator still needs improvement and specialisation if applied to node-forward/discussions#15. --- package.json | 2 +- src/cerberus.js | 6 ++-- src/{ => fuzzer}/fuzzer.js | 4 +-- src/{ => fuzzer}/payloads/arrays.js | 4 +-- src/{ => fuzzer}/payloads/booleans.js | 0 src/{ => fuzzer}/payloads/buffers.js | 1 - src/{ => fuzzer}/payloads/dates.js | 0 src/{ => fuzzer}/payloads/errors.js | 0 src/{ => fuzzer}/payloads/falsies.js | 0 src/{ => fuzzer}/payloads/functions.js | 0 src/{ => fuzzer}/payloads/numbers.js | 0 src/{ => fuzzer}/payloads/objects.js | 0 src/{ => fuzzer}/payloads/regexps.js | 0 src/fuzzer/payloads/strings.js | 42 +++++++++++++++++++++++ src/payloads/strings.js | 32 ----------------- src/{ => validator}/validator.js | 16 +++++---- src/{ => validator}/validators/formats.js | 0 src/{ => validator}/validators/types.js | 0 test/validator/negative-test-common.js | 5 ++- test/validator/test-common.js | 5 ++- test/validator/test-optional-mode.js | 5 ++- test/validator/test-strict-mode.js | 3 +- 22 files changed, 65 insertions(+), 60 deletions(-) rename src/{ => fuzzer}/fuzzer.js (99%) rename src/{ => fuzzer}/payloads/arrays.js (58%) rename src/{ => fuzzer}/payloads/booleans.js (100%) rename src/{ => fuzzer}/payloads/buffers.js (92%) rename src/{ => fuzzer}/payloads/dates.js (100%) rename src/{ => fuzzer}/payloads/errors.js (100%) rename src/{ => fuzzer}/payloads/falsies.js (100%) rename src/{ => fuzzer}/payloads/functions.js (100%) rename src/{ => fuzzer}/payloads/numbers.js (100%) rename src/{ => fuzzer}/payloads/objects.js (100%) rename src/{ => fuzzer}/payloads/regexps.js (100%) create mode 100644 src/fuzzer/payloads/strings.js delete mode 100644 src/payloads/strings.js rename src/{ => validator}/validator.js (91%) rename src/{ => validator}/validators/formats.js (100%) rename src/{ => validator}/validators/types.js (100%) diff --git a/package.json b/package.json index 3485ac0..b155e3f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "node-cerberus", "private": false, "version": "0.0.2", - "description": "Cerberus aka. Fluffy, is a validation and fuzzing library for arguments of Node.js core modules.", + "description": "Cerberus aka. Fluffy, is a validation and fuzzing library for Node.js core module method parameters.", "main": "src/cerberus.js", "scripts": { "test": "grunt ci" diff --git a/src/cerberus.js b/src/cerberus.js index cac78bd..8726026 100644 --- a/src/cerberus.js +++ b/src/cerberus.js @@ -1,4 +1,5 @@ -var Fuzzer = require('./fuzzer'); +var Validator = require('./validator/validator'); +var Fuzzer = require('./fuzzer/fuzzer'); module.exports = (function () { 'use strict'; @@ -11,9 +12,6 @@ module.exports = (function () { }; - Fuzzer.fuzzModules(); - // console.log(Fuzzer.generatePayloads()); - return Cerberus; })(); diff --git a/src/fuzzer.js b/src/fuzzer/fuzzer.js similarity index 99% rename from src/fuzzer.js rename to src/fuzzer/fuzzer.js index 93c0cb6..a1b88a5 100644 --- a/src/fuzzer.js +++ b/src/fuzzer/fuzzer.js @@ -48,11 +48,11 @@ module.exports = (function () { this.modules = { crypto: crypto - } + }; this.schemas = { crypto: CryptoSchema - } + }; } Fuzzer.prototype.fuzzModules = function (config) { diff --git a/src/payloads/arrays.js b/src/fuzzer/payloads/arrays.js similarity index 58% rename from src/payloads/arrays.js rename to src/fuzzer/payloads/arrays.js index 0be30d0..704b8df 100644 --- a/src/payloads/arrays.js +++ b/src/fuzzer/payloads/arrays.js @@ -1,9 +1,9 @@ -var list = ['a']; +var array = ['a']; module.exports = [ [], [1, 'a'], - (list['test'] = 1) && list, + (array.a = 1) && array, new Array(), new Array(5) ]; diff --git a/src/payloads/booleans.js b/src/fuzzer/payloads/booleans.js similarity index 100% rename from src/payloads/booleans.js rename to src/fuzzer/payloads/booleans.js diff --git a/src/payloads/buffers.js b/src/fuzzer/payloads/buffers.js similarity index 92% rename from src/payloads/buffers.js rename to src/fuzzer/payloads/buffers.js index 2311af2..9226c5c 100644 --- a/src/payloads/buffers.js +++ b/src/fuzzer/payloads/buffers.js @@ -1,7 +1,6 @@ module.exports = [ new Buffer(0), new Buffer(1), - new Buffer(10, 'binary'), new Buffer('fl∂∏', 'utf8'), new Buffer('fl∂∏', 'ucs2'), new Buffer('fl∂∏', 'utf16le'), diff --git a/src/payloads/dates.js b/src/fuzzer/payloads/dates.js similarity index 100% rename from src/payloads/dates.js rename to src/fuzzer/payloads/dates.js diff --git a/src/payloads/errors.js b/src/fuzzer/payloads/errors.js similarity index 100% rename from src/payloads/errors.js rename to src/fuzzer/payloads/errors.js diff --git a/src/payloads/falsies.js b/src/fuzzer/payloads/falsies.js similarity index 100% rename from src/payloads/falsies.js rename to src/fuzzer/payloads/falsies.js diff --git a/src/payloads/functions.js b/src/fuzzer/payloads/functions.js similarity index 100% rename from src/payloads/functions.js rename to src/fuzzer/payloads/functions.js diff --git a/src/payloads/numbers.js b/src/fuzzer/payloads/numbers.js similarity index 100% rename from src/payloads/numbers.js rename to src/fuzzer/payloads/numbers.js diff --git a/src/payloads/objects.js b/src/fuzzer/payloads/objects.js similarity index 100% rename from src/payloads/objects.js rename to src/fuzzer/payloads/objects.js diff --git a/src/payloads/regexps.js b/src/fuzzer/payloads/regexps.js similarity index 100% rename from src/payloads/regexps.js rename to src/fuzzer/payloads/regexps.js diff --git a/src/fuzzer/payloads/strings.js b/src/fuzzer/payloads/strings.js new file mode 100644 index 0000000..3dae637 --- /dev/null +++ b/src/fuzzer/payloads/strings.js @@ -0,0 +1,42 @@ +module.exports = [ + '', + '`', + '´', + '_', + '.', + '-', + '+', + '˙', + '"', + '<', + '>', + ';', + '&', + '@', + '--', + '\\', + '//', + '\n', + '\r', + '\'', + '://', + '\n\r', + '%00', + '\\x00', + '\u0000', + '\\0', + 'null', + 'undefined', + new Buffer(0x32).toString('utf8'), + String.fromCharCode(0), + String.fromCharCode(1), + String.fromCharCode(Math.pow(2, 8)), + String.fromCharCode(Math.pow(2, 8) + 1), + String.fromCharCode(Math.pow(2, 8) - 1), + String.fromCharCode(Math.pow(2, 16)), + String.fromCharCode(Math.pow(2, 16) + 1), + String.fromCharCode(Math.pow(2, 16) - 1), + String.fromCharCode(Math.pow(2, 32)), + String.fromCharCode(Math.pow(2, 32) + 1), + String.fromCharCode(Math.pow(2, 32) - 1) +]; diff --git a/src/payloads/strings.js b/src/payloads/strings.js deleted file mode 100644 index 3559516..0000000 --- a/src/payloads/strings.js +++ /dev/null @@ -1,32 +0,0 @@ -module.exports = [ - '', - '`', - '´', - '_', - '.', - '-', - '+', - '˙', - '"', - '<', - '>', - ';', - '&', - '@', - '--', - '\\', - '//', - '\n', - '\r', - '\'', - '://', - '\n\r', - '%00', - '\x00', - '\u0000', - '\0', - 'null', - 'fl∂∏ı', - 'undefined', - new Buffer(0x64).toString() -]; diff --git a/src/validator.js b/src/validator/validator.js similarity index 91% rename from src/validator.js rename to src/validator/validator.js index ec0ec88..6314558 100644 --- a/src/validator.js +++ b/src/validator/validator.js @@ -8,11 +8,13 @@ module.exports = (function () { function Validator () { this.config = { + wildcardKey: '$', + typeStrict: true, formatStrict: true, existenceStrict: true, - nullAsExistence: false, + nullAsExistence: true, undefinedAsExistence: false }; } @@ -22,7 +24,7 @@ module.exports = (function () { for (var key in (config || {})) { val = config[key]; - if (key in this.config && (typeof val === 'boolean')) { + if (typeof this.config[key] === typeof val) { this.config[key] = val; } } @@ -55,20 +57,20 @@ module.exports = (function () { var isArray = util.isArray(object); - var diffMismatch = Math.abs(len - Object.keys(object).length); - var hasDiffMismatch = !isArray && diffMismatch !== 0; + var diffMissmatch = Math.abs(len - Object.keys(object).length); + var hasDiffMissmatch = !isArray && diffMissmatch !== 0; while (len-- && valid) { key = keys[len]; - if (isArray && key === '$') { + if (isArray && key === this.config.wildcardKey) { valid = this.loopArray(schema[key], object); } else if (!(key in object)) { valid = schema[key].optional || !this.config.existenceStrict; if (valid) { - diffMismatch -= 1; + diffMissmatch -= 1; } } else if ((val = object[key]) == null) { @@ -87,7 +89,7 @@ module.exports = (function () { } } - return (!hasDiffMismatch || diffMismatch === 0) && valid; + return (!hasDiffMissmatch || diffMissmatch === 0) && valid; }; Validator.prototype.loopArray = function (schema, array) { diff --git a/src/validators/formats.js b/src/validator/validators/formats.js similarity index 100% rename from src/validators/formats.js rename to src/validator/validators/formats.js diff --git a/src/validators/types.js b/src/validator/validators/types.js similarity index 100% rename from src/validators/types.js rename to src/validator/validators/types.js diff --git a/test/validator/negative-test-common.js b/test/validator/negative-test-common.js index bc4f317..8583de0 100644 --- a/test/validator/negative-test-common.js +++ b/test/validator/negative-test-common.js @@ -1,7 +1,6 @@ var assert = require('assert'); -var Validator = require('../coverage/instrument/src/validator'); -// var Validator = require('../../src/Validator'); +var Validator = require('../coverage/instrument/src/validator/validator'); module.exports = function () { 'use strict'; @@ -160,7 +159,7 @@ module.exports = function () { }), false); }); - it('missing-key, array, $-selector, multi-key', function () { + it('missing-key, array, $ wildcard key selector, multi-key', function () { assert.deepEqual(Validator.validate({ a: { type: 'Array', diff --git a/test/validator/test-common.js b/test/validator/test-common.js index 7c7f896..c35ca53 100644 --- a/test/validator/test-common.js +++ b/test/validator/test-common.js @@ -1,7 +1,6 @@ var assert = require('assert'); -var Validator = require('../coverage/instrument/src/validator'); -// var Validator = require('../../src/Validator'); +var Validator = require('../coverage/instrument/src/validator/validator'); module.exports = function () { 'use strict'; @@ -185,7 +184,7 @@ module.exports = function () { }), true); }); - it('array, $-selector, multi-key', function () { + it('array, $ wildcard key selector, multi-key', function () { assert.deepEqual(Validator.validate({ a: { type: 'Array', diff --git a/test/validator/test-optional-mode.js b/test/validator/test-optional-mode.js index 50f2c41..2c21a89 100644 --- a/test/validator/test-optional-mode.js +++ b/test/validator/test-optional-mode.js @@ -1,7 +1,6 @@ var assert = require('assert'); -var Validator = require('../coverage/instrument/src/validator'); -// var Validator = require('../../src/Validator'); +var Validator = require('../coverage/instrument/src/validator/validator'); module.exports = function () { 'use strict'; @@ -42,7 +41,7 @@ module.exports = function () { }), true); }); - it('unspecified null value', function () { + it('unspecified undefined value', function () { Validator.configure({ nullAsExistence: false }); diff --git a/test/validator/test-strict-mode.js b/test/validator/test-strict-mode.js index df65171..5f39b8c 100644 --- a/test/validator/test-strict-mode.js +++ b/test/validator/test-strict-mode.js @@ -1,7 +1,6 @@ var assert = require('assert'); -var Validator = require('../coverage/instrument/src/validator'); -// var Validator = require('../../src/Validator'); +var Validator = require('../coverage/instrument/src/validator/validator'); module.exports = function () { 'use strict';