Skip to content

Commit

Permalink
Refactoring of the module. The validator still needs improvement and …
Browse files Browse the repository at this point in the history
…specialisation if applied to node-forward/discussions#15.
  • Loading branch information
Mickael van der Beek committed Nov 18, 2014
1 parent dc0d318 commit 4b8d759
Show file tree
Hide file tree
Showing 22 changed files with 65 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 2 additions & 4 deletions src/cerberus.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Fuzzer = require('./fuzzer');
var Validator = require('./validator/validator');
var Fuzzer = require('./fuzzer/fuzzer');

module.exports = (function () {
'use strict';
Expand All @@ -11,9 +12,6 @@ module.exports = (function () {

};

Fuzzer.fuzzModules();
// console.log(Fuzzer.generatePayloads());

return Cerberus;

})();
4 changes: 2 additions & 2 deletions src/fuzzer.js → src/fuzzer/fuzzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ module.exports = (function () {

this.modules = {
crypto: crypto
}
};

this.schemas = {
crypto: CryptoSchema
}
};
}

Fuzzer.prototype.fuzzModules = function (config) {
Expand Down
4 changes: 2 additions & 2 deletions src/payloads/arrays.js → src/fuzzer/payloads/arrays.js
Original file line number Diff line number Diff line change
@@ -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)
];
File renamed without changes.
1 change: 0 additions & 1 deletion src/payloads/buffers.js → src/fuzzer/payloads/buffers.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
42 changes: 42 additions & 0 deletions src/fuzzer/payloads/strings.js
Original file line number Diff line number Diff line change
@@ -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)
];
32 changes: 0 additions & 32 deletions src/payloads/strings.js

This file was deleted.

16 changes: 9 additions & 7 deletions src/validator.js → src/validator/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ module.exports = (function () {

function Validator () {
this.config = {
wildcardKey: '$',

typeStrict: true,
formatStrict: true,
existenceStrict: true,

nullAsExistence: false,
nullAsExistence: true,
undefinedAsExistence: false
};
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -87,7 +89,7 @@ module.exports = (function () {
}
}

return (!hasDiffMismatch || diffMismatch === 0) && valid;
return (!hasDiffMissmatch || diffMissmatch === 0) && valid;
};

Validator.prototype.loopArray = function (schema, array) {
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 2 additions & 3 deletions test/validator/negative-test-common.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
Expand Down
5 changes: 2 additions & 3 deletions test/validator/test-common.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
Expand Down
5 changes: 2 additions & 3 deletions test/validator/test-optional-mode.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -42,7 +41,7 @@ module.exports = function () {
}), true);
});

it('unspecified null value', function () {
it('unspecified undefined value', function () {
Validator.configure({
nullAsExistence: false
});
Expand Down
3 changes: 1 addition & 2 deletions test/validator/test-strict-mode.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit 4b8d759

Please sign in to comment.