From ae4deeb9bacbeab6e46a8c45c2021e143834aca1 Mon Sep 17 00:00:00 2001
From: MichaelThomas-1 <greeman2.9@gmail.com>
Date: Thu, 22 Oct 2020 05:58:24 +1100
Subject: [PATCH] Updated Fuzzer - Added 'boundary tests' - Fixed minimum int
 value - Tweaked full stack chances

---
 fuzzer/Fuzzer.java      | 95 ++++++++++++++++++++++++++++++++++++++++-
 fuzzer/Instruction.java |  2 +-
 2 files changed, 94 insertions(+), 3 deletions(-)

diff --git a/fuzzer/Fuzzer.java b/fuzzer/Fuzzer.java
index 8b4caca..88e5fd3 100644
--- a/fuzzer/Fuzzer.java
+++ b/fuzzer/Fuzzer.java
@@ -13,7 +13,7 @@ public class Fuzzer {
     private static final String OUTPUT_FILE = "fuzz.txt";
     
     // 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
     private static final int INPUT_ERROR_PERCENTAGE = 5;
     // The percentage of instructions that will be incorrect
@@ -28,7 +28,7 @@ public class Fuzzer {
     private static final int VAR_ASCII_MIN = 33;
     private static final int VAR_ASCII_MAX = 127;
     // Variable range
-    private static final int VAR_MIN = -2147483647;
+    private static final int VAR_MIN = -2147483648;
     private static final int VAR_MAX = 2147483647;
     // Maximum number of items in stack
     private static final int MAX_STACK_SIZE = 512;
@@ -50,6 +50,7 @@ public class Fuzzer {
             /* We just print one instruction.
                Hint: you might want to make use of the instruction
                grammar which is effectively encoded in Instruction.java */
+            pw.print(getStaticTests());
             pw.println(generateInput(false, INSTRUCTION_MAX));
             
         }catch (Exception e){
@@ -226,5 +227,95 @@ public class Fuzzer {
         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();
+    }
 
 }
diff --git a/fuzzer/Instruction.java b/fuzzer/Instruction.java
index c9b509e..197299a 100644
--- a/fuzzer/Instruction.java
+++ b/fuzzer/Instruction.java
@@ -15,7 +15,7 @@ public enum Instruction {
     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
     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),
     PRINT("print",new OperandType[]{}, 0, 0, 1);
 
-- 
GitLab