-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuzzer.test.js
159 lines (126 loc) · 5.02 KB
/
fuzzer.test.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
it('has a working test harness', () => {
expect(1).toBe(1);
});
it('allows instantiation of the fuzzer', () => {
let FuzzMeMaybe = require('./fuzzer');
let fuzzer = new FuzzMeMaybe();
expect(fuzzer).toBeInstanceOf(FuzzMeMaybe);
});
it('doesnt fuzz by default', () => {
let FuzzMeMaybe = require('./fuzzer');
let fuzzer = new FuzzMeMaybe();
let input = 'foo';
fuzzer.setRadamsaSeed('2');
let result = fuzzer.maybe(input);
expect(result).toBe(input);
});
it('allows setting of the seed as string or function', () => {
let FuzzMeMaybe = require('./fuzzer');
let fuzzer = new FuzzMeMaybe();
let seedString = `${Math.PI}`;
let seedFunction = () => {
return Math.PI;
};
fuzzer.setRadamsaSeed(seedString);
expect(fuzzer._fuzzer.options.seed).toBe(seedString);
fuzzer.setRadamsaSeed(seedFunction);
expect(fuzzer._fuzzer.options.seed).toBe(seedFunction);
});
describe('Fuzzer input from environment variables:', () => {
const EXISTING_ENV = process.env;
beforeEach(() => {
jest.resetModules();
process.env.FUZZER_ENABLED = 1;
});
afterEach(() => {
process.env = EXISTING_ENV;
});
it('respects enabled environment variable when fuzzing', () => {
let FuzzMeMaybe = require('./fuzzer');
let fuzzer = new FuzzMeMaybe();
let input = 'foo';
fuzzer.setRadamsaSeed('3');
let result = fuzzer.maybe(input);
expect(result).toBe('fono');
});
it('registers boolean flags and respects defaults', () => {
let FuzzMeMaybe = require('./fuzzer');
let fuzzer = new FuzzMeMaybe();
let input = 'foo';
fuzzer.setRadamsaSeed('3');
fuzzer.registerFlag('arbitrary_boolean', 'boolean', false);
expect(fuzzer.maybe(input, 'arbitrary_boolean')).toBe('foo');
expect(fuzzer.maybe(input)).toBe('fono');
});
it('registers boolean flags and respects changes on ENV', () => {
process.env.FUZZER_ARBITRARY_BOOLEAN = "1";
let FuzzMeMaybe = require('./fuzzer');
let fuzzer = new FuzzMeMaybe();
let input = 'foo';
fuzzer.setRadamsaSeed('3');
fuzzer.registerFlag('arbitrary_boolean', 'boolean', false);
expect(fuzzer.maybe(input, 'arbitrary_boolean')).toBe('fono');
expect(fuzzer.maybe(input)).toBe('fono');
});
it('registers skip string flags and respects defaults', () => {
let FuzzMeMaybe = require('./fuzzer');
let fuzzer = new FuzzMeMaybe();
fuzzer.setRadamsaSeed('3');
let dogInput = "This is DOG";
let catInput = "This is CAT";
let pigInput = "This is PIG";
fuzzer.registerFlag('animal', 'skip_string', 'DOG');
expect(fuzzer.maybe(dogInput, 'animal')).toBe(dogInput);
expect(fuzzer.maybe(catInput, 'animal')).toBe('This isn CAT');
expect(fuzzer.maybe(pigInput)).toBe('This isn PIG');
});
it('registers skip string flags and respects ENV', () => {
process.env.FUZZER_ANIMAL_SKIP_STRING = "CAT";
let FuzzMeMaybe = require('./fuzzer');
let fuzzer = new FuzzMeMaybe();
fuzzer.setRadamsaSeed('3');
let dogInput = "This is DOG";
let catInput = "This is CAT";
let pigInput = "This is PIG";
fuzzer.registerFlag('animal', 'skip_string', 'DOG');
expect(fuzzer.maybe(dogInput, 'animal')).toBe('This isn DOG');
expect(fuzzer.maybe(catInput, 'animal')).toBe('This is CAT');
expect(fuzzer.maybe(pigInput)).toBe('This isn PIG');
});
it('registers skip first flags and respects defaults', () => {
let FuzzMeMaybe = require('./fuzzer');
let fuzzer = new FuzzMeMaybe();
fuzzer.setRadamsaSeed('3');
let dogInput = "This is DOG";
let catInput = "This is CAT";
let pigInput = "This is PIG";
fuzzer.registerFlag('animal', 'skip_first', 5);
for (let i=1; i<=6; i++) {
if (i <= 5) {
expect(fuzzer.maybe(dogInput, 'animal')).toBe(dogInput);
expect(fuzzer.maybe(catInput)).toBe('This isn CAT');
} else {
expect(fuzzer.maybe(dogInput, 'animal')).toBe('This isn DOG');
}
}
expect(fuzzer.maybe(pigInput)).toBe('This isn PIG');
});
it('registers skip first flags and respects ENV', () => {
let FuzzMeMaybe = require('./fuzzer');
let fuzzer = new FuzzMeMaybe();
fuzzer.setRadamsaSeed('3');
let dogInput = "This is DOG";
let catInput = "This is CAT";
let pigInput = "This is PIG";
fuzzer.registerFlag('animal', 'skip_first', 5);
for (let i=1; i<=6; i++) {
if (i <= 5) {
expect(fuzzer.maybe(dogInput, 'animal')).toBe(dogInput);
expect(fuzzer.maybe(catInput)).toBe('This isn CAT');
} else {
expect(fuzzer.maybe(dogInput, 'animal')).toBe('This isn DOG');
}
}
expect(fuzzer.maybe(pigInput)).toBe('This isn PIG');
});
});