Skip to content

Commit

Permalink
Zest: Support seed directory for both jqf-zest and maven plugin
Browse files Browse the repository at this point in the history
Addresses #63
  • Loading branch information
rohanpadhye committed Sep 26, 2019
1 parent 48d3b66 commit 3da34b8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
15 changes: 11 additions & 4 deletions fuzz/src/main/java/edu/berkeley/cs/jqf/fuzz/ei/ZestDriver.java
Expand Up @@ -43,7 +43,7 @@ public class ZestDriver {

public static void main(String[] args) {
if (args.length < 2){
System.err.println("Usage: java " + ZestDriver.class + " TEST_CLASS TEST_METHOD [OUTPUT_DIR [SEEDS...]]");
System.err.println("Usage: java " + ZestDriver.class + " TEST_CLASS TEST_METHOD [OUTPUT_DIR [SEED_DIR | SEED_FILES...]]");
System.exit(1);
}

Expand All @@ -62,9 +62,16 @@ public static void main(String[] args) {
try {
// Load the guidance
String title = testClassName+"#"+testMethodName;
ZestGuidance guidance = seedFiles != null ?
new ZestGuidance(title, null, outputDirectory, seedFiles) :
new ZestGuidance(title, null, outputDirectory);
ZestGuidance guidance = null;

if (seedFiles == null) {
guidance = new ZestGuidance(title, null, outputDirectory);
} else if (seedFiles.length == 1 && seedFiles[0].isDirectory()) {
guidance = new ZestGuidance(title, null, outputDirectory, seedFiles[0]);
} else {
guidance = new ZestGuidance(title, null, outputDirectory, seedFiles);
}


// Run the Junit test
Result res = GuidedFuzzing.run(testClassName, testMethodName, guidance, System.out);
Expand Down
27 changes: 26 additions & 1 deletion fuzz/src/main/java/edu/berkeley/cs/jqf/fuzz/ei/ZestGuidance.java
Expand Up @@ -272,13 +272,38 @@ public ZestGuidance(String testName, Duration duration, File outputDirectory) th
* @param seedInputFiles one or more input files to be used as initial inputs
* @throws IOException if the output directory could not be prepared
*/
public ZestGuidance(String testName, Duration duration, File outputDirectory, File... seedInputFiles) throws IOException {
public ZestGuidance(String testName, Duration duration, File outputDirectory, File[] seedInputFiles) throws IOException {
this(testName, duration, outputDirectory);
for (File seedInputFile : seedInputFiles) {
seedInputs.add(new SeedInput(seedInputFile));
}
}


/**
* Creates a new guidance instance.
*
* @param testName the name of test to display on the status screen
* @param duration the amount of time to run fuzzing for, where
* {@code null} indicates unlimited time.
* @param outputDirectory the directory where fuzzing results will be written
* @param seedInputDir the directory containing one or more input files to be used as initial inputs
* @throws IOException if the output directory could not be prepared
*/
public ZestGuidance(String testName, Duration duration, File outputDirectory, File seedInputDir) throws IOException {
this(testName, duration, outputDirectory);

if (!seedInputDir.isDirectory()) {
throw new IllegalArgumentException(String.format("%s is not a directory", seedInputDir));
}

File[] seedInputFiles = seedInputDir.listFiles();
for (File seedInputFile : seedInputFiles) {
seedInputs.add(new SeedInput(seedInputFile));
}
}


private void prepareOutputDirectory() throws IOException {

// Create the output directory if it does not exist
Expand Down
Expand Up @@ -142,6 +142,15 @@ public class FuzzGoal extends AbstractMojo {
@Parameter(property="blind")
private boolean blind;

/**
* The name of the input directory containing seed files.
*
* <p>If not provided, then fuzzing starts with randomly generated
* initial inputs.</p>
*/
@Parameter(property="in")
private String inputDirectory;

/**
* The name of the output directory where fuzzing results will
* be stored.
Expand Down Expand Up @@ -239,7 +248,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
try {
File resultsDir = new File(target, outputDirectory);
String targetName = testClassName + "#" + testMethod;
guidance = new ZestGuidance(targetName, duration, resultsDir);
if (inputDirectory != null) {
File seedsDir = new File(inputDirectory);
guidance = new ZestGuidance(targetName, duration, resultsDir, seedsDir);
} else {
guidance = new ZestGuidance(targetName, duration, resultsDir);
}
guidance.setBlind(blind);
} catch (IOException e) {
throw new MojoExecutionException("Could not create output directory", e);
Expand Down

0 comments on commit 3da34b8

Please sign in to comment.