Skip to content

Commit 6ccf724

Browse files
committed
Switch return value test to new test wrapper
1 parent 44fd213 commit 6ccf724

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

tests/helpers.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class FuzzTest {
4444
sync,
4545
verbose,
4646
coverage,
47+
expectedErrors,
4748
) {
4849
this.customHooks = customHooks;
4950
this.dictionaries = dictionaries;
@@ -61,6 +62,7 @@ class FuzzTest {
6162
this.sync = sync;
6263
this.verbose = verbose;
6364
this.coverage = coverage;
65+
this.expectedErrors = expectedErrors;
6466
}
6567

6668
execute() {
@@ -71,17 +73,19 @@ class FuzzTest {
7173
const options = ["jazzer", this.fuzzFile];
7274
options.push("-f " + this.fuzzEntryPoint);
7375
if (this.sync) options.push("--sync");
76+
if (this.customHooks) options.push("--custom_hooks=" + this.customHooks);
77+
if (this.dryRun) options.push("--dry_run=" + this.dryRun);
78+
if (this.coverage) options.push("--coverage");
7479
for (const bugDetector of this.disableBugDetectors) {
7580
options.push("--disable_bug_detectors=" + bugDetector);
7681
}
77-
78-
if (this.customHooks) {
79-
options.push("--custom_hooks=" + this.customHooks);
82+
for (const expectedError of this.expectedErrors) {
83+
options.push("-x=" + expectedError);
8084
}
81-
options.push("--dry_run=" + this.dryRun);
82-
if (this.coverage) options.push("--coverage");
85+
8386
options.push("--");
84-
options.push("-runs=" + this.runs);
87+
88+
if (this.runs !== undefined) options.push("-runs=" + this.runs);
8589
if (this.forkMode) options.push("-fork=" + this.forkMode);
8690
options.push("-seed=" + this.seed);
8791
for (const dictionary of this.dictionaries) {
@@ -92,7 +96,8 @@ class FuzzTest {
9296

9397
executeWithJest() {
9498
// Put together the libfuzzer options.
95-
const fuzzerOptions = ["-runs=" + this.runs, "-seed=" + this.seed];
99+
const fuzzerOptions = ["-seed=" + this.seed];
100+
if (this.runs !== undefined) fuzzerOptions.push("-runs=" + this.runs);
96101
const dictionaries = this.dictionaries.map(
97102
(dictionary) => "-dict=" + dictionary,
98103
);
@@ -125,6 +130,9 @@ class FuzzTest {
125130
}
126131

127132
runTest(cmd, options, env) {
133+
if (this.verbose) {
134+
console.log("Running: " + cmd + " " + options.join(" "));
135+
}
128136
const proc = spawnSync(cmd, options, {
129137
stdio: "pipe",
130138
cwd: this.dir,
@@ -150,7 +158,7 @@ class FuzzTestBuilder {
150158
_sync = false;
151159
_runs = 0;
152160
_verbose = false;
153-
_dryRun = false;
161+
_dryRun = undefined;
154162
_fuzzEntryPoint = "";
155163
_dir = "";
156164
_fuzzFile = "fuzz";
@@ -163,6 +171,7 @@ class FuzzTestBuilder {
163171
_jestRunInFuzzingMode = false;
164172
_dictionaries = [];
165173
_coverage = false;
174+
_expectedErrors = [];
166175

167176
/**
168177
* @param {boolean} sync - whether to run the fuzz test in synchronous mode.
@@ -302,6 +311,11 @@ class FuzzTestBuilder {
302311
return this;
303312
}
304313

314+
expectedErrors(...expectedError) {
315+
this._expectedErrors = expectedError;
316+
return this;
317+
}
318+
305319
build() {
306320
if (this._jestTestFile === "" && this._fuzzEntryPoint === "") {
307321
throw new Error("fuzzEntryPoint or jestTestFile are not set.");
@@ -328,6 +342,7 @@ class FuzzTestBuilder {
328342
this._sync,
329343
this._verbose,
330344
this._coverage,
345+
this._expectedErrors,
331346
);
332347
}
333348
}

tests/return_values/return_values.test.js

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,67 +14,63 @@
1414
* limitations under the License.
1515
*/
1616

17-
const { spawnSync } = require("child_process");
1817
const path = require("path");
19-
const SyncInfo =
20-
"Exclusively observed synchronous return values from fuzzed function. Fuzzing in synchronous mode seems beneficial!";
21-
const AsyncInfo =
22-
"Observed asynchronous return values from fuzzed function. Fuzzing in asynchronous mode seems beneficial!";
18+
const { FuzzTestBuilder } = require("../helpers.js");
2319

24-
// current working directory
2520
const testDirectory = __dirname;
21+
const syncInfo =
22+
"Exclusively observed synchronous return values from fuzzed function. Fuzzing in synchronous mode seems beneficial!";
23+
const asyncInfo =
24+
"Observed asynchronous return values from fuzzed function. Fuzzing in asynchronous mode seems beneficial!";
2625

2726
describe("Execute a sync runner", () => {
2827
it("Expect a hint due to async and sync return values", () => {
2928
const testCaseDir = path.join(testDirectory, "syncRunnerMixedReturns");
30-
const log = executeFuzzTest(true, false, testCaseDir);
31-
expect(log).toContain(AsyncInfo.trim());
29+
const log = executeFuzzTest(true, true, testCaseDir);
30+
expect(log).toContain(asyncInfo.trim());
3231
});
3332
it("Expect a hint due to exclusively async return values", () => {
3433
const testCaseDir = path.join(testDirectory, "syncRunnerAsyncReturns");
3534
const log = executeFuzzTest(true, false, testCaseDir);
36-
expect(log.trim()).toContain(AsyncInfo.trim());
35+
expect(log.trim()).toContain(asyncInfo.trim());
3736
});
3837
it("Expect no hint due to strict synchronous return values", () => {
3938
const testCaseDir = path.join(testDirectory, "syncRunnerSyncReturns");
4039
const log = executeFuzzTest(true, false, testCaseDir);
41-
expect(log.includes(SyncInfo)).toBeFalsy();
42-
expect(log.includes(AsyncInfo)).toBeFalsy();
40+
expect(log.includes(syncInfo)).toBeFalsy();
41+
expect(log.includes(asyncInfo)).toBeFalsy();
4342
});
4443
});
4544

4645
describe("Execute a async runner", () => {
4746
it("Expect no hint due to async and sync return values", () => {
4847
const testCaseDir = path.join(testDirectory, "asyncRunnerMixedReturns");
4948
const log = executeFuzzTest(false, false, testCaseDir);
50-
expect(log.includes(SyncInfo)).toBeFalsy();
51-
expect(log.includes(AsyncInfo)).toBeFalsy();
49+
expect(log.includes(syncInfo)).toBeFalsy();
50+
expect(log.includes(asyncInfo)).toBeFalsy();
5251
});
5352
it("Expect a hint due to exclusively sync return values", () => {
5453
const testCaseDir = path.join(testDirectory, "asyncRunnerSyncReturns");
5554
const log = executeFuzzTest(false, false, testCaseDir);
56-
expect(log.trim()).toContain(SyncInfo.trim());
55+
expect(log.trim()).toContain(syncInfo.trim());
5756
});
5857
it("Expect no hint due to strict asynchronous return values", () => {
5958
const testCaseDir = path.join(testDirectory, "asyncRunnerAsyncReturns");
6059
const log = executeFuzzTest(false, false, testCaseDir);
61-
expect(log.includes(SyncInfo)).toBeFalsy();
62-
expect(log.includes(AsyncInfo)).toBeFalsy();
60+
expect(log.includes(syncInfo)).toBeFalsy();
61+
expect(log.includes(asyncInfo)).toBeFalsy();
6362
});
6463
});
6564

6665
function executeFuzzTest(sync, verbose, dir) {
67-
let options = ["jazzer", "fuzz"];
68-
// Specify mode
69-
if (sync) options.push("--sync");
70-
options.push("--");
71-
const process = spawnSync("npx", options, {
72-
stdio: "pipe",
73-
cwd: dir,
74-
shell: true,
75-
windowsHide: true,
76-
});
77-
let stdout = process.output.toString();
78-
if (verbose) console.log(stdout);
79-
return stdout;
66+
const fuzzTest = new FuzzTestBuilder()
67+
.fuzzEntryPoint("fuzz")
68+
.runs(5000)
69+
.dir(dir)
70+
.sync(sync)
71+
.verbose(verbose)
72+
.expectedErrors("Error")
73+
.build();
74+
fuzzTest.execute();
75+
return fuzzTest.stderr;
8076
}

0 commit comments

Comments
 (0)