-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Honor explicitly stated corpus directory
The JUnit integration always created corpus files in .cifuzz-corpus subdirectories, even if an explicit corpus directory was stated via command line. Now explicit corpus parameters are honored.
- Loading branch information
1 parent
1485531
commit 10954df
Showing
5 changed files
with
275 additions
and
1 deletion.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
examples/junit/src/test/java/com/example/CorpusDirectoryFuzzTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2023 Code Intelligence GmbH | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.example; | ||
|
||
import com.code_intelligence.jazzer.api.FuzzedDataProvider; | ||
import com.code_intelligence.jazzer.api.FuzzerSecurityIssueMedium; | ||
import com.code_intelligence.jazzer.junit.FuzzTest; | ||
|
||
public class CorpusDirectoryFuzzTest { | ||
private static int invocations = 0; | ||
|
||
@FuzzTest(maxDuration = "5s") | ||
public void corpusDirectoryFuzz(FuzzedDataProvider data) { | ||
// Throw on the third invocation to generate corpus entries. | ||
if (data.remainingBytes() == 0) { | ||
return; | ||
} | ||
// Add a few branch statements to generate different coverage. | ||
switch (invocations) { | ||
case 0: | ||
invocations++; | ||
break; | ||
case 1: | ||
invocations++; | ||
break; | ||
case 2: | ||
invocations++; | ||
break; | ||
case 3: | ||
throw new FuzzerSecurityIssueMedium(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
src/test/java/com/code_intelligence/jazzer/junit/CorpusDirectoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// Copyright 2023 Code Intelligence GmbH | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.code_intelligence.jazzer.junit; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static com.google.common.truth.Truth8.assertThat; | ||
import static org.junit.Assume.assumeFalse; | ||
import static org.junit.Assume.assumeTrue; | ||
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; | ||
import static org.junit.platform.testkit.engine.EventConditions.container; | ||
import static org.junit.platform.testkit.engine.EventConditions.displayName; | ||
import static org.junit.platform.testkit.engine.EventConditions.event; | ||
import static org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully; | ||
import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure; | ||
import static org.junit.platform.testkit.engine.EventConditions.test; | ||
import static org.junit.platform.testkit.engine.EventConditions.type; | ||
import static org.junit.platform.testkit.engine.EventConditions.uniqueIdSubstrings; | ||
import static org.junit.platform.testkit.engine.EventType.DYNAMIC_TEST_REGISTERED; | ||
import static org.junit.platform.testkit.engine.EventType.FINISHED; | ||
import static org.junit.platform.testkit.engine.EventType.REPORTING_ENTRY_PUBLISHED; | ||
import static org.junit.platform.testkit.engine.EventType.STARTED; | ||
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; | ||
|
||
import com.code_intelligence.jazzer.api.FuzzerSecurityIssueMedium; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.stream.Stream; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.platform.testkit.engine.EngineExecutionResults; | ||
import org.junit.platform.testkit.engine.EngineTestKit; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
public class CorpusDirectoryTest { | ||
private static final String ENGINE = "engine:junit-jupiter"; | ||
private static final String CLAZZ = "class:com.example.CorpusDirectoryFuzzTest"; | ||
private static final String INPUTS_FUZZ = | ||
"test-template:corpusDirectoryFuzz(com.code_intelligence.jazzer.api.FuzzedDataProvider)"; | ||
private static final String INVOCATION = "test-template-invocation:#"; | ||
|
||
@Rule public TemporaryFolder temp = new TemporaryFolder(); | ||
Path baseDir; | ||
|
||
@Before | ||
public void setup() { | ||
baseDir = temp.getRoot().toPath(); | ||
} | ||
|
||
@Test | ||
public void fuzzingEnabled() throws IOException { | ||
assumeFalse(System.getenv("JAZZER_FUZZ").isEmpty()); | ||
|
||
// Create a fake test resource directory structure with an inputs directory to verify that | ||
// Jazzer uses it and emits a crash file into it. | ||
Path artifactsDirectory = baseDir.resolve(Paths.get("src", "test", "resources", "com", | ||
"example", "CorpusDirectoryFuzzTestInputs", "corpusDirectoryFuzz")); | ||
Files.createDirectories(artifactsDirectory); | ||
|
||
// An explicitly stated corpus directory should be used to save new corpus entries. | ||
Path explicitGeneratedCorpus = baseDir.resolve(Paths.get("corpus")); | ||
Files.createDirectories(explicitGeneratedCorpus); | ||
|
||
// The default generated corpus directory should only be used if no explicit corpus directory | ||
// is given. | ||
Path defaultGeneratedCorpus = baseDir.resolve( | ||
Paths.get(".cifuzz-corpus", "com.example.CorpusDirectoryFuzzTest", "corpusDirectoryFuzz")); | ||
|
||
EngineExecutionResults results = | ||
EngineTestKit.engine("junit-jupiter") | ||
.selectors(selectClass("com.example.CorpusDirectoryFuzzTest")) | ||
.configurationParameter("jazzer.internal.basedir", baseDir.toAbsolutePath().toString()) | ||
// Add corpus directory as initial libFuzzer parameter. | ||
.configurationParameter("jazzer.internal.arg.0", "fake_test_argv0") | ||
.configurationParameter( | ||
"jazzer.internal.arg.1", explicitGeneratedCorpus.toAbsolutePath().toString()) | ||
.execute(); | ||
|
||
results.containerEvents().assertEventsMatchExactly(event(type(STARTED), container(ENGINE)), | ||
event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ))), | ||
event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))), | ||
event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ)), | ||
finishedSuccessfully()), | ||
event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ)), finishedSuccessfully()), | ||
event(type(FINISHED), container(ENGINE), finishedSuccessfully())); | ||
|
||
results.testEvents().assertEventsMatchExactly( | ||
event(type(DYNAMIC_TEST_REGISTERED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))), | ||
event(type(STARTED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ, INVOCATION + 1))), | ||
event(type(FINISHED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ, INVOCATION + 1)), | ||
displayName("<empty input>"), finishedSuccessfully()), | ||
event(type(DYNAMIC_TEST_REGISTERED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))), | ||
event(type(STARTED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ, INVOCATION + 2))), | ||
event(type(FINISHED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ, INVOCATION + 2)), | ||
displayName("seed"), finishedSuccessfully()), | ||
event(type(DYNAMIC_TEST_REGISTERED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))), | ||
event(type(STARTED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ, INVOCATION + 3)), | ||
displayName("Fuzzing...")), | ||
event(type(FINISHED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ, INVOCATION + 3)), | ||
displayName("Fuzzing..."), | ||
finishedWithFailure(instanceOf(FuzzerSecurityIssueMedium.class)))); | ||
|
||
// Crash file should be emitted into the artifacts directory and not into corpus directory. | ||
assertCrashFileExistsIn(artifactsDirectory); | ||
assertNoCrashFileExistsIn(baseDir); | ||
assertNoCrashFileExistsIn(explicitGeneratedCorpus); | ||
assertNoCrashFileExistsIn(defaultGeneratedCorpus); | ||
|
||
// Verify that corpus files are written to given corpus directory and not generated one. | ||
assertThat(Files.list(explicitGeneratedCorpus)).isNotEmpty(); | ||
assertThat(Files.list(defaultGeneratedCorpus)).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void fuzzingDisabled() throws IOException { | ||
assumeTrue(System.getenv("JAZZER_FUZZ").isEmpty()); | ||
|
||
Path corpusDirectory = baseDir.resolve(Paths.get("corpus")); | ||
Files.createDirectories(corpusDirectory); | ||
Files.createFile(corpusDirectory.resolve("corpus_entry")); | ||
|
||
EngineExecutionResults results = | ||
EngineTestKit.engine("junit-jupiter") | ||
.selectors(selectClass("com.example.CorpusDirectoryFuzzTest")) | ||
.configurationParameter("jazzer.internal.basedir", baseDir.toAbsolutePath().toString()) | ||
// Add corpus directory as initial libFuzzer parameter. | ||
.configurationParameter("jazzer.internal.arg.0", "fake_test_argv0") | ||
.configurationParameter( | ||
"jazzer.internal.arg.1", corpusDirectory.toAbsolutePath().toString()) | ||
.execute(); | ||
|
||
results.containerEvents().assertEventsMatchExactly(event(type(STARTED), container(ENGINE)), | ||
event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ))), | ||
event(type(STARTED), container(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))), | ||
event(type(REPORTING_ENTRY_PUBLISHED), | ||
container(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))), | ||
event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))), | ||
event(type(FINISHED), container(uniqueIdSubstrings(ENGINE, CLAZZ)), finishedSuccessfully()), | ||
event(type(FINISHED), container(ENGINE), finishedSuccessfully())); | ||
|
||
// Verify that corpus_entry is not picked up and corpus directory is ignored in regression mode. | ||
results.testEvents().assertEventsMatchExactly( | ||
event(type(DYNAMIC_TEST_REGISTERED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))), | ||
event(type(STARTED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ, INVOCATION + 1))), | ||
event(type(FINISHED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ, INVOCATION + 1)), | ||
displayName("<empty input>"), finishedSuccessfully()), | ||
event(type(DYNAMIC_TEST_REGISTERED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ))), | ||
event(type(STARTED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ, INVOCATION + 2))), | ||
event(type(FINISHED), test(uniqueIdSubstrings(ENGINE, CLAZZ, INPUTS_FUZZ, INVOCATION + 2)), | ||
displayName("seed"), finishedSuccessfully())); | ||
} | ||
|
||
private static void assertCrashFileExistsIn(Path artifactsDirectory) throws IOException { | ||
try (Stream<Path> crashFiles = | ||
Files.list(artifactsDirectory) | ||
.filter(path -> path.getFileName().toString().startsWith("crash-"))) { | ||
assertThat(crashFiles).isNotEmpty(); | ||
} | ||
} | ||
|
||
private static void assertNoCrashFileExistsIn(Path generatedCorpus) throws IOException { | ||
try (Stream<Path> crashFiles = | ||
Files.list(generatedCorpus) | ||
.filter(path -> path.getFileName().toString().startsWith("crash-"))) { | ||
assertThat(crashFiles).isEmpty(); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...it/test_resources_root/com/example/CorpusDirectoryFuzzTestInputs/corpusDirectoryFuzz/seed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
seed |