Skip to content

Commit 4b8d759

Browse files
author
Mickael van der Beek
committed
Refactoring of the module. The validator still needs improvement and specialisation if applied to node-forward/discussions#15.
1 parent dc0d318 commit 4b8d759

22 files changed

+65
-60
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "node-cerberus",
33
"private": false,
44
"version": "0.0.2",
5-
"description": "Cerberus aka. Fluffy, is a validation and fuzzing library for arguments of Node.js core modules.",
5+
"description": "Cerberus aka. Fluffy, is a validation and fuzzing library for Node.js core module method parameters.",
66
"main": "src/cerberus.js",
77
"scripts": {
88
"test": "grunt ci"

src/cerberus.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var Fuzzer = require('./fuzzer');
1+
var Validator = require('./validator/validator');
2+
var Fuzzer = require('./fuzzer/fuzzer');
23

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

1213
};
1314

14-
Fuzzer.fuzzModules();
15-
// console.log(Fuzzer.generatePayloads());
16-
1715
return Cerberus;
1816

1917
})();

src/fuzzer.js src/fuzzer/fuzzer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ module.exports = (function () {
4848

4949
this.modules = {
5050
crypto: crypto
51-
}
51+
};
5252

5353
this.schemas = {
5454
crypto: CryptoSchema
55-
}
55+
};
5656
}
5757

5858
Fuzzer.prototype.fuzzModules = function (config) {
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var list = ['a'];
1+
var array = ['a'];
22

33
module.exports = [
44
[],
55
[1, 'a'],
6-
(list['test'] = 1) && list,
6+
(array.a = 1) && array,
77
new Array(),
88
new Array(5)
99
];
File renamed without changes.

src/payloads/buffers.js src/fuzzer/payloads/buffers.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports = [
22
new Buffer(0),
33
new Buffer(1),
4-
new Buffer(10, 'binary'),
54
new Buffer('fl∂∏', 'utf8'),
65
new Buffer('fl∂∏', 'ucs2'),
76
new Buffer('fl∂∏', 'utf16le'),
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.

src/fuzzer/payloads/strings.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module.exports = [
2+
'',
3+
'`',
4+
'´',
5+
'_',
6+
'.',
7+
'-',
8+
'+',
9+
'˙',
10+
'"',
11+
'<',
12+
'>',
13+
';',
14+
'&',
15+
'@',
16+
'--',
17+
'\\',
18+
'//',
19+
'\n',
20+
'\r',
21+
'\'',
22+
'://',
23+
'\n\r',
24+
'%00',
25+
'\\x00',
26+
'\u0000',
27+
'\\0',
28+
'null',
29+
'undefined',
30+
new Buffer(0x32).toString('utf8'),
31+
String.fromCharCode(0),
32+
String.fromCharCode(1),
33+
String.fromCharCode(Math.pow(2, 8)),
34+
String.fromCharCode(Math.pow(2, 8) + 1),
35+
String.fromCharCode(Math.pow(2, 8) - 1),
36+
String.fromCharCode(Math.pow(2, 16)),
37+
String.fromCharCode(Math.pow(2, 16) + 1),
38+
String.fromCharCode(Math.pow(2, 16) - 1),
39+
String.fromCharCode(Math.pow(2, 32)),
40+
String.fromCharCode(Math.pow(2, 32) + 1),
41+
String.fromCharCode(Math.pow(2, 32) - 1)
42+
];

src/payloads/strings.js

-32
This file was deleted.

src/validator.js src/validator/validator.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ module.exports = (function () {
88

99
function Validator () {
1010
this.config = {
11+
wildcardKey: '$',
12+
1113
typeStrict: true,
1214
formatStrict: true,
1315
existenceStrict: true,
1416

15-
nullAsExistence: false,
17+
nullAsExistence: true,
1618
undefinedAsExistence: false
1719
};
1820
}
@@ -22,7 +24,7 @@ module.exports = (function () {
2224

2325
for (var key in (config || {})) {
2426
val = config[key];
25-
if (key in this.config && (typeof val === 'boolean')) {
27+
if (typeof this.config[key] === typeof val) {
2628
this.config[key] = val;
2729
}
2830
}
@@ -55,20 +57,20 @@ module.exports = (function () {
5557

5658
var isArray = util.isArray(object);
5759

58-
var diffMismatch = Math.abs(len - Object.keys(object).length);
59-
var hasDiffMismatch = !isArray && diffMismatch !== 0;
60+
var diffMissmatch = Math.abs(len - Object.keys(object).length);
61+
var hasDiffMissmatch = !isArray && diffMissmatch !== 0;
6062

6163
while (len-- && valid) {
6264
key = keys[len];
6365

64-
if (isArray && key === '$') {
66+
if (isArray && key === this.config.wildcardKey) {
6567
valid = this.loopArray(schema[key], object);
6668
}
6769
else if (!(key in object)) {
6870
valid = schema[key].optional || !this.config.existenceStrict;
6971

7072
if (valid) {
71-
diffMismatch -= 1;
73+
diffMissmatch -= 1;
7274
}
7375
}
7476
else if ((val = object[key]) == null) {
@@ -87,7 +89,7 @@ module.exports = (function () {
8789
}
8890
}
8991

90-
return (!hasDiffMismatch || diffMismatch === 0) && valid;
92+
return (!hasDiffMissmatch || diffMissmatch === 0) && valid;
9193
};
9294

9395
Validator.prototype.loopArray = function (schema, array) {
File renamed without changes.
File renamed without changes.

test/validator/negative-test-common.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var assert = require('assert');
22

3-
var Validator = require('../coverage/instrument/src/validator');
4-
// var Validator = require('../../src/Validator');
3+
var Validator = require('../coverage/instrument/src/validator/validator');
54

65
module.exports = function () {
76
'use strict';
@@ -160,7 +159,7 @@ module.exports = function () {
160159
}), false);
161160
});
162161

163-
it('missing-key, array, $-selector, multi-key', function () {
162+
it('missing-key, array, $ wildcard key selector, multi-key', function () {
164163
assert.deepEqual(Validator.validate({
165164
a: {
166165
type: 'Array',

test/validator/test-common.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var assert = require('assert');
22

3-
var Validator = require('../coverage/instrument/src/validator');
4-
// var Validator = require('../../src/Validator');
3+
var Validator = require('../coverage/instrument/src/validator/validator');
54

65
module.exports = function () {
76
'use strict';
@@ -185,7 +184,7 @@ module.exports = function () {
185184
}), true);
186185
});
187186

188-
it('array, $-selector, multi-key', function () {
187+
it('array, $ wildcard key selector, multi-key', function () {
189188
assert.deepEqual(Validator.validate({
190189
a: {
191190
type: 'Array',

test/validator/test-optional-mode.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var assert = require('assert');
22

3-
var Validator = require('../coverage/instrument/src/validator');
4-
// var Validator = require('../../src/Validator');
3+
var Validator = require('../coverage/instrument/src/validator/validator');
54

65
module.exports = function () {
76
'use strict';
@@ -42,7 +41,7 @@ module.exports = function () {
4241
}), true);
4342
});
4443

45-
it('unspecified null value', function () {
44+
it('unspecified undefined value', function () {
4645
Validator.configure({
4746
nullAsExistence: false
4847
});

test/validator/test-strict-mode.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var assert = require('assert');
22

3-
var Validator = require('../coverage/instrument/src/validator');
4-
// var Validator = require('../../src/Validator');
3+
var Validator = require('../coverage/instrument/src/validator/validator');
54

65
module.exports = function () {
76
'use strict';

0 commit comments

Comments
 (0)