diff --git a/.vscode/settings.json b/.vscode/settings.json index 4784c9f..2c454d1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -38,6 +38,6 @@ ], "sonarlint.connectedMode.project": { "connectionId": "mattrixwvSonarqube", - "projectKey": "mattrixwv_cipherstreamjava_AYGcdy79opaC7KAbzMEs" + "projectKey": "CipherStreamJava" } } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 872ef9b..daf037a 100644 --- a/pom.xml +++ b/pom.xml @@ -63,12 +63,7 @@ org.mockito mockito-core 5.3.0 - - - - org.mockito - mockito-junit-jupiter - 5.3.0 + test diff --git a/src/main/java/com/mattrixwv/cipherstream/combination/ADFGVX.java b/src/main/java/com/mattrixwv/cipherstream/combination/ADFGVX.java index 32a5969..d928d68 100644 --- a/src/main/java/com/mattrixwv/cipherstream/combination/ADFGVX.java +++ b/src/main/java/com/mattrixwv/cipherstream/combination/ADFGVX.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java //Mattrixwv // Created: 01-26-22 -//Modified: 07-09-22 +//Modified: 04-14-23 package com.mattrixwv.cipherstream.combination; @@ -18,11 +18,11 @@ import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare; public class ADFGVX{ - private static final Logger logger = LoggerFactory.getLogger(ADFGVX.class); + protected static Logger logger = LoggerFactory.getLogger(ADFGVX.class); //Internal classes - private class LargePolybiusSquare extends PolybiusSquare{ - private static final Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class); + protected class LargePolybiusSquare extends PolybiusSquare{ + protected static final Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class); @Override protected void createGrid(){ @@ -183,38 +183,38 @@ public class ADFGVX{ } } //Fields - private boolean preserveCapitals; //Whether to respect capitals in the output string - private boolean preserveWhitespace; //Whether to respect whitespace in the output string - private boolean preserveSymbols; //Whether to respect symbols in the output string - private String inputString; //The string that needs encoded/decoded - private String outputString; //The string that is output after encoding/decoding - private String squareKeyword; //The keyword used in the Polybius Square - private String keyword; //The Keyword used in the Columnar cipher + protected boolean preserveCapitals; //Whether to respect capitals in the output string + protected boolean preserveWhitespace; //Whether to respect whitespace in the output string + protected boolean preserveSymbols; //Whether to respect symbols in the output string + protected String inputString; //The string that needs encoded/decoded + protected String outputString; //The string that is output after encoding/decoding + protected String squareKeyword; //The keyword used in the Polybius Square + protected String keyword; //The Keyword used in the Columnar cipher //Internal ciphers - private LargePolybiusSquare largePolybiusSquare; //The first step in encoding - private Columnar columnar; //The second step in encoding + protected LargePolybiusSquare largePolybiusSquare; //The first step in encoding + protected Columnar columnar; //The second step in encoding //Ensures Polybius keyword constraints - private void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{ + protected void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{ if(squareKeyword == null){ throw new InvalidKeywordException("Square Keyword cannot be null"); } - logger.debug("Square Keyword '{}'", squareKeyword); + logger.debug("squareKeyword = {}", squareKeyword); this.squareKeyword = squareKeyword; } //Ensures Columnar keyword constraints - private void setKeyword(String keyword) throws InvalidKeywordException{ + protected void setKeyword(String keyword) throws InvalidKeywordException{ if(keyword == null){ throw new InvalidKeywordException("Keyword cannot be null"); } - logger.debug("Keyword '{}'", keyword); + logger.debug("keyword = {}", keyword); this.keyword = keyword; } //Ensures inputString constraints - private void setInputString(String inputString) throws InvalidInputException{ + protected void setInputString(String inputString) throws InvalidInputException{ if(inputString == null){ throw new InvalidInputException("Input cannot be null"); } @@ -246,31 +246,37 @@ public class ADFGVX{ } } //Format the output string with capitals, symbols, and numbers that are in the input string - private void formatOutputStringEncode(){ - logger.debug("Formatting output string"); + protected void formatOutputStringEncode(){ + logger.debug("Formatting output string to match input string"); StringBuilder output = new StringBuilder(); int outputLocation = 0; for(char ch : inputString.toCharArray()){ - logger.debug("Input string {}", ch); + logger.debug("Input character {}", ch); if(Character.isUpperCase(ch)){ + logger.debug("Converting output to uppercase"); + output.append(Character.toUpperCase(outputString.charAt(outputLocation++))); output.append(Character.toUpperCase(outputString.charAt(outputLocation++))); } else if(Character.isLowerCase(ch)){ + logger.debug("Converting output to lowercase"); + output.append(Character.toLowerCase(outputString.charAt(outputLocation++))); output.append(Character.toLowerCase(outputString.charAt(outputLocation++))); } else{ + logger.debug("Appending symbol to output"); + output.append(ch); } } - logger.debug("Saving output string '{}'", output); outputString = output.toString(); + logger.debug("Saving output string '{}'", outputString); } - private void formatOutputStringDecode(){ - logger.debug("Formatting output string"); + protected void formatOutputStringDecode(){ + logger.debug("Formatting output string to match input string"); StringBuilder output = new StringBuilder(); int outputLocation = 0; @@ -278,23 +284,29 @@ public class ADFGVX{ char ch = inputString.charAt(inputLocation); logger.debug("Input character {}", ch); if(Character.isUpperCase(ch)){ + logger.debug("Converting output to uppercase"); + output.append(Character.toUpperCase(outputString.charAt(outputLocation++))); ++inputLocation; } else if(Character.isLowerCase(ch)){ + logger.debug("Converting output to lowercase"); + output.append(Character.toLowerCase(outputString.charAt(outputLocation++))); ++inputLocation; } else{ + logger.debug("Appending symbol to output"); + output.append(ch); } } - logger.debug("Saving output string '{}'", output); outputString = output.toString(); + logger.debug("Saving output string '{}'", outputString); } //Encodes the inputString and stores the result in outputString - private String encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{ + protected String encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{ //Encode the input with polybius logger.debug("Encoding using Polybius Square"); String polybiusOutput = largePolybiusSquare.encode(squareKeyword, inputString); @@ -303,7 +315,7 @@ public class ADFGVX{ polybiusOutput = polybiusOutput.replace("1", "A").replace("2", "D").replace("3", "F").replace("4", "G").replace("5", "V").replace("6", "X"); //Encode polybius's output with columnar - logger.debug("Encoding with columnar"); + logger.debug("Encoding using columnar"); String columnarOutput = columnar.encode(keyword, polybiusOutput); outputString = columnarOutput; @@ -313,16 +325,16 @@ public class ADFGVX{ return outputString; } //Decodes the inputString and stores the result in outputString - private String decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{ + protected String decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{ //Decode the input with columnar - logger.debug("Decoding columnar"); + logger.debug("Decoding using columnar"); String columnarOutput = columnar.decode(keyword, inputString); //Change the symbols to the correct ones for polybius - logger.debug("Replacing characters with coordinates"); + logger.debug("Replacing letters with coordinates"); columnarOutput = columnarOutput.replace("A", "1").replace("D", "2").replace("F", "3").replace("G", "4").replace("V", "5").replace("X", "6"); //Decode with polybius - logger.debug("Decoding Polybius Square"); + logger.debug("Decoding using Polybius Square"); String polybiusOutput = largePolybiusSquare.decode(squareKeyword, columnarOutput); outputString = polybiusOutput; @@ -380,7 +392,7 @@ public class ADFGVX{ } //Makes sure all of the variables are empty public void reset() throws InvalidCharacterException{ - logger.debug("Reseting fields"); + logger.debug("Resetting fields"); largePolybiusSquare = new LargePolybiusSquare(false, false); columnar = new Columnar(false, false, false, true, 'B'); diff --git a/src/main/java/com/mattrixwv/cipherstream/combination/ADFGX.java b/src/main/java/com/mattrixwv/cipherstream/combination/ADFGX.java index d1bd7f6..6ffd5d0 100644 --- a/src/main/java/com/mattrixwv/cipherstream/combination/ADFGX.java +++ b/src/main/java/com/mattrixwv/cipherstream/combination/ADFGX.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java //Mattrixwv // Created: 01-25-22 -//Modified: 07-09-22 +//Modified: 04-14-23 package com.mattrixwv.cipherstream.combination; @@ -55,7 +55,7 @@ public class ADFGX{ throw new InvalidInputException("Input cannot be null"); } - logger.debug("original input string '{}'", inputString); + logger.debug("Original input string '{}'", inputString); if(!preserveCapitals){ logger.debug("Removing capitals"); @@ -77,7 +77,7 @@ public class ADFGX{ if(this.inputString.isBlank()){ throw new InvalidInputException("Input cannot be blank"); } - logger.debug("cleaned input string '{}'", inputString); + logger.debug("Cleaned input string '{}'", inputString); } //Format the output string with capitals, symbols, and numbers that are in the input string protected void formatOutputStringEncode(){ @@ -106,8 +106,8 @@ public class ADFGX{ } } - logger.debug("Saving output string '{}'", outputString); outputString = output.toString(); + logger.debug("Saving output string '{}'", outputString); } protected void formatOutputStringDecode(){ logger.debug("Formatting output string to match input string"); @@ -135,8 +135,8 @@ public class ADFGX{ } } - logger.debug("Saving output string '{}'", output); outputString = output.toString(); + logger.debug("Saving output string '{}'", outputString); } //Encodes the inputString and stores the result in outputString protected void encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{ diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Affine.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Affine.java index 8d5bf92..54c5067 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Affine.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Affine.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Affine.java //Mattrixwv // Created: 01-26-22 -//Modified: 07-09-22 +//Modified: 04-15-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -15,24 +15,26 @@ import com.mattrixwv.NumberAlgorithms; public class Affine{ - private static final Logger logger = LoggerFactory.getLogger(Affine.class); + protected static Logger logger = LoggerFactory.getLogger(Affine.class); //Fields - private boolean preserveCapitals; //Whether to respect capitals in the output string - private boolean preserveSymbols; //Whether to respect symbols in the output string - private boolean preserveWhitespace; //Whether to respect whitespace in the output string - private String inputString; //The string that needs encoded/decoded - private String outputString; //The string that is output after encoding/decoding - private int key1; //The multiplicative key. Key1 must be relatively prime to 26 - private int key2; //The additive key + protected boolean preserveCapitals; //Whether to respect capitals in the output string + protected boolean preserveSymbols; //Whether to respect symbols in the output string + protected boolean preserveWhitespace; //Whether to respect whitespace in the output string + protected String inputString; //The string that needs encoded/decoded + protected String outputString; //The string that is output after encoding/decoding + protected int key1; //The multiplicative key. Key1 must be relatively prime to 26 + protected int key2; //The additive key //Ensures key1 constraints - private void setKey1(int key1) throws InvalidKeywordException{ + protected void setKey1(int key1) throws InvalidKeywordException{ logger.debug("Setting key1 {}", key1); + //Mod 26 to ensure no overflow + key1 %= 26; + //If the key is negative change it to possitive if(key1 < 0){ - key1 %= 26; key1 += 26; } @@ -47,12 +49,14 @@ public class Affine{ logger.debug("Cleaned key1 {}", key1); } //Ensures key2 constraints - private void setKey2(int key2){ + protected void setKey2(int key2){ logger.debug("Setting key2 {}", key2); + //Mod 26 to ensure no overflow + key2 %= 26; + //If the key is negative change it to possitive if(key2 < 0){ - key2 %= 26; key2 += 26; } @@ -62,7 +66,7 @@ public class Affine{ logger.debug("Cleaned key2 {}", key2); } //Ensures inputString constraints - private void setInputString(String inputString) throws InvalidInputException{ + protected void setInputString(String inputString) throws InvalidInputException{ if(inputString == null){ throw new InvalidInputException("Input must not be null"); } @@ -94,7 +98,7 @@ public class Affine{ } } //Encodes the inputString and stores the result in outputString - private String encode(){ + protected void encode(){ logger.debug("Encoding"); //Step through every character in the input and encode it if needed @@ -108,7 +112,7 @@ public class Affine{ //Encode the number letter = ((key1 * letter) + key2) % 26; //Change the new number back to a character and append it to the output - char newChar = (char)(letter + 65); + char newChar = (char)(letter + 'A'); output.append(newChar); logger.debug("Encoded char {}", newChar); @@ -119,7 +123,7 @@ public class Affine{ //Encode the number letter = ((key1 * letter) + key2) % 26; //Change the new number back to a character and append it to the output - char newChar = (char)(letter + 97); + char newChar = (char)(letter + 'a'); output.append(newChar); logger.debug("Encoded char {}", newChar); @@ -131,12 +135,11 @@ public class Affine{ } //Save and return the output - logger.debug("Saving output string '{}'", output); outputString = output.toString(); - return outputString; + logger.debug("Saving output string '{}'", outputString); } //Decodes the inputString and stores the result in outputString - private String decode(){ + protected void decode(){ logger.debug("Decoding"); //Find the multiplicative inverse of key1 @@ -186,9 +189,8 @@ public class Affine{ } //Save and return the output - logger.debug("Saving output string '{}'", output); outputString = output.toString(); - return outputString; + logger.debug("Saving output string '{}'", outputString); } @@ -211,14 +213,16 @@ public class Affine{ setKey1(key1); setKey2(key2); setInputString(inputString); - return encode(); + encode(); + return outputString; } //Decodes inputString using key1 and key2 and returns the result public String decode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{ setKey1(key1); setKey2(key2); setInputString(inputString); - return decode(); + decode(); + return outputString; } //Returns the cleaned inputString diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Atbash.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Atbash.java index a804c13..76c0835 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Atbash.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Atbash.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java //Mattrixwv // Created: 07-25-21 -//Modified: 07-09-22 +//Modified: 04-15-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -12,17 +12,17 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class Atbash{ - private static final Logger logger = LoggerFactory.getLogger(Atbash.class); + protected static Logger logger = LoggerFactory.getLogger(Atbash.class); - private String inputString; //Holds the string that needs encoded or decoded - private String outputString; //The encoded/decoded string - private boolean preserveCapitals; //Whether to respect capitals in the output string - private boolean preserveWhitespace; //Whether to respect whitespace in the output string - private boolean preserveSymbols; //Whether to respect symbols in the output string + protected String inputString; //Holds the string that needs encoded or decoded + protected String outputString; //The encoded/decoded string + protected boolean preserveCapitals; //Whether to respect capitals in the output string + protected boolean preserveWhitespace; //Whether to respect whitespace in the output string + protected boolean preserveSymbols; //Whether to respect symbols in the output string //Encodes inputString and stores in outputString - private String encode(){ + protected String encode(){ logger.debug("Encoding"); StringBuilder output = new StringBuilder(); //Step through every element in the inputString and shift it the correct amount @@ -52,9 +52,9 @@ public class Atbash{ return outputString; } //Removes all invalid characters and sets inputString - private void setInputString(String inputString) throws InvalidInputException{ + protected void setInputString(String inputString) throws InvalidInputException{ if(inputString == null){ - throw new NullPointerException("Input cannot be null"); + throw new InvalidInputException("Input cannot be null"); } logger.debug("Original input string '{}'", inputString); diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Autokey.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Autokey.java index ad3340a..2015b3c 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Autokey.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Autokey.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java //Mattrixwv // Created: 07-25-21 -//Modified: 07-09-22 +//Modified: 04-15-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -13,10 +13,10 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Autokey extends Vigenere{ - private static final Logger logger = LoggerFactory.getLogger(Autokey.class); + protected static Logger logger = LoggerFactory.getLogger(Autokey.class); //Special rules for setting the strings for encoding - private void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ + protected void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ logger.debug("Setting fields for encoding"); //Set the input @@ -44,7 +44,7 @@ public class Autokey extends Vigenere{ setOffset(); } //Setting the strings for decoding - private void decodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ + protected void decodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ logger.debug("Setting fields for decoding"); //Remove all unneccessary elements from the key @@ -88,11 +88,6 @@ public class Autokey extends Vigenere{ letter += 26; } - else if(letter > 'Z'){ - logger.debug("Wrapping around to A"); - - letter -= 26; - } } else if(Character.isLowerCase(letter)){ logger.debug("Appending lowercase"); @@ -103,11 +98,6 @@ public class Autokey extends Vigenere{ letter += 26; } - else if(letter > 'z'){ - logger.debug("Wrapping around to a"); - - letter -= 26; - } } logger.debug("Decoded letter {}", letter); @@ -117,8 +107,8 @@ public class Autokey extends Vigenere{ fullOutput.append(currentOutput); //Save and return the results - logger.debug("Saving output string '{}'", fullOutput); outputString = fullOutput.toString(); + logger.debug("Saving output string '{}'", outputString); return outputString; } diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Baconian.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Baconian.java index b0d2da6..7e7a080 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Baconian.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Baconian.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java //Mattrixwv // Created: 01-12-22 -//Modified: 07-09-22 +//Modified: 04-15-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -17,21 +17,21 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class Baconian{ - private static final Logger logger = LoggerFactory.getLogger(Baconian.class); + protected static Logger logger = LoggerFactory.getLogger(Baconian.class); //Conversions - private static final ArrayList code = new ArrayList<>(Arrays.asList( + protected static final ArrayList code = new ArrayList<>(Arrays.asList( "aaaaa", "aaaab", "aaaba", "aaabb", "aabaa", "aabab", "aabba","aabbb", "abaaa", "abaaa", "abaab", "ababa", "ababb", //A-M "abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z )); - private String inputString; //The string that needs encoded/decoded - private String outputString; //The encoded/decoded string - private boolean preserveCapitals; //Whether to respect capitals in the output string + protected String inputString; //The string that needs encoded/decoded + protected String outputString; //The encoded/decoded string + protected boolean preserveCapitals; //Whether to respect capitals in the output string //Sets the input string - private void setInputStringEncode(String inputString) throws InvalidInputException{ + protected void setInputStringEncode(String inputString) throws InvalidInputException{ if(inputString == null){ - throw new NullPointerException("Input cannot be null"); + throw new InvalidInputException("Input cannot be null"); } logger.debug("Setting input string for encoding '{}'", inputString); @@ -52,9 +52,12 @@ public class Baconian{ throw new InvalidInputException("Input must contain at least 1 letter"); } } - private void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{ + protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{ if(inputString == null){ - throw new NullPointerException("Input cannot be null"); + throw new InvalidInputException("Input cannot be null"); + } + else if(inputString.isBlank()){ + throw new InvalidInputException("Input cannot be empty"); } logger.debug("Setting input string for decoding '{}'", inputString); @@ -86,13 +89,9 @@ public class Baconian{ this.inputString = inputString; logger.debug("Cleaned input string '{}'", inputString); - - if(this.inputString.isBlank()){ - throw new InvalidInputException("Input cannot be empty"); - } } //Encodes the inputString and stores the result in outputString - private String encode(){ + protected void encode(){ logger.debug("Encoding"); StringJoiner output = new StringJoiner(" "); //Go through every character in the inputString and encode it @@ -113,17 +112,16 @@ public class Baconian{ } //Add the encoded character to the output - logger.debug("Output 'letter' {}", binary); + logger.debug("Output letter {}", binary); output.add(binary); } //Save and return the output - logger.debug("Saving output string '{}'", output); outputString = output.toString(); - return outputString; + logger.debug("Saving output string '{}'", outputString); } //Decodes the inputString and stores the result in outputString - private String decode(){ + protected String decode(){ logger.debug("Decoding"); StringBuilder output = new StringBuilder(); @@ -154,8 +152,8 @@ public class Baconian{ } //Save and return the output - logger.debug("Saving output string '{}'", output); outputString = output.toString(); + logger.debug("Saving output string '{}'", outputString); return outputString; } @@ -180,7 +178,8 @@ public class Baconian{ public String encode(String inputString) throws InvalidInputException{ reset(); setInputStringEncode(inputString); - return encode(); + encode(); + return outputString; } //Sets the inputString and decodes the message public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{ diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/BaseX.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/BaseX.java index 9aa5eb9..ad5c959 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/BaseX.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/BaseX.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/BaseX.java //Mattrixwv // Created: 01-08-22 -//Modified: 07-09-22 +//Modified: 04-16-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -16,16 +16,16 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException; public class BaseX{ - private static final Logger logger = LoggerFactory.getLogger(BaseX.class); + protected static Logger logger = LoggerFactory.getLogger(BaseX.class); - private String inputString; //The string that needs encoded/decoded - private String outputString; //The encoded/decoded string - private int base; //The base that the number will be encoded at + protected String inputString; //The string that needs encoded/decoded + protected String outputString; //The encoded/decoded string + protected int base; //The base that the number will be encoded at //Sets the input string - private void setInputStringEncode(String inputString) throws InvalidInputException{ + protected void setInputStringEncode(String inputString) throws InvalidInputException{ if(inputString == null){ - throw new NullPointerException("Input cannot be null"); + throw new InvalidInputException("Input cannot be null"); } logger.debug("Setting input string for encoding '{}'", inputString); @@ -36,9 +36,9 @@ public class BaseX{ throw new InvalidInputException("Input must contain at least 1 letter"); } } - private void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{ + protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{ if(inputString == null){ - throw new NullPointerException("Input cannot be null"); + throw new InvalidInputException("Input cannot be null"); } logger.debug("Setting input string for decoding '{}'", inputString); @@ -67,9 +67,12 @@ public class BaseX{ } } //Sets the numeric base - private void setBase(int base) throws InvalidBaseException{ - if(base <= 0){ - throw new InvalidBaseException("Base cannot be a negative number"); + protected void setBase(int base) throws InvalidBaseException{ + if(base < Character.MIN_RADIX){ + throw new InvalidBaseException("Base cannot be less than " + Character.MIN_RADIX); + } + else if(base > Character.MAX_RADIX){ + throw new InvalidBaseException("Base cannot be larger than " + Character.MAX_RADIX); } logger.debug("Setting base {}", base); @@ -77,7 +80,7 @@ public class BaseX{ this.base = base; } //Encode inputString, store it in outputString, and return it - private String encode(){ + protected String encode(){ logger.debug("Encoding"); //Encode every character in inputString @@ -101,7 +104,7 @@ public class BaseX{ return outputString; } //Decode inputString, store it in outputString, and return it - private String decode() throws InvalidCharacterException{ + protected String decode() throws InvalidCharacterException{ logger.debug("Decoding"); //Decode every binary number in the string @@ -114,8 +117,8 @@ public class BaseX{ logger.debug("Decoded number {}", num); //Make sure it is in a valid range - if((num < 0) && (num > 255)){ - throw new InvalidCharacterException("The base" + base + " string '" + baseXString + "' is not a valid character"); + if((num < 0) || (num > 255)){ + throw new InvalidCharacterException("The base" + base + " string '" + baseXString + "' is not a valid ASCII character"); } //Convert the int to a char and save it @@ -123,8 +126,8 @@ public class BaseX{ } //Save the output - logger.debug("Saving output string '{}'", output); outputString = output.toString(); + logger.debug("Saving output string '{}'", outputString); //Return the output return outputString; diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Beaufort.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Beaufort.java index 3b77a7d..cda9e5f 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Beaufort.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Beaufort.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Beaufort.java //Mattrixwv // Created: 02-23-22 -//Modified: 07-09-22 +//Modified: 04-16-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -13,19 +13,19 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Beaufort{ - private static final Logger logger = LoggerFactory.getLogger(Beaufort.class); + protected static Logger logger = LoggerFactory.getLogger(Beaufort.class); //Fields - private String inputString; //This is the string that needs encoded/decoded - private String outputString; //This is the string that is output after encoding/decoding - private String keyword; //This is the keyword that is responsible for determining the offsets that you change each character by - private boolean preserveCapitals; //Whether to respect capitals in the output string - private boolean preserveWhitespace; //Whether to respect whitespace in the output string - private boolean preserveSymbols; //Whether to respect symbols in the output string + protected String inputString; //This is the string that needs encoded/decoded + protected String outputString; //This is the string that is output after encoding/decoding + protected String keyword; //This is the keyword that is responsible for determining the offsets that you change each character by + protected boolean preserveCapitals; //Whether to respect capitals in the output string + protected boolean preserveWhitespace; //Whether to respect whitespace in the output string + protected boolean preserveSymbols; //Whether to respect symbols in the output string //Internal ciphers - private Atbash atbash; //The first step in encoding/decoding the cipher - private Caesar caesar; //The second step in encoding/decoding the cipher - private Vigenere vigenere; //The third step in encoding/decoding the cipher + protected Atbash atbash; //The first step in encoding/decoding the cipher + protected Caesar caesar; //The second step in encoding/decoding the cipher + protected Vigenere vigenere; //The third step in encoding/decoding the cipher //Ensures inputString constraints public void setInputString(String inputString) throws InvalidInputException{ @@ -88,9 +88,20 @@ public class Beaufort{ } } //Encodes the inputString and stores the result in outputString - public void encode() throws InvalidKeywordException, InvalidInputException{ + protected void encode() throws InvalidKeywordException, InvalidInputException{ logger.debug("Encoding"); + code(); + } + //Decodes the inputString and stores the result in outputString + protected void decode() throws InvalidKeywordException, InvalidInputException{ + logger.debug("Decoding"); + + //Decoding is just encoding again + code(); + } + //Codes input and saves to output + protected void code(){ //Reverse the string logger.debug("Encoding with Atbash"); String atbashString = atbash.encode(inputString); @@ -106,13 +117,6 @@ public class Beaufort{ logger.debug("Saving output string '{}'", vigenereString); this.outputString = vigenereString; } - //Decodes the inputString and stores the result in outputString - public void decode() throws InvalidKeywordException, InvalidInputException{ - logger.debug("Decoding"); - - //Decoding is just encoding again - encode(); - } //Constructor diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Caesar.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Caesar.java index b64b697..7d2e74b 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Caesar.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Caesar.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Caesar.java //Matthew Ellison // Created: 07-25-21 -//Modified: 07-09-22 +//Modified: 04-16-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -12,18 +12,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class Caesar{ - private static final Logger logger = LoggerFactory.getLogger(Caesar.class); + protected static Logger logger = LoggerFactory.getLogger(Caesar.class); //Fields - private String inputString; //The string that needs encoded/decoded - private String outputString; //The encoded/decoded string - private int shift; //The amount that you need to shift each letter - private boolean preserveCapitals; //Whether to respect capitals in the output string - private boolean preserveWhitespace; //Whether to respect whitespace in the output string - private boolean preserveSymbols; //Whether to respect symbols in the output string + protected String inputString; //The string that needs encoded/decoded + protected String outputString; //The encoded/decoded string + protected int shift; //The amount that you need to shift each letter + protected boolean preserveCapitals; //Whether to respect capitals in the output string + protected boolean preserveWhitespace; //Whether to respect whitespace in the output string + protected boolean preserveSymbols; //Whether to respect symbols in the output string //Sets shift and makes sure it is within the propper bounds - private void setShift(int shiftAmount){ + protected void setShift(int shiftAmount){ logger.debug("Setting shift {}", shiftAmount); //If you shift more than 26 you will just be wrapping back around again @@ -32,9 +32,9 @@ public class Caesar{ logger.debug("Cleaned shift {}", shift); } //Sets the input string - private void setInputString(String inputString) throws InvalidInputException{ + protected void setInputString(String inputString) throws InvalidInputException{ if(inputString == null){ - throw new NullPointerException("Input cannot be null"); + throw new InvalidInputException("Input cannot be null"); } logger.debug("Original input string '{}'", inputString); @@ -63,7 +63,7 @@ public class Caesar{ } } //Encodes the inputString and stores the result in outputString - private String encode(){ + protected String encode(){ logger.debug("Encoding"); StringBuilder output = new StringBuilder(); @@ -107,12 +107,12 @@ public class Caesar{ output.append(currentChar); } - logger.debug("Saving encoded string '{}'", output); outputString = output.toString(); + logger.debug("Saving encoded string '{}'", outputString); return outputString; } //Decodes the inputString and stores the result in outputString - private String decode(){ + protected String decode(){ logger.debug("Decoding"); StringBuilder output = new StringBuilder(); @@ -156,11 +156,12 @@ public class Caesar{ } //If it is whitespace, number, or punctuation just let it pass through //Add it to the output string + logger.debug("Decoded character {}", currentChar); output.append(currentChar); } - logger.debug("Saving decoded string '{}'", output); outputString = output.toString(); + logger.debug("Saving decoded string '{}'", outputString); return outputString; } diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java index d2b6661..048c6b1 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java @@ -16,7 +16,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Vigenere{ - private static final Logger logger = LoggerFactory.getLogger(Vigenere.class); + protected static Logger logger = LoggerFactory.getLogger(Vigenere.class); //Fields protected String inputString; //This is the string that needs encoded/decoded diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java index 7e82e97..36213e6 100644 --- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java +++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java @@ -15,7 +15,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class PolybiusSquare{ - private static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class); + protected static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class); //A class representing the location of a character in the grid protected class CharLocation{ diff --git a/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGVX.java b/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGVX.java index fcfb291..0890bc2 100644 --- a/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGVX.java +++ b/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGVX.java @@ -1,450 +1,293 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/combination/TestADFGVX.java //Mattrixwv // Created: 01-26-22 -//Modified: 07-09-22 +//Modified: 04-14-23 package com.mattrixwv.cipherstream.combination; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyChar; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; -import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; +import com.mattrixwv.cipherstream.combination.ADFGVX.LargePolybiusSquare; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +import com.mattrixwv.cipherstream.polysubstitution.Columnar; public class TestADFGVX{ - @Test - public void testEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{ - ADFGVX cipher = new ADFGVX(true, true, true); + private ADFGVX cipher; + private Logger logger; + //Variables + private String encodedString = "Message to^encode"; + private String encodedStringClean = "MESSAGETOENCODE"; + private String decodedString = "AXgvdavfxgagfa afag^aaxdxfgdagda"; + private String decodedStringClean = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA"; + private String keyword = "keyword"; + private String squareKeyword = "SquareKeyword"; - //Test lowercase encoding - String inputString = "messagetoencode"; - String squareKeyword = "SquareKeyword"; - String keyword = "keyword"; - String correctOutput = "axgvdavfxgagfaafagaaxdxfgdagda"; - String output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test whitespace encoding - inputString = "message to encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "axgvdavfxgagfa afag aaxdxfgdagda"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "axgvdavfxgagfa*afag+aaxdxfgdagda"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to-encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AXgvdavfxgagfa afag-aaxdxfgdagda"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - } - @Test - public void testNoCapitalEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{ - ADFGX cipher = new ADFGX(false, true, true); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String squareKeyword = "SquareKeyword"; - String keyword = "keyword"; - String correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF"; - String output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAGAGADFAGAXXD AXDX ADAFAFXDDGDF"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode-"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAGAGADFAGAXXD*AXDX+ADAFAFXDDGDF-"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to^encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAGAGADFAGAXXD AXDX^ADAFAFXDDGDF"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - } - @Test - public void testNoWhitespaceEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{ - ADFGX cipher = new ADFGX(true, false, true); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String squareKeyword = "SquareKeyword"; - String keyword = "keyword"; - String correctOutput = "aagagadfagaxxdaxdxadafafxddgdf"; - String output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "aagagadfagaxxdaxdxadafafxddgdf"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode-"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "aagagadfagaxxd*axdx+adafafxddgdf-"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to^encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAgagadfagaxxdaxdx^adafafxddgdf"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - } - @Test - public void testNoSymbolEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{ - ADFGX cipher = new ADFGX(true, true, false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String squareKeyword = "SquareKeyword"; - String keyword = "keyword"; - String correctOutput = "aagagadfagaxxdaxdxadafafxddgdf"; - String output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "aagagadfagaxxd axdx adafafxddgdf"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode-"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "aagagadfagaxxdaxdxadafafxddgdf"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to^encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAgagadfagaxxd axdxadafafxddgdf"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - } - @Test - public void testNoCapitalWhitespaceSymbolEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{ - ADFGX cipher = new ADFGX(false, false, false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String squareKeyword = "SquareKeyword"; - String keyword = "keyword"; - String correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF"; - String output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode-"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to^encode"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF"; - output = cipher.encode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + @BeforeEach + public void setup(){ + cipher = new ADFGVX(); + logger = mock(Logger.class); + ADFGVX.logger = logger; } @Test - public void testDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{ - ADFGVX cipher = new ADFGVX(true, true, true); + public void testSquareKeyword(){ + assertThrows(InvalidKeywordException.class, () -> { + cipher.setSquareKeyword(null); + }); + assertEquals("", cipher.squareKeyword); + verify(logger, never()).debug(anyString(), anyString()); - //Test lowercase decoding - String inputString = "axgvdavfxgagfaafagaaxdxfgdagda"; - String squareKeyword = "SquareKeyword"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "axgvdavfxgagfa afag aaxdxfgdagda"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "message to encode"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "message*to+encode-"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "Message to^encode"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + cipher.setSquareKeyword(squareKeyword); + assertEquals(squareKeyword, cipher.squareKeyword); + verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword); } + @Test - public void testNoCapitalDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{ - ADFGVX cipher = new ADFGVX(false, true, true); + public void testSetKeyword(){ + assertThrows(InvalidKeywordException.class, () -> { + cipher.setKeyword(null); + }); + assertEquals("", cipher.keyword); + verify(logger, never()).debug(anyString(), anyString()); - //Test lowercase decoding - String inputString = "axgvdavfxgagfaafagaaxdxfgdagda"; - String squareKeyword = "SquareKeyword"; String keyword = "keyword"; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "axgvdavfxgagfa afag aaxdxfgdagda"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGE TO ENCODE"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGE*TO+ENCODE-"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGE TO^ENCODE"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + cipher.setKeyword(keyword); + assertEquals(keyword, cipher.keyword); + verify(logger, times(1)).debug("keyword = {}", keyword); } + @Test - public void testNoWhitespaceDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{ - ADFGVX cipher = new ADFGVX(true, false, true); + public void testSetInputString(){ + //Null input + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(null); + }); + assertEquals("", cipher.inputString); + verify(logger, never()).debug(anyString(), anyString()); - //Test lowercase decoding - String inputString = "axgvdavfxgagfaafagaaxdxfgdagda"; - String squareKeyword = "SquareKeyword"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + String originalInputString = "Original input string '{}'"; + String cleanedInputString = "Cleaned input string '{}'"; - //Test whitespace decoding - inputString = "axgvdavfxgagfa afag aaxdxfgdagda"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "messagetoencode"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + //Blank input + cipher.preserveCapitals = true; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = true; + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(""); + }); + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug(originalInputString, ""); - //Test symbol decoding - inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "message*to+encode-"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + //No options + String inputString = "input String*"; + cipher.setInputString(inputString); + assertEquals(inputString, cipher.inputString); + verify(logger, times(1)).debug(originalInputString, inputString); + verify(logger, times(1)).debug(cleanedInputString, inputString); - //Test mixed case, whitespace, and symbol decoding - inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "Messageto^encode"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + //capitals + cipher.preserveCapitals = false; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + inputString = "input String*"; + cipher.setInputString(inputString); + assertEquals(inputString.toUpperCase(), cipher.inputString); + verify(logger, times(2)).debug(originalInputString, inputString); + verify(logger, times(1)).debug("Removing capitals"); + verify(logger, times(1)).debug(cleanedInputString, inputString.toUpperCase()); + + //whitespace + cipher.preserveCapitals = true; + cipher.preserveWhitespace = false; + cipher.preserveSymbols = true; + inputString = "input String*"; + cipher.setInputString(inputString); + assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString); + verify(logger, times(3)).debug(originalInputString, inputString); + verify(logger, times(1)).debug("Removing whitespace"); + verify(logger, times(1)).debug(cleanedInputString, inputString.replaceAll("\\s", "")); + + //symbols + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = false; + inputString = "input String*"; + cipher.setInputString(inputString); + assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); + verify(logger, times(4)).debug(originalInputString, inputString); + verify(logger, times(1)).debug("Removing symbols"); + verify(logger, times(1)).debug(cleanedInputString, inputString.replaceAll("[^a-zA-Z\\s]", "")); } + @Test - public void testNoSymbolDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{ - ADFGVX cipher = new ADFGVX(true, true, false); + public void testFormateOutputStringEncode(){ + cipher.inputString = encodedString; + cipher.outputString = decodedStringClean; - //Test lowercase decoding - String inputString = "axgvdavfxgagfaafagaaxdxfgdagda"; - String squareKeyword = "SquareKeyword"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "axgvdavfxgagfa afag aaxdxfgdagda"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "message to encode"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "messagetoencode"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "Message toencode"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + cipher.formatOutputStringEncode(); + assertEquals(decodedString, cipher.outputString); + verify(logger, times(1)).debug("Formatting output string to match input string"); + verify(logger, times(17)).debug(eq("Input character {}"), anyChar()); + verify(logger, times(1)).debug("Converting output to uppercase"); + verify(logger, times(14)).debug("Converting output to lowercase"); + verify(logger, times(2)).debug("Appending symbol to output"); + verify(logger, times(1)).debug("Saving output string '{}'", decodedString); } + @Test - public void testNoCapitalWhitespaceSymbolDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{ - ADFGVX cipher = new ADFGVX(false, false, false); + public void testFormatOutputStringDecode(){ + cipher.outputString = encodedStringClean; + cipher.inputString = decodedString; - //Test lowercase decoding - String inputString = "axgvdavfxgagfaafagaaxdxfgdagda"; - String squareKeyword = "SquareKeyword"; - String keyword = "keyword"; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + cipher.formatOutputStringDecode(); + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Formatting output string to match input string"); + verify(logger, times(17)).debug(eq("Input character {}"), anyChar()); + verify(logger, times(1)).debug("Converting output to uppercase"); + verify(logger, times(14)).debug("Converting output to lowercase"); + verify(logger, times(2)).debug("Appending symbol to output"); + verify(logger, times(1)).debug("Saving output string '{}'", encodedString); + } - //Test whitespace decoding - inputString = "axgvdavfxgagfa afag aaxdxfgdagda"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + @Test + public void testEncode(){ + cipher.inputString = encodedString; + cipher.keyword = keyword; + cipher.squareKeyword = squareKeyword; - //Test symbol decoding - inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + cipher.encode(); - //Test mixed case, whitespace, and symbol decoding - inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda"; - squareKeyword = "SquareKeyword"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(squareKeyword, keyword, inputString); - assertEquals(correctOutput, output); + assertEquals(decodedString, cipher.outputString); + verify(logger, times(1)).debug("Encoding using Polybius Square"); + verify(logger, times(1)).debug("Replacing coordinates with letters"); + verify(logger, times(1)).debug("Encoding using columnar"); + } + + @Test + public void testDecode(){ + cipher.inputString = decodedString; + cipher.keyword = keyword; + cipher.squareKeyword = squareKeyword; + + cipher.decode(); + + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Decoding using columnar"); + verify(logger, times(1)).debug("Replacing letters with coordinates"); + verify(logger, times(1)).debug("Decoding using Polybius Square"); + } + + @Test + public void testConstructors(){ + cipher = new ADFGVX(); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.squareKeyword); + assertEquals("", cipher.squareKeyword); + assertNotNull(cipher.largePolybiusSquare); + assertNotNull(cipher.columnar); + + cipher = new ADFGVX(true, false, false); + assertTrue(cipher.preserveCapitals); + assertFalse(cipher.preserveSymbols); + assertFalse(cipher.preserveWhitespace); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.squareKeyword); + assertEquals("", cipher.squareKeyword); + assertNotNull(cipher.largePolybiusSquare); + assertNotNull(cipher.columnar); + + cipher = new ADFGVX(false, true, false); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveSymbols); + assertTrue(cipher.preserveWhitespace); + + cipher = new ADFGVX(false, false, true); + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveSymbols); + assertFalse(cipher.preserveWhitespace); + } + + @Test + public void testGetters(){ + cipher.inputString = encodedString; + cipher.outputString = decodedString; + cipher.squareKeyword = squareKeyword; + cipher.keyword = keyword; + + assertEquals(encodedString, cipher.getInputString()); + assertEquals(decodedString, cipher.getOutputString()); + assertEquals(squareKeyword, cipher.getSquareKeyword()); + assertEquals(keyword, cipher.getKeyword()); + } + + @Test + public void testReset(){ + LargePolybiusSquare ps = cipher.largePolybiusSquare; + Columnar columnar = cipher.columnar; + cipher.inputString = encodedString; + cipher.outputString = decodedString; + cipher.squareKeyword = squareKeyword; + cipher.keyword = keyword; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.squareKeyword); + assertEquals("", cipher.keyword); + assertNotSame(ps, cipher.largePolybiusSquare); + assertNotSame(columnar, cipher.columnar); + verify(logger, times(1)).debug("Resetting fields"); + } + + @Test + public void testPracticalEncoding(){ + //Test as original + cipher = new ADFGVX(true, true, true); + String output = cipher.encode(squareKeyword, keyword, encodedString); + assertEquals(decodedString, output); + + //Test fully cleaned + cipher = new ADFGVX(false, false, false); + output = cipher.encode(squareKeyword, keyword, encodedString); + assertEquals(decodedStringClean, output); + } + + @Test + public void testPracticalDecoding(){ + //Test as original + cipher = new ADFGVX(true, true, true); + String output = cipher.decode(squareKeyword, keyword, decodedString); + assertEquals(encodedString, output); + + //Test fully cleaned + cipher = new ADFGVX(false, false, false); + output = cipher.decode(squareKeyword, keyword, decodedString); + assertEquals(encodedStringClean, output); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGX.java b/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGX.java index 522f153..eef1b6b 100644 --- a/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGX.java +++ b/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGX.java @@ -6,8 +6,14 @@ package com.mattrixwv.cipherstream.combination; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyChar; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; @@ -15,23 +21,29 @@ import static org.mockito.Mockito.verify; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +import com.mattrixwv.cipherstream.polysubstitution.Columnar; +import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare; -@ExtendWith(MockitoExtension.class) public class TestADFGX{ private ADFGX cipher; private Logger logger; + //Variables + private String decodedString = "Message to^encode"; + private String decodedStringClean = "MESSAGETOENCODE"; + private String encodedString = "AAgagadfagaxxd axdx^adafafxddgdf"; + private String encodedStringClean = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF"; + private String keyword = "keyword"; + private String squareKeyword = "SquareKeyword"; @BeforeEach public void setup(){ - cipher = new ADFGX(); + cipher = new ADFGX(true, true, true); logger = mock(Logger.class); ADFGX.logger = logger; } @@ -45,7 +57,6 @@ public class TestADFGX{ assertEquals("", cipher.squareKeyword); verify(logger, never()).debug(anyString(), anyString()); - String squareKeyword = "squareKeyword"; cipher.setSquareKeyword(squareKeyword); assertEquals(squareKeyword, cipher.squareKeyword); verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword); @@ -74,8 +85,8 @@ public class TestADFGX{ assertEquals("", cipher.inputString); verify(logger, never()).debug(anyString(), anyString()); - String originalInputString = "original input string '{}'"; - String cleanedInputString = "cleaned input string '{}'"; + String originalInputString = "Original input string '{}'"; + String cleanedInputString = "Cleaned input string '{}'"; //Blank input cipher.preserveCapitals = true; @@ -130,70 +141,153 @@ public class TestADFGX{ @Test public void testFormatOutputStringEncode(){ - //TODO: + cipher.inputString = decodedString; + cipher.outputString = encodedStringClean; + + cipher.formatOutputStringEncode(); + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Formatting output string to match input string"); + verify(logger, times(17)).debug(eq("Input character {}"), anyChar()); + verify(logger, times(1)).debug("Converting output to uppercase"); + verify(logger, times(14)).debug("Converting output to lowercase"); + verify(logger, times(2)).debug("Appending symbol to output"); + verify(logger, times(1)).debug("Saving output string '{}'", encodedString); } @Test - public void formatOutputStringDecode(){ - //TODO: + public void testFormatOutputStringDecode(){ + cipher.outputString = decodedStringClean; + cipher.inputString = encodedString; + + cipher.formatOutputStringDecode(); + assertEquals(decodedString, cipher.outputString); + verify(logger, times(1)).debug("Formatting output string to match input string"); + verify(logger, times(17)).debug(eq("Input character {}"), anyChar()); + verify(logger, times(1)).debug("Converting output to uppercase"); + verify(logger, times(14)).debug("Converting output to lowercase"); + verify(logger, times(2)).debug("Appending symbol to output"); + verify(logger, times(1)).debug("Saving output string '{}'", decodedString); } @Test - public void testEncodePrivate(){ - //TODO: + public void testEncode(){ + cipher.inputString = decodedString; + cipher.keyword = keyword; + cipher.squareKeyword = squareKeyword; + + cipher.encode(); + + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Encoding using Polybius Square"); + verify(logger, times(1)).debug("Replacing coordinates with letters"); + verify(logger, times(1)).debug("Encoding using columnar"); } @Test - public void testDecodePrivate(){ - //TODO: + public void testDecode(){ + cipher.inputString = encodedString; + cipher.keyword = keyword; + cipher.squareKeyword = squareKeyword; + + cipher.decode(); + + assertEquals(decodedString, cipher.outputString); + verify(logger, times(1)).debug("Decoding using columnar"); + verify(logger, times(1)).debug("Replacing letters with coordinates"); + verify(logger, times(1)).debug("Decoding using Polybius Square"); } @Test public void testConstructors(){ - //TODO: + cipher = new ADFGX(); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.squareKeyword); + assertEquals("", cipher.squareKeyword); + assertNotNull(cipher.polybiusSquare); + assertNotNull(cipher.columnar); + + cipher = new ADFGX(true, false, false); + assertTrue(cipher.preserveCapitals); + assertFalse(cipher.preserveSymbols); + assertFalse(cipher.preserveWhitespace); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.squareKeyword); + assertEquals("", cipher.squareKeyword); + assertNotNull(cipher.polybiusSquare); + assertNotNull(cipher.columnar); + + cipher = new ADFGX(false, true, false); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveSymbols); + assertTrue(cipher.preserveWhitespace); + + cipher = new ADFGX(false, false, true); + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveSymbols); + assertFalse(cipher.preserveWhitespace); } @Test public void testGetters(){ - //TODO: + cipher.inputString = decodedString; + cipher.outputString = encodedString; + cipher.squareKeyword = squareKeyword; + cipher.keyword = keyword; + + assertEquals(decodedString, cipher.getInputString()); + assertEquals(encodedString, cipher.getOutputString()); + assertEquals(squareKeyword, cipher.getSquareKeyword()); + assertEquals(keyword, cipher.getKeyword()); } @Test public void testReset(){ - //TODO: + PolybiusSquare polybius = cipher.polybiusSquare; + Columnar columnar = cipher.columnar; + cipher.inputString = decodedString; + cipher.outputString = encodedString; + cipher.squareKeyword = squareKeyword; + cipher.keyword = keyword; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.squareKeyword); + assertEquals("", cipher.keyword); + assertNotSame(polybius, cipher.polybiusSquare); + assertNotSame(columnar, cipher.columnar); + verify(logger, times(1)).debug("Resetting fields"); } @Test public void testPracticalEncoding(){ //Test as original - cipher.preserveCapitals = true; - cipher.preserveWhitespace = true; - cipher.preserveSymbols = true; - String output = cipher.encode("SquareKeyword", "keyword", "Message to^encode"); - assertEquals("AAgagadfagaxxd axdx^adafafxddgdf", output); + cipher = new ADFGX(true, true, true); + String output = cipher.encode(squareKeyword, keyword, decodedString); + assertEquals(encodedString, output); //Test fully cleaned - cipher.preserveCapitals = false; - cipher.preserveWhitespace = false; - cipher.preserveSymbols = false; - output = cipher.encode("SquareKeyword", "keyword", "Message to^encode"); - assertEquals("AAGAGADFAGAXXDAXDXADAFAFXDDGDF", output); + cipher = new ADFGX(false, false, false); + output = cipher.encode(squareKeyword, keyword, decodedString); + assertEquals(encodedStringClean, output); } @Test public void testPracticalDecoding(){ //Test as original - cipher.preserveCapitals = true; - cipher.preserveWhitespace = true; - cipher.preserveSymbols = true; - String output = cipher.decode("SquareKeyword", "keyword", "AAgagadfagaxxd axdx^adafafxddgdf"); - assertEquals("Message to^encode", output); + cipher = new ADFGX(true, true, true); + String output = cipher.decode(squareKeyword, keyword, encodedString); + assertEquals(decodedString, output); //Test fully cleaned - cipher.preserveCapitals = false; - cipher.preserveWhitespace = false; - cipher.preserveSymbols = false; - output = cipher.decode("SquareKeyword", "keyword", "AAgagadfagaxxd axdx^adafafxddgdf"); - assertEquals("MESSAGETOENCODE", output); + cipher = new ADFGX(false, false, false); + output = cipher.decode(squareKeyword, keyword, encodedString); + assertEquals(decodedStringClean, output); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAffine.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAffine.java index 9774899..e25f694 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAffine.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAffine.java @@ -1,449 +1,342 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestAffine.java //Mattrixwv // Created: 01-26-22 -//Modified: 07-09-22 +//Modified: 04-15-23 package com.mattrixwv.cipherstream.monosubstitution; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyChar; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class TestAffine{ - @Test - public void testEncode() throws InvalidKeywordException, InvalidInputException{ - Affine cipher = new Affine(true, true, true); + private Affine cipher; + private Logger logger; + //Variables + private String decodedString = "Message to^encode"; + private String decodedStringClean = "messagetoencode"; + private String encodedString = "Pbtthlb yz^burzwb"; + private String encodedStringClean = "pbtthlbyzburzwb"; + private int key1 = 5; + private int key2 = 7; - //Test lowercase encoding - String inputString = "messagetoencode"; - int key1 = 5; - int key2 = 7; - String correctOutput = "pbtthlbyzburzwb"; - String output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - key1 = 5; - key2 = 7; - correctOutput = "PBTTHLBYZBURZWB"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test whitespace encoding - inputString = "message to encode"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlb yz burzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode-"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlb*yz+burzwb-"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - key1 = 5; - key2 = 7; - correctOutput = "Pbtthlb yz^burzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - } - @Test - public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{ - Affine cipher = new Affine(false, true, true); - - //Test lowercase encoding - String inputString = "messagetoencode"; - int key1 = 5; - int key2 = 7; - String correctOutput = "pbtthlbyzburzwb"; - String output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlbyzburzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlb yz burzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode-"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlb*yz+burzwb-"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlb yz^burzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - } - @Test - public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{ - Affine cipher = new Affine(true, false, true); - - //Test lowercase encoding - String inputString = "messagetoencode"; - int key1 = 5; - int key2 = 7; - String correctOutput = "pbtthlbyzburzwb"; - String output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - key1 = 5; - key2 = 7; - correctOutput = "PBTTHLBYZBURZWB"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlbyzburzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode-"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlb*yz+burzwb-"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - key1 = 5; - key2 = 7; - correctOutput = "Pbtthlbyz^burzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - } - @Test - public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{ - Affine cipher = new Affine(true, true, false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - int key1 = 5; - int key2 = 7; - String correctOutput = "pbtthlbyzburzwb"; - String output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - key1 = 5; - key2 = 7; - correctOutput = "PBTTHLBYZBURZWB"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlb yz burzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode-"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlbyzburzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - key1 = 5; - key2 = 7; - correctOutput = "Pbtthlb yzburzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - } - @Test - public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{ - Affine cipher = new Affine(false, false, false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - int key1 = 5; - int key2 = 7; - String correctOutput = "pbtthlbyzburzwb"; - String output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlbyzburzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlbyzburzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode-"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlbyzburzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - key1 = 5; - key2 = 7; - correctOutput = "pbtthlbyzburzwb"; - output = cipher.encode(key1, key2, inputString); - assertEquals(correctOutput, output); + @BeforeEach + public void setup(){ + cipher = new Affine(); + logger = mock(Logger.class); + Affine.logger = logger; } @Test - public void testDecode() throws InvalidKeywordException, InvalidInputException{ - Affine cipher = new Affine(true, true, true); - - //Test lowercase decoding - String inputString = "pbtthlbyzburzwb"; - int key1 = 5; - int key2 = 7; - String correctOutput = "messagetoencode"; - String output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test uppsercase decoding - inputString = "PBTTHLBYZBURZWB"; - key1 = 5; - key2 = 7; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "pbtthlb yz burzwb"; - key1 = 5; - key2 = 7; - correctOutput = "message to encode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "pbtthlb*yz+burzwb-"; - key1 = 5; - key2 = 7; - correctOutput = "message*to+encode-"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Pbtthlb yz^burzwb"; - key1 = 5; - key2 = 7; - correctOutput = "Message to^encode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); + public void testConstructor_default(){ + cipher = new Affine(); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveSymbols); + assertFalse(cipher.preserveWhitespace); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals(0, cipher.key1); + assertEquals(0, cipher.key2); } + @Test - public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{ - Affine cipher = new Affine(false, true, true); + public void testConstructor_preserves(){ + cipher = new Affine(true, false, false); + assertTrue(cipher.preserveCapitals); + assertFalse(cipher.preserveSymbols); + assertFalse(cipher.preserveWhitespace); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals(0, cipher.key1); + assertEquals(0, cipher.key2); - //Test lowercase decoding - String inputString = "pbtthlbyzburzwb"; - int key1 = 5; - int key2 = 7; - String correctOutput = "messagetoencode"; - String output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test uppsercase decoding - inputString = "PBTTHLBYZBURZWB"; - key1 = 5; - key2 = 7; - correctOutput = "messagetoencode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); + cipher = new Affine(false, true, false); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveSymbols); + assertTrue(cipher.preserveWhitespace); - //Test whitespace decoding - inputString = "pbtthlb yz burzwb"; - key1 = 5; - key2 = 7; - correctOutput = "message to encode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "pbtthlb*yz+burzwb-"; - key1 = 5; - key2 = 7; - correctOutput = "message*to+encode-"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Pbtthlb yz^burzwb"; - key1 = 5; - key2 = 7; - correctOutput = "message to^encode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); + cipher = new Affine(false, false, true); + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveSymbols); + assertFalse(cipher.preserveWhitespace); } + @Test - public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{ - Affine cipher = new Affine(true, false, true); - - //Test lowercase decoding - String inputString = "pbtthlbyzburzwb"; - int key1 = 5; - int key2 = 7; - String correctOutput = "messagetoencode"; - String output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test uppsercase decoding - inputString = "PBTTHLBYZBURZWB"; - key1 = 5; - key2 = 7; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "pbtthlb yz burzwb"; - key1 = 5; - key2 = 7; - correctOutput = "messagetoencode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "pbtthlb*yz+burzwb-"; - key1 = 5; - key2 = 7; - correctOutput = "message*to+encode-"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Pbtthlb yz^burzwb"; - key1 = 5; - key2 = 7; - correctOutput = "Messageto^encode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); + public void testKey1(){ + cipher.setKey1(key1); + assertEquals(key1, cipher.key1); + verify(logger, times(1)).debug("Setting key1 {}", key1); + verify(logger, times(1)).debug("Cleaned key1 {}", key1); + verify(logger, times(2)).debug(anyString(), anyInt()); + verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); } + @Test - public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{ - Affine cipher = new Affine(true, true, false); - - //Test lowercase decoding - String inputString = "pbtthlbyzburzwb"; - int key1 = 5; - int key2 = 7; - String correctOutput = "messagetoencode"; - String output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test uppsercase decoding - inputString = "PBTTHLBYZBURZWB"; - key1 = 5; - key2 = 7; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "pbtthlb yz burzwb"; - key1 = 5; - key2 = 7; - correctOutput = "message to encode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "pbtthlb*yz+burzwb-"; - key1 = 5; - key2 = 7; - correctOutput = "messagetoencode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Pbtthlb yz^burzwb"; - key1 = 5; - key2 = 7; - correctOutput = "Message toencode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); + public void testSetKey1_notPrime(){ + assertThrows(InvalidKeywordException.class, () -> { + cipher.setKey1(2); + }); + verify(logger, times(1)).debug("Setting key1 {}", 2); + verify(logger, never()).debug("Cleaned key1 {}", 2); + verify(logger, times(1)).debug(anyString(), anyInt()); + verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); } + @Test - public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{ - Affine cipher = new Affine(false, false, false); + public void testSetKey1_negative(){ + cipher.setKey1(-27); + assertEquals(25, cipher.key1); + verify(logger, times(1)).debug("Setting key1 {}", -27); + verify(logger, times(1)).debug("Cleaned key1 {}", 25); + verify(logger, times(2)).debug(anyString(), anyInt()); + verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); + } - //Test lowercase decoding - String inputString = "pbtthlbyzburzwb"; - int key1 = 5; - int key2 = 7; - String correctOutput = "messagetoencode"; - String output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); - //Test uppsercase decoding - inputString = "PBTTHLBYZBURZWB"; - key1 = 5; - key2 = 7; - correctOutput = "messagetoencode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); + @Test + public void testSetKey1_large(){ + cipher.setKey1(key1 + 26); + assertEquals(key1, cipher.key1); + verify(logger, times(1)).debug("Setting key1 {}", key1 + 26); + verify(logger, times(1)).debug("Cleaned key1 {}", key1); + verify(logger, times(2)).debug(anyString(), anyInt()); + verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); + } - //Test whitespace decoding - inputString = "pbtthlb yz burzwb"; - key1 = 5; - key2 = 7; - correctOutput = "messagetoencode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); + @Test + public void testSetKey2(){ + cipher.setKey2(key2); + assertEquals(key2, cipher.key2); + verify(logger, times(1)).debug("Setting key2 {}", key2); + verify(logger, times(1)).debug("Cleaned key2 {}", key2); + verify(logger, times(2)).debug(anyString(), anyInt()); + verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); + } - //Test symbol decoding - inputString = "pbtthlb*yz+burzwb-"; - key1 = 5; - key2 = 7; - correctOutput = "messagetoencode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); + @Test + public void testSetKey2_negative(){ + cipher.setKey2(-27); + assertEquals(25, cipher.key2); + verify(logger, times(1)).debug("Setting key2 {}", -27); + verify(logger, times(1)).debug("Cleaned key2 {}", cipher.key2); + verify(logger, times(2)).debug(anyString(), anyInt()); + verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); + } - //Test mixed case, whitespace, symbol decoding - inputString = "Pbtthlb yz^burzwb"; - key1 = 5; - key2 = 7; - correctOutput = "messagetoencode"; - output = cipher.decode(key1, key2, inputString); - assertEquals(correctOutput, output); + @Test + public void testSetKey2_large(){ + cipher.setKey2(key2 + 26); + assertEquals(key2, cipher.key2); + verify(logger, times(1)).debug("Setting key2 {}", key2 + 26); + verify(logger, times(1)).debug("Cleaned key2 {}", key2); + verify(logger, times(2)).debug(anyString(), anyInt()); + verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); + } + + @Test + public void testSetInputString(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = true; + + cipher.setInputString(decodedString); + + assertEquals(decodedString, cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputString_noCapitals(){ + cipher.preserveCapitals = false; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = true; + + cipher.setInputString(decodedString); + + assertEquals(decodedString.toLowerCase(), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toLowerCase()); + verify(logger, times(2)).debug(anyString(), anyString()); + verify(logger, times(1)).debug(anyString()); + } + + @Test + public void testSetInputString_noWhitespace(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = false; + + cipher.setInputString(decodedString); + + assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Removing whitespace"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", "")); + verify(logger, times(2)).debug(anyString(), anyString()); + verify(logger, times(1)).debug(anyString()); + } + + @Test + public void testSetInputString_noSymbols(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = false; + cipher.preserveWhitespace = true; + + cipher.setInputString(decodedString); + + assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", "")); + verify(logger, times(2)).debug(anyString(), anyString()); + verify(logger, times(1)).debug(anyString()); + } + + @Test + public void testSetInputString_null(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(null); + }); + verify(logger, never()).debug(anyString()); + verify(logger, never()).debug(anyString(), anyString()); + } + + @Test + public void testSetInputString_blank(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(""); + }); + verify(logger, times(1)).debug("Original input string '{}'", ""); + verify(logger, times(1)).debug("Cleaned input string '{}'", ""); + verify(logger, times(2)).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); + } + + @Test + public void testEncode(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + cipher.inputString = decodedString; + cipher.key1 = key1; + cipher.key2 = key2; + + cipher.encode(); + + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(17)).debug(eq("Current char {}"), anyChar()); + verify(logger, times(15)).debug(eq("Encoded char {}"), anyChar()); + verify(logger, times(1)).debug("Saving output string '{}'", encodedString); + } + + @Test + public void testDecode(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = true; + cipher.inputString = encodedString; + cipher.key1 = key1; + cipher.key2 = key2; + + cipher.decode(); + + assertEquals(decodedString, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(1)).debug("Key1 inverse {}", 21); + verify(logger, times(17)).debug(eq("Current char {}"), anyChar()); + verify(logger, times(15)).debug(eq("Decoded char {}"), anyChar()); + verify(logger, times(1)).debug("Saving output string '{}'", decodedString); + } + + @Test + public void testGetters(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + cipher.key1 = key1; + cipher.key2 = key2; + + assertEquals(decodedString, cipher.getInputString()); + assertEquals(encodedString, cipher.getOutputString()); + assertEquals(key1, cipher.getKey1()); + assertEquals(key2, cipher.getKey2()); + } + + @Test + public void testReset(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + cipher.key1 = key1; + cipher.key2 = key2; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals(0, cipher.key1); + assertEquals(0, cipher.key2); + verify(logger, times(1)).debug("Resetting fields"); + } + + @Test + public void testPracticalEncoding(){ + //Test as original + cipher = new Affine(true, true, true); + String output = cipher.encode(key1, key2, decodedString); + assertEquals(encodedString, output); + + //Test fully cleaned + cipher = new Affine(false, false, false); + output = cipher.encode(key1, key2, decodedString); + assertEquals(encodedStringClean, output); + } + + @Test + public void testPracticalDecoding(){ + //Test as original + cipher = new Affine(true, true, true); + String output = cipher.decode(key1, key2, encodedString); + assertEquals(decodedString, output); + + //Test fully cleaned + cipher = new Affine(false, false, false); + output = cipher.decode(key1, key2, encodedString); + assertEquals(decodedStringClean, output); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAtbash.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAtbash.java index 9c3d63f..a923bcf 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAtbash.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAtbash.java @@ -1,356 +1,224 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java //Mattrixwv // Created: 07-25-21 -//Modified: 07-09-22 +//Modified: 04-15-23 package com.mattrixwv.cipherstream.monosubstitution; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class TestAtbash{ - @Test - public void testEncode() throws InvalidInputException{ - Atbash cipher = new Atbash(true, true, true); + private Atbash cipher; + private Logger logger; + //Variables + private String decodedString = "Message to^encode"; + private String decodedStringClean = "MESSAGETOENCODE"; + private String encodedString = "Nvhhztv gl^vmxlwv"; + private String encodedStringClean = "NVHHZTVGLVMXLWV"; - //Test lowercase encoding - String inputString = "messagetoencode"; - String correctOutput = "nvhhztvglvmxlwv"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - correctOutput = "NVHHZTVGLVMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test whitespace encoding - inputString = "message to encode"; - correctOutput = "nvhhztv gl vmxlwv"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - correctOutput = "nvhhztv*gl+vmxlwv"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to^encode"; - correctOutput = "Nvhhztv gl^vmxlwv"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoCapitalEncode() throws InvalidInputException{ - Atbash cipher = new Atbash(false, true, true); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String correctOutput = "NVHHZTVGLVMXLWV"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - correctOutput = "NVHHZTVGLVMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - correctOutput = "NVHHZTV GL VMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - correctOutput = "NVHHZTV*GL+VMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to^encode"; - correctOutput = "NVHHZTV GL^VMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoWhitespaceEncode() throws InvalidInputException{ - Atbash cipher = new Atbash(true, false, true); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String correctOutput = "nvhhztvglvmxlwv"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - correctOutput = "NVHHZTVGLVMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - correctOutput = "nvhhztvglvmxlwv"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - correctOutput = "nvhhztv*gl+vmxlwv"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to^encode"; - correctOutput = "Nvhhztvgl^vmxlwv"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoSymbolEncode() throws InvalidInputException{ - Atbash cipher = new Atbash(true, true, false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String correctOutput = "nvhhztvglvmxlwv"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - correctOutput = "NVHHZTVGLVMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - correctOutput = "nvhhztv gl vmxlwv"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - correctOutput = "nvhhztvglvmxlwv"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to^encode"; - correctOutput = "Nvhhztv glvmxlwv"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoCapitalWhitespaceSymbolEncode() throws InvalidInputException{ - Atbash cipher = new Atbash(false, false, false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String correctOutput = "NVHHZTVGLVMXLWV"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - correctOutput = "NVHHZTVGLVMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - correctOutput = "NVHHZTVGLVMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - correctOutput = "NVHHZTVGLVMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to^encode"; - correctOutput = "NVHHZTVGLVMXLWV"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + @BeforeEach + public void setup(){ + cipher = new Atbash(); + logger = mock(Logger.class); + Atbash.logger = logger; } @Test - public void testDecode() throws InvalidInputException{ - Atbash cipher = new Atbash(true, true, true); - - //Test lowercase decoding - String inputString = "nvhhztvglvmxlwv"; - String correctOutput = "messagetoencode"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "NVHHZTVGLVMXLWV"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "nvhhztv gl vmxlwv"; - correctOutput = "message to encode"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "nvhhztv*gl+vmxlwv"; - correctOutput = "message*to+encode"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "Nvhhztv gl^vmxlwv"; - correctOutput = "Message to^encode"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + public void testConstructor_default(){ + cipher = new Atbash(); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); } @Test - public void testNoCapitalDecode() throws InvalidInputException{ - Atbash cipher = new Atbash(false, true, true); + public void testConstructor_preserves(){ + cipher = new Atbash(true, false, false); + assertTrue(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); - //Test lowercase decoding - String inputString = "nvhhztvglvmxlwv"; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "NVHHZTVGLVMXLWV"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + cipher = new Atbash(false, true, false); + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); - //Test whitespace decoding - inputString = "nvhhztv gl vmxlwv"; - correctOutput = "MESSAGE TO ENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "nvhhztv*gl+vmxlwv"; - correctOutput = "MESSAGE*TO+ENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "Nvhhztv gl^vmxlwv"; - correctOutput = "MESSAGE TO^ENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + cipher = new Atbash(false, false, true); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertTrue(cipher.preserveSymbols); } @Test - public void testNoWhitespaceDecode() throws InvalidInputException{ - Atbash cipher = new Atbash(true, false, true); + public void testEncode(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; - //Test lowercase decoding - String inputString = "nvhhztvglvmxlwv"; - String correctOutput = "messagetoencode"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "NVHHZTVGLVMXLWV"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "nvhhztv gl vmxlwv"; - correctOutput = "messagetoencode"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "nvhhztv*gl+vmxlwv"; - correctOutput = "message*to+encode"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "Nvhhztv gl^vmxlwv"; - correctOutput = "Messageto^encode"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + assertEquals(decodedString, cipher.inputString); + assertEquals(encodedString, cipher.outputString); } @Test - public void testNoSymbolDecode() throws InvalidInputException{ - Atbash cipher = new Atbash(true, true, false); + public void testSetInputString(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = true; - //Test lowercase decoding - String inputString = "nvhhztvglvmxlwv"; - String correctOutput = "messagetoencode"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "NVHHZTVGLVMXLWV"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + cipher.setInputString(decodedString); - //Test whitespace decoding - inputString = "nvhhztv gl vmxlwv"; - correctOutput = "message to encode"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "nvhhztv*gl+vmxlwv"; - correctOutput = "messagetoencode"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "Nvhhztv gl^vmxlwv"; - correctOutput = "Message toencode"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + assertEquals(decodedString, cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString); + verify(logger, times(2)).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); } @Test - public void testNoCapitalWhitespaceSymbolDecode() throws InvalidInputException{ - Atbash cipher = new Atbash(false, false, false); + public void testSetInputString_noCapitals(){ + cipher.preserveCapitals = false; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = true; - //Test lowercase decoding - String inputString = "nvhhztvglvmxlwv"; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "NVHHZTVGLVMXLWV"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + cipher.setInputString(decodedString); - //Test whitespace decoding - inputString = "nvhhztv gl vmxlwv"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + assertEquals(decodedString.toUpperCase(), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase()); + verify(logger, times(2)).debug(anyString(), anyString()); + verify(logger, times(1)).debug(anyString()); + } - //Test symbol decoding - inputString = "nvhhztv*gl+vmxlwv"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + @Test + public void testSetInputString_noWhitespace(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = false; - //Test mixed case, whitespace, and symbol decoding - inputString = "Nvhhztv gl^vmxlwv"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + cipher.setInputString(decodedString); + + assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Removing whitespace"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", "")); + verify(logger, times(2)).debug(anyString(), anyString()); + verify(logger, times(1)).debug(anyString()); + } + + @Test + public void testSetInputString_noSymbols(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = false; + cipher.preserveWhitespace = true; + + cipher.setInputString(decodedString); + + assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", "")); + verify(logger, times(2)).debug(anyString(), anyString()); + verify(logger, times(1)).debug(anyString()); + } + + @Test + public void testSetInputString_null(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(null); + }); + + assertEquals("", cipher.inputString); + verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); + } + + @Test + public void testSetInputString_blank(){ + cipher.preserveCapitals = true; + cipher.preserveSymbols = true; + cipher.preserveWhitespace = true; + cipher.inputString = decodedString; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(""); + }); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", ""); + verify(logger, times(1)).debug("Cleaned input string '{}'", ""); + verify(logger, times(2)).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); + } + + @Test + public void testGetters(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + + assertEquals(decodedString, cipher.getInputString()); + assertEquals(encodedString, cipher.getOutputString()); + } + + @Test + public void testReset(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + verify(logger, times(1)).debug("Resetting fields"); + } + + @Test + public void testPracticalEncoding(){ + //Test as original + cipher = new Atbash(true, true, true); + String output = cipher.encode(decodedString); + assertEquals(encodedString, output); + + //Test fully cleaned + cipher = new Atbash(false, false, false); + output = cipher.encode(decodedString); + assertEquals(encodedStringClean, output); + } + + @Test + public void testPracticalDecoding(){ + //Test as original + cipher = new Atbash(true, true, true); + String output = cipher.decode(encodedString); + assertEquals(decodedString, output); + + //Test fully cleaned + cipher = new Atbash(false, false, false); + output = cipher.decode(encodedString); + assertEquals(decodedStringClean, output); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAutokey.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAutokey.java index 68c48e7..f3b0e68 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAutokey.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAutokey.java @@ -1,413 +1,198 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAutokey.java //Mattrixwv // Created: 07-26-21 -//Modified: 07-09-22 +//Modified: 04-15-23 package com.mattrixwv.cipherstream.monosubstitution; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyChar; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; -import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class TestAutokey{ - @Test - public void testEncode() throws InvalidKeywordException, InvalidInputException{ - Autokey cipher = new Autokey(true, true, true); + private Autokey cipher; + private Logger logger; + //Variables + private String decodedString = "MeSsage to^encode"; + private String decodedStringClean = "MESSAGETOENCODE"; + private String encodedString = "WiQooxh fs^wfcuhx"; + private String encodedStringClean = "WIQOOXHFSWFCUHX"; + private String keyword = "keyword"; + private ArrayList offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3, 12, 4, 18, 18, 0, 6, 4, 19)); - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "wiqooxhfswfcuhx"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "WIQOOXHFSWFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "wiqooxh fs wfcuhx"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - keyword = "keyword"; - correctOutput = "wiqooxh*fs+wfcuhx"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "Wiqooxh fs^wfcuhx"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{ - Autokey cipher = new Autokey(false, true, true); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "WIQOOXHFSWFCUHX"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "WIQOOXHFSWFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "WIQOOXH FS WFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - keyword = "keyword"; - correctOutput = "WIQOOXH*FS+WFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "WIQOOXH FS^WFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{ - Autokey cipher = new Autokey(true, false, true); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "wiqooxhfswfcuhx"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "WIQOOXHFSWFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "wiqooxhfswfcuhx"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - keyword = "keyword"; - correctOutput = "wiqooxh*fs+wfcuhx"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "Wiqooxhfs^wfcuhx"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{ - Autokey cipher = new Autokey(true, true, false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "wiqooxhfswfcuhx"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "WIQOOXHFSWFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "wiqooxh fs wfcuhx"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - keyword = "keyword"; - correctOutput = "wiqooxhfswfcuhx"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "Wiqooxh fswfcuhx"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{ - Autokey cipher = new Autokey(false, false, false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "WIQOOXHFSWFCUHX"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "WIQOOXHFSWFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "WIQOOXHFSWFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - keyword = "keyword"; - correctOutput = "WIQOOXHFSWFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "WIQOOXHFSWFCUHX"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); + @BeforeEach + public void setup(){ + cipher = new Autokey(); + logger = mock(Logger.class); + Autokey.logger = logger; } @Test - public void testDecode() throws InvalidKeywordException, InvalidInputException{ - Autokey cipher = new Autokey(true, true, true); - - //Test lowercase decoding - String inputString = "wiqooxhfswfcuhx"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "WIQOOXHFSWFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "wiqooxh fs wfcuhx"; - keyword = "keyword"; - correctOutput = "message to encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "wiqooxh*fs+wfcuhx"; - keyword = "keyword"; - correctOutput = "message*to+encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Wiqooxh fs^wfcuhx"; - keyword = "keyword"; - correctOutput = "Message to^encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + public void testConstructor_default(){ + cipher = new Autokey(); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(0, cipher.offset.size()); } @Test - public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{ - Autokey cipher = new Autokey(false, true, true); + public void testConstructor_preserves(){ + cipher = new Autokey(true, false, false); + assertTrue(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(0, cipher.offset.size()); - //Test lowercase decoding - String inputString = "WIQOOXHFSWFCUHX"; - String keyword = "keyword"; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "WIQOOXHFSWFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + cipher = new Autokey(false, true, false); + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); - //Test whitespace decoding - inputString = "WIQOOXH FS WFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGE TO ENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "WIQOOXH*FS+WFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGE*TO+ENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "WIQOOXH FS^WFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGE TO^ENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + cipher = new Autokey(false, false, true); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertTrue(cipher.preserveSymbols); } @Test - public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{ - Autokey cipher = new Autokey(true, false, true); + public void testEncodeSet(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; - //Test lowercase decoding - String inputString = "wiqooxhfswfcuhx"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "WIQOOXHFSWFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + cipher.encodeSet(keyword, decodedString); - //Test whitespace decoding - inputString = "wiqooxh fs wfcuhx"; - keyword = "keyword"; - correctOutput = "messagetoencode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "wiqooxh*fs+wfcuhx"; - keyword = "keyword"; - correctOutput = "message*to+encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Wiqooxh fs^wfcuhx"; - keyword = "keyword"; - correctOutput = "Messageto^encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals(decodedString, cipher.inputString); + assertEquals((keyword + decodedString.replaceAll("\\s", "").replaceAll("[^a-zA-Z\\s]", "").substring(0, 8)).toUpperCase(), cipher.keyword); + assertEquals(offset, cipher.offset); + verify(logger, times(1)).debug("Setting fields for encoding"); + verify(logger, times(1)).debug("Setting keyword"); + verify(logger, times(1)).debug("Adding input to keyword"); + verify(logger, times(1)).debug("Removing last letters in the keyword"); + verify(logger, times(4)).debug(anyString()); } @Test - public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{ - Autokey cipher = new Autokey(true, true, false); + public void testDecodeSet(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; - //Test lowercase decoding - String inputString = "wiqooxhfswfcuhx"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "WIQOOXHFSWFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + cipher.decodeSet(keyword, decodedString); - //Test whitespace decoding - inputString = "wiqooxh fs wfcuhx"; - keyword = "keyword"; - correctOutput = "message to encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "wiqooxh*fs+wfcuhx"; - keyword = "keyword"; - correctOutput = "messagetoencode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Wiqooxh fs^wfcuhx"; - keyword = "keyword"; - correctOutput = "Message toencode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(decodedString, cipher.inputString); + verify(logger, times(1)).debug("Setting fields for decoding"); + verify(logger, times(1)).debug("Setting keyword"); + verify(logger, times(1)).debug("Setting input string"); + verify(logger, times(3)).debug(anyString()); } @Test - public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{ - Autokey cipher = new Autokey(false, false, false); + public void testDecode(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + Autokey.logger = mock(Logger.class); + cipher.decodeSet(keyword, encodedString); + Autokey.logger = logger; - //Test lowercase decoding - String inputString = "WIQOOXHFSWFCUHX"; - String keyword = "keyword"; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "WIQOOXHFSWFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + cipher.decode(); - //Test whitespace decoding - inputString = "WIQOOXH FS WFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals(decodedString, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(2)).debug("Appending partial output to keyword"); + verify(logger, times(17)).debug(eq("Working character {}"), anyChar()); + verify(logger, times(2)).debug("Appending uppercase"); + verify(logger, times(1)).debug("Wrapping around to Z"); + verify(logger, times(13)).debug("Appending lowercase"); + verify(logger, times(3)).debug("Wrapping around to z"); + verify(logger, times(17)).debug(eq("Decoded letter {}"), anyChar()); + verify(logger, times(1)).debug("Saving output string '{}'", decodedString); + } - //Test symbol decoding - inputString = "WIQOOXH*FS+WFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + @Test + public void testGetters(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + cipher.keyword = keyword; + cipher.offset.add(1); - //Test mixed case, whitespace, symbol decoding - inputString = "WIQOOXH FS^WFCUHX"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals(decodedString, cipher.getInputString()); + assertEquals(encodedString, cipher.getOutputString()); + assertEquals(keyword, cipher.getKeyword()); + assertEquals(new ArrayList<>(List.of(1)), cipher.getOffsets()); + } + + @Test + public void testReset(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + cipher.keyword = keyword; + cipher.offset.add(1); + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(0, cipher.offset.size()); + } + + @Test + public void testPracticalEncoding(){ + //Test as original + cipher = new Autokey(true, true, true); + String output = cipher.encode(keyword, decodedString); + assertEquals(encodedString, output); + + //Test fully cleaned + cipher = new Autokey(false, false, false); + output = cipher.encode(keyword, decodedString); + assertEquals(encodedStringClean, output); + } + + @Test + public void testPracticalDecoding(){ + //Test as original + cipher = new Autokey(true, true, true); + String output = cipher.decode(keyword, encodedString); + assertEquals(decodedString, output); + + //Test fully cleaned + cipher = new Autokey(false, false, false); + output = cipher.decode(keyword, encodedString); + assertEquals(decodedStringClean, output); } @Test - public void testKeyword() throws InvalidKeywordException{ + public void testSetKeyword() throws InvalidKeywordException{ Autokey cipher = new Autokey(); //Test keyword with whitespace diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaconian.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaconian.java index 7a848a4..a8118dc 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaconian.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaconian.java @@ -1,103 +1,319 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestBaconian.java //Mattrixwv // Created: 01-12-22 -//Modified: 07-09-22 +//Modified: 04-16-23 package com.mattrixwv.cipherstream.monosubstitution; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyChar; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class TestBaconian{ - @Test - public void testEncode() throws InvalidInputException{ - Baconian cipher = new Baconian(true); + private Baconian cipher; + private Logger logger; + //Variables + private String decodedString = "Message to-encode"; + private String decodedStringCleanUpper = "Messagetoencode"; + private String decodedStringCleanLower = "messagetoencode"; + private String encodedString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - //Test lowercase encoding - String inputString = "messagetoencode"; - String correctOutput = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - correctOutput = "ABABB AABAA BAAAB BAAAB AAAAA AABBA AABAA BAABA ABBAB AABAA ABBAA AAABA ABBAB AAABB AABAA"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to-encode"; - correctOutput = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - } - @Test - public void testEncodeNoCapital() throws InvalidInputException{ - Baconian cipher = new Baconian(false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String correctOutput = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - correctOutput = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "Message to-encode"; - correctOutput = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + @BeforeEach + public void setup(){ + cipher = new Baconian(); + logger = mock(Logger.class); + Baconian.logger = logger; } @Test - public void testDecode() throws InvalidCharacterException, InvalidInputException{ - Baconian cipher = new Baconian(true); + public void testConstructor_default(){ + cipher = new Baconian(); - //Test lowercase decoding - String inputString = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "ABABB AABAA BAAAB BAAAB AAAAA AABBA AABAA BAABA ABBAB AABAA ABBAA AAABA ABBAB AAABB AABAA"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - correctOutput = "Messagetoencode"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); + assertFalse(cipher.preserveCapitals); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); } + @Test - public void testDecodeNoCapital() throws InvalidCharacterException, InvalidInputException{ - Baconian cipher = new Baconian(false); + public void testConstructor_preserves(){ + cipher = new Baconian(true); - //Test lowercase decoding - String inputString = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "ABABB AABAA BAAAB BAAAB AAAAA AABBA AABAA BAABA ABBAB AABAA ABBAA AAABA ABBAB AAABB AABAA"; - correctOutput = "messagetoencode"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); + assertTrue(cipher.preserveCapitals); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + } - //Test mixed case, whitespace, and symbol decoding - inputString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - correctOutput = "messagetoencode"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); + @Test + public void testSetInputStringEncode(){ + cipher.preserveCapitals = true; + + cipher.setInputStringEncode(decodedString); + + assertEquals(decodedStringCleanUpper, cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString); + verify(logger, never()).debug("Removing case"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringCleanUpper); + verify(logger, never()).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputStringEncode_noCapitals(){ + cipher.preserveCapitals = false; + + cipher.setInputStringEncode(decodedString); + + assertEquals(decodedStringCleanLower, cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringCleanLower); + verify(logger, times(1)).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputStringEncode_blank(){ + cipher.preserveCapitals = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringEncode(""); + }, "Input must contain at least 1 letter"); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", ""); + verify(logger, never()).debug("Removing case"); + verify(logger, times(1)).debug("Cleaned input string '{}'", ""); + verify(logger, never()).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputStringEncode_null(){ + cipher.preserveCapitals = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringEncode(null); + }, "Input cannot be null"); + + assertEquals("", cipher.inputString); + verify(logger, never()).debug(anyString()); + verify(logger, never()).debug(anyString(), anyString()); + } + + @Test + public void testSetInputStringDecode(){ + cipher.preserveCapitals = true; + + cipher.setInputStringDecode(encodedString); + + assertEquals(encodedString, cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString); + verify(logger, never()).debug("Removing case"); + verify(logger, times(1)).debug("Ensuring all 'letters' contain 5 characters"); + verify(logger, times(15)).debug(eq("Current 'letter' {}"), anyString()); + verify(logger, times(15)).debug("Replacing all non-abAB characters"); + verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString); + verify(logger, times(16)).debug(anyString()); + verify(logger, times(17)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputStringDecode_noCapitals(){ + cipher.preserveCapitals = false; + + cipher.setInputStringDecode(encodedString); + + assertEquals(encodedString.toLowerCase(), cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Ensuring all 'letters' contain 5 characters"); + verify(logger, times(15)).debug(eq("Current 'letter' {}"), anyString()); + verify(logger, times(15)).debug("Replacing all non-abAB characters"); + verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.toLowerCase()); + verify(logger, times(17)).debug(anyString()); + verify(logger, times(17)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputStringDecode_invalidLength(){ + cipher.preserveCapitals = true; + + assertThrows(InvalidCharacterException.class, () -> { + cipher.setInputStringDecode("a"); + }, "All Baconian letters contain exactly 5 characters: a"); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", "a"); + verify(logger, never()).debug("Removing case"); + verify(logger, times(1)).debug("Ensuring all 'letters' contain 5 characters"); + verify(logger, times(1)).debug(eq("Current 'letter' {}"), anyString()); + verify(logger, never()).debug("Replacing all non-abAB characters"); + verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); + verify(logger, times(1)).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputStringDecode_invalidChars(){ + cipher.preserveCapitals = true; + + assertThrows(InvalidCharacterException.class, () -> { + cipher.setInputStringDecode("ccccc"); + }, "Baconian letters contain only a's and b's: ccccc"); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", "ccccc"); + verify(logger, never()).debug("Removing case"); + verify(logger, times(1)).debug("Ensuring all 'letters' contain 5 characters"); + verify(logger, times(1)).debug("Current 'letter' {}", "ccccc"); + verify(logger, times(1)).debug("Replacing all non-abAB characters"); + verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); + verify(logger, times(2)).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputStringDecode_blank(){ + cipher.preserveCapitals = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringDecode(""); + }, "Input cannot be empty"); + + assertEquals("", cipher.inputString); + verify(logger, never()).debug("Setting input string for decoding '{}'", ""); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Ensuring all 'letters' contain 5 characters"); + verify(logger, never()).debug(eq("Current 'letter' {}"), anyString()); + verify(logger, never()).debug("Replacing all non-abAB characters"); + verify(logger, never()).debug("Cleaned input string '{}'", ""); + verify(logger, never()).debug(anyString()); + verify(logger, never()).debug(anyString(), anyString()); + } + + @Test + public void testSetInputStringDecode_null(){ + cipher.preserveCapitals = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringDecode(null); + }, "Input cannot be null"); + + assertEquals("", cipher.inputString); + verify(logger, never()).debug(anyString()); + verify(logger, never()).debug(anyString(), anyString()); + } + + @Test + public void testEncode(){ + cipher.preserveCapitals = true; + cipher.inputString = decodedStringCleanUpper; + + cipher.encode(); + + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(15)).debug(eq("Working character {}"), anyChar()); + verify(logger, times(1)).debug("Encoding uppercase"); + verify(logger, times(14)).debug("Encoding lowercase"); + verify(logger, times(15)).debug(eq("Output letter {}"), anyString()); + verify(logger, times(1)).debug("Saving output string '{}'", encodedString); + verify(logger, times(16)).debug(anyString()); + verify(logger, times(16)).debug(anyString(), anyString()); + } + + @Test + public void testDecode(){ + cipher.preserveCapitals = true; + cipher.inputString = encodedString; + + cipher.decode(); + + assertEquals(decodedStringCleanUpper, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(15)).debug(eq("Working letter {}"), anyString()); + verify(logger, times(15)).debug(eq("Location of letter {}"), anyInt()); + verify(logger, times(1)).debug("Decoding uppercase"); + verify(logger, times(14)).debug("Decoding lowercase"); + verify(logger, times(15)).debug(eq("Decoded character {}"), anyChar()); + verify(logger, times(1)).debug("Saving output string '{}'", decodedStringCleanUpper); + verify(logger, times(16)).debug(anyString()); + verify(logger, times(16)).debug(anyString(), anyString()); + } + + @Test + public void testGetters(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + + assertEquals(decodedString, cipher.getInputString()); + assertEquals(encodedString, cipher.getOutputString()); + } + + @Test + public void testReset(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + verify(logger, times(1)).debug("Resetting fields"); + } + + @Test + public void testPracticalEncoding(){ + //Test as original + cipher.preserveCapitals = true; + String output = cipher.encode(decodedString); + assertEquals(encodedString, output); + + //Test fully cleaned + cipher.preserveCapitals = false; + output = cipher.encode(decodedString); + assertEquals(encodedString.toLowerCase(), output); + } + + @Test + public void testPracticalDecoding(){ + //Test as original + cipher.preserveCapitals = true; + String output = cipher.decode(encodedString); + assertEquals(decodedStringCleanUpper, output); + + //Test fully cleaned + cipher.preserveCapitals = false; + output = cipher.decode(encodedString); + assertEquals(decodedStringCleanLower, output); + + //Test uppercase input + cipher.preserveCapitals = false; + output = cipher.decode(encodedString.toUpperCase()); + assertEquals(decodedStringCleanLower, output); + + //Test lowercase input + cipher.preserveCapitals = false; + output = cipher.decode(encodedString.toLowerCase()); + assertEquals(decodedStringCleanLower, output); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaseX.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaseX.java index aa2f772..0c64fe9 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaseX.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaseX.java @@ -1,13 +1,24 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBaseX.java //Mattrixwv // Created: 01-08-22 -//Modified: 07-09-22 +//Modified: 04-16-23 package com.mattrixwv.cipherstream.monosubstitution; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.anyChar; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidBaseException; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; @@ -15,270 +26,292 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class TestBaseX{ - @Test - public void testBinaryEncode() throws InvalidBaseException, InvalidInputException{ - BaseX cipher = new BaseX(); + private BaseX cipher; + private Logger logger; + //Variables + private String decodedString = "A+B@C d\te\nf"; + private String encodedString_2 = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110"; + private String encodedString_8 = "101 53 102 100 103 40 144 11 145 12 146"; + private String encodedString_10 = "65 43 66 64 67 32 100 9 101 10 102"; + private String encodedString_16 = "41 2B 42 40 43 20 64 9 65 A 66"; - //Test lowercase encoding - String inputString = "a"; - String correctOutput = "1100001"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "A"; - correctOutput = "1000001"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test whitespace encoding - inputString = "A B\tC\n"; - correctOutput = "1000001 100000 1000010 1001 1000011 1010"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "A@B-C+"; - correctOutput = "1000001 1000000 1000010 101101 1000011 101011"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "A+B@C d\te\nf"; - correctOutput = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - } - @Test - public void testOctalEncode() throws InvalidBaseException, InvalidInputException{ - BaseX cipher = new BaseX(8); - - //Test lowercase encoding - String inputString = "a"; - String correctOutput = "141"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "A"; - correctOutput = "101"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "A B\tC\n"; - correctOutput = "101 40 102 11 103 12"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "A@B-C+"; - correctOutput = "101 100 102 55 103 53"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "A+B@C d\te\nf"; - correctOutput = "101 53 102 100 103 40 144 11 145 12 146"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - } - @Test - public void testDecimalEncode() throws InvalidBaseException, InvalidInputException{ - BaseX cipher = new BaseX(10); - - //Test lowercase encoding - String inputString = "a"; - String correctOutput = "97"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "A"; - correctOutput = "65"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "A B\tC\n"; - correctOutput = "65 32 66 9 67 10"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "A@B-C+"; - correctOutput = "65 64 66 45 67 43"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "A+B@C d\te\nf"; - correctOutput = "65 43 66 64 67 32 100 9 101 10 102"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - } - @Test - public void testHexEncode() throws InvalidBaseException, InvalidInputException{ - BaseX cipher = new BaseX(16); - - //Test lowercase encoding - String correctOutput = "61"; - String inputString = "a"; - String output = cipher.encode(inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "A"; - correctOutput = "41"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "A B\tC\n"; - correctOutput = "41 20 42 9 43 A"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "A@B-C+"; - correctOutput = "41 40 42 2D 43 2B"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol encoding - inputString = "A+B@C d\te\nf"; - correctOutput = "41 2B 42 40 43 20 64 9 65 A 66"; - output = cipher.encode(inputString); - assertEquals(correctOutput, output); + @BeforeEach + public void setup(){ + cipher = new BaseX(); + logger = mock(Logger.class); + BaseX.logger = logger; } @Test - public void testBinaryDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{ - BaseX cipher = new BaseX(); + public void testConstructor_default(){ + cipher = new BaseX(); - //Test lowercase decoding - String inputString = "1100001"; - String correctOutput = "a"; - String output = cipher.decode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "1000001"; - correctOutput = "A"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "1000001 100000 1000010 1001 1000011 1010"; - correctOutput = "A B\tC\n"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "1000001 1000000 1000010 101101 1000011 101011"; - correctOutput = "A@B-C+"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110"; - correctOutput = "A+B@C d\te\nf"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals(2, cipher.base); } + @Test - public void testOctalDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{ - BaseX cipher = new BaseX(8); + public void testConstructor_base(){ + cipher = new BaseX(8); - //Test lowercase decoding - String inputString = "141"; - String correctOutput = "a"; - String output = cipher.decode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "101"; - correctOutput = "A"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "101 40 102 11 103 12"; - correctOutput = "A B\tC\n"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "101 100 102 55 103 53"; - correctOutput = "A@B-C+"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "101 53 102 100 103 40 144 11 145 12 146"; - correctOutput = "A+B@C d\te\nf"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals(8, cipher.base); } + @Test - public void testDecimalDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{ - BaseX cipher = new BaseX(10); + public void testSetInputStringEncode(){ + cipher.setInputStringEncode(decodedString); - //Test lowercase decoding - String inputString = "97"; - String correctOutput = "a"; - String output = cipher.decode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "65"; - correctOutput = "A"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "65 32 66 9 67 10"; - correctOutput = "A B\tC\n"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "65 64 66 45 67 43"; - correctOutput = "A@B-C+"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, and symbol decoding - inputString = "65 43 66 64 67 32 100 9 101 10 102"; - correctOutput = "A+B@C d\te\nf"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); + assertEquals(decodedString, cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString); + verify(logger, times(1)).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); } + @Test - public void testHexDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{ - BaseX cipher = new BaseX(16); + public void testSetInputStringEncode_blank(){ + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringEncode(""); + }, "Input must contain at least 1 letter"); - //Test lowercase decoding - String inputString = "61"; - String correctOutput = "a"; - String output = cipher.decode(inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "41"; - correctOutput = "A"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", ""); + verify(logger, times(1)).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); + } - //Test whitespace decoding - inputString = "41 20 42 9 43 A"; - correctOutput = "A B\tC\n"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); + @Test + public void testSetInputStringEncode_null(){ + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringEncode(null); + }, "Input cannot be null"); - //Test symbol decoding - inputString = "41 40 42 2D 43 2B"; - correctOutput = "A@B-C+"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + verify(logger, never()).debug("Setting input string for encoding '{}'", decodedString); + verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug(anyString()); + } - //Test mixed case, whitespace, and symbol decoding - inputString = "41 2B 42 40 43 20 64 9 65 A 66"; - correctOutput = "A+B@C d\te\nf"; - output = cipher.decode(inputString); - assertEquals(correctOutput, output); + @Test + public void testSetInputStringDecode(){ + cipher.base = 16; + + cipher.setInputStringDecode(encodedString_16); + + assertEquals(encodedString_16, cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString_16); + verify(logger, times(1)).debug("Creating string of valid 'numbers'"); + verify(logger, times(16)).debug(eq("Current number {}, converted {}"), anyInt(), anyString()); + verify(logger, times(1)).debug("Checking for invalid characters"); + verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString_16); + } + + @Test + public void testSetInputStringDecode_invalid(){ + cipher.base = 16; + + assertThrows(InvalidCharacterException.class, () -> { + cipher.setInputStringDecode("G"); + }, "inputString cannot contain anything except numbers 0-15, and whitespace"); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", "G"); + verify(logger, times(1)).debug("Creating string of valid 'numbers'"); + verify(logger, times(16)).debug(eq("Current number {}, converted {}"), anyInt(), anyString()); + verify(logger, times(1)).debug("Checking for invalid characters"); + verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); + } + + @Test + public void testSetInputStringDecode_blank(){ + cipher.base = 16; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringDecode(""); + }, "Input must contain at least 1 letter"); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", ""); + verify(logger, times(1)).debug("Creating string of valid 'numbers'"); + verify(logger, times(16)).debug(eq("Current number {}, converted {}"), anyInt(), anyString()); + verify(logger, times(1)).debug("Checking for invalid characters"); + verify(logger, times(1)).debug("Cleaned input string '{}'", ""); + } + + @Test + public void testSetInputStringDecode_null(){ + cipher.base = 16; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringDecode(null); + }, "Input cannot be null"); + + assertEquals("", cipher.inputString); + verify(logger, never()).debug(eq("Setting input string for decoding '{}'"), anyString()); + verify(logger, never()).debug("Creating string of valid 'numbers'"); + verify(logger, never()).debug(eq("Current number {}, converted {}"), anyInt(), anyString()); + verify(logger, never()).debug("Checking for invalid characters"); + verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); + } + + @Test + public void testSetBase(){ + cipher.setBase(16); + + assertEquals(16, cipher.base); + verify(logger, times(1)).debug("Setting base {}", 16); + verify(logger, never()).debug(anyString()); + verify(logger, never()).debug(anyString(), anyString()); + } + + @Test + public void testSetBase_min(){ + assertThrows(InvalidBaseException.class, () -> { + cipher.setBase(Character.MIN_RADIX - 1); + }, "Base cannot be less than " + Character.MIN_RADIX); + + assertEquals(2, cipher.base); + verify(logger, never()).debug(eq("Setting base {}"), anyInt()); + verify(logger, never()).debug(anyString()); + verify(logger, never()).debug(anyString(), anyString()); + } + + @Test + public void testSetBase_max(){ + assertThrows(InvalidBaseException.class, () -> { + cipher.setBase(Character.MAX_RADIX + 1); + }, "Base cannot be larger than " + Character.MAX_RADIX); + + assertEquals(2, cipher.base); + verify(logger, never()).debug(eq("Setting base {}"), anyInt()); + verify(logger, never()).debug(anyString()); + verify(logger, never()).debug(anyString(), anyString()); + } + + @Test + public void testEncode(){ + cipher.base = 16; + cipher.inputString = decodedString; + + cipher.encode(); + + assertEquals(encodedString_16, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(11)).debug(eq("Working number {}"), anyChar()); + verify(logger, times(11)).debug(eq("Converted number {}"), anyString()); + verify(logger, times(1)).debug("Saving output string '{}'", encodedString_16); + } + + @Test + public void testDecode(){ + cipher.base = 16; + cipher.inputString = encodedString_16; + + cipher.decode(); + + assertEquals(decodedString, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(11)).debug(eq("Current number {}"), anyString()); + verify(logger, times(11)).debug(eq("Decoded number {}"), anyInt()); + verify(logger, times(1)).debug("Saving output string '{}'", decodedString); + } + + @Test + public void testDecode_invalidCharacter(){ + cipher.base = 16; + cipher.inputString = "FFF"; + + assertThrows(InvalidCharacterException.class, () -> { + cipher.decode(); + }, "The base 16 string FFF is not a valid ASCII character"); + + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(1)).debug("Current number {}", "FFF"); + verify(logger, times(1)).debug("Decoded number {}", 4095); + verify(logger, never()).debug(eq("Saving output string '{}'"), anyString()); + } + + @Test + public void testGetters(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString_2; + cipher.base = 8; + + assertEquals(decodedString, cipher.getInputString()); + assertEquals(encodedString_2, cipher.getOutputString()); + assertEquals(8, cipher.getBase()); + } + + @Test + public void testReset(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString_2; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + verify(logger, times(1)).debug("Resetting fields"); + verify(logger, times(1)).debug(anyString()); + } + + @Test + public void testPracticalEncoding_2(){ + String output = cipher.encode(2, decodedString); + + assertEquals(encodedString_2, output); + } + + @Test + public void testPracticalEncoding_8(){ + String output = cipher.encode(8, decodedString); + + assertEquals(encodedString_8, output); + } + + @Test + public void testPracticalEncoding_10(){ + String output = cipher.encode(10, decodedString); + + assertEquals(encodedString_10, output); + } + + @Test + public void testPracticalEncoding_16(){ + String output = cipher.encode(16, decodedString); + + assertEquals(encodedString_16, output); + } + + @Test + public void testPracticalDecoding_2(){ + String output = cipher.decode(2, encodedString_2); + + assertEquals(decodedString, output); + } + + @Test + public void testPracticalDecoding_8(){ + String output = cipher.decode(8, encodedString_8); + + assertEquals(decodedString, output); + } + + @Test + public void testPracticalDecoding_10(){ + String output = cipher.decode(10, encodedString_10); + + assertEquals(decodedString, output); + } + + @Test + public void testPracticalDecoding_16(){ + String output = cipher.decode(16, encodedString_16); + + assertEquals(decodedString, output); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBeaufort.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBeaufort.java index 1947cc8..94a4cd7 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBeaufort.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBeaufort.java @@ -1,407 +1,399 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBeaufort.java //Mattrixwv // Created: 02-23-22 -//Modified: 07-09-22 +//Modified: 04-16-23 package com.mattrixwv.cipherstream.monosubstitution; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class TestBeaufort{ - @Test - public void testEncode() throws InvalidKeywordException, InvalidInputException{ - Beaufort cipher = new Beaufort(true, true, true); + private Beaufort cipher; + private Logger logger; + //Variables + private String decodedString = "Message to^encode"; + private String decodedStringClean = "MESSAGETOENCODE"; + private String encodedString = "Yageolz rq^ujmdag"; + private String encodedStringClean = "YAGEOLZRQUJMDAG"; + private String keyword = "keyword"; + private String keywordDirty = "Ke*y word"; - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "yageolzrqujmdag"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "YAGEOLZRQUJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "yageolz rq ujmdag"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - keyword = "keyword"; - correctOutput = "yageolz*rq+ujmdag"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "Yageolz rq^ujmdag"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{ - Beaufort cipher = new Beaufort(false, true, true); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "YAGEOLZRQUJMDAG"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "YAGEOLZRQUJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "YAGEOLZ RQ UJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - keyword = "keyword"; - correctOutput = "YAGEOLZ*RQ+UJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "YAGEOLZ RQ^UJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{ - Beaufort cipher = new Beaufort(true, false, true); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "yageolzrqujmdag"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "YAGEOLZRQUJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "yageolzrqujmdag"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - keyword = "keyword"; - correctOutput = "yageolz*rq+ujmdag"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "Yageolzrq^ujmdag"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{ - Beaufort cipher = new Beaufort(true, true, false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "yageolzrqujmdag"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "YAGEOLZRQUJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "yageolz rq ujmdag"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - keyword = "keyword"; - correctOutput = "yageolzrqujmdag"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "Yageolz rqujmdag"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - } - - @Test - public void testNoCapitalwhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{ - Beaufort cipher = new Beaufort(false, false, false); - - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "YAGEOLZRQUJMDAG"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "YAGEOLZRQUJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "YAGEOLZRQUJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - keyword = "keyword"; - correctOutput = "YAGEOLZRQUJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "YAGEOLZRQUJMDAG"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); + @BeforeEach + public void setup(){ + cipher = new Beaufort(); + logger = mock(Logger.class); + Beaufort.logger = logger; } @Test - public void testDecode() throws InvalidKeywordException, InvalidInputException{ - Beaufort cipher = new Beaufort(true, true, true); + public void testConstructor_default(){ + cipher = new Beaufort(); - //Test lowercase decoding - String inputString = "yageolzrqujmdag"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "YAGEOLZRQUJMDAG"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "yageolz rq ujmdag"; - keyword = "keyword"; - correctOutput = "message to encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "yageolz*rq+ujmdag"; - keyword = "keyword"; - correctOutput = "message*to+encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Yageolz rq^ujmdag"; - keyword = "keyword"; - correctOutput = "Message to^encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertFalse(cipher.atbash.preserveCapitals); + assertFalse(cipher.atbash.preserveWhitespace); + assertFalse(cipher.atbash.preserveSymbols); + assertFalse(cipher.caesar.preserveCapitals); + assertFalse(cipher.caesar.preserveWhitespace); + assertFalse(cipher.caesar.preserveSymbols); + assertFalse(cipher.vigenere.preserveCapitals); + assertFalse(cipher.vigenere.preserveWhitespace); + assertFalse(cipher.vigenere.preserveSymbols); } @Test - public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{ - Beaufort cipher = new Beaufort(false, true, true); + public void testConstructor_preservesCapitals(){ + cipher = new Beaufort(true, false, false); - //Test lowercase decoding - String inputString = "yageolzrqujmdag"; - String keyword = "keyword"; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "YAGEOLZRQUJMDAG"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "yageolz rq ujmdag"; - keyword = "keyword"; - correctOutput = "MESSAGE TO ENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "yageolz*rq+ujmdag"; - keyword = "keyword"; - correctOutput = "MESSAGE*TO+ENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Yageolz rq^ujmdag"; - keyword = "keyword"; - correctOutput = "MESSAGE TO^ENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertTrue(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertTrue(cipher.atbash.preserveCapitals); + assertFalse(cipher.atbash.preserveWhitespace); + assertFalse(cipher.atbash.preserveSymbols); + assertTrue(cipher.caesar.preserveCapitals); + assertFalse(cipher.caesar.preserveWhitespace); + assertFalse(cipher.caesar.preserveSymbols); + assertTrue(cipher.vigenere.preserveCapitals); + assertFalse(cipher.vigenere.preserveWhitespace); + assertFalse(cipher.vigenere.preserveSymbols); } @Test - public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{ - Beaufort cipher = new Beaufort(true, false, true); + public void testConstructor_preservesWhitespace(){ + cipher = new Beaufort(false, true, false); - //Test lowercase decoding - String inputString = "yageolzrqujmdag"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "YAGEOLZRQUJMDAG"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "yageolz rq ujmdag"; - keyword = "keyword"; - correctOutput = "messagetoencode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "yageolz*rq+ujmdag"; - keyword = "keyword"; - correctOutput = "message*to+encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Yageolz rq^ujmdag"; - keyword = "keyword"; - correctOutput = "Messageto^encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertFalse(cipher.atbash.preserveCapitals); + assertTrue(cipher.atbash.preserveWhitespace); + assertFalse(cipher.atbash.preserveSymbols); + assertFalse(cipher.caesar.preserveCapitals); + assertTrue(cipher.caesar.preserveWhitespace); + assertFalse(cipher.caesar.preserveSymbols); + assertFalse(cipher.vigenere.preserveCapitals); + assertTrue(cipher.vigenere.preserveWhitespace); + assertFalse(cipher.vigenere.preserveSymbols); } @Test - public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{ - Beaufort cipher = new Beaufort(true, true, false); + public void testConstructor_preservesSymbols(){ + cipher = new Beaufort(false, false, true); - //Test lowercase decoding - String inputString = "yageolzrqujmdag"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "YAGEOLZRQUJMDAG"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "yageolz rq ujmdag"; - keyword = "keyword"; - correctOutput = "message to encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "yageolz*rq+ujmdag"; - keyword = "keyword"; - correctOutput = "messagetoencode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Yageolz rq^ujmdag"; - keyword = "keyword"; - correctOutput = "Message toencode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertTrue(cipher.preserveSymbols); + assertFalse(cipher.atbash.preserveCapitals); + assertFalse(cipher.atbash.preserveWhitespace); + assertTrue(cipher.atbash.preserveSymbols); + assertFalse(cipher.caesar.preserveCapitals); + assertFalse(cipher.caesar.preserveWhitespace); + assertTrue(cipher.caesar.preserveSymbols); + assertFalse(cipher.vigenere.preserveCapitals); + assertFalse(cipher.vigenere.preserveWhitespace); + assertTrue(cipher.vigenere.preserveSymbols); } @Test - public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{ - Beaufort cipher = new Beaufort(false, false, false); + public void testSetInputString(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; - //Test lowercase decoding - String inputString = "yageolzrqujmdag"; - String keyword = "keyword"; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "YAGEOLZRQUJMDAG"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + cipher.setInputString(decodedString); - //Test whitespace decoding - inputString = "yageolz rq ujmdag"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals(decodedString, cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString); + verify(logger, never()).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } - //Test symbol decoding - inputString = "yageolz*rq+ujmdag"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + @Test + public void testSetInputString_noCapitals(){ + cipher.preserveCapitals = false; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; - //Test mixed case, whitespace, symbol decoding - inputString = "Yageolz rq^ujmdag"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + cipher.setInputString(decodedString); + + assertEquals(decodedString.toUpperCase(), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase()); + verify(logger, times(1)).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputString_noWhitespace(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = false; + cipher.preserveSymbols = true; + + cipher.setInputString(decodedString); + + assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, never()).debug("Removing case"); + verify(logger, times(1)).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", "")); + verify(logger, times(1)).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputString_noSymbols(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = false; + + cipher.setInputString(decodedString); + + assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, times(1)).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", "")); + verify(logger, times(1)).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputString_blank(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(""); + }, "Input must contain at least 1 letter"); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", ""); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", ""); + verify(logger, never()).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetInputString_null(){ + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(null); + }, "Input must conatin at least 1 letter"); + + assertEquals("", cipher.inputString); + verify(logger, never()).debug(eq("Original input string '{}'"), anyString()); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); + verify(logger, never()).debug(anyString()); + verify(logger, never()).debug(anyString(), anyString()); + } + + @Test + public void testSetKeyword(){ + cipher.setKeyword(keyword); + + assertEquals(keyword.toUpperCase(), cipher.keyword); + verify(logger, times(1)).debug("Original keyword '{}'", keyword); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Removing all non-letters"); + verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase()); + verify(logger, times(2)).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetKeyword_dirty(){ + cipher.setKeyword(keywordDirty); + + assertEquals(keyword.toUpperCase(), cipher.keyword); + verify(logger, times(1)).debug("Original keyword '{}'", keywordDirty); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Removing all non-letters"); + verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase()); + verify(logger, times(2)).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetKeyword_blank(){ + assertThrows(InvalidKeywordException.class, () -> { + cipher.setKeyword(""); + }, "Keyword must contain at least 2 letters"); + + assertEquals("", cipher.keyword); + verify(logger, times(1)).debug("Original keyword '{}'", ""); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Removing all non-letters"); + verify(logger, times(1)).debug("Cleaned keyword '{}'", ""); + verify(logger, times(2)).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetKeyword_short(){ + assertThrows(InvalidKeywordException.class, () -> { + cipher.setKeyword("A"); + }, "Keyword must contain at least 2 letters"); + + assertEquals("A", cipher.keyword); + verify(logger, times(1)).debug("Original keyword '{}'", "A"); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Removing all non-letters"); + verify(logger, times(1)).debug("Cleaned keyword '{}'", "A"); + verify(logger, times(2)).debug(anyString()); + verify(logger, times(2)).debug(anyString(), anyString()); + } + + @Test + public void testSetKeyword_null(){ + assertThrows(InvalidKeywordException.class, () -> { + cipher.setKeyword(null); + }, "Keyword cannot be null"); + + assertEquals("", cipher.keyword); + verify(logger, never()).debug(eq("Original keyword '{}'"), anyString()); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing all non-letters"); + verify(logger, never()).debug(eq("Cleaned keyword '{}'"), anyString()); + verify(logger, never()).debug(anyString()); + verify(logger, never()).debug(anyString(), anyString()); + } + + @Test + public void testEncode(){ + cipher = new Beaufort(true, true, true); + cipher.keyword = keyword.toUpperCase(); + cipher.inputString = decodedString; + logger = mock(Logger.class); + Beaufort.logger = logger; + + cipher.encode(); + + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(1)).debug("Encoding with Atbash"); + verify(logger, times(1)).debug("Shifting all letters by 1"); + verify(logger, times(1)).debug("Encoding with Vigenere"); + verify(logger, times(1)).debug("Saving output string '{}'", encodedString); + verify(logger, times(4)).debug(anyString()); + verify(logger, times(1)).debug(anyString(), anyString()); + } + + @Test + public void testDecode(){ + cipher = new Beaufort(true, true, true); + cipher.keyword = keyword.toUpperCase(); + cipher.inputString = decodedString; + logger = mock(Logger.class); + Beaufort.logger = logger; + + cipher.decode(); + + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(1)).debug("Encoding with Atbash"); + verify(logger, times(1)).debug("Shifting all letters by 1"); + verify(logger, times(1)).debug("Encoding with Vigenere"); + verify(logger, times(1)).debug("Saving output string '{}'", encodedString); + verify(logger, times(4)).debug(anyString()); + verify(logger, times(1)).debug(anyString(), anyString()); + } + + @Test + public void testGetters(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + cipher.keyword = keyword; + + assertEquals(decodedString, cipher.inputString); + assertEquals(encodedString, cipher.outputString); + assertEquals(keyword, cipher.keyword); + } + + @Test + public void testReset(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + cipher.keyword = keyword; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + } + + @Test + public void testPracticalEncoding(){ + //Test as original + cipher = new Beaufort(true, true, true); + String output = cipher.encode(keyword, decodedString); + assertEquals(encodedString, output); + + //Test fully cleaned + cipher = new Beaufort(false, false, false); + output = cipher.encode(keyword, decodedString); + assertEquals(encodedStringClean, output); + } + + @Test + public void testPracticalDecoding(){ + //Test as original + cipher = new Beaufort(true, true, true); + String output = cipher.decode(keyword, encodedString); + assertEquals(decodedString, output); + + //Test fully cleaned + cipher = new Beaufort(false, false, false); + output = cipher.decode(keyword, encodedString); + assertEquals(decodedStringClean, output); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestCaesar.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestCaesar.java index dfc1fe2..df6b679 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestCaesar.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestCaesar.java @@ -1,628 +1,405 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestCaesar.java //Matthew Ellison // Created: 07-25-21 -//Modified: 07-09-22 +//Modified: 04-16-23 package com.mattrixwv.cipherstream.monosubstitution; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyChar; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class TestCaesar{ - @Test - public void testEncode() throws InvalidInputException{ - Caesar cipher = new Caesar(true, true, true); - - //Test lowercase encode - String input = "abc"; - int shift = 3; - String correctOutput = "def"; - String output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test uppercase encoding - input = "ABC"; - shift = 3; - correctOutput = "DEF"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); + private Caesar cipher; + private Logger logger; + //Variables + private String inputString = "The quick brown fox jumps over - the lAzy dog"; + private String inputStringClean = "thequickbrownfoxjumpsoverthelazydog"; + private String outputString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb iXwv ald"; + private String outputStringClean = "qebnrfzhyoltkclugrjmplsboqebixwvald"; + private int shift = 23; - //Test out of bounds shift encoding - input = "abc"; - shift = 29; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test out of bounds shift encoding negative - input = "abc"; - shift = -23; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - //Test whitespace encoding - input = "abc def"; - shift = 3; - correctOutput = "def ghi"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test symbol encoding - input = "abc-def@"; - shift = 3; - correctOutput = "def-ghi@"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test mixed case, whitespace, and symbol encoding - input = "The quick brown fox jumps over - the lazy dog"; - shift = 23; - correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test mixed case, whitespace, and symbol encoding with negative shift - input = "The quick brown fox jumps over - the lazy dog"; - shift = -3; - correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - } - @Test - public void testNoCapitalEncode() throws InvalidInputException{ - Caesar cipher = new Caesar(false, true, true); - - //Test lowercase encode - String input = "abc"; - int shift = 3; - String correctOutput = "def"; - String output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test uppercase encoding - input = "ABC"; - shift = 3; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test out of bounds shift encoding - input = "abc"; - shift = 29; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test out of bounds shift encoding negative - input = "abc"; - shift = -23; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test whitespace encoding - input = "abc def"; - shift = 3; - correctOutput = "def ghi"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test symbol decoding - input = "abc-def@"; - shift = 3; - correctOutput = "def-ghi@"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test mixed case, whitespace, and symbol encoding - input = "The quick brown fox jumps over - the lazy dog"; - shift = 23; - correctOutput = "qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test mixed case, whitespace, and symbol encoding with negative shift - input = "The quick brown fox jumps over - the lazy dog"; - shift = -3; - correctOutput = "qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - } - @Test - public void testNoWhitespaceEncode() throws InvalidInputException{ - Caesar cipher = new Caesar(true, false, true); - - //Test lowercase encoding - String input = "abc"; - int shift = 3; - String correctOutput = "def"; - String output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test uppercase encoding - input = "ABC"; - shift = 3; - correctOutput = "DEF"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test out of bounds shift encoding - input = "abc"; - shift = 29; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test out of bounds shift encoding negative - input = "abc"; - shift = -23; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test whitespace encoding - input = "abc def"; - shift = 3; - correctOutput = "defghi"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test symbol encoding - input = "abc-def@"; - shift = 3; - correctOutput = "def-ghi@"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Testing mixed case, whitespace, and symbol encoding - input = "The quick brown fox jumps over - the lazy dog"; - shift = 23; - correctOutput = "Qebnrfzhyoltkclugrjmplsbo-qebixwvald"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Testing mixed case, whitespace, and symbol encoding - input = "The quick brown fox jumps over - the lazy dog"; - shift = -3; - correctOutput = "Qebnrfzhyoltkclugrjmplsbo-qebixwvald"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - } - @Test - public void testNoSymbolEncode() throws InvalidInputException{ - Caesar cipher = new Caesar(true, true, false); - - //Test lowercase encode - String input = "abc"; - int shift = 3; - String correctOutput = "def"; - String output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test uppercase encoding - input = "ABC"; - shift = 3; - correctOutput = "DEF"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test out of bounds shift encoding - input = "abc"; - shift = 29; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test out of bounds shift encoding negative - input = "abc"; - shift = -23; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test whitespace encoding - input = "abc def"; - shift = 3; - correctOutput = "def ghi"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test symbol decoding - input = "abc-def@"; - shift = 3; - correctOutput = "defghi"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test mixed case, whitespace, and symbol encoding - input = "The quick brown fox jumps over - the lazy dog"; - shift = 23; - correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //test mixed case, whitespace, and symbol encoding with negative shift - input = "The quick brown fox jumps over - the lazy dog"; - shift = -3; - correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - } - @Test - public void testNoCapitalWhitespaceSymbolEncode() throws InvalidInputException{ - Caesar cipher = new Caesar(false, false, false); - - //Test lowercase encode - String input = "abc"; - int shift = 3; - String correctOutput = "def"; - String output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test uppercase encoding - input = "ABC"; - shift = 3; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test out of bounds shift encoding - input = "abc"; - shift = 29; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test out of bounds shift encoding negative - input = "abc"; - shift = 29; - correctOutput = "def"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test whitespace encoding - input = "abc def"; - shift = 3; - correctOutput = "defghi"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test symbol encoding - input = "abc-def@"; - shift = 3; - correctOutput = "defghi"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - - - //Test mixed case, whitespace, and symbol encoding - input = "The quick brown fox jumps over - the lazy dog"; - shift = 23; - correctOutput = "qebnrfzhyoltkclugrjmplsboqebixwvald"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); - //Test mixed case, whitespace, and symbol encoding with negative shift - input = "The quick brown fox jumps over - the lazy dog"; - shift = -3; - correctOutput = "qebnrfzhyoltkclugrjmplsboqebixwvald"; - output = cipher.encode(shift, input); - assertEquals(correctOutput, output); + @BeforeEach + public void setup(){ + cipher = new Caesar(); + logger = mock(Logger.class); + Caesar.logger = logger; } @Test - public void testDecode() throws InvalidInputException{ - Caesar cipher = new Caesar(true, true, true); + public void testConstructor_default(){ + cipher = new Caesar(); - //Test lowercase decoding - String input = "def"; - int shift = 3; - String correctOutput = "abc"; - String output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test uppercase decoding - input = "DEF"; - shift = 3; - correctOutput = "ABC"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test out of bounds shift decoding - input = "def"; - shift = 29; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test out of bounds shift negative decoding - input = "def"; - shift = -23; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test whitespace decoding - input = "def ghi"; - shift = 3; - correctOutput = "abc def"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test symbol decoding - input = "def-ghi@"; - shift = 3; - correctOutput = "abc-def@"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test mixed case, whitespace, and symbol decoding - input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - shift = 23; - correctOutput = "The quick brown fox jumps over - the lazy dog"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test mixed case, whitespace, and symbol decoding with negative shift - input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - shift = -3; - correctOutput = "The quick brown fox jumps over - the lazy dog"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals(0, cipher.shift); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); } + @Test - public void testNoCapitalDecode() throws InvalidInputException{ - Caesar cipher = new Caesar(false, true, true); + public void testConstructor_preservesCapitals(){ + cipher = new Caesar(true, false, false); - //Test lowercase decoding - String input = "def"; - int shift = 3; - String correctOutput = "abc"; - String output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test uppercase decoding - input = "DEF"; - shift = 3; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test out of bounds shift decoding - input = "def"; - shift = 29; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test out of bounds shift negative decoding - input = "def"; - shift = -23; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test whitespace decoding - input = "def ghi"; - shift = 3; - correctOutput = "abc def"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test symbol decoding - input = "def-ghi@"; - shift = 3; - correctOutput = "abc-def@"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test mixed case, whitespace, and symbol decoding - input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - shift = 23; - correctOutput = "the quick brown fox jumps over - the lazy dog"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test mixed case, whitespace, and symbol with negative shift - input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - shift = -3; - correctOutput = "the quick brown fox jumps over - the lazy dog"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals(0, cipher.shift); + assertTrue(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); } + @Test - public void testNoWhitespaceDecode() throws InvalidInputException{ - Caesar cipher = new Caesar(true, false, true); + public void testConstructor_preservesSymbols(){ + cipher = new Caesar(false, true, false); - //Test lowercase decoding - String input = "def"; - int shift = 3; - String correctOutput = "abc"; - String output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test uppercase decoding - input = "DEF"; - shift = 3; - correctOutput = "ABC"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test out of bounds shift decoding - input = "def"; - shift = 29; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test out of bounds shift negative decoding - input = "def"; - shift = -23; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test whitespace decoding - input = "def ghi"; - shift = 3; - correctOutput = "abcdef"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test symbol decoding - input = "def-ghi@"; - shift = 3; - correctOutput = "abc-def@"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test mixed case, whitespace, and symbol decoding - input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - shift = 23; - correctOutput = "Thequickbrownfoxjumpsover-thelazydog"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test mixed case, whitespace, and symbol decoding with negative shift - input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - shift = -3; - correctOutput = "Thequickbrownfoxjumpsover-thelazydog"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals(0, cipher.shift); + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); } + @Test - public void testNoSymbolDecode() throws InvalidInputException{ - Caesar cipher = new Caesar(true, true, false); + public void testConstructor_preservesWhitespace(){ + cipher = new Caesar(false, false, true); - //Test lowercase decoding - String input = "def"; - int shift = 3; - String correctOutput = "abc"; - String output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test uppercase decoding - input = "DEF"; - shift = 3; - correctOutput = "ABC"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test out of bounds shift decoding - input = "def"; - shift = 29; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test out of bounds shift negative decoding - input = "def"; - shift = -23; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test whitepace decoding - input = "def ghi"; - shift = 3; - correctOutput = "abc def"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test symbol decoding - input = "def-ghi!"; - shift = 3; - correctOutput = "abcdef"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - - - //Test mixed case, whitespace, and symbol decoding - input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - shift = 23; - correctOutput = "The quick brown fox jumps over the lazy dog"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test mixed case, whitespace, and symbol decoding with negative shift - input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - shift = -3; - correctOutput = "The quick brown fox jumps over the lazy dog"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals(0, cipher.shift); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertTrue(cipher.preserveSymbols); } + @Test - public void testNoCapitalWhitespaceSymbolDecode() throws InvalidInputException{ - Caesar cipher = new Caesar(false, false, false); + public void testSetShift(){ + cipher.setShift(shift); - //Test lowercase decoding - String input = "def"; - int shift = 3; - String correctOutput = "abc"; - String output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test uppercase decoding - input = "DEF"; - shift = 3; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); + assertEquals(shift, cipher.shift); + verify(logger, times(1)).debug("Setting shift {}", shift); + verify(logger, times(1)).debug("Cleaned shift {}", shift); + } + @Test + public void testSetShift_large(){ + cipher.setShift(shift + 26); - //Test out of bounds shift decoding - input = "def"; - shift = 29; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test out of bounds shift negative decoding - input = "def"; - shift = -23; - correctOutput = "abc"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); + assertEquals(shift, cipher.shift); + verify(logger, times(1)).debug("Setting shift {}", shift + 26); + verify(logger, times(1)).debug("Cleaned shift {}", shift); + } + @Test + public void testSetShift_negative(){ + cipher.setShift(shift - 26); - //Test whitespace decoding - input = "def ghi"; - shift = 3; - correctOutput = "abcdef"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); + assertEquals(shift - 26, cipher.shift); + verify(logger, times(1)).debug("Setting shift {}", shift - 26); + verify(logger, times(1)).debug("Cleaned shift {}", shift - 26); + } + @Test + public void testSetInputString(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; - //Test symbol decoding - input = "def-ghi@"; - shift = 3; - correctOutput = "abcdef"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); + cipher.setInputString(inputString); + assertEquals(inputString, cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", inputString); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", inputString); + } - //Test mixed case, whitespace, and symbol decoding - input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - shift = 23; - correctOutput = "thequickbrownfoxjumpsoverthelazydog"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); - //Test mixed case, whitespace, and symbol decoding with negative shift - input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; - shift = -3; - correctOutput = "thequickbrownfoxjumpsoverthelazydog"; - output = cipher.decode(shift, input); - assertEquals(correctOutput, output); + @Test + public void testSetInputString_noCapitals(){ + cipher.preserveCapitals = false; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + + cipher.setInputString(inputString); + + assertEquals(inputString.toLowerCase(), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", inputString); + verify(logger, times(1)).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toLowerCase()); + } + + @Test + public void testSetInputString_noWhitespace(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = false; + cipher.preserveSymbols = true; + + cipher.setInputString(inputString); + + assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", inputString); + verify(logger, never()).debug("Removing case"); + verify(logger, times(1)).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("\\s", "")); + } + + @Test + public void testSetInputString_noSymbols(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = false; + + cipher.setInputString(inputString); + + assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", inputString); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, times(1)).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("[^a-zA-Z\\s]", "")); + } + + @Test + public void testSetInputString_blank(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(""); + }, "Input must contain at least 1 letter"); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", ""); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", ""); + } + + @Test + public void testSetInputString_null(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(null); + }, "Input must contain at least 1 letter"); + + assertEquals("", cipher.inputString); + verify(logger, never()).debug("Original input string '{}'", ""); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, never()).debug("Cleaned input string '{}'", ""); + } + + @Test + public void testEncode(){ + cipher.inputString = inputString; + cipher.shift = shift; + + cipher.encode(); + + assertEquals(outputString, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(45)).debug(eq("Working character {}"), anyChar()); + verify(logger, times(2)).debug("Encoding uppercase"); + verify(logger, never()).debug("Wrapping arround to Z"); + verify(logger, times(1)).debug("Wrapping around to A"); + verify(logger, times(33)).debug("Encoding lowercase"); + verify(logger, never()).debug("Wrapping around to z"); + verify(logger, times(31)).debug("Wrapping around to a"); + verify(logger, times(45)).debug(eq("Encoded character {}"), anyChar()); + verify(logger, times(1)).debug("Saving encoded string '{}'", outputString); + } + + @Test + public void testEncode_negative(){ + cipher.inputString = inputString; + cipher.shift = shift - 26; + + cipher.encode(); + + assertEquals(outputString, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(45)).debug(eq("Working character {}"), anyChar()); + verify(logger, times(2)).debug("Encoding uppercase"); + verify(logger, times(1)).debug("Wrapping around to Z"); + verify(logger, never()).debug("Wrapping around to A"); + verify(logger, times(33)).debug("Encoding lowercase"); + verify(logger, times(2)).debug("Wrapping around to z"); + verify(logger, never()).debug("Wrapping around to a"); + verify(logger, times(45)).debug(eq("Encoded character {}"), anyChar()); + verify(logger, times(1)).debug("Saving encoded string '{}'", outputString); + } + + @Test + public void testDecode(){ + cipher.inputString = outputString; + cipher.shift = shift; + + cipher.decode(); + + assertEquals(inputString, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(45)).debug(eq("Working character {}"), anyChar()); + verify(logger, times(2)).debug("Decoding uppercase"); + verify(logger, times(1)).debug("Wrapping around to Z"); + verify(logger, never()).debug("Wrapping around to A"); + verify(logger, times(33)).debug("Decoding lowercase"); + verify(logger, times(31)).debug("Wrapping around to z"); + verify(logger, never()).debug("Wrapping around to a"); + verify(logger, times(45)).debug(eq("Decoded character {}"), anyChar()); + verify(logger, times(1)).debug("Saving decoded string '{}'", inputString); + } + + @Test + public void testDecode_negative(){ + cipher.inputString = outputString; + cipher.shift = shift - 26; + + cipher.decode(); + + assertEquals(inputString, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(45)).debug(eq("Working character {}"), anyChar()); + verify(logger, times(2)).debug("Decoding uppercase"); + verify(logger, never()).debug("Wrapping around to Z"); + verify(logger, times(1)).debug("Wrapping around to A"); + verify(logger, times(33)).debug("Decoding lowercase"); + verify(logger, never()).debug("Wrapping around to z"); + verify(logger, times(2)).debug("Wrapping around to a"); + verify(logger, times(45)).debug(eq("Decoded character {}"), anyChar()); + verify(logger, times(1)).debug("Saving decoded string '{}'", inputString); + } + + @Test + public void testGetters(){ + cipher.inputString = inputString; + cipher.outputString = outputString; + cipher.shift = shift; + + assertEquals(inputString, cipher.getInputString()); + assertEquals(outputString, cipher.getOutputString()); + assertEquals(shift, cipher.getShift()); + } + + @Test + public void testReset(){ + cipher.inputString = inputString; + cipher.outputString = outputString; + cipher.shift = shift; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals(0, cipher.shift); + verify(logger, times(1)).debug("Resetting fields"); + } + + @Test + public void testPracticalEncoding(){ + cipher = new Caesar(true, true, true); + logger = mock(Logger.class); + Caesar.logger = logger; + String output = cipher.encode(shift, inputString); + + assertEquals(inputString, cipher.inputString); + assertEquals(shift, cipher.shift); + assertEquals(outputString, cipher.outputString); + assertEquals(outputString, output); + } + + @Test + public void testPracticalEncoding_clean(){ + cipher = new Caesar(false, false, false); + logger = mock(Logger.class); + Caesar.logger = logger; + String output = cipher.encode(shift, inputString); + + assertEquals(inputStringClean, cipher.inputString); + assertEquals(shift, cipher.shift); + assertEquals(outputStringClean, cipher.outputString); + assertEquals(outputStringClean, output); + } + + @Test + public void testPracticalEncoding_negative(){ + cipher = new Caesar(true, true, true); + logger = mock(Logger.class); + Caesar.logger = logger; + String output = cipher.encode(shift, inputString); + + assertEquals(inputString, cipher.inputString); + assertEquals(shift, cipher.shift); + assertEquals(outputString, cipher.outputString); + assertEquals(outputString, output); + } + + @Test + public void testPracticalDecoding(){ + cipher = new Caesar(true, true, true); + logger = mock(Logger.class); + Caesar.logger = logger; + + String output = cipher.decode(shift, outputString); + + assertEquals(outputString, cipher.inputString); + assertEquals(shift, cipher.shift); + assertEquals(inputString, cipher.outputString); + assertEquals(inputString, output); + } + + @Test + public void testPracticalDecoding_clean(){ + cipher = new Caesar(false, false, false); + logger = mock(Logger.class); + Caesar.logger = logger; + + String output = cipher.decode(shift, outputString); + + assertEquals(outputStringClean, cipher.inputString); + assertEquals(shift, cipher.shift); + assertEquals(inputStringClean, cipher.outputString); + assertEquals(inputStringClean, output); + } + + @Test + public void testPracticalDecoding_negative(){ + cipher = new Caesar(true, true, true); + logger = mock(Logger.class); + Caesar.logger = logger; + String output = cipher.decode(shift - 26, outputString); + + assertEquals(outputString, cipher.inputString); + assertEquals(shift - 26, cipher.shift); + assertEquals(inputString, cipher.outputString); + assertEquals(inputString, output); } }