Skip to content
Snippets Groups Projects
Commit ae4deeb9 authored by MichaelThomas-1's avatar MichaelThomas-1
Browse files

Updated Fuzzer

- Added 'boundary tests'
- Fixed minimum int value
- Tweaked full stack chances
parent bd7368bf
No related branches found
No related tags found
No related merge requests found
Pipeline #58160 passed
...@@ -13,7 +13,7 @@ public class Fuzzer { ...@@ -13,7 +13,7 @@ public class Fuzzer {
private static final String OUTPUT_FILE = "fuzz.txt"; private static final String OUTPUT_FILE = "fuzz.txt";
// The percentage of outputs that will start with a full stack // The percentage of outputs that will start with a full stack
private static final int STACK_FULL_PERCENTAGE = 20; private static final int STACK_FULL_PERCENTAGE = 10;
// The percentage of inputs that will be incorrect // The percentage of inputs that will be incorrect
private static final int INPUT_ERROR_PERCENTAGE = 5; private static final int INPUT_ERROR_PERCENTAGE = 5;
// The percentage of instructions that will be incorrect // The percentage of instructions that will be incorrect
...@@ -28,7 +28,7 @@ public class Fuzzer { ...@@ -28,7 +28,7 @@ public class Fuzzer {
private static final int VAR_ASCII_MIN = 33; private static final int VAR_ASCII_MIN = 33;
private static final int VAR_ASCII_MAX = 127; private static final int VAR_ASCII_MAX = 127;
// Variable range // Variable range
private static final int VAR_MIN = -2147483647; private static final int VAR_MIN = -2147483648;
private static final int VAR_MAX = 2147483647; private static final int VAR_MAX = 2147483647;
// Maximum number of items in stack // Maximum number of items in stack
private static final int MAX_STACK_SIZE = 512; private static final int MAX_STACK_SIZE = 512;
...@@ -50,6 +50,7 @@ public class Fuzzer { ...@@ -50,6 +50,7 @@ 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.print(getStaticTests());
pw.println(generateInput(false, INSTRUCTION_MAX)); pw.println(generateInput(false, INSTRUCTION_MAX));
}catch (Exception e){ }catch (Exception e){
...@@ -226,5 +227,95 @@ public class Fuzzer { ...@@ -226,5 +227,95 @@ public class Fuzzer {
return min + rand.nextInt(max - min); return min + rand.nextInt(max - min);
} }
/***
* Get the desired static boundary tests
*
* @return a string containing all of the boundary tests' instructions
*/
private static String getStaticTests() {
// Note. Some operations will crash dc if performed, so they cannot be tested
// Examples include *, /, -, +, pop, store operations on empty stack
StringBuilder result = new StringBuilder();
// Test opreations that need variables on no variables defined
result.append(Instruction.LIST.getOpcode() + "\n");
result.append(Instruction.LOAD.getOpcode() + " VAR1\n");
result.append(Instruction.REM.getOpcode() + " VAR1\n");
// Test operations that use stack on empty stack
result.append(Instruction.PRINT.getOpcode() + "\n");
// Prep for next tests
result.append(Instruction.PUSH.getOpcode() + " 5\n");
result.append(Instruction.STORE.getOpcode() + " VAR1\n");
result.append(Instruction.PUSH.getOpcode() + " 10\n");
result.append(Instruction.STORE.getOpcode() + " VAR2\n");
result.append(Instruction.SAVE.getOpcode() + "\n");
// Test incorrect arguements for operation
result.append(Instruction.LOAD.getOpcode() + " VAR3\n");
// Test Variables stored and used corectly
result.append(Instruction.LIST.getOpcode() + "\n");
result.append(Instruction.PRINT.getOpcode() + "\n");
result.append(Instruction.LOAD.getOpcode() + " VAR2\n");
result.append(Instruction.LOAD.getOpcode() + " VAR1\n");
result.append(Instruction.PRINT.getOpcode() + "\n");
result.append(Instruction.POP.getOpcode() + "\n");
result.append(Instruction.PRINT.getOpcode() + "\n");
result.append(Instruction.REM.getOpcode() + " VAR2\n");
result.append(Instruction.LIST.getOpcode() + "\n");
result.append(Instruction.LOAD.getOpcode() + " VAR2\n");
result.append(Instruction.PRINT.getOpcode() + "\n");
result.append(Instruction.STORE.getOpcode() + " VAR1\n");
result.append(Instruction.LIST.getOpcode() + "\n");
result.append(Instruction.LOAD.getOpcode() + " VAR1\n");
result.append(Instruction.STORE.getOpcode() + " VAR1\n");
result.append(Instruction.LIST.getOpcode() + "\n");
result.append(Instruction.REM.getOpcode() + " VAR1\n");
// Test Plus operation
result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n");
result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MIN + "\n");
result.append(Instruction.PLUS.getOpcode() + "\n");
result.append(Instruction.POP.getOpcode() + "\n");
result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n");
result.append(Instruction.PUSH.getOpcode() + " 1\n");
result.append(Instruction.PLUS.getOpcode() + "\n");
result.append(Instruction.POP.getOpcode() + "\n");
// Test Subtract operation
result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n");
result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n");
result.append(Instruction.SUB.getOpcode() + "\n");
result.append(Instruction.POP.getOpcode() + "\n");
result.append(Instruction.PUSH.getOpcode() + " 1\n");
result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MIN + "\n");
result.append(Instruction.SUB.getOpcode() + "\n");
result.append(Instruction.POP.getOpcode() + "\n");
// Test Divide operation
result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n");
result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n");
result.append(Instruction.DIV.getOpcode() + "\n");
result.append(Instruction.POP.getOpcode() + "\n");
result.append(Instruction.PUSH.getOpcode() + " 1\n");
result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MIN + "\n");
result.append(Instruction.DIV.getOpcode() + "\n");
result.append(Instruction.POP.getOpcode() + "\n");
// Test Multiply operation
result.append(Instruction.PUSH.getOpcode() + " "+ VAR_MAX + "\n");
result.append(Instruction.PUSH.getOpcode() + " 2\n");
result.append(Instruction.MULT.getOpcode() + "\n");
result.append(Instruction.POP.getOpcode() + "\n");
result.append(Instruction.PUSH.getOpcode() + " 2\n");
result.append(Instruction.PUSH.getOpcode() + " "+ Math.floorDiv(VAR_MIN, 2) + "\n");
result.append(Instruction.MULT.getOpcode() + "\n");
result.append(Instruction.POP.getOpcode() + "\n");
return result.toString();
}
} }
...@@ -15,7 +15,7 @@ public enum Instruction { ...@@ -15,7 +15,7 @@ public enum Instruction {
LOAD("load",new OperandType[]{OperandType.STRING}, 0, 1, 1), //Note. Check variable existence LOAD("load",new OperandType[]{OperandType.STRING}, 0, 1, 1), //Note. Check variable existence
REM("remove",new OperandType[]{OperandType.STRING}, 0, 0, 1), //Note. Check variable existence REM("remove",new OperandType[]{OperandType.STRING}, 0, 0, 1), //Note. Check variable existence
STORE("store",new OperandType[]{OperandType.STRING}, 1, -1, 1), STORE("store",new OperandType[]{OperandType.STRING}, 1, -1, 1),
SAVE("save",new OperandType[]{OperandType.STRING}, 1, 0, 1), SAVE("save",new OperandType[]{OperandType.STRING}, 0, 0, 1),
LIST("list",new OperandType[]{}, 0, 0, 1), LIST("list",new OperandType[]{}, 0, 0, 1),
PRINT("print",new OperandType[]{}, 0, 0, 1); PRINT("print",new OperandType[]{}, 0, 0, 1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment