forked from jessexu20/Fuzzing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
133 lines (109 loc) · 3.2 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var test = require('tap').test,
//fuzzer = require('fuzzer'),
Random = require('random-js')
marqdown = require('./marqdown.js'),
fs = require('fs'),
//stackTrace = require('stack-trace')
stackTrace = require('stacktrace-parser')
;
var fuzzer =
{
random : new Random(Random.engines.mt19937().seed(0)),
seed: function (kernel)
{
fuzzer.random = new Random(Random.engines.mt19937().seed(kernel));
},
mutate:
{
string: function(val)
{
do{
// MUTATE IMPLEMENTATION HERE
var array = val.split('');
if( fuzzer.random.bool(0.05) )
{
//Reverse Input
array.reverse();
}
if(fuzzer.random.bool(0.25))
{
//Remove random set of characters
array.splice(fuzzer.random.integer(0, array.length), fuzzer.random.integer(0, array.length));
}
if(fuzzer.random.bool(0.25)){
//Insert 10 random characters
var randString = fuzzer.random.string(10);
var randArray = randString.split('');
array.splice.apply(array, [array.length -1, 0].concat(randArray));
}
}
while(fuzzer.random.bool(.05)) //Maybe Repeat
return array.join('');
}
}
};
fuzzer.seed(0);
var failedTests = [];
var reducedTests = [];
var passedTests = 0;
function mutationTesting()
{
var markDown = fs.readFileSync('test.md','utf-8');
var markDownSimple = fs.readFileSync('simple.md','utf-8');
for (var i = 0; i < 1000; i++) {
var mutuatedString;
if (i % 2 == 0){
mutuatedString = fuzzer.mutate.string(markDown);
}
else{
mutuatedString = fuzzer.mutate.string(markDownSimple);
}
try
{
marqdown.render(mutuatedString);
passedTests++;
}
catch(e)
{
failedTests.push( {input:mutuatedString, stack: e.stack} );
}
}
// RESULTS OF FUZZING
for( var i =0; i < failedTests.length; i++ )
{
var failed = failedTests[i];
var trace = stackTrace.parse( failed.stack );
var msg = failed.stack.split("\n")[0];
var skip = false;
for (tst in reducedTests){
console.log(JSON.stringify(tst) + "/n");
if((tst.line == trace[0].lineNumber) && (tst.method == trace[0].methodName)){
console.log("HERE");
skip = true;
}
}
if (!skip){
reducedTests.push({
line: trace[0].lineNumber,
method: trace[0].methodName
})
console.log( msg, trace[0].methodName, trace[0].lineNumber );
}
}
console.log( "passed {0}, failed {1}, reduced {2}".format(passedTests, failedTests.length, reducedTests.length) );
}
mutationTesting();
//test('markedMutation', function(t) {
//
//});
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}