Skip to content

Commit 5d83cbd

Browse files
committed
jest-runner: Always instrument in fuzzing mode
1 parent 75e8f3c commit 5d83cbd

File tree

8 files changed

+252
-18
lines changed

8 files changed

+252
-18
lines changed

packages/jest-runner/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export function loadConfig(optionsKey = "jazzerjs"): Options {
6161
config.mode = "fuzzing";
6262
}
6363

64+
if (config.mode === "fuzzing") {
65+
config.dryRun = false;
66+
}
67+
6468
if (config.verbose) {
6569
process.env.JAZZER_DEBUG = "1";
6670
}

tests/bug-detectors/prototype-pollution.test.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -393,24 +393,6 @@ describe("Prototype Pollution Jest tests", () => {
393393
"Prototype Pollution: Prototype of Object changed",
394394
);
395395
});
396-
397-
it("Fuzzing mode instrumentation off - variable declaration", () => {
398-
const fuzzTest = new FuzzTestBuilder()
399-
.runs(0)
400-
.customHooks([
401-
path.join(bugDetectorDirectory, "instrument-all.config.js"),
402-
])
403-
.dir(bugDetectorDirectory)
404-
.dryRun(true)
405-
.jestRunInFuzzingMode(true)
406-
.jestTestFile("tests.fuzz.js")
407-
.jestTestName("Variable declarations")
408-
.build();
409-
expect(() => {
410-
fuzzTest.execute();
411-
}).toThrow();
412-
expect(fuzzTest.stderr).toContain("[Prototype Pollution Configuration]");
413-
});
414396
});
415397

416398
describe("Prototype Pollution instrumentation correctness tests", () => {
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2023 Code Intelligence GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const {
18+
FuzzTestBuilder,
19+
FuzzingExitCode,
20+
WindowsExitCode,
21+
} = require("../helpers.js");
22+
const path = require("path");
23+
const fs = require("fs");
24+
25+
describe("Jest integration", () => {
26+
const projectDir = path.join(__dirname, "jest_project");
27+
const jestTestFile = "integration.fuzz";
28+
29+
beforeEach(() => {
30+
fs.rmSync(path.join(projectDir, ".jazzerjsrc.json"), {
31+
force: true,
32+
});
33+
fs.rmSync(path.join(projectDir, ".cifuzz-corpus"), {
34+
force: true,
35+
recursive: true,
36+
});
37+
fs.rmSync(path.join(projectDir, jestTestFile), {
38+
force: true,
39+
recursive: true,
40+
});
41+
});
42+
43+
describe("Fuzzing mode", () => {
44+
const fuzzingExitCode =
45+
process.platform === "win32" ? WindowsExitCode : FuzzingExitCode;
46+
const fuzzTestBuilder = new FuzzTestBuilder()
47+
.dir(projectDir)
48+
.runs(1_000_000)
49+
.jestRunInFuzzingMode(true)
50+
.jestTestFile(jestTestFile + ".js");
51+
52+
it("executes sync test", () => {
53+
const fuzzTest = fuzzTestBuilder
54+
.jestTestName("execute sync test")
55+
.build();
56+
expect(() => {
57+
fuzzTest.execute();
58+
}).toThrow(fuzzingExitCode);
59+
});
60+
61+
it("execute async test", () => {
62+
const fuzzTest = fuzzTestBuilder
63+
.jestTestName("execute async test")
64+
.build();
65+
expect(() => {
66+
fuzzTest.execute();
67+
}).toThrow(fuzzingExitCode);
68+
});
69+
70+
it("execute async test returning a promise", () => {
71+
const fuzzTest = fuzzTestBuilder
72+
.jestTestName("execute async test returning a promise")
73+
.build();
74+
expect(() => {
75+
fuzzTest.execute();
76+
}).toThrow(fuzzingExitCode);
77+
});
78+
79+
it("execute async test using a callback", () => {
80+
const fuzzTest = fuzzTestBuilder
81+
.jestTestName("execute async test using a callback")
82+
.build();
83+
expect(() => {
84+
fuzzTest.execute();
85+
}).toThrow(fuzzingExitCode);
86+
});
87+
});
88+
89+
describe("Regression mode", () => {
90+
const regressionTestBuilder = new FuzzTestBuilder()
91+
.dir(projectDir)
92+
.jestTestFile(jestTestFile + ".js");
93+
94+
it("executes sync test", () => {
95+
const fuzzTest = regressionTestBuilder
96+
.jestTestName("execute sync test")
97+
.build()
98+
.execute();
99+
});
100+
101+
it("execute async test", () => {
102+
const fuzzTest = regressionTestBuilder
103+
.jestTestName("execute async test")
104+
.build()
105+
.execute();
106+
});
107+
108+
it("execute async test returning a promise", () => {
109+
const fuzzTest = regressionTestBuilder
110+
.jestTestName("execute async test returning a promise")
111+
.build()
112+
.execute();
113+
});
114+
115+
it("execute async test using a callback", () => {
116+
const fuzzTest = regressionTestBuilder
117+
.jestTestName("execute async test using a callback")
118+
.build()
119+
.execute();
120+
});
121+
});
122+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.jazzerjsrc.json
2+
.cifuzz-corpus
3+
integration.fuzz
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2023 Code Intelligence GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const target = require("./target.js");
18+
19+
describe("Jest Integration", () => {
20+
it.fuzz("execute sync test", (data) => {
21+
target.fuzzMe(data);
22+
});
23+
24+
it.fuzz("execute async test", async (data) => {
25+
await target.asyncFuzzMe(data);
26+
});
27+
28+
it.fuzz("execute async test returning a promise", (data) => {
29+
return target.asyncFuzzMe(data);
30+
});
31+
32+
it.fuzz("execute async test using a callback", (data, done) => {
33+
target.callbackFuzzMe(data, done);
34+
});
35+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "jazzerjs-jest-integration-tests-project",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"test": "jest",
6+
"fuzz": "JAZZER_FUZZ=1 jest "
7+
},
8+
"devDependencies": {
9+
"@jazzer.js/jest-runner": "file:../../../packages/jest-runner",
10+
"jest": "^29.3.1"
11+
},
12+
"jest": {
13+
"projects": [
14+
{
15+
"displayName": "test"
16+
},
17+
{
18+
"runner": "@jazzer.js/jest-runner",
19+
"displayName": {
20+
"name": "Jazzer.js",
21+
"color": "cyan"
22+
},
23+
"testMatch": [
24+
"<rootDir>/**/*.fuzz.js"
25+
]
26+
}
27+
]
28+
}
29+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023 Code Intelligence GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const fuzzMe = function (data) {
18+
if (data.toString() === "Awesome") {
19+
throw Error("Welcome to Awesome Fuzzing!");
20+
}
21+
};
22+
23+
const asyncFuzzMe = function (data) {
24+
return new Promise((resolve, reject) => {
25+
try {
26+
fuzzMe(data);
27+
resolve();
28+
} catch (e) {
29+
reject(e);
30+
}
31+
});
32+
};
33+
34+
const callbackFuzzMe = function (data, done) {
35+
setImmediate(() => {
36+
try {
37+
fuzzMe(data);
38+
done();
39+
} catch (e) {
40+
done(e);
41+
}
42+
});
43+
};
44+
45+
module.exports.fuzzMe = fuzzMe;
46+
module.exports.asyncFuzzMe = asyncFuzzMe;
47+
module.exports.callbackFuzzMe = callbackFuzzMe;

tests/jest_integration/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "jazzerjs-jest-integration-tests",
3+
"version": "1.0.0",
4+
"description": "Jest integration tests.",
5+
"scripts": {
6+
"fuzz": "jest"
7+
},
8+
"devDependencies": {
9+
"@types/jest": "^29.5.3",
10+
"jest": "^29.6.2"
11+
}
12+
}

0 commit comments

Comments
 (0)