Skip to content
Snippets Groups Projects
Commit 6e019b07 authored by Jane Hoh's avatar Jane Hoh
Browse files

generate valid inputs with random strings

parent 2a6debf2
No related branches found
No related tags found
2 merge requests!16Brownian motion fuzzer,!13Fuzzer
This commit is part of merge request !13. Comments created here will be created in the context of that merge request.
import java.io.IOException; import java.io.IOException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
/* a stub for your team's fuzzer */ /* a stub for your team's fuzzer */
public class Fuzzer { public class Fuzzer {
private static final String OUTPUT_FILE = "fuzz.txt"; private static final String OUTPUT_FILE = "fuzz.txt";
private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz";
private static final String CHAR_UPPER = CHAR_LOWER.toUpperCase();
private static final String NUMBER = "0123456789";
private static final String DATA_FOR_RANDOM_STRING = CHAR_LOWER + CHAR_UPPER + NUMBER;
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
System.out.println(Instruction.getBNF()); System.out.println(Instruction.getBNF());
FileOutputStream out = null; FileOutputStream out = null;
PrintWriter pw = null; PrintWriter pw = null;
ArrayList<String> inputs = new ArrayList<String>();
try { try {
out = new FileOutputStream(OUTPUT_FILE); out = new FileOutputStream(OUTPUT_FILE);
pw = new PrintWriter(out); pw = new PrintWriter(out);
...@@ -19,7 +27,22 @@ public class Fuzzer { ...@@ -19,7 +27,22 @@ public class Fuzzer {
/* We just print one instruction. /* We just print one instruction.
Hint: you might want to make use of the instruction Hint: you might want to make use of the instruction
grammar which is effectively encoded in Instruction.java */ grammar which is effectively encoded in Instruction.java */
pw.println("list");
/**Using generation-based fuzzing and Instruction.java,
* create random, valid inputs*/
//Indicates the number of inputs we wish to generate
int numInputs = 10;
for(int i=0; i<numInputs; i++) {
String input = generateValidInputs();
//only add the input if it hasn't already been generated
if(inputAlreadyGenerated(input, inputs)) {
i--;
}else {
inputs.add(input);
pw.println(input);
}
}
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(System.err); e.printStackTrace(System.err);
...@@ -35,4 +58,65 @@ public class Fuzzer { ...@@ -35,4 +58,65 @@ public class Fuzzer {
} }
/**Generates random, valid inputs based on Instruction.java*/
public static String generateValidInputs() {
int maxStringLength = 100;
//add the instruction string
Instruction inst = getRandomInstruction();
String input = inst.getOpcode();
//add the operands
for (OperandType op : inst.getOperands()){
if(op.equals(OperandType.STRING)) {
input += " "+generateRandomString(maxStringLength);
}
}
//debug
//System.out.println(input);
return input;
}
/**Selects a random instruction*/
public static Instruction getRandomInstruction() {
Instruction[] INSTS = Instruction.values();
int index = new Random().nextInt(INSTS.length);
return INSTS[index];
}
/**Generates a random string.
* Sourced from: https://www.mkyong.com/java/java-how-to-generate-a-random-string/
* and modified*/
public static String generateRandomString(int maxlength) {
if (maxlength < 1) throw new IllegalArgumentException();
int length = new Random().nextInt(maxlength)+1;
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
// 0-62 (exclusive), random returns 0-61
int rndCharAt = new Random().nextInt(DATA_FOR_RANDOM_STRING.length());
char rndChar = DATA_FOR_RANDOM_STRING.charAt(rndCharAt);
sb.append(rndChar);
}
return sb.toString();
}
/**Checks whether the input has already been generated*/
public static boolean inputAlreadyGenerated(String newInput, ArrayList<String> inputs) {
for(String input:inputs) {
if(input.equals(newInput)) {
return true;
}
}
return false;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment