diff --git a/pom.xml b/pom.xml index a894263..0a094f7 100644 --- a/pom.xml +++ b/pom.xml @@ -44,25 +44,32 @@ 2.0.7 - - org.slf4j - slf4j-nop - 2.0.7 - test - - org.junit.jupiter junit-jupiter-api - 5.9.2 + 5.9.3 test org.mockito mockito-core - 5.3.0 + 5.3.1 + test + + + + org.mockito + mockito-junit-jupiter + 5.3.1 + test + + + + org.simplify4u + slf4j2-mock + 2.3.0 test @@ -120,6 +127,10 @@ maven-deploy-plugin 3.1.1 + + maven-assembly-plugin + 3.5.0 + maven-site-plugin @@ -127,7 +138,7 @@ maven-project-info-reports-plugin - 3.4.2 + 3.4.3 @@ -152,7 +163,7 @@ org.jacoco jacoco-maven-plugin - 0.8.9 + 0.8.10 jacoco-initialize diff --git a/src/main/java/com/mattrixwv/cipherstream/combination/ADFGVX.java b/src/main/java/com/mattrixwv/cipherstream/combination/ADFGVX.java index a1f3de4..dd6b24c 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: 04-21-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.combination; @@ -16,16 +16,16 @@ import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare; public class ADFGVX{ - protected static Logger logger = LoggerFactory.getLogger(ADFGVX.class); - + private static final Logger logger = LoggerFactory.getLogger(ADFGVX.class); //Fields - 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 + 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 + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string //Internal ciphers protected LargePolybiusSquare largePolybiusSquare; //The first step in encoding protected Columnar columnar; //The second step in encoding @@ -37,7 +37,7 @@ public class ADFGVX{ throw new InvalidKeywordException("Square Keyword cannot be null"); } - logger.debug("squareKeyword = {}", squareKeyword); + logger.debug("squareKeyword '{}'", squareKeyword); this.squareKeyword = squareKeyword; } //Ensures Columnar keyword constraints @@ -46,7 +46,7 @@ public class ADFGVX{ throw new InvalidKeywordException("Keyword cannot be null"); } - logger.debug("keyword = {}", keyword); + logger.debug("keyword '{}'", keyword); this.keyword = keyword; } //Ensures inputString constraints @@ -210,23 +210,20 @@ public class ADFGVX{ return decode(); } - //Returns the cleaned inputString + //Getters public String getInputString(){ return inputString; } - //Returns the outputString public String getOutputString(){ return outputString; } - //Returns the cleaned Polybius Square keyword public String getSquareKeyword(){ return squareKeyword; } - //Returns the cleaned Columnar keyword public String getKeyword(){ return keyword; } - //Makes sure all of the variables are empty + //Makes sure all variables are empty public void reset() throws InvalidCharacterException{ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/combination/ADFGX.java b/src/main/java/com/mattrixwv/cipherstream/combination/ADFGX.java index 6ffd5d0..d696b7e 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: 04-14-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.combination; @@ -16,16 +16,17 @@ import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare; public class ADFGX{ - protected static Logger logger = LoggerFactory.getLogger(ADFGX.class); + private static final Logger logger = LoggerFactory.getLogger(ADFGX.class); //Internal fields - protected String inputString; //The string that needs encoded/decoded + 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 - 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 keyword; //The keyword used in the Columnar cipher + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string //Internal ciphers protected PolybiusSquare polybiusSquare; //The first step in encoding protected Columnar columnar; //The second step in encoding @@ -37,7 +38,7 @@ public class ADFGX{ throw new InvalidKeywordException("Square keyword cannot be null"); } - logger.debug("squareKeyword = {}", squareKeyword); + logger.debug("Square keyword '{}'", squareKeyword); this.squareKeyword = squareKeyword; } //Ensures Columnar keyword constraints @@ -46,7 +47,7 @@ public class ADFGX{ throw new InvalidKeywordException("Keyword cannot be null"); } - logger.debug("keyword = {}", keyword); + logger.debug("Keyword '{}'", keyword); this.keyword = keyword; } //Ensures inputString constraints @@ -74,10 +75,10 @@ public class ADFGX{ this.inputString = inputString; + logger.debug("Cleaned input string '{}'", inputString); if(this.inputString.isBlank()){ throw new InvalidInputException("Input cannot be blank"); } - logger.debug("Cleaned input string '{}'", inputString); } //Format the output string with capitals, symbols, and numbers that are in the input string protected void formatOutputStringEncode(){ @@ -209,23 +210,20 @@ public class ADFGX{ return outputString; } - //Returns the cleaned inputString + //Getters public String getInputString(){ return inputString; } - //Returns the outputString public String getOutputString(){ return outputString; } - //Returns the cleaned Polybius Square keyword public String getSquareKeyword(){ return squareKeyword; } - //Returns the cleaned Columnar keyword public String getKeyword(){ return keyword; } - //Makes sure all of the variables are empty + //Makes sure all variables are empty public void reset() throws InvalidCharacterException{ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Affine.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Affine.java index 54c5067..6147028 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: 04-15-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -15,16 +15,17 @@ import com.mattrixwv.NumberAlgorithms; public class Affine{ - protected static Logger logger = LoggerFactory.getLogger(Affine.class); - + private static final Logger logger = LoggerFactory.getLogger(Affine.class); //Fields - 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 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 + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveSymbols; //Persist symbols in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + //Ensures key1 constraints protected void setKey1(int key1) throws InvalidKeywordException{ @@ -225,19 +226,16 @@ public class Affine{ return outputString; } - //Returns the cleaned inputString + //Getters public String getInputString(){ return inputString; } - //Returns the outputString public String getOutputString(){ return outputString; } - //Returns the cleaned key1 public int getKey1(){ return key1; } - //Returns the cleaned key2 public int getKey2(){ return key2; } diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Atbash.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Atbash.java index 76c0835..68dafd0 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: 04-15-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -12,17 +12,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class Atbash{ - protected static Logger logger = LoggerFactory.getLogger(Atbash.class); - + private static final Logger logger = LoggerFactory.getLogger(Atbash.class); + //Fields 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 + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string //Encodes inputString and stores in outputString - protected String encode(){ + protected void encode(){ logger.debug("Encoding"); StringBuilder output = new StringBuilder(); //Step through every element in the inputString and shift it the correct amount @@ -34,22 +35,27 @@ public class Atbash{ //Use either uppercase or lowercase for the base //(letterbase + 25 - (currentChar - letterBase)) if(Character.isUpperCase(currentChar)){ + logger.debug("Encoding uppercase"); + output.append((char)(155 - currentChar)); } else{ + logger.debug("Encoding lowercase"); + output.append((char)(219 - currentChar)); } } //Keep any punctuatio/whitespace the way it is else{ + logger.debug("Appending symbol"); + output.append(currentChar); } } //Return the output - logger.debug("Saving output string '{}'", output); outputString = output.toString(); - return outputString; + logger.debug("Saving output string '{}'", outputString); } //Removes all invalid characters and sets inputString protected void setInputString(String inputString) throws InvalidInputException{ @@ -108,22 +114,22 @@ public class Atbash{ //Make sure everything is empty before you begin reset(); setInputString(inputString); - return encode(); + encode(); + return outputString; } //Decodes inputString and returns the result public String decode(String inputString) throws InvalidInputException{ return encode(inputString); } - //Returns the cleaned input string + //Getters public String getInputString(){ return inputString; } - //Returns the output string public String getOutputString(){ return outputString; } - //Makes sure all of the variables are empty + //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Autokey.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Autokey.java index 2015b3c..821644d 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: 04-15-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -13,7 +13,8 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Autokey extends Vigenere{ - protected static Logger logger = LoggerFactory.getLogger(Autokey.class); + private static final Logger logger = LoggerFactory.getLogger(Autokey.class); + //Special rules for setting the strings for encoding protected void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ @@ -57,7 +58,7 @@ public class Autokey extends Vigenere{ } //Decodes the inputString @Override - protected String decode(){ + protected void decode(){ logger.debug("Decoding"); //Decode what the key will allow, add that to the key and continue @@ -109,7 +110,6 @@ public class Autokey extends Vigenere{ //Save and return the results outputString = fullOutput.toString(); logger.debug("Saving output string '{}'", outputString); - return outputString; } @@ -125,13 +125,15 @@ public class Autokey extends Vigenere{ public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ reset(); encodeSet(keyword, inputString); - return encode(); + encode(); + return outputString; } //Decodes inputString using the Autokey cipher @Override public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ reset(); decodeSet(keyword, inputString); - return decode(); + decode(); + 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 7e7a080..3b327ae 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: 04-15-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -17,7 +17,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class Baconian{ - protected static Logger logger = LoggerFactory.getLogger(Baconian.class); + private static final Logger logger = LoggerFactory.getLogger(Baconian.class); //Conversions protected static final ArrayList code = new ArrayList<>(Arrays.asList( @@ -25,8 +25,9 @@ public class Baconian{ "abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z )); 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 + protected String outputString; //The encoded/decoded string + protected boolean preserveCapitals; //Persist capitals in the output string + //Sets the input string protected void setInputStringEncode(String inputString) throws InvalidInputException{ @@ -121,7 +122,7 @@ public class Baconian{ logger.debug("Saving output string '{}'", outputString); } //Decodes the inputString and stores the result in outputString - protected String decode(){ + protected void decode(){ logger.debug("Decoding"); StringBuilder output = new StringBuilder(); @@ -154,7 +155,6 @@ public class Baconian{ //Save and return the output outputString = output.toString(); logger.debug("Saving output string '{}'", outputString); - return outputString; } //Constructor @@ -166,14 +166,7 @@ public class Baconian{ reset(); this.preserveCapitals = preserveCapitals; } - //Returns the outputString - public String getOutputString(){ - return outputString; - } - //Returns the inputString - public String getInputString(){ - return inputString; - } + //Sets the inputString and encodes the message public String encode(String inputString) throws InvalidInputException{ reset(); @@ -185,9 +178,18 @@ public class Baconian{ public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{ reset(); setInputStringDecode(inputString); - return decode(); + decode(); + return outputString; } - //Makes sure all of the variables are empty + + //Getters + public String getInputString(){ + return inputString; + } + public String getOutputString(){ + return outputString; + } + //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/BaseX.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/BaseX.java index ad5c959..3d16978 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: 04-16-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -16,12 +16,14 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException; public class BaseX{ - protected static Logger logger = LoggerFactory.getLogger(BaseX.class); - + private static final Logger logger = LoggerFactory.getLogger(BaseX.class); + //Fields protected String inputString; //The string that needs encoded/decoded protected String outputString; //The encoded/decoded string + //Settings protected int base; //The base that the number will be encoded at + //Sets the input string protected void setInputStringEncode(String inputString) throws InvalidInputException{ if(inputString == null){ @@ -80,7 +82,7 @@ public class BaseX{ this.base = base; } //Encode inputString, store it in outputString, and return it - protected String encode(){ + protected void encode(){ logger.debug("Encoding"); //Encode every character in inputString @@ -99,12 +101,9 @@ public class BaseX{ //Save the output outputString = output.toString().toUpperCase(); logger.debug("Saving output string '{}'", outputString); - - //Return the output - return outputString; } //Decode inputString, store it in outputString, and return it - protected String decode() throws InvalidCharacterException{ + protected void decode() throws InvalidCharacterException{ logger.debug("Decoding"); //Decode every binary number in the string @@ -128,9 +127,6 @@ public class BaseX{ //Save the output outputString = output.toString(); logger.debug("Saving output string '{}'", outputString); - - //Return the output - return outputString; } //Constructor @@ -142,43 +138,47 @@ public class BaseX{ reset(); setBase(base); } - //Returns the inputString - public String getInputString(){ - return inputString; - } - //Returns the outputString - public String getOutputString(){ - return outputString; - } - //Returns the base - public int getBase(){ - return base; - } + //Sets the inputString and encodes the message public String encode(String inputString) throws InvalidInputException{ reset(); setInputStringEncode(inputString); - return encode(); + encode(); + return outputString; } public String encode(int base, String inputString) throws InvalidBaseException, InvalidInputException{ reset(); setBase(base); setInputStringEncode(inputString); - return encode(); + encode(); + return outputString; } //Sets the inputString and decodes the message public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{ reset(); setInputStringDecode(inputString); - return decode(); + decode(); + return outputString; } public String decode(int base, String inputString) throws InvalidBaseException, InvalidCharacterException, InvalidInputException{ reset(); setBase(base); setInputStringDecode(inputString); - return decode(); + decode(); + return outputString; } - //Makes sure all of the variables are empty + + //Getters + public String getInputString(){ + return inputString; + } + public String getOutputString(){ + return outputString; + } + public int getBase(){ + return base; + } + //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Beaufort.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Beaufort.java index cda9e5f..a68cbde 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: 04-16-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -13,20 +13,21 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Beaufort{ - protected static Logger logger = LoggerFactory.getLogger(Beaufort.class); - + private static final Logger logger = LoggerFactory.getLogger(Beaufort.class); //Fields 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 + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string //Internal ciphers 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{ //Make sure the input isn't null @@ -138,18 +139,7 @@ public class Beaufort{ vigenere = new Vigenere(preserveCapitals, preserveWhitespace, preserveSymbols); reset(); } - //Returns the current inputString - public String getInputString(){ - return inputString; - } - //Returns the current outputString - public String getOutputString(){ - return outputString; - } - //Returns the current keyword - public String getKeyword(){ - return keyword; - } + //Encodes inputString using keyword and returns the result public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ //Set the parameters @@ -170,7 +160,18 @@ public class Beaufort{ decode(); return outputString; } - //Makes sure all of the variables are empty + + //Getters + public String getInputString(){ + return inputString; + } + public String getOutputString(){ + return outputString; + } + public String getKeyword(){ + return keyword; + } + //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Caesar.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Caesar.java index 7d2e74b..f65ef8f 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: 04-16-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -12,15 +12,16 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException; public class Caesar{ - protected static Logger logger = LoggerFactory.getLogger(Caesar.class); - + private static final Logger logger = LoggerFactory.getLogger(Caesar.class); //Fields 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 + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string + //Sets shift and makes sure it is within the propper bounds protected void setShift(int shiftAmount){ @@ -63,7 +64,7 @@ public class Caesar{ } } //Encodes the inputString and stores the result in outputString - protected String encode(){ + protected void encode(){ logger.debug("Encoding"); StringBuilder output = new StringBuilder(); @@ -109,10 +110,9 @@ public class Caesar{ outputString = output.toString(); logger.debug("Saving encoded string '{}'", outputString); - return outputString; } //Decodes the inputString and stores the result in outputString - protected String decode(){ + protected void decode(){ logger.debug("Decoding"); StringBuilder output = new StringBuilder(); @@ -162,7 +162,6 @@ public class Caesar{ outputString = output.toString(); logger.debug("Saving decoded string '{}'", outputString); - return outputString; } //Constructor @@ -184,29 +183,29 @@ public class Caesar{ reset(); setShift(shiftAmount); setInputString(inputString); - return encode(); + encode(); + return outputString; } //Sets the shift and inputString and decodes the message public String decode(int shiftAmount, String inputString) throws InvalidInputException{ reset(); setShift(shiftAmount); setInputString(inputString); - return decode(); + decode(); + return outputString; } - //Returns the inputString + //Getters public String getInputString(){ return inputString; } - //Returns shift public int getShift(){ return shift; } - //Returns the outputString public String getOutputString(){ return outputString; } - //Makes sure all of the variables are empty + //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/OneTimePad.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/OneTimePad.java index e83a636..72d639c 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/OneTimePad.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/OneTimePad.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/OneTimePad.java //Mattrixwv // Created: 02-23-22 -//Modified: 07-09-22 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -13,10 +13,9 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class OneTimePad extends Vigenere{ - protected static Logger logger = LoggerFactory.getLogger(OneTimePad.class); + private static final Logger logger = LoggerFactory.getLogger(OneTimePad.class); //?Add some kind of entropy calculator? - //?Add some kind of "book passage includer"? //Constructor diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Porta.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Porta.java index bc21f37..24f7d22 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Porta.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Porta.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Porta.java //Mattrixwv // Created: 02-28-22 -//Modified: 04-17-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -13,7 +13,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Porta{ - protected static Logger logger = LoggerFactory.getLogger(Porta.class); + private static final Logger logger = LoggerFactory.getLogger(Porta.class); private static final String[] tableau = { "NOPQRSTUVWXYZABCDEFGHIJKLM", //A-B @@ -32,12 +32,14 @@ public class Porta{ }; //Fields - protected String inputString; //The string that needs encoded/decoded + protected String inputString; //The string that needs encoded/decoded protected String outputString; //The encoded/decoded string protected String keyword; //The keyword used to encode the input 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 + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string + //Ensure all keyword constraints are followed protected void setKeyword(String keyword) throws InvalidKeywordException{ @@ -210,19 +212,17 @@ public class Porta{ return outputString; } - //Returns the inputString + //Getters public String getInputString(){ return inputString; } - //Returns the shift public String getOutputString(){ return outputString; } - //Returns the outputString public String getKeyword(){ return keyword; } - //Makes sure all of the fields are empty + //Makes sure all fields are empty public void reset(){ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Substitution.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Substitution.java index e9ef78a..056bdd1 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Substitution.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Substitution.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Substitution.java //Mattrixwv // Created: 02-22-22 -//Modified: 04-18-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -13,15 +13,16 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Substitution{ - protected static Logger logger = LoggerFactory.getLogger(Substitution.class); - + private static final Logger logger = LoggerFactory.getLogger(Substitution.class); //Fields protected String inputString; //The string that needs encoded/decoded protected String outputString; //The encoded/decoded string protected String keyword; //The keyword used to encode/decode the input - 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 + //Getters + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string + //Ensures key constraints are followed protected void setKeyword(String key) throws InvalidKeywordException{ @@ -172,6 +173,7 @@ public class Substitution{ logger.debug("Decoded message '{}'", outputString); } + //Constructors public Substitution(){ preserveCapitals = false; preserveWhitespace = false; @@ -184,6 +186,23 @@ public class Substitution{ this.preserveSymbols = preserveSymbols; reset(); } + + //Encodes inputString using the provided key + public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{ + setKeyword(key); + setInputString(inputString); + encode(); + return outputString; + } + //Decodes inputString using the provided key + public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{ + setKeyword(key); + setInputString(inputString); + decode(); + return outputString; + } + + //Getters public String getInputString(){ return inputString; } @@ -193,18 +212,7 @@ public class Substitution{ public String getKeyword(){ return keyword; } - public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{ - setKeyword(key); - setInputString(inputString); - encode(); - return outputString; - } - public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{ - setKeyword(key); - setInputString(inputString); - decode(); - return outputString; - } + //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java index cf0563d..351a42e 100644 --- a/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java +++ b/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Vigenere.java //Matthew Ellison // Created: 07-25-21 -//Modified: 04-18-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -16,16 +16,17 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Vigenere{ - protected static Logger logger = LoggerFactory.getLogger(Vigenere.class); - + private static final Logger logger = LoggerFactory.getLogger(Vigenere.class); //Fields 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 resposible for determining the offsets that you change each character by protected ArrayList offset; //Holds the offsets coputed from each character in the keyword - 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 + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string + //Uses keyword to calculate the offset for the Caesar cipher for each character protected void setOffset(){ @@ -102,7 +103,7 @@ public class Vigenere{ } } //Encodes inputString and stores the result in outputString - protected String encode(){ + protected void encode(){ logger.debug("Encoding"); StringBuilder output = new StringBuilder(); @@ -145,10 +146,9 @@ public class Vigenere{ //Save output outputString = output.toString(); logger.debug("Encoded message '{}'", outputString); - return outputString; } //Decodes inputString and stores the result in outputString - protected String decode(){ + protected void decode(){ logger.debug("Decoding"); StringBuilder output = new StringBuilder(); @@ -191,7 +191,6 @@ public class Vigenere{ //Save output outputString = output.toString(); logger.debug("Decoded message '{}'", outputString); - return outputString; } @@ -210,37 +209,39 @@ public class Vigenere{ this.preserveWhitespace = preserveWhitespace; this.preserveSymbols = preserveSymbols; } - //Returns the current inputString - public String getInputString(){ - return inputString; - } - //Returns the current outputString - public String getOutputString(){ - return outputString; - } - //Returns the current keyword - public String getKeyword(){ - return keyword; - } - //Returns the current offsets (Used mostly in bug fixing) - public List getOffsets(){ - return offset; - } + //Encodes input using key and returns the result public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ reset(); setKeyword(keyword); setInputString(inputString); - return encode(); + encode(); + return outputString; } //Decodes input using key and returns the result public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ reset(); setKeyword(keyword); setInputString(inputString); - return decode(); + decode(); + return outputString; } - //Makes sure all of the variables are empty + + //Getters + public String getInputString(){ + return inputString; + } + public String getOutputString(){ + return outputString; + } + public String getKeyword(){ + return keyword; + } + public List getOffsets(){ + return offset; + } + + //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Bifid.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Bifid.java index 1e1838a..7312214 100644 --- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Bifid.java +++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Bifid.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Bifid.java //Mattrixwv // Created: 03-03-22 -//Modified: 04-23-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -14,16 +14,17 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Bifid{ - protected static Logger logger = LoggerFactory.getLogger(Bifid.class); - + private static final Logger logger = LoggerFactory.getLogger(Bifid.class); //Fields protected String inputString; //The message that needs to be encoded/decoded protected String outputString; //The encoded/decoded message protected String keyword; //The keyword used to create the grid - protected PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters - protected boolean preserveCapitals; //Persist capitals in the output string + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string protected boolean preserveWhitespace; //Persist whitespace in the output string - protected boolean preserveSymbols; //Persist symbols in the output string + protected boolean preserveSymbols; //Persist symbols in the output string + //Internal ciphers + protected PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters //Strips invalid characters from the keyword and creates the grid @@ -169,7 +170,7 @@ public class Bifid{ } - //Constructor + //Constructors public Bifid() throws InvalidCharacterException{ preserveCapitals = false; preserveWhitespace = false; @@ -184,6 +185,7 @@ public class Bifid{ polybiusSquare = new PolybiusSquare(false, false); reset(); } + //Encodes inputString using keyword and returns the result public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{ //Set the parameters @@ -207,7 +209,7 @@ public class Bifid{ return outputString; } - //Gets + //Getters public String getInputString(){ return inputString; } @@ -217,6 +219,7 @@ public class Bifid{ public String getKeyword(){ return keyword; } + //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Columnar.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Columnar.java index 9b3dbc0..37fbe6c 100644 --- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Columnar.java +++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Columnar.java @@ -1,7 +1,7 @@ //MattrixwvWebsite/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Columnar.java //Mattrixwv // Created: 01-16-22 -//Modified: 04-26-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -18,19 +18,20 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Columnar{ - protected static Logger logger = LoggerFactory.getLogger(Columnar.class); - + private static final Logger logger = LoggerFactory.getLogger(Columnar.class); //Fields - protected String inputString; //The message that needs to be encoded/decoded + protected String inputString; //The message that needs to be encoded/decoded protected String outputString; //The encoded/decoded message - protected String keyword; //The keyword used to create the grid + protected String keyword; //The keyword used to create the grid protected char characterToAdd; //The character that is added to the end of a string to bring it to the correct length - protected int charsAdded; //The number of characters that were added to the end of the message + protected int charsAdded; //The number of characters that were added to the end of the message protected ArrayList> grid; //The grid used to encode/decode the message - protected boolean preserveCapitals; //Persist capitals in the output string + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string protected boolean preserveWhitespace; //Persist whitespace in the output string - protected boolean preserveSymbols; //Persist symbols in the output string - protected boolean removePadding; //Remove the padding letters added to the cipher + protected boolean preserveSymbols; //Persist symbols in the output string + protected boolean removePadding; //Remove the padding letters added to the cipher + //Strip the inputString of all non-letter characters and change them to capitals protected String getCleanInputString(){ @@ -467,7 +468,7 @@ public class Columnar{ createOutputStringFromRows(); } - //Constructor + //Constructors public Columnar() throws InvalidCharacterException{ preserveCapitals = false; preserveWhitespace = false; @@ -492,6 +493,7 @@ public class Columnar{ setCharacterToAdd(characterToAdd); reset(); } + //Encodes inputString using keyword and returns the result public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ //Set the parameters @@ -525,7 +527,8 @@ public class Columnar{ grid = new ArrayList<>(); charsAdded = 0; } - //Gets + + //Getters public String getInputString(){ return inputString; } diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Hill.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Hill.java index a21f8f2..addfb69 100644 --- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Hill.java +++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Hill.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Hill.java //Mattrixwv // Created: 01-31-22 -//Modified: 04-27-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -19,17 +19,19 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException; public class Hill{ - protected static Logger logger = LoggerFactory.getLogger(Hill.class); + private static final Logger logger = LoggerFactory.getLogger(Hill.class); + //Fields + protected String inputString; //The message that needs to be encoded/decoded + protected String outputString; //The encoded/decoded message + protected char characterToAdd; //The keyword used to create the grid + protected ModMatrix key; //The matrix used perform the encoding/decoding + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string - protected boolean preserveCapitals; - protected boolean preserveWhitespace; - protected boolean preserveSymbols; - protected String inputString; - protected String outputString; - protected char characterToAdd; - protected ModMatrix key; - + //Validate and set the key protected void setKey(ModMatrix key) throws InvalidKeyException{ logger.debug("Setting key"); @@ -55,9 +57,10 @@ public class Hill{ } //Set the key - logger.debug("key = {}", key); + logger.debug("key\n{}", key); this.key = new ModMatrix(key); } + //Validate and set the input string protected void setInputStringEncode(String inputString) throws InvalidInputException{ logger.debug("Setting input string for encoding"); @@ -142,6 +145,7 @@ public class Hill{ throw new InvalidInputException("Length of input string must be a multiple of the number of rows in the key"); } } + //Get a perfectly clean copy of input string protected String getCleanInputString(){ logger.debug("Cleaning inputString"); @@ -150,6 +154,7 @@ public class Hill{ return cleanInputString; } + //Validate and set character to add protected void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{ logger.debug("Setting character to add {}", characterToAdd); @@ -168,6 +173,7 @@ public class Hill{ logger.debug("Cleaned character {}", characterToAdd); this.characterToAdd = characterToAdd; } + //Add capitalization, whitespace, and symbols to the output based on the input protected String polishOutputString(){ logger.debug("Polishing output string"); @@ -197,6 +203,7 @@ public class Hill{ logger.debug("Polished string '{}'", cleanString); return cleanString; } + //Get vectors representing the input string protected ArrayList getInputVectors(){ logger.debug("Generating input vectors"); @@ -230,6 +237,7 @@ public class Hill{ //Return the array of vectors return vectors; } + //Get the string represented by the vectors that were passed in protected String getOutputFromVectors(ArrayList outputVectors){ logger.debug("Turning vectors into a string"); @@ -249,6 +257,7 @@ public class Hill{ logger.debug("Converted string '{}'", convertedString); return convertedString; } + //Encode inputString and store the value in outputString protected void encode(){ logger.debug("Encoding"); @@ -272,6 +281,7 @@ public class Hill{ //Add the extra characters back to the output and remove the added characters outputString = polishOutputString(); } + //Decode inputString and store the value in outputString protected void decode(){ logger.debug("Decoding"); @@ -298,6 +308,7 @@ public class Hill{ outputString = polishOutputString(); } + //Constructors public Hill() throws InvalidCharacterException{ preserveCapitals = false; preserveWhitespace = false; @@ -320,6 +331,7 @@ public class Hill{ reset(); } + //Encode inputString using the provided key public String encode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{ return encode(new ModMatrix(key, 26), inputString); } @@ -329,6 +341,7 @@ public class Hill{ encode(); return outputString; } + //Decode inputString using the provided key public String decode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{ return decode(new ModMatrix(key, 26), inputString); } @@ -338,6 +351,8 @@ public class Hill{ decode(); return outputString; } + + //Makes sure all variables are empty public void reset(){ logger.debug("Resetting fields"); @@ -345,6 +360,8 @@ public class Hill{ outputString = ""; key = new ModMatrix(26); } + + //Getters public String getInputString(){ return inputString; } diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java index 29885de..f09bcb5 100644 --- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java +++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java //Mattrixwv // Created: 04-21-23 -// Modified: 04-21-23 +// Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -16,9 +16,10 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class LargePolybiusSquare extends PolybiusSquare{ - protected static Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class); + private static final Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class); + //Create the grid from the keyword @Override protected void createGrid(){ logger.debug("Creating grid"); @@ -30,6 +31,7 @@ public class LargePolybiusSquare extends PolybiusSquare{ } } } + //Strips invalid characters from the string that needs encoded/decoded @Override protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{ if(inputString == null){ @@ -73,6 +75,7 @@ public class LargePolybiusSquare extends PolybiusSquare{ throw new InvalidInputException("Input must contain at least 1 letter"); } } + //Returns the input string ready for encoding @Override protected String getPreparedInputStringEncode(){ logger.debug("Preparing input string for encoding"); @@ -84,6 +87,7 @@ public class LargePolybiusSquare extends PolybiusSquare{ return cleanString; } + //Strips invalid characters from the keyword and creates the grid @Override protected void setKeyword(String keyword) throws InvalidKeywordException{ if(keyword == null){ @@ -112,6 +116,7 @@ public class LargePolybiusSquare extends PolybiusSquare{ //Create the grid from the sanitized keyword createGrid(); } + //Adds characters that aren't letters to the output @Override protected void addCharactersToCleanStringEncode(String cleanString){ logger.debug("Formatting output string"); @@ -166,6 +171,8 @@ public class LargePolybiusSquare extends PolybiusSquare{ outputString = fullOutput.toString(); logger.debug("Saving output string {}", outputString); } + + //Makes sure all variables are empty @Override public void reset(){ logger.debug("Resetting"); @@ -176,6 +183,7 @@ public class LargePolybiusSquare extends PolybiusSquare{ keyword = ""; } + //Constructors public LargePolybiusSquare() throws InvalidCharacterException{ super(); } diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Morse.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Morse.java index 32a6def..f2b2131 100644 --- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Morse.java +++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Morse.java @@ -1,94 +1,151 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Morse.java -//Matthew Ellison +//Mattrixwv // Created: 07-28-21 -//Modified: 01-16-22 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; +import java.util.HashMap; +import java.util.Map; +import java.util.StringJoiner; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.mattrixwv.cipherstream.exceptions.InvalidInputException; + + public class Morse{ - //TODO: This needs updated to match new standards - //Holds the Morse representation of the alphanumeric characters - private static final String[] code = { + private static final Logger logger = LoggerFactory.getLogger(Morse.class); + //Code representations + private static final String[] letters = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", //A-L - "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", //M-Z + "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." //M-Z + }; + private static final String[] numbers = { "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." //0-9 }; - private String inputString; //The string that needs encoded/decoded - private String outputString; //The encoded/decoded message + private static final Map map; //The map to help convert from Morse to letters and numbers + static{ + //Setup the map + map = new HashMap<>(); + for(int cnt = 0;cnt < letters.length;++cnt){ + String letter = letters[cnt]; + map.put(letter, (char)('A' + cnt)); + } + for(int cnt = 0;cnt < numbers.length;++cnt){ + String number = numbers[cnt]; + map.put(number, (char)('0' + cnt)); + } + } + //Fields + protected String inputString; //The string that needs encoded/decoded + protected String outputString; //The encoded/decoded message - //Encodes inputString and stores the result in outputString - private String encode(){ - StringBuilder output = new StringBuilder(); - //Loop through every element in the input string and see what type it is + + //Validate and set the input string for encoding + protected void setInputStringEncode(String inputString){ + logger.debug("Setting input string for encoding '{}'", inputString); + + //Check for null + if(inputString == null){ + throw new InvalidInputException("Input cannot be null"); + } + + //Convert all letters to uppercase + logger.debug("Removing case"); + inputString = inputString.toUpperCase(); + + //Remove all except alpha-numeric characters + logger.debug("Removing whitespace and symbols"); + inputString = inputString.replaceAll("[^A-Z0-9]", ""); + + logger.debug("Cleaned input string '{}'", inputString); + + //Check for a blank input + if(inputString.isBlank()){ + throw new InvalidInputException("Input must contain at least 1 letter"); + } + + //Save the input + this.inputString = inputString; + } + //Validate and set the input string for decoding + protected void setInputStringDecode(String inputString){ + logger.debug("Setting input string for decoding '{}'", inputString); + + //Check for null + if(inputString == null){ + throw new InvalidInputException("Input cannot be null"); + } + + //Remove all non-morse code characters and check if there is a difference + logger.debug("Checking for invalid characters"); + if(!inputString.equals(inputString.replaceAll("[^ \\.\\-]", ""))){ + throw new InvalidInputException("Invalid Morse characters found"); + } + + //Check for blank input + if(inputString.isBlank()){ + throw new InvalidInputException("Input must contain at least 1 letter"); + } + + //Save the input + logger.debug("Saving"); + this.inputString = inputString; + } + //Encodes the inputString and stores the result in outputString + protected void encode(){ + logger.debug("Encoding"); + + StringJoiner output = new StringJoiner(" "); + //Loop through every element in the input string and encode it for(char letter : inputString.toCharArray()){ - //If the character is a letter get teh correct combination from code and add it to the output + logger.debug("Working character {}", letter); + //If the character is a letter or a number get the correct code and add it to the output if(Character.isUpperCase(letter)){ - output.append(code[letter - 65]); - output.append(' '); + logger.debug("Appending letter"); + + output.add(letters[letter - 'A']); } - //If it is a number get the correct combination from code and add it to the output else if(Character.isDigit(letter)){ - int tempNum = Integer.parseInt(Character.toString(letter)); - output.append(code[tempNum + 26]); - output.append(' '); + logger.debug("Appending number"); + + output.add(numbers[letter - '0']); + } + //If the character is not a letter or number throw an exception because it cannot be encoded + else{ + throw new InvalidInputException("Invalid characters found in input"); } } - //Remove the final space from the output - if(output.length() > 0){ - output.replace(output.length() - 1, output.length(), ""); - } - //Save and return the output - outputString = output.toString(); - return outputString; + //Save the output + this.outputString = output.toString(); + logger.debug("Saving encoded string '{}'", outputString); } - //Decodes inputString and stores the result in outputString - private String decode(){ + //Decodes the inputString and stores the result in outputString + protected void decode(){ + logger.debug("Decoding"); + StringBuilder output = new StringBuilder(); + //Loop through every element in the input string and encode it for(String current : inputString.split(" ")){ - boolean found = false; - //Loop through the code and see if the current letter - for(int cnt = 0;(cnt < 26) && (!found);++cnt){ - //See if current is the same as an element in code - if(current.equals(code[cnt])){ - //Add 65 to cnt to get the correct capital letter - output.append((char)(cnt + 'A')); - found = true; - } + logger.debug("Working letter {}", current); + + //Get the current letter from the map and append it to the output + if(map.containsKey(current)){ + char letter = map.get(current); + logger.debug("Decoded letter {}", letter); + output.append(letter); } - //Loop through code and see if current is a number - for(int cnt = 26;(cnt < 36) && (!found);++cnt){ - if(current.equals(code[cnt])){ - //Remove 26 from cnt to get the correct number - output.append(Integer.toString(cnt - 26)); - found = true; - } - } - //If it is neither print an error in the output - if(!found){ - output.append(""); + else{ + throw new InvalidInputException("Invalid characters found in input"); } } - //Save and return the output - outputString = output.toString(); - return outputString; - } - //Encodes input and returns the result - private void setEncodeInputString(String input){ - //Make sure the letters are all uppercase - input = input.toUpperCase(); - //Remove all characters that are not capital letters - input = input.replaceAll("[^A-Z0-9]", ""); - //Save the new input - inputString = input; - } - //Decodes input and returns the result - private void setDecodeInputString(String input){ - //Remove all characters except ., -, and ' ' - input = input.replaceAll("[^ \\.\\-]", ""); - //Save the new input - inputString = input; + //Save the output + this.outputString = output.toString(); + logger.debug("Saving decoded string '{}'", outputString); } @@ -96,26 +153,34 @@ public class Morse{ public Morse(){ reset(); } - //Returns inputString + + + //Sets the inputString and encodes the message + public String encode(String inputString){ + setInputStringEncode(inputString); + encode(); + return outputString; + } + //Sets the inputString and decodes the message + public String decode(String inputString){ + setInputStringDecode(inputString); + decode(); + return outputString; + } + + + //Getters public String getInputString(){ return inputString; } - //Returns outputString public String getOutputString(){ return outputString; } - //Encodes input and returns the result - public String encode(String input){ - setEncodeInputString(input); - return encode(); - } - //Decoes input and returns the result - public String decode(String input){ - setDecodeInputString(input); - return decode(); - } //Makes sure all variables are empty public void reset(){ - inputString = outputString = ""; + logger.debug("Resetting"); + + inputString = ""; + outputString = ""; } } diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Playfair.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Playfair.java index 2ae2fca..3de6775 100644 --- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Playfair.java +++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Playfair.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java //Matthew Ellison // Created: 07-30-21 -//Modified: 04-28-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -16,7 +16,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class Playfair{ - protected static Logger logger = LoggerFactory.getLogger(Playfair.class); + private static final Logger logger = LoggerFactory.getLogger(Playfair.class); //A class representing the location of a character in the grid protected class CharLocation{ @@ -35,16 +35,17 @@ public class Playfair{ } //Fields - protected boolean preserveCapitals; //Whether to respect captials 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 char replaced; //The letter that will need to be replaced in the grid and any input string or keyword - protected char replacer; //The letter that replaced replaced in the input string or keyword - protected char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even protected String inputString; //The message that needs to be encoded/decoded protected String outputString; //The encoded/decoded message protected String keyword; //The keyword used to create the grid protected char[][] grid; //The grid used to encode/decode the message + protected char replaced; //The letter that will need to be replaced in the grid and any input string or keyword + protected char replacer; //The letter that replaced replaced in the input string or keyword + protected char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even + //Settings + protected boolean preserveCapitals; //Persist captials in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string //Set the doubled character @@ -482,6 +483,7 @@ public class Playfair{ addCharactersToCleanString(output.toString()); } + //Constructor public Playfair() throws InvalidCharacterException{ reset(); preserveCapitals = false; @@ -509,6 +511,7 @@ public class Playfair{ setReplacer(replacer); setDoubled(doubled); } + //Sets the keyword and inputString and encodes the message public String encode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{ reset(); @@ -535,7 +538,7 @@ public class Playfair{ outputString = ""; keyword = ""; } - //Gets + //Getters public char getReplaced(){ return replaced; } diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java index 9662669..95f875e 100644 --- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java +++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/PolybiusSquare.java //Mattrixwv // Created: 01-04-22 -//Modified: 04-29-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -16,7 +16,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; public class PolybiusSquare{ - protected static Logger logger = LoggerFactory.getLogger(PolybiusSquare.class); + private static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class); //A class representing the location of a character in the grid protected class CharLocation{ @@ -41,8 +41,9 @@ public class PolybiusSquare{ protected char[][] grid; //The grid used to encode/decode the message protected char replaced; //The letter that will need to be replaced in the grid and any input string or keyword protected char replacer; //The letter that replaces replaced in the input string or keyword - protected boolean preserveWhitespace; //Whether to respect whitespace in the output string - protected boolean preserveSymbols; //Whether to respect symbols in the output string + //Settings + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string //Setting the character to be replaced @@ -362,6 +363,7 @@ public class PolybiusSquare{ addCharactersToCleanStringDecode(output.toString()); } + //Constructors public PolybiusSquare() throws InvalidCharacterException{ reset(); setReplaced('J'); @@ -383,6 +385,7 @@ public class PolybiusSquare{ this.preserveWhitespace = preserveWhitespace; this.preserveSymbols = preserveSymbols; } + //Sets the keyword and inputString and encodes the message public String encode(String inputString) throws InvalidCharacterException, InvalidInputException{ return encode("", inputString); @@ -415,7 +418,8 @@ public class PolybiusSquare{ outputString = ""; keyword = ""; } - //Gets + + //Getters public char getReplaced(){ return replaced; } diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/RailFence.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/RailFence.java index 2fdefcc..d94023f 100644 --- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/RailFence.java +++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/RailFence.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/RailFence.java //Mattrixwv // Created: 03-21-22 -//Modified: 07-09-22 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -16,17 +16,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException; public class RailFence{ private static final Logger logger = LoggerFactory.getLogger(RailFence.class); - //Fields - private String inputString; //The message that needs to be encoded/decoded - private String outputString; //The encoded/decoded message - private StringBuilder[] fence; //The fence used for encoding/decoding - private boolean preserveCapitals; //Persist capitals in the output string - private boolean preserveWhitespace; //Persist whitespace in the output string - private boolean preserveSymbols; //Persist symbols in the output string + protected String inputString; //The message that needs to be encoded/decoded + protected String outputString; //The encoded/decoded message + protected StringBuilder[] fence; //The fence used for encoding/decoding + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string + //Strips invalid characters from the string that needs encoded/decoded - private void setInputString(String inputString) throws InvalidInputException{ + protected void setInputString(String inputString) throws InvalidInputException{ //Ensure the input string isn't null if(inputString == null){ throw new InvalidInputException("Input cannot be null"); @@ -61,7 +62,7 @@ public class RailFence{ } } //Ensures the number of rails is valid and sets up the fence - private void setNumRails(int numRails) throws InvalidBaseException{ + protected void setNumRails(int numRails) throws InvalidBaseException{ if(numRails < 2){ throw new InvalidBaseException("You must use at least 2 rails"); } @@ -74,13 +75,13 @@ public class RailFence{ } } //Strip the inputString of all non-letter characters - private String getCleanInputString(){ + protected String getCleanInputString(){ logger.debug("Getting input string for encoding"); return inputString.replaceAll("[^a-zA-Z]", ""); } //Ensures capitals, lowercase, and symbols are displayed in the output string - private void formatOutput(String outputString){ + protected void formatOutput(String outputString){ logger.debug("Formatting output string"); StringBuilder output = new StringBuilder(); @@ -105,11 +106,11 @@ public class RailFence{ } } - logger.debug("Formatted output '{}'", output); this.outputString = output.toString(); + logger.debug("Formatted output '{}'", this.outputString); } //Returns the decoded string found in the fence after all characters are placed correctly - private String getDecodedStringFromFence(){ + protected String getDecodedStringFromFence(){ logger.debug("Getting decoded string from the fence"); boolean down = true; @@ -118,7 +119,7 @@ public class RailFence{ int insideCol = -1; StringBuilder output = new StringBuilder(); while(true){ - //Get the next character based on what rail you are currently usinig + //Get the next character based on what rail you are currently using if(rail == 0){ if(outsideCol >= fence[rail].length()){ break; @@ -161,13 +162,13 @@ public class RailFence{ return output.toString(); } //Encodes inputString using the RailFence cipher and stores the result in outputString - private void encode(){ + protected void encode(){ logger.debug("Encoding"); boolean up = true; int rail = 0; for(char ch : getCleanInputString().toCharArray()){ - logger.debug("Working character {}", ch); + logger.debug("Working character '{}'", ch); fence[rail].append(ch); //Advance to the next rail @@ -204,7 +205,7 @@ public class RailFence{ formatOutput(output.toString()); } //Decodes inputString using the RailFence cipher and stores the result in outputString - private void decode(){ + protected void decode(){ logger.debug("Decoding"); //Determine the number of characters on each rail @@ -260,11 +261,13 @@ public class RailFence{ preserveCapitals = false; preserveWhitespace = false; preserveSymbols = false; + reset(); } public RailFence(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){ this.preserveCapitals = preserveCapitals; this.preserveWhitespace = preserveWhitespace; this.preserveSymbols = preserveSymbols; + reset(); } //Encodes inputString using a Rail Fence of length numRails and returns the result @@ -296,7 +299,8 @@ public class RailFence{ outputString = ""; fence = null; } - //Gets + + //Getters public String getInputString(){ return inputString; } diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Trifid.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Trifid.java index ac6036a..69dd11d 100644 --- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Trifid.java +++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Trifid.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Trifid.java //Mattrixwv // Created: 03-03-22 -//Modified: 03-03-22 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -21,10 +21,10 @@ public class Trifid{ private static final Logger logger = LoggerFactory.getLogger(Trifid.class); //A class representing the location of a character in the grid - private class CharLocation{ - private int x; - private int y; - private int z; + protected class CharLocation{ + protected int x; + protected int y; + protected int z; public CharLocation(int x, int y, int z){ this.x = x; this.y = y; @@ -45,18 +45,19 @@ public class Trifid{ } //Fields - private String inputString; //The message that needs to be encoded/decoded - private String outputString; //The encoded/decoded message - private String keyword; //The keyword used to create the grid - private int groupSize; //The size of the groups used to break up the input - private char[][][] grid; //The grid used to encode/decode the message - private char fillIn; //The character added to the alphabet to meet the 27 character requirement - private boolean preserveCapitals; //Persist capitals in the output string - private boolean preserveWhitespace; //Persist whitespace in the output string - private boolean preserveSymbols; //Persist symbols in the output string + protected String inputString; //The message that needs to be encoded/decoded + protected String outputString; //The encoded/decoded message + protected String keyword; //The keyword used to create the grid + protected int groupSize; //The size of the groups used to break up the input + protected char[][][] grid; //The grid used to encode/decode the message + protected char fillIn; //The character added to the alphabet to meet the 27 character requirement + //Settings + protected boolean preserveCapitals; //Persist capitals in the output string + protected boolean preserveWhitespace; //Persist whitespace in the output string + protected boolean preserveSymbols; //Persist symbols in the output string //Makes sure the fillIn is a valid character - private void setFillIn(char fillIn) throws InvalidCharacterException{ + protected void setFillIn(char fillIn) throws InvalidCharacterException{ //Make sure the character is a printing character if((fillIn < ' ') || (fillIn > '~')){ throw new InvalidCharacterException("Fill in character must be a printing character"); @@ -72,7 +73,7 @@ public class Trifid{ this.fillIn = fillIn; } //Strips invalid characters from the keyword and creates the grid - private void setKeyword(String keyword) throws InvalidKeywordException{ + protected void setKeyword(String keyword) throws InvalidKeywordException{ //Ensure the keyword isn't null if(keyword == null){ throw new InvalidKeywordException("Keyword cannot be null"); @@ -93,7 +94,7 @@ public class Trifid{ keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + fillIn; //Remove all duplicate characters - logger.debug("Removing duplicated characters"); + logger.debug("Removing duplicate characters"); StringBuilder uniqueKey = new StringBuilder(); keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c)); this.keyword = uniqueKey.toString(); @@ -104,7 +105,7 @@ public class Trifid{ createGrid(); } //Creates the grid from the keyword - private void createGrid(){ + protected void createGrid(){ logger.debug("Creating grid from keyword"); for(int layerCnt = 0;layerCnt < grid.length;++layerCnt){ @@ -120,7 +121,7 @@ public class Trifid{ logger.debug("Completed grid\n{}", getGrid()); } //Ensures groupSize constraints - private void setGroupSize(int groupSize) throws InvalidBaseException{ + protected void setGroupSize(int groupSize) throws InvalidBaseException{ if(groupSize <= 0){ throw new InvalidBaseException("Group size must be > 0"); } @@ -130,7 +131,7 @@ public class Trifid{ this.groupSize = groupSize; } //Ensures inputString constraints - private void setInputString(String inputString) throws InvalidInputException{ + protected void setInputString(String inputString) throws InvalidInputException{ //Ensure the input string isn't null if(inputString == null){ throw new InvalidInputException("Input cannot be null"); @@ -165,12 +166,12 @@ public class Trifid{ } } //Returns the inputString with only letters - private String getCleanInputString(){ + protected String getCleanInputString(){ logger.debug("Cleaning input string for encoding"); return inputString.toUpperCase().replaceAll("[^A-Z" + fillIn + "]", ""); } //Returns the location of the given character in the grid - private CharLocation findChar(char letter) throws InvalidCharacterException{ + protected CharLocation findChar(char letter) throws InvalidCharacterException{ logger.debug("Finding character {} in grid", letter); for(int layer = 0;layer < grid.length;++layer){ @@ -189,7 +190,7 @@ public class Trifid{ throw new InvalidCharacterException("The character '" + letter + "' was not found in the grid"); } //Return the character from the location provided - private char getChar(CharLocation location) throws InvalidCharacterException{ + protected char getChar(CharLocation location) throws InvalidCharacterException{ if(location.getX() > 2){ throw new InvalidCharacterException("x cannot be larget than 2"); } @@ -205,7 +206,7 @@ public class Trifid{ return grid[location.getZ()][location.getX()][location.getY()]; } //Adds all non-letter characters back to the output string - private void formatOutput(String outputString){ + protected void formatOutput(String outputString){ logger.debug("Formatting output"); //Keep track of where you are in the output @@ -222,6 +223,15 @@ public class Trifid{ logger.debug("Formatting lowercase"); output.append(Character.toLowerCase(outputString.charAt(outputCnt++))); } + else if(ch == fillIn){ + logger.debug("Adding fillIn"); + if(preserveCapitals){ + output.append(Character.toLowerCase(outputString.charAt(outputCnt++))); + } + else{ + output.append(Character.toUpperCase(outputString.charAt(outputCnt++))); + } + } else{ logger.debug("Appending symbol"); output.append(ch); @@ -229,15 +239,15 @@ public class Trifid{ } //Save the output - logger.debug("Formatted output '{}'", output); this.outputString = output.toString(); + logger.debug("Formatted output '{}'", this.outputString); } //Encodes inputString using a polybius square and stores the result in outputString - private void encode() throws InvalidCharacterException{ + protected void encode() throws InvalidCharacterException{ logger.debug("Encoding"); //Step through every element in the sanitized inputString encoding the letters - logger.debug("Conveting letters to coordinates"); + logger.debug("Converting letters to coordinates"); ArrayList locations = new ArrayList<>(); for(char ch : getCleanInputString().toCharArray()){ //Get the location of the char in the grid @@ -247,10 +257,7 @@ public class Trifid{ //Split the locations up by group logger.debug("Splitting locations into groups"); - int numGroups = inputString.length() / groupSize; - if(numGroups == 0){ - numGroups = 1; - } + int numGroups = (int)Math.ceil((double)inputString.length() / (double)groupSize); ArrayList> groups = new ArrayList<>(numGroups); for(int cnt = 0;cnt < numGroups;++cnt){ groups.add(new ArrayList<>()); @@ -302,7 +309,7 @@ public class Trifid{ formatOutput(output.toString()); } //Decodes inputString using a polybius square and stores the result in outputString - private void decode() throws InvalidCharacterException{ + protected void decode() throws InvalidCharacterException{ logger.debug("Decoding"); //Step through every element in the sanitized inputString encoding the letters @@ -316,10 +323,7 @@ public class Trifid{ //Split the locations up by group logger.debug("Splitting locations into groups"); - int numGroups = inputString.length() / groupSize; - if(numGroups == 0){ - numGroups = 1; - } + int numGroups = (int)Math.ceil((double)inputString.length() / (double)groupSize); ArrayList> groups = new ArrayList<>(numGroups); for(int cnt = 0;cnt < numGroups;++cnt){ groups.add(new ArrayList<>()); @@ -382,18 +386,21 @@ public class Trifid{ preserveWhitespace = false; preserveSymbols = false; setFillIn('+'); + reset(); } public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{ this.preserveCapitals = preserveCapitals; this.preserveWhitespace = preserveWhitespace; this.preserveSymbols = preserveSymbols; setFillIn('+'); + reset(); } public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char fillIn) throws InvalidCharacterException{ this.preserveCapitals = preserveCapitals; this.preserveWhitespace = preserveWhitespace; this.preserveSymbols = preserveSymbols; setFillIn(fillIn); + reset(); } //Encodes inputString using keyword and groupSize and returns the result @@ -437,7 +444,8 @@ public class Trifid{ groupSize = Integer.MAX_VALUE; grid = new char[3][3][3]; } - //Gets + + //Getters public String getInputString(){ return inputString; } diff --git a/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGVX.java b/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGVX.java index eff93a2..d158b5e 100644 --- a/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGVX.java +++ b/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGVX.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/combination/TestADFGVX.java //Mattrixwv // Created: 01-26-22 -//Modified: 04-21-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.combination; @@ -14,13 +14,15 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; @@ -29,8 +31,11 @@ import com.mattrixwv.cipherstream.polysubstitution.Columnar; import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare; +@ExtendWith(MockitoExtension.class) public class TestADFGVX{ + @InjectMocks private ADFGVX cipher; + @Mock private Logger logger; //Variables private String decodedString = "Message to^encode"; @@ -41,102 +46,200 @@ public class TestADFGVX{ private String squareKeyword = "SquareKeyword"; - @BeforeEach - public void setup(){ + @Test + public void testConstructor_default(){ cipher = new ADFGVX(); - logger = mock(Logger.class); - ADFGVX.logger = logger; + + 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); } + @Test + public void testConstructor_preservesCapitals(){ + 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); + } @Test - public void testSquareKeyword(){ + public void testConstructor_preservesWhitespace(){ + cipher = new ADFGVX(false, true, false); + + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveSymbols); + assertTrue(cipher.preserveWhitespace); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.squareKeyword); + assertEquals("", cipher.squareKeyword); + assertNotNull(cipher.largePolybiusSquare); + assertNotNull(cipher.columnar); + } + + @Test + public void testConstructor_preservesSymbols(){ + cipher = new ADFGVX(false, false, true); + + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveSymbols); + assertFalse(cipher.preserveWhitespace); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.squareKeyword); + assertEquals("", cipher.squareKeyword); + assertNotNull(cipher.largePolybiusSquare); + assertNotNull(cipher.columnar); + } + + @Test + public void testSetSquareKeyword(){ + cipher.setSquareKeyword(squareKeyword); + + assertEquals(squareKeyword, cipher.squareKeyword); + verify(logger, times(1)).debug("squareKeyword '{}'", squareKeyword); + } + + @Test + public void testSetSquareKeyword_null(){ assertThrows(InvalidKeywordException.class, () -> { cipher.setSquareKeyword(null); }); - assertEquals("", cipher.squareKeyword); - verify(logger, never()).debug(anyString(), anyString()); - cipher.setSquareKeyword(squareKeyword); - assertEquals(squareKeyword, cipher.squareKeyword); - verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword); + assertEquals("", cipher.squareKeyword); + verify(logger, never()).debug(eq("squareKeyword '{}'"), anyString()); } @Test public void testSetKeyword(){ + cipher.setKeyword(keyword); + + assertEquals(keyword, cipher.keyword); + verify(logger, times(1)).debug("keyword '{}'", keyword); + } + + @Test + public void testSetKeyword_null(){ assertThrows(InvalidKeywordException.class, () -> { cipher.setKeyword(null); }); - assertEquals("", cipher.keyword); - verify(logger, never()).debug(anyString(), anyString()); - String keyword = "keyword"; - cipher.setKeyword(keyword); - assertEquals(keyword, cipher.keyword); - verify(logger, times(1)).debug("keyword = {}", keyword); + assertEquals("", cipher.keyword); + verify(logger, never()).debug(eq("keyword '{}'"), anyString()); } @Test public void testSetInputString(){ - //Null input - assertThrows(InvalidInputException.class, () -> { - cipher.setInputString(null); - }); - assertEquals("", cipher.inputString); - verify(logger, never()).debug(anyString(), anyString()); - - String originalInputString = "Original input string '{}'"; - String cleanedInputString = "Cleaned input string '{}'"; - - //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, ""); + cipher.preserveSymbols = true; - //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); + cipher.setInputString(decodedString); - //capitals + assertEquals(decodedString, cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, never()).debug("Removing capitals"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString); + } + + @Test + public void testSetInputString_noCapitals(){ 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.setInputString(decodedString); + + assertEquals(decodedString.toUpperCase(), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Removing capitals"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase()); + } + + @Test + public void testSetInputString_noWhitespace(){ 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.setInputString(decodedString); + + assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, never()).debug("Removing capitals"); + verify(logger, times(1)).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", "")); + } + + @Test + public void testSetInputString_noSymbols(){ 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); + + 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 capitals"); + verify(logger, never()).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing symbols"); - verify(logger, times(1)).debug(cleanedInputString, inputString.replaceAll("[^a-zA-Z\\s]", "")); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", "")); + } + + @Test + public void testSetInputString_blank(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(""); + }); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", ""); + verify(logger, never()).debug("Removing capitals"); + 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); + }); + + assertEquals("", cipher.inputString); + verify(logger, never()).debug(eq("Original input string '{}'"), anyString()); + verify(logger, never()).debug("Removing capitals"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); } @Test @@ -145,6 +248,7 @@ public class TestADFGVX{ 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()); @@ -160,6 +264,7 @@ public class TestADFGVX{ 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()); @@ -197,62 +302,6 @@ public class TestADFGVX{ verify(logger, times(1)).debug("Decoding using Polybius Square"); } - @Test - public void testConstructor_default(){ - 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); - } - - @Test - public void testConstructor_preservesCapitals(){ - 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); - } - - @Test - public void testConstructor_preservesWhitespace(){ - cipher = new ADFGVX(false, true, false); - assertFalse(cipher.preserveCapitals); - assertFalse(cipher.preserveSymbols); - assertTrue(cipher.preserveWhitespace); - assertEquals("", cipher.inputString); - assertEquals("", cipher.outputString); - assertEquals("", cipher.squareKeyword); - assertEquals("", cipher.squareKeyword); - assertNotNull(cipher.largePolybiusSquare); - assertNotNull(cipher.columnar); - } - - @Test - public void testConstructor_preservesSymbols(){ - cipher = new ADFGVX(false, false, true); - assertFalse(cipher.preserveCapitals); - assertTrue(cipher.preserveSymbols); - assertFalse(cipher.preserveWhitespace); - assertEquals("", cipher.inputString); - assertEquals("", cipher.outputString); - assertEquals("", cipher.squareKeyword); - assertEquals("", cipher.squareKeyword); - assertNotNull(cipher.largePolybiusSquare); - assertNotNull(cipher.columnar); - } - @Test public void testGetters(){ cipher.inputString = decodedString; diff --git a/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGX.java b/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGX.java index 794de57..cdc0507 100644 --- a/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGX.java +++ b/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGX.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamjava/combination/TestADFGX.java //Mattrixwv // Created: 01-25-22 -//Modified: 04-17-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.combination; @@ -14,13 +14,15 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; @@ -29,8 +31,11 @@ import com.mattrixwv.cipherstream.polysubstitution.Columnar; import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare; +@ExtendWith(MockitoExtension.class) public class TestADFGX{ + @InjectMocks private ADFGX cipher; + @Mock private Logger logger; //Variables private String decodedString = "Message to^encode"; @@ -41,102 +46,199 @@ public class TestADFGX{ private String squareKeyword = "SquareKeyword"; - @BeforeEach - public void setup(){ - cipher = new ADFGX(true, true, true); - logger = mock(Logger.class); - ADFGX.logger = logger; + @Test + public void testConstructor_default(){ + 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); } + @Test + public void testConstructor_preservesCapitals(){ + 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); + } + + @Test + public void testConstructor_preservesWhitespace(){ + cipher = new ADFGX(false, true, false); + + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveSymbols); + assertTrue(cipher.preserveWhitespace); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.squareKeyword); + assertEquals("", cipher.squareKeyword); + assertNotNull(cipher.polybiusSquare); + assertNotNull(cipher.columnar); + } + + @Test + public void testConstructor_preservesSymbols(){ + cipher = new ADFGX(false, false, true); + + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveSymbols); + assertFalse(cipher.preserveWhitespace); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.squareKeyword); + assertEquals("", cipher.squareKeyword); + assertNotNull(cipher.polybiusSquare); + assertNotNull(cipher.columnar); + } @Test public void testSetSquareKeyword(){ + cipher.setSquareKeyword(squareKeyword); + assertEquals(squareKeyword, cipher.squareKeyword); + verify(logger, times(1)).debug("Square keyword '{}'", squareKeyword); + } + + @Test + public void testSetSquareKeyword_null(){ assertThrows(InvalidKeywordException.class, () -> { cipher.setSquareKeyword(null); }); - assertEquals("", cipher.squareKeyword); - verify(logger, never()).debug(anyString(), anyString()); - cipher.setSquareKeyword(squareKeyword); - assertEquals(squareKeyword, cipher.squareKeyword); - verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword); + assertEquals("", cipher.squareKeyword); + verify(logger, never()).debug(eq("Square keyword '{}'"), anyString()); } @Test public void testSetKeyword(){ + cipher.setKeyword(keyword); + + assertEquals(keyword, cipher.keyword); + verify(logger, times(1)).debug("Keyword '{}'", keyword); + } + + @Test + public void testSetKeyword_null(){ assertThrows(InvalidKeywordException.class, () -> { cipher.setKeyword(null); }); - assertEquals("", cipher.keyword); - verify(logger, never()).debug(anyString(), anyString()); - String keyword = "keyword"; - cipher.setKeyword(keyword); - assertEquals(keyword, cipher.keyword); - verify(logger, times(1)).debug("keyword = {}", keyword); + assertEquals("", cipher.keyword); + verify(logger, never()).debug(eq("Keyword '{}'"), anyString()); } @Test public void testSetInputString(){ - //Null input - assertThrows(InvalidInputException.class, () -> { - cipher.setInputString(null); - }); - assertEquals("", cipher.inputString); - verify(logger, never()).debug(anyString(), anyString()); - - String originalInputString = "Original input string '{}'"; - String cleanedInputString = "Cleaned input string '{}'"; - - //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, ""); + cipher.preserveSymbols = true; - //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); + cipher.setInputString(decodedString); - //capitals + assertEquals(decodedString, cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, never()).debug("Removing capitals"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString); + } + + @Test + public void testSetInputString_noCapitals(){ 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.setInputString(decodedString); + + assertEquals(decodedString.toUpperCase(), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, times(1)).debug("Removing capitals"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase()); + } + + @Test + public void testSetInputString_noWhitespace(){ 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.setInputString(decodedString); + + assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", decodedString); + verify(logger, never()).debug("Removing capitals"); + verify(logger, times(1)).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", "")); + } + + @Test + public void testSetInputString_noSymbols(){ 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); + + 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 capitals"); + verify(logger, never()).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing symbols"); - verify(logger, times(1)).debug(cleanedInputString, inputString.replaceAll("[^a-zA-Z\\s]", "")); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", "")); + } + + @Test + public void testSetInputString_blank(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(""); + }); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Original input string '{}'", ""); + verify(logger, never()).debug("Removing capitals"); + 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); + }); + + assertEquals("", cipher.inputString); + verify(logger, never()).debug(eq("Original input string '{}'"), anyString()); + verify(logger, never()).debug("Removing capitals"); + verify(logger, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); } @Test @@ -145,6 +247,7 @@ public class TestADFGX{ 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()); @@ -160,6 +263,7 @@ public class TestADFGX{ 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()); @@ -197,62 +301,6 @@ public class TestADFGX{ verify(logger, times(1)).debug("Decoding using Polybius Square"); } - @Test - public void testConstructor_default(){ - 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); - } - - @Test - public void testConstructor_preservesCapitals(){ - 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); - } - - @Test - public void testConstructor_preservesWhitespace(){ - cipher = new ADFGX(false, true, false); - assertFalse(cipher.preserveCapitals); - assertFalse(cipher.preserveSymbols); - assertTrue(cipher.preserveWhitespace); - assertEquals("", cipher.inputString); - assertEquals("", cipher.outputString); - assertEquals("", cipher.squareKeyword); - assertEquals("", cipher.squareKeyword); - assertNotNull(cipher.polybiusSquare); - assertNotNull(cipher.columnar); - } - - @Test - public void testConstructor_preservesSymbols(){ - cipher = new ADFGX(false, false, true); - assertFalse(cipher.preserveCapitals); - assertTrue(cipher.preserveSymbols); - assertFalse(cipher.preserveWhitespace); - assertEquals("", cipher.inputString); - assertEquals("", cipher.outputString); - assertEquals("", cipher.squareKeyword); - assertEquals("", cipher.squareKeyword); - assertNotNull(cipher.polybiusSquare); - assertNotNull(cipher.columnar); - } - @Test public void testGetters(){ cipher.inputString = decodedString; diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAffine.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAffine.java index cd545cb..d2155db 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAffine.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAffine.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestAffine.java //Mattrixwv // Created: 01-26-22 -//Modified: 04-17-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -10,24 +10,28 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +@ExtendWith(MockitoExtension.class) public class TestAffine{ + @InjectMocks private Affine cipher; + @Mock private Logger logger; //Variables private String decodedString = "MEssage to^encode"; @@ -38,17 +42,10 @@ public class TestAffine{ private int key2 = 7; - @BeforeEach - public void setup(){ - cipher = new Affine(); - logger = mock(Logger.class); - Affine.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new Affine(); + assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveSymbols); assertFalse(cipher.preserveWhitespace); @@ -61,6 +58,7 @@ public class TestAffine{ @Test public void testConstructor_preservesCapitals(){ cipher = new Affine(true, false, false); + assertTrue(cipher.preserveCapitals); assertFalse(cipher.preserveSymbols); assertFalse(cipher.preserveWhitespace); @@ -73,6 +71,7 @@ public class TestAffine{ @Test public void testConstructor_preservesWhitespace(){ cipher = new Affine(false, true, false); + assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveSymbols); assertTrue(cipher.preserveWhitespace); @@ -85,6 +84,7 @@ public class TestAffine{ @Test public void testConstructor_preservesSymbols(){ cipher = new Affine(false, false, true); + assertFalse(cipher.preserveCapitals); assertTrue(cipher.preserveSymbols); assertFalse(cipher.preserveWhitespace); @@ -97,12 +97,10 @@ public class TestAffine{ @Test 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 @@ -110,66 +108,54 @@ public class TestAffine{ 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 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 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 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 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 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 @@ -182,8 +168,10 @@ public class TestAffine{ 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, times(2)).debug(anyString(), anyString()); } @Test @@ -197,9 +185,9 @@ public class TestAffine{ assertEquals(decodedString.toLowerCase(), 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.toLowerCase()); - verify(logger, times(2)).debug(anyString(), anyString()); - verify(logger, times(1)).debug(anyString()); } @Test @@ -212,10 +200,10 @@ public class TestAffine{ 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 sybols"); 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 @@ -228,10 +216,10 @@ public class TestAffine{ 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(2)).debug(anyString(), anyString()); - verify(logger, times(1)).debug(anyString()); } @Test @@ -243,8 +231,13 @@ public class TestAffine{ assertThrows(InvalidInputException.class, () -> { cipher.setInputString(null); }); - verify(logger, never()).debug(anyString()); - verify(logger, never()).debug(anyString(), anyString()); + + 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()); } @Test @@ -256,10 +249,13 @@ public class TestAffine{ assertThrows(InvalidInputException.class, () -> { cipher.setInputString(""); }); + + 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, times(2)).debug(anyString(), anyString()); - verify(logger, never()).debug(anyString()); } @Test diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAtbash.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAtbash.java index 4f6cf1e..0593001 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAtbash.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAtbash.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java //Mattrixwv // Created: 07-25-21 -//Modified: 04-17-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -9,21 +9,28 @@ 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.anyString; -import static org.mockito.Mockito.mock; +import static org.mockito.ArgumentMatchers.eq; 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; +@ExtendWith(MockitoExtension.class) public class TestAtbash{ + @InjectMocks private Atbash cipher; + @Mock private Logger logger; //Variables private String decodedString = "Message to^encode"; @@ -32,17 +39,10 @@ public class TestAtbash{ private String encodedStringClean = "NVHHZTVGLVMXLWV"; - @BeforeEach - public void setup(){ - cipher = new Atbash(); - logger = mock(Logger.class); - Atbash.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new Atbash(); + assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); @@ -53,6 +53,7 @@ public class TestAtbash{ @Test public void testConstructor_preservesCapitals(){ cipher = new Atbash(true, false, false); + assertTrue(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); @@ -63,6 +64,7 @@ public class TestAtbash{ @Test public void testConstructor_preservesWhitespace(){ cipher = new Atbash(false, true, false); + assertFalse(cipher.preserveCapitals); assertTrue(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); @@ -73,6 +75,7 @@ public class TestAtbash{ @Test public void testConstructor_preservesSymbols(){ cipher = new Atbash(false, false, true); + assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertTrue(cipher.preserveSymbols); @@ -83,10 +86,16 @@ public class TestAtbash{ @Test public void testEncode(){ cipher.inputString = decodedString; - cipher.outputString = encodedString; - assertEquals(decodedString, cipher.inputString); + cipher.encode(); + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(17)).debug(eq("Encoding char {}"), anyChar()); + verify(logger, times(1)).debug("Encoding uppercase"); + verify(logger, times(14)).debug("Encoding lowercase"); + verify(logger, times(2)).debug("Appending symbol"); + verify(logger, times(1)).debug("Saving output string '{}'", encodedString); } @Test @@ -99,9 +108,10 @@ public class TestAtbash{ 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, times(2)).debug(anyString(), anyString()); - verify(logger, never()).debug(anyString()); } @Test @@ -115,9 +125,9 @@ public class TestAtbash{ 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(2)).debug(anyString(), anyString()); - verify(logger, times(1)).debug(anyString()); } @Test @@ -130,10 +140,10 @@ public class TestAtbash{ 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(2)).debug(anyString(), anyString()); - verify(logger, times(1)).debug(anyString()); } @Test @@ -146,10 +156,10 @@ public class TestAtbash{ 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(2)).debug(anyString(), anyString()); - verify(logger, times(1)).debug(anyString()); } @Test @@ -163,8 +173,11 @@ public class TestAtbash{ }); assertEquals("", cipher.inputString); - verify(logger, never()).debug(anyString(), anyString()); - verify(logger, never()).debug(anyString()); + 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()); } @Test @@ -180,9 +193,10 @@ public class TestAtbash{ 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, times(2)).debug(anyString(), anyString()); - verify(logger, never()).debug(anyString()); } @Test diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAutokey.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAutokey.java index 4863d05..5caa199 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAutokey.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestAutokey.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAutokey.java //Mattrixwv // Created: 07-26-21 -//Modified: 04-17-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -9,24 +9,26 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; -import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; - +@ExtendWith(MockitoExtension.class) public class TestAutokey{ + @InjectMocks private Autokey cipher; + @Mock(name = "com.mattrixwv.cipherstream.monosubstitution.Autokey") private Logger logger; //Variables private String decodedString = "MeSsage to^encode"; @@ -37,17 +39,10 @@ public class TestAutokey{ private ArrayList offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3, 12, 4, 18, 18, 0, 6, 4, 19)); - @BeforeEach - public void setup(){ - cipher = new Autokey(); - logger = mock(Logger.class); - Autokey.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new Autokey(); + assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); @@ -60,6 +55,7 @@ public class TestAutokey{ @Test public void testConstructor_preservesCapitals(){ cipher = new Autokey(true, false, false); + assertTrue(cipher.preserveCapitals); assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveSymbols); @@ -110,7 +106,6 @@ public class TestAutokey{ 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 @@ -126,7 +121,6 @@ public class TestAutokey{ 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 @@ -134,9 +128,7 @@ public class TestAutokey{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - Autokey.logger = mock(Logger.class); cipher.decodeSet(keyword, encodedString); - Autokey.logger = logger; cipher.decode(); @@ -227,40 +219,4 @@ public class TestAutokey{ assertEquals(decodedStringClean, cipher.outputString); assertEquals(decodedStringClean, output); } - - - @Test - public void testSetKeyword() throws InvalidKeywordException{ - Autokey cipher = new Autokey(); - - //Test keyword with whitespace - String keyword = "x y z "; - String correctOutput = "XYZ"; - cipher.setKeyword(keyword); - String output = cipher.getKeyword(); - assertEquals(correctOutput, output); - - - //Test keyword with symbol - keyword = "x-y@z0"; - correctOutput = "XYZ"; - cipher.setKeyword(keyword); - output = cipher.getKeyword(); - assertEquals(correctOutput, output); - - - //Test keyword with mixed case - keyword = "xYz"; - correctOutput = "XYZ"; - cipher.setKeyword(keyword); - output = cipher.getKeyword(); - assertEquals(correctOutput, output); - - //Test keyword with whitespace, symbol and keyword - keyword = "x Y%z "; - correctOutput = "XYZ"; - cipher.setKeyword(keyword); - output = cipher.getKeyword(); - assertEquals(correctOutput, output); - } } diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaconian.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaconian.java index 0a97244..823b9dd 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaconian.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaconian.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestBaconian.java //Mattrixwv // Created: 01-12-22 -//Modified: 04-17-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -13,21 +13,26 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; +@ExtendWith(MockitoExtension.class) public class TestBaconian{ + @InjectMocks private Baconian cipher; + @Mock private Logger logger; //Variables private String decodedString = "Message to-encode"; @@ -36,14 +41,6 @@ public class TestBaconian{ private String encodedString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; - @BeforeEach - public void setup(){ - cipher = new Baconian(); - logger = mock(Logger.class); - Baconian.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new Baconian(); @@ -72,8 +69,6 @@ public class TestBaconian{ 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 '{}'", decodedStringClean); - verify(logger, never()).debug(anyString()); - verify(logger, times(2)).debug(anyString(), anyString()); } @Test @@ -86,8 +81,6 @@ public class TestBaconian{ 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 @@ -102,8 +95,6 @@ public class TestBaconian{ 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 @@ -115,8 +106,9 @@ public class TestBaconian{ }); assertEquals("", cipher.inputString); - verify(logger, never()).debug(anyString()); - verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug("Setting input string for encoding '{}'", ""); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Cleaned input string '{}'", ""); } @Test @@ -132,8 +124,6 @@ public class TestBaconian{ 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 @@ -149,8 +139,6 @@ public class TestBaconian{ 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 @@ -168,8 +156,6 @@ public class TestBaconian{ 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 @@ -187,8 +173,6 @@ public class TestBaconian{ 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 @@ -200,14 +184,12 @@ public class TestBaconian{ }); assertEquals("", cipher.inputString); - verify(logger, never()).debug("Setting input string for decoding '{}'", ""); + verify(logger, never()).debug(eq("Setting input string for decoding '{}'"), anyString()); 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()); + verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); } @Test @@ -219,8 +201,12 @@ public class TestBaconian{ }); assertEquals("", cipher.inputString); - verify(logger, never()).debug(anyString()); - verify(logger, never()).debug(anyString(), anyString()); + verify(logger, never()).debug(eq("Setting input string for decoding '{}'"), anyString()); + 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(eq("Cleaned input string '{}'"), anyString()); } @Test @@ -237,8 +223,6 @@ public class TestBaconian{ 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 @@ -256,8 +240,6 @@ public class TestBaconian{ verify(logger, times(14)).debug("Decoding lowercase"); verify(logger, times(15)).debug(eq("Decoded character {}"), anyChar()); verify(logger, times(1)).debug("Saving output string '{}'", decodedStringClean); - verify(logger, times(16)).debug(anyString()); - verify(logger, times(16)).debug(anyString(), anyString()); } @Test diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaseX.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaseX.java index 86f48a8..4f0315b 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaseX.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBaseX.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBaseX.java //Mattrixwv // Created: 01-08-22 -//Modified: 04-16-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -11,13 +11,15 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidBaseException; @@ -25,8 +27,11 @@ import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; +@ExtendWith(MockitoExtension.class) public class TestBaseX{ + @InjectMocks private BaseX cipher; + @Mock private Logger logger; //Variables private String decodedString = "A+B@C d\te\nf"; @@ -36,14 +41,6 @@ public class TestBaseX{ private String encodedString_16 = "41 2B 42 40 43 20 64 9 65 A 66"; - @BeforeEach - public void setup(){ - cipher = new BaseX(); - logger = mock(Logger.class); - BaseX.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new BaseX(); @@ -68,8 +65,6 @@ public class TestBaseX{ 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 @@ -80,8 +75,6 @@ public class TestBaseX{ 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 @@ -164,8 +157,6 @@ public class TestBaseX{ assertEquals(16, cipher.base); verify(logger, times(1)).debug("Setting base {}", 16); - verify(logger, never()).debug(anyString()); - verify(logger, never()).debug(anyString(), anyString()); } @Test @@ -176,8 +167,6 @@ public class TestBaseX{ assertEquals(2, cipher.base); verify(logger, never()).debug(eq("Setting base {}"), anyInt()); - verify(logger, never()).debug(anyString()); - verify(logger, never()).debug(anyString(), anyString()); } @Test @@ -188,8 +177,6 @@ public class TestBaseX{ assertEquals(2, cipher.base); verify(logger, never()).debug(eq("Setting base {}"), anyInt()); - verify(logger, never()).debug(anyString()); - verify(logger, never()).debug(anyString(), anyString()); } @Test @@ -256,13 +243,15 @@ public class TestBaseX{ 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(decodedString, cipher.inputString); + assertEquals(2, cipher.base); + assertEquals(encodedString_2, cipher.outputString); assertEquals(encodedString_2, output); } @@ -270,6 +259,9 @@ public class TestBaseX{ public void testPracticalEncoding_8(){ String output = cipher.encode(8, decodedString); + assertEquals(decodedString, cipher.inputString); + assertEquals(8, cipher.base); + assertEquals(encodedString_8, cipher.outputString); assertEquals(encodedString_8, output); } @@ -277,6 +269,9 @@ public class TestBaseX{ public void testPracticalEncoding_10(){ String output = cipher.encode(10, decodedString); + assertEquals(decodedString, cipher.inputString); + assertEquals(10, cipher.base); + assertEquals(encodedString_10, cipher.outputString); assertEquals(encodedString_10, output); } @@ -284,6 +279,9 @@ public class TestBaseX{ public void testPracticalEncoding_16(){ String output = cipher.encode(16, decodedString); + assertEquals(decodedString, cipher.inputString); + assertEquals(16, cipher.base); + assertEquals(encodedString_16, cipher.outputString); assertEquals(encodedString_16, output); } @@ -291,6 +289,9 @@ public class TestBaseX{ public void testPracticalEncoding_inputOnly(){ String output = cipher.encode(decodedString); + assertEquals(decodedString, cipher.inputString); + assertEquals(2, cipher.base); + assertEquals(encodedString_2, cipher.outputString); assertEquals(encodedString_2, output); } @@ -298,6 +299,9 @@ public class TestBaseX{ public void testPracticalDecoding_2(){ String output = cipher.decode(2, encodedString_2); + assertEquals(encodedString_2, cipher.inputString); + assertEquals(2, cipher.base); + assertEquals(decodedString, cipher.outputString); assertEquals(decodedString, output); } @@ -305,6 +309,9 @@ public class TestBaseX{ public void testPracticalDecoding_8(){ String output = cipher.decode(8, encodedString_8); + assertEquals(encodedString_8, cipher.inputString); + assertEquals(8, cipher.base); + assertEquals(decodedString, cipher.outputString); assertEquals(decodedString, output); } @@ -312,6 +319,9 @@ public class TestBaseX{ public void testPracticalDecoding_10(){ String output = cipher.decode(10, encodedString_10); + assertEquals(encodedString_10, cipher.inputString); + assertEquals(10, cipher.base); + assertEquals(decodedString, cipher.outputString); assertEquals(decodedString, output); } @@ -319,6 +329,9 @@ public class TestBaseX{ public void testPracticalDecoding_16(){ String output = cipher.decode(16, encodedString_16); + assertEquals(encodedString_16, cipher.inputString); + assertEquals(16, cipher.base); + assertEquals(decodedString, cipher.outputString); assertEquals(decodedString, output); } @@ -326,6 +339,9 @@ public class TestBaseX{ public void testPracticalDecoding_inputOnly(){ String output = cipher.decode(encodedString_2); + assertEquals(encodedString_2, cipher.inputString); + assertEquals(2, cipher.base); + assertEquals(decodedString, cipher.outputString); 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 38f15c4..e8fb27c 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBeaufort.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestBeaufort.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBeaufort.java //Mattrixwv // Created: 02-23-22 -//Modified: 04-17-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -11,37 +11,34 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +@ExtendWith(MockitoExtension.class) public class TestBeaufort{ + @InjectMocks private Beaufort cipher; + @Mock 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"; - - - @BeforeEach - public void setup(){ - cipher = new Beaufort(); - logger = mock(Logger.class); - Beaufort.logger = logger; - } + private String keyword = "Ke*y word"; + private String keywordClean = "KEYWORD"; @Test @@ -142,8 +139,6 @@ public class TestBeaufort{ 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 @@ -160,8 +155,6 @@ public class TestBeaufort{ 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 @@ -178,8 +171,6 @@ public class TestBeaufort{ 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 @@ -196,8 +187,6 @@ public class TestBeaufort{ 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 @@ -216,8 +205,6 @@ public class TestBeaufort{ 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 @@ -232,34 +219,17 @@ public class TestBeaufort{ 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); + assertEquals(keywordClean, 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()); + verify(logger, times(1)).debug("Cleaned keyword '{}'", keywordClean); } @Test @@ -273,8 +243,6 @@ public class TestBeaufort{ 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 @@ -288,8 +256,6 @@ public class TestBeaufort{ 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 @@ -303,17 +269,13 @@ public class TestBeaufort{ 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.keyword = keywordClean; cipher.inputString = decodedString; - logger = mock(Logger.class); - Beaufort.logger = logger; cipher.encode(); @@ -323,17 +285,13 @@ public class TestBeaufort{ 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.keyword = keywordClean; cipher.inputString = decodedString; - logger = mock(Logger.class); - Beaufort.logger = logger; cipher.decode(); @@ -343,8 +301,6 @@ public class TestBeaufort{ 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 diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestCaesar.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestCaesar.java index de5f409..80ad9d2 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestCaesar.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestCaesar.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestCaesar.java //Matthew Ellison // Created: 07-25-21 -//Modified: 04-16-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -11,20 +11,25 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; +@ExtendWith(MockitoExtension.class) public class TestCaesar{ + @InjectMocks private Caesar cipher; + @Mock private Logger logger; //Variables private String decodedString = "The quick brown fox jumps over - the lAzy dog"; @@ -34,15 +39,6 @@ public class TestCaesar{ private int shift = 23; - - @BeforeEach - public void setup(){ - cipher = new Caesar(); - logger = mock(Logger.class); - Caesar.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new Caesar(); diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestOneTimePad.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestOneTimePad.java index 8109616..df5fecb 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestOneTimePad.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestOneTimePad.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestOneTimePad.java //Mattrixwv // Created: 02-23-22 -//Modified: 04-17-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -10,7 +10,6 @@ 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; @@ -18,15 +17,21 @@ import static org.mockito.Mockito.verify; import java.util.ArrayList; import java.util.Arrays; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +@ExtendWith(MockitoExtension.class) public class TestOneTimePad{ + @InjectMocks private OneTimePad cipher; + @Mock(name = "com.mattrixwv.cipherstream.monosubstitution.OneTimePad") private Logger logger; //Variables private String decodedString = "Message to^encode"; @@ -37,14 +42,6 @@ public class TestOneTimePad{ private ArrayList offset = new ArrayList<>(Arrays.asList(10, 4, 24, 22, 14, 17, 3, 19, 7, 0, 19, 8, 18, 19, 14, 19, 0, 11, 11, 24, 17, 0, 13, 3, 14, 12)); - @BeforeEach - public void setup(){ - cipher = new OneTimePad(); - logger = mock(Logger.class); - OneTimePad.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new OneTimePad(); diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestPorta.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestPorta.java index 75cf9f1..e7aca41 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestPorta.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestPorta.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestPorta.java //Mattrixwv // Created: 02-28-22 -//Modified: 04-17-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -12,21 +12,26 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +@ExtendWith(MockitoExtension.class) public class TestPorta{ + @InjectMocks private Porta cipher; + @Mock private Logger logger; //Variables private String decodedString = "Message to^encode"; @@ -34,14 +39,7 @@ public class TestPorta{ private String encodedString = "Rtghuos bm^qcwgrw"; private String encodedStringClean = "RTGHUOSBMQCWGRW"; private String keyword = "keyword"; - - - @BeforeEach - public void setup(){ - cipher = new Porta(); - logger = mock(Logger.class); - Porta.logger = logger; - } + private String keywordDirty = "Ke yw*ord"; @Test @@ -103,6 +101,17 @@ public class TestPorta{ verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase()); } + @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()); + } + @Test public void testSetKeyword_blank(){ assertThrows(InvalidKeywordException.class, () -> { @@ -136,8 +145,10 @@ public class TestPorta{ }); assertEquals("", cipher.keyword); - verify(logger, never()).debug(anyString()); - verify(logger, never()).debug(anyString(), anyString()); + 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()); } @Test @@ -244,8 +255,11 @@ public class TestPorta{ }); assertEquals("", cipher.inputString); - verify(logger, never()).debug(anyString(), anyString()); - verify(logger, never()).debug(anyString()); + 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()); } @Test diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestSubstitution.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestSubstitution.java index a53e5ac..aaa3c4c 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestSubstitution.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestSubstitution.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestSubstitution.java //Mattrixwv // Created: 02-22-22 -//Modified: 04-18-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -12,21 +12,26 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +@ExtendWith(MockitoExtension.class) public class TestSubstitution{ + @InjectMocks private Substitution cipher; + @Mock private Logger logger; //Variables private String decodedString = "Message to^encode"; @@ -38,15 +43,9 @@ public class TestSubstitution{ private String encodedStringAlNum = "Oguucig vq^gpeqfg 876"; private String encodedStringAlNumClean = "OGUUCIGVQGPEQFG"; private String keyword = "cdefghijklmnopqrstuvwxyzab"; + private String keywordClean = "CDEFGHIJKLMNOPQRSTUVWXYZAB"; private String keywordAlNum = "cdefghijklmnopqrstuvwxyzab9876543210"; - - - @BeforeEach - public void setup(){ - cipher = new Substitution(); - logger = mock(Logger.class); - Substitution.logger = logger; - } + private String keywordAlNumClean = "CDEFGHIJKLMNOPQRSTUVWXYZAB9876543210"; @Test @@ -98,33 +97,33 @@ public class TestSubstitution{ } @Test - public void testSetKey(){ + public void testSetKeyword(){ cipher.setKeyword(keyword); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); verify(logger, times(1)).debug("Original key '{}'", keyword); verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Ensuring there are no duplicate mappings"); verify(logger, times(1)).debug("Ensuring there are only letters in the key"); verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key"); - verify(logger, times(1)).debug("Cleaned key '{}'", keyword.toUpperCase()); + verify(logger, times(1)).debug("Cleaned key '{}'", keywordClean); } @Test - public void testSetKey_alNum(){ + public void testSetKeyword_alNum(){ cipher.setKeyword(keywordAlNum); - assertEquals(keywordAlNum.toUpperCase(), cipher.keyword); + assertEquals(keywordAlNumClean, cipher.keyword); verify(logger, times(1)).debug("Original key '{}'", keywordAlNum); verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Ensuring there are no duplicate mappings"); verify(logger, never()).debug("Ensuring there are only letters in the key"); verify(logger, times(1)).debug("Ensuring there are only alpha-numeric characters in the key"); - verify(logger, times(1)).debug("Cleaned key '{}'", keywordAlNum.toUpperCase()); + verify(logger, times(1)).debug("Cleaned key '{}'", keywordAlNumClean); } @Test - public void testSetKey_duplicate(){ + public void testSetKeyword_duplicate(){ assertThrows(InvalidKeywordException.class, () -> { cipher.setKeyword("ABA"); }); @@ -139,7 +138,7 @@ public class TestSubstitution{ } @Test - public void testSetKey_invalidLetter(){ + public void testSetKeyword_invalidLetter(){ assertThrows(InvalidKeywordException.class, () ->{ cipher.setKeyword("abcdefghijklmnop1rstuvwxyz"); }); @@ -154,7 +153,7 @@ public class TestSubstitution{ } @Test - public void testSetKey_invalidAlNum(){ + public void testSetKeyword_invalidAlNum(){ assertThrows(InvalidKeywordException.class, () -> { cipher.setKeyword("abcdefghijklmnop^rstuvwxyz0123456789"); }); @@ -169,7 +168,7 @@ public class TestSubstitution{ } @Test - public void testSetKey_invalidLength(){ + public void testSetKeyword_invalidLength(){ assertThrows(InvalidKeywordException.class, () -> { cipher.setKeyword("AB"); }); @@ -184,7 +183,7 @@ public class TestSubstitution{ } @Test - public void testSetKey_null(){ + public void testSetKeyword_null(){ assertThrows(InvalidKeywordException.class, () -> { cipher.setKeyword(null); }); @@ -304,7 +303,7 @@ public class TestSubstitution{ cipher.preserveWhitespace = true; cipher.preserveSymbols = true; cipher.inputString = decodedStringAlNum; - cipher.keyword = keywordAlNum.toUpperCase(); + cipher.keyword = keywordAlNumClean; cipher.encode(); @@ -324,7 +323,7 @@ public class TestSubstitution{ cipher.preserveWhitespace = true; cipher.preserveSymbols = true; cipher.inputString = encodedStringAlNum; - cipher.keyword = keywordAlNum.toUpperCase(); + cipher.keyword = keywordAlNumClean; cipher.decode(); @@ -370,7 +369,7 @@ public class TestSubstitution{ String output = cipher.encode(keyword, decodedString); assertEquals(decodedString, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(encodedString, cipher.outputString); assertEquals(encodedString, output); } @@ -382,7 +381,7 @@ public class TestSubstitution{ String output = cipher.encode(keyword, decodedString); assertEquals(decodedStringClean, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(encodedStringClean, cipher.outputString); assertEquals(encodedStringClean, output); } @@ -394,7 +393,7 @@ public class TestSubstitution{ String output = cipher.encode(keywordAlNum, decodedStringAlNum); assertEquals(decodedStringAlNum, cipher.inputString); - assertEquals(keywordAlNum.toUpperCase(), cipher.keyword); + assertEquals(keywordAlNumClean, cipher.keyword); assertEquals(encodedStringAlNum, cipher.outputString); assertEquals(encodedStringAlNum, output); } @@ -406,7 +405,7 @@ public class TestSubstitution{ String output = cipher.encode(keywordAlNum, decodedStringAlNum); assertEquals(decodedStringAlNumClean, cipher.inputString); - assertEquals(keywordAlNum.toUpperCase(), cipher.keyword); + assertEquals(keywordAlNumClean, cipher.keyword); assertEquals(encodedStringAlNumClean, cipher.outputString); assertEquals(encodedStringAlNumClean, output); } @@ -418,7 +417,7 @@ public class TestSubstitution{ String output = cipher.encode(keyword, decodedStringAlNum); assertEquals(decodedStringAlNum, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(encodedString + " 123", cipher.outputString); assertEquals(encodedString + " 123", output); } @@ -430,7 +429,7 @@ public class TestSubstitution{ String output = cipher.decode(keyword, encodedString); assertEquals(encodedString, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(decodedString, cipher.outputString); assertEquals(decodedString, output); } @@ -442,7 +441,7 @@ public class TestSubstitution{ String output = cipher.decode(keyword, encodedString); assertEquals(encodedStringClean, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(decodedStringClean, cipher.outputString); assertEquals(decodedStringClean, output); } @@ -454,7 +453,7 @@ public class TestSubstitution{ String output = cipher.decode(keywordAlNum, encodedStringAlNum); assertEquals(encodedStringAlNum, cipher.inputString); - assertEquals(keywordAlNum.toUpperCase(), cipher.keyword); + assertEquals(keywordAlNumClean, cipher.keyword); assertEquals(decodedStringAlNum, cipher.outputString); assertEquals(decodedStringAlNum, output); } @@ -466,7 +465,7 @@ public class TestSubstitution{ String output = cipher.decode(keywordAlNum, encodedStringAlNum); assertEquals(encodedStringAlNumClean, cipher.inputString); - assertEquals(keywordAlNum.toUpperCase(), cipher.keyword); + assertEquals(keywordAlNumClean, cipher.keyword); assertEquals(decodedStringAlNumClean, cipher.outputString); assertEquals(decodedStringAlNumClean, output); } @@ -478,7 +477,7 @@ public class TestSubstitution{ String output = cipher.decode(keyword, encodedString + " 123"); assertEquals(encodedString + " 123", cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(decodedString + " 123", cipher.outputString); assertEquals(decodedString + " 123", output); } diff --git a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestVigenere.java b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestVigenere.java index 83fc3d4..4c0c0d8 100644 --- a/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestVigenere.java +++ b/src/test/java/com/mattrixwv/cipherstream/monosubstitution/TestVigenere.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestVigenere.java //Mattrixwv // Created: 07-25-21 -//Modified: 04-18-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.monosubstitution; @@ -12,7 +12,6 @@ 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; @@ -20,34 +19,33 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +@ExtendWith(MockitoExtension.class) public class TestVigenere{ + @InjectMocks private Vigenere cipher; + @Mock private Logger logger; //Variables private String inputString = "MeSsage to^encode"; private String inputStringClean = "MESSAGETOENCODE"; private String outputString = "WiQooxh ds^cjqfgo"; private String outputStringClean = "WIQOOXHDSCJQFGO"; - private String keyword = "keyword"; + private String keyword = "ke yw*ord"; + private String keywordClean = "KEYWORD"; private ArrayList offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3)); - @BeforeEach - public void setup(){ - cipher = new Vigenere(); - logger = mock(Logger.class); - Vigenere.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new Vigenere(); @@ -102,11 +100,11 @@ public class TestVigenere{ @Test public void testSetOffset(){ - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.setOffset(); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(offset, cipher.offset); verify(logger, times(1)).debug("Setting offset array from keyword"); verify(logger, times(1)).debug("Offset {}", offset); @@ -117,7 +115,7 @@ public class TestVigenere{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.offset = offset; cipher.setInputString(inputString); @@ -135,7 +133,7 @@ public class TestVigenere{ cipher.preserveCapitals = false; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.offset = offset; cipher.setInputString(inputString); @@ -153,7 +151,7 @@ public class TestVigenere{ cipher.preserveCapitals = true; cipher.preserveWhitespace = false; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.offset = offset; cipher.setInputString(inputString); @@ -171,7 +169,7 @@ public class TestVigenere{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = false; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.offset = offset; cipher.setInputString(inputString); @@ -189,7 +187,7 @@ public class TestVigenere{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.offset = offset; assertThrows(InvalidInputException.class, () -> { @@ -209,7 +207,7 @@ public class TestVigenere{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.offset = offset; assertThrows(InvalidInputException.class, () -> { @@ -228,12 +226,12 @@ public class TestVigenere{ public void testSetKeyword(){ cipher.setKeyword(keyword); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(offset, cipher.offset); verify(logger, times(1)).debug("Original keyword '{}'", keyword); verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing all non-letter characters"); - verify(logger, times(1)).debug("Clean keyword '{}'", keyword.toUpperCase()); + verify(logger, times(1)).debug("Clean keyword '{}'", keywordClean); } @Test @@ -270,7 +268,7 @@ public class TestVigenere{ cipher.preserveWhitespace = true; cipher.preserveSymbols = true; cipher.inputString = inputString; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.offset = offset; cipher.encode(); @@ -292,7 +290,7 @@ public class TestVigenere{ cipher.preserveWhitespace = true; cipher.preserveSymbols = true; cipher.inputString = outputString; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.offset = offset; cipher.decode(); @@ -344,7 +342,7 @@ public class TestVigenere{ String output = cipher.encode(keyword, inputString); assertEquals(inputString, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(offset, cipher.offset); assertEquals(outputString, cipher.outputString); assertEquals(outputString, output); @@ -357,7 +355,7 @@ public class TestVigenere{ String output = cipher.encode(keyword, inputString); assertEquals(inputStringClean, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(offset, cipher.offset); assertEquals(outputStringClean, cipher.outputString); assertEquals(outputStringClean, output); @@ -370,7 +368,7 @@ public class TestVigenere{ String output = cipher.decode(keyword, outputString); assertEquals(outputString, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(offset, cipher.offset); assertEquals(inputString, cipher.outputString); assertEquals(inputString, output); @@ -383,7 +381,7 @@ public class TestVigenere{ String output = cipher.decode(keyword, outputString); assertEquals(outputStringClean, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(offset, cipher.offset); assertEquals(inputStringClean, cipher.outputString); assertEquals(inputStringClean, output); diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestBifid.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestBifid.java index 6c92b20..e698965 100644 --- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestBifid.java +++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestBifid.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestBifid.java //Mattrixwv // Created: 03-03-22 -//Modified: 04-23-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -13,21 +13,26 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +@ExtendWith(MockitoExtension.class) public class TestBifid{ + @InjectMocks private Bifid cipher; + @Mock private Logger logger; //Test private String inputString = "Message to^encode"; @@ -38,14 +43,6 @@ public class TestBifid{ private String keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ"; - @BeforeEach - public void setup(){ - cipher = new Bifid(); - logger = mock(Logger.class); - Bifid.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new Bifid(); @@ -121,7 +118,6 @@ public class TestBifid{ }); verify(logger, never()).debug(eq("Setting keyword '{}'"), anyString()); - verify(logger, never()).debug(anyString(), anyString()); } @Test diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestColumnar.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestColumnar.java index 9c790d0..96ff9c9 100644 --- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestColumnar.java +++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestColumnar.java @@ -1,7 +1,7 @@ //Mattrixwv/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestColumnar.java //Mattrixwv // Created: 01-16-22 -//Modified: 04-26-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -13,7 +13,6 @@ 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; @@ -21,8 +20,11 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; @@ -30,8 +32,11 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +@ExtendWith(MockitoExtension.class) public class TestColumnar{ + @InjectMocks private Columnar cipher; + @Mock private Logger logger; //Variables private String decodedString = "Message to*encode"; @@ -42,7 +47,8 @@ public class TestColumnar{ private String encodedStringPadingAdded = "Edxeoxmte ac*xgoxsnxsex"; //When padding is added to outputString for decoding private String encodedStringPadded = "Edxeoxm te*acxgoxsnxsex"; //When padding is left in outputString private String encodedStringClean = "EDXEOXMTEACXGOXSNXSEX"; - private String keyword = "keyword"; + private String keyword = "ke yw*ord"; + private String keywordClean = "KEYWORD"; private ArrayList> encodeGrid = new ArrayList<>( List.of( new ArrayList<>( @@ -77,14 +83,6 @@ public class TestColumnar{ ); - @BeforeEach - public void setup(){ - cipher = new Columnar(); - logger = mock(Logger.class); - Columnar.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new Columnar(); @@ -257,7 +255,7 @@ public class TestColumnar{ @Test public void testCreateGridEncode(){ cipher.inputString = decodedStringPadded; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.createGridEncode(); @@ -268,7 +266,7 @@ public class TestColumnar{ @Test public void testCreateGridDecode(){ cipher.inputString = encodedStringPadingAdded; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.createGridDecode(); @@ -281,7 +279,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword; + cipher.keyword = keywordClean; cipher.characterToAdd = 'x'; cipher.setInputStringEncode(decodedString); @@ -301,7 +299,7 @@ public class TestColumnar{ cipher.preserveCapitals = false; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword; + cipher.keyword = keywordClean; cipher.characterToAdd = 'X'; cipher.setInputStringEncode(decodedString); @@ -321,7 +319,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.preserveWhitespace = false; cipher.preserveSymbols = true; - cipher.keyword = keyword; + cipher.keyword = keywordClean; cipher.characterToAdd = 'x'; cipher.setInputStringEncode(decodedString); @@ -341,7 +339,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = false; - cipher.keyword = keyword; + cipher.keyword = keywordClean; cipher.characterToAdd = 'x'; cipher.setInputStringEncode(decodedString); @@ -427,7 +425,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.characterToAdd = 'x'; cipher.setInputStringDecode(encodedString); @@ -446,7 +444,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.characterToAdd = 'x'; cipher.setInputStringDecode("Message to encod"); @@ -465,7 +463,7 @@ public class TestColumnar{ cipher.preserveCapitals = false; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.characterToAdd = 'X'; cipher.setInputStringDecode(encodedString); @@ -484,7 +482,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.preserveWhitespace = false; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.characterToAdd = 'x'; cipher.setInputStringDecode(encodedString); @@ -503,7 +501,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = false; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.characterToAdd = 'x'; cipher.setInputStringDecode(encodedString); @@ -522,7 +520,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.characterToAdd = 'x'; assertThrows(InvalidInputException.class, () -> { @@ -543,7 +541,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.characterToAdd = 'x'; assertThrows(InvalidInputException.class, () -> { @@ -564,7 +562,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.characterToAdd = 'x'; assertThrows(InvalidInputException.class, () -> { @@ -584,7 +582,7 @@ public class TestColumnar{ public void testCreateOutputStringFromColumns(){ cipher.removePadding = true; cipher.inputString = decodedStringPadded; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.grid = decodeGrid; cipher.charsAdded = 6; cipher.characterToAdd = 'x'; @@ -603,7 +601,7 @@ public class TestColumnar{ public void testCreateOutputStringFromColumns_padding(){ cipher.removePadding = false; cipher.inputString = decodedStringPadded; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.grid = decodeGrid; cipher.charsAdded = 6; cipher.characterToAdd = 'x'; @@ -622,7 +620,7 @@ public class TestColumnar{ public void testCreateOutputStringFromRows(){ cipher.removePadding = false; cipher.inputString = encodedStringPadded; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.grid = encodeGrid; cipher.charsAdded = 6; cipher.characterToAdd = 'x'; @@ -645,7 +643,7 @@ public class TestColumnar{ public void testCreateOuputStringFromRows_removePadding(){ cipher.removePadding = true; cipher.inputString = encodedStringPadingAdded; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.grid = encodeGrid; cipher.charsAdded = 6; cipher.characterToAdd = 'x'; @@ -668,9 +666,9 @@ public class TestColumnar{ public void testSetKeyword(){ cipher.setKeyword(keyword); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); verify(logger, times(1)).debug("Original keyword {}", keyword); - verify(logger, times(1)).debug("Cleaned keyword {}", keyword.toUpperCase()); + verify(logger, times(1)).debug("Cleaned keyword {}", keywordClean); } @Test @@ -732,7 +730,7 @@ public class TestColumnar{ public void testGetKeywordAlphaLocations(){ ArrayList alphaLocations = new ArrayList<>(List.of(6, 1, 0, 4, 5, 3, 2)); - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; ArrayList returnedLocations = cipher.getKeywordAlphaLocations(); @@ -745,7 +743,7 @@ public class TestColumnar{ public void testGetKeywordOriginalLocations(){ ArrayList orderedLocations = new ArrayList<>(List.of(2, 1, 6, 5, 3, 4, 0)); - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; ArrayList returnedLocations = cipher.getKeywordOriginalLocations(); @@ -785,7 +783,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.removePadding = true; cipher.inputString = decodedStringPadded; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.charsAdded = 6; cipher.characterToAdd = 'x'; @@ -804,7 +802,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.removePadding = false; cipher.inputString = decodedStringPadded; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.charsAdded = 6; cipher.characterToAdd = 'x'; @@ -823,7 +821,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.removePadding = true; cipher.inputString = encodedStringPadingAdded; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.charsAdded = 6; cipher.characterToAdd = 'x'; @@ -842,7 +840,7 @@ public class TestColumnar{ cipher.preserveCapitals = true; cipher.removePadding = false; cipher.inputString = encodedStringPadded; - cipher.keyword = keyword.toUpperCase(); + cipher.keyword = keywordClean; cipher.charsAdded = 6; cipher.characterToAdd = 'x'; @@ -891,7 +889,7 @@ public class TestColumnar{ String output = cipher.encode(keyword, decodedString); assertEquals(decodedStringPadded, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(encodedString, cipher.outputString); assertEquals(encodedString, output); } @@ -903,7 +901,7 @@ public class TestColumnar{ String output = cipher.encode(keyword, decodedString); assertEquals(decodedStringPadded, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(encodedStringPadded, cipher.outputString); assertEquals(encodedStringPadded, output); } @@ -915,7 +913,7 @@ public class TestColumnar{ String output = cipher.encode(keyword, decodedString); assertEquals(decodedStringClean, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(encodedStringClean, cipher.outputString); assertEquals(encodedStringClean, output); } @@ -927,7 +925,7 @@ public class TestColumnar{ String output = cipher.decode(keyword, encodedString); assertEquals(encodedStringPadingAdded, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(decodedString, cipher.outputString); assertEquals(decodedString, output); } @@ -939,7 +937,7 @@ public class TestColumnar{ String output = cipher.decode(keyword, encodedStringPadded); assertEquals(encodedStringPadded, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(decodedStringPadded, cipher.outputString); assertEquals(decodedStringPadded, output); } @@ -951,7 +949,7 @@ public class TestColumnar{ String output = cipher.decode(keyword, encodedString); assertEquals(encodedStringClean, cipher.inputString); - assertEquals(keyword.toUpperCase(), cipher.keyword); + assertEquals(keywordClean, cipher.keyword); assertEquals(decodedStringClean, cipher.outputString); assertEquals(decodedStringClean, output); } diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestHill.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestHill.java index c56e03d..fa70e71 100644 --- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestHill.java +++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestHill.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamjava/polySubstitution/TestHill.java //Mattrixwv // Created: 01-31-22 -//Modified: 04-28-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -14,15 +14,17 @@ 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 java.util.ArrayList; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; @@ -31,8 +33,11 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeyException; import com.mattrixwv.matrix.ModMatrix; +@ExtendWith(MockitoExtension.class) public class TestHill{ + @InjectMocks private Hill cipher; + @Mock private Logger logger; //Fields private String decodedString = "Message to^encoded"; @@ -44,14 +49,6 @@ public class TestHill{ private ModMatrix key = new ModMatrix(keyArray, 26); - @BeforeEach - public void setup(){ - cipher = new Hill(); - logger = mock(Logger.class); - Hill.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new Hill(); @@ -152,7 +149,7 @@ public class TestHill{ verify(logger, times(1)).debug("Testing mod"); verify(logger, times(1)).debug("Testing square"); verify(logger, times(1)).debug("Testing invertable"); - verify(logger, times(1)).debug("key = {}", key); + verify(logger, times(1)).debug("key\n{}", key); } @Test @@ -168,7 +165,7 @@ public class TestHill{ verify(logger, times(1)).debug("Testing mod"); verify(logger, never()).debug("Testing square"); verify(logger, never()).debug("Testing invertable"); - verify(logger, never()).debug(eq("key = {}"), any(ModMatrix.class)); + verify(logger, never()).debug(eq("key\n{}"), any(ModMatrix.class)); } @Test @@ -184,7 +181,7 @@ public class TestHill{ verify(logger, times(1)).debug("Testing mod"); verify(logger, times(1)).debug("Testing square"); verify(logger, never()).debug("Testing invertable"); - verify(logger, never()).debug(eq("key = {}"), any(ModMatrix.class)); + verify(logger, never()).debug(eq("key\n{}"), any(ModMatrix.class)); } @Test @@ -200,7 +197,7 @@ public class TestHill{ verify(logger, times(1)).debug("Testing mod"); verify(logger, times(1)).debug("Testing square"); verify(logger, times(1)).debug("Testing invertable"); - verify(logger, never()).debug(eq("key = {}"), any(ModMatrix.class)); + verify(logger, never()).debug(eq("key\n{}"), any(ModMatrix.class)); } @Test diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java index b238757..91f276e 100644 --- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java +++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java //Mattrixwv // Created: 04-21-23 -//Modified: 04-21-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -13,28 +13,33 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +@ExtendWith(MockitoExtension.class) public class TestLargePolybiusSquare{ + @InjectMocks private LargePolybiusSquare cipher; + @Mock(name = "com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare") private Logger logger; //Variables private String decodedString = "Message to^encode"; private String decodedStringClean = "MESSAGETOENCODE"; private String encodedString = "35124343222612 4415^123624152112"; private String encodedStringClean = "31 15 41 41 11 21 15 42 33 15 32 13 33 14 15"; - private String keyword = "keyword"; + private String keyword = "ke yw*ord"; private String keywordClean = "KEYWORDABCFGHIJLMNPQSTUVXZ0123456789"; private String keywordBlank = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; private char[][] grid = { @@ -55,14 +60,6 @@ public class TestLargePolybiusSquare{ }; - @BeforeEach - public void setup(){ - cipher = new LargePolybiusSquare(); - logger = mock(Logger.class); - LargePolybiusSquare.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new LargePolybiusSquare(); diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestMorse.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestMorse.java index 0232e16..2998568 100644 --- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestMorse.java +++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestMorse.java @@ -1,58 +1,243 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/TestMorse.java //Matthew Ellison // Created: 07-28-21 -//Modified: 07-09-22 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; 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.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; + +import com.mattrixwv.cipherstream.exceptions.InvalidInputException; +@ExtendWith(MockitoExtension.class) public class TestMorse{ + @InjectMocks + private Morse cipher; + @Mock + private Logger logger; + //Fields + private String inputString = "Message to^encode123"; + private String inputStringClean = "MESSAGETOENCODE123"; + private String outputString = "-- . ... ... .- --. . - --- . -. -.-. --- -.. . .---- ..--- ...--"; + + + @Test + public void testConstructor(){ + cipher = new Morse(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + } + + @Test + public void testSetInputStringEncode(){ + cipher.setInputStringEncode(inputString); + + assertEquals(inputStringClean, cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", inputString); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Removing whitespace and symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", inputStringClean); + } + + @Test + public void testSetInputStringEncode_blank(){ + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringEncode(""); + }); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", ""); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Removing whitespace and symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", ""); + } + + @Test + public void testSetInputStringEncode_blankClean(){ + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringEncode("*&^"); + }); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", "*&^"); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Removing whitespace and symbols"); + verify(logger, times(1)).debug("Cleaned input string '{}'", ""); + } + + @Test + public void testSetInputStringEncode_null(){ + String nullString = null; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringEncode(null); + }); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", nullString); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing whitespace and symbols"); + verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); + } + + @Test + public void testSetInputStringDecode(){ + cipher.setInputStringDecode(outputString); + + assertEquals(outputString, cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString); + verify(logger, times(1)).debug("Checking for invalid characters"); + verify(logger, times(1)).debug("Saving"); + } + + @Test + public void testSetInputStringDecode_invalid(){ + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringDecode("*"); + }); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", "*"); + verify(logger, times(1)).debug("Checking for invalid characters"); + verify(logger, never()).debug("Saving"); + } + + @Test + public void testSetInputStringDecode_blank(){ + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringDecode(""); + }); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", ""); + verify(logger, times(1)).debug("Checking for invalid characters"); + verify(logger, never()).debug("Saving"); + } + + @Test + public void testSetInputStringDecode_null(){ + String nullString = null; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputStringDecode(null); + }); + + assertEquals("", cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", nullString); + verify(logger, never()).debug("Checking for invalid characters"); + verify(logger, never()).debug("Saving"); + } + @Test public void testEncode(){ - Morse cipher = new Morse(); + cipher.inputString = inputStringClean; - //Test 1 - String input = "sos"; - String correctOutput = "... --- ..."; - String output = cipher.encode(input); - assertEquals(correctOutput, output); + cipher.encode(); - //Test 2 - input = "MORSE, CODE"; - correctOutput = "-- --- .-. ... . -.-. --- -.. ."; - output = cipher.encode(input); - assertEquals(correctOutput, output); - - //Test 3 - input = "1.23 987"; - correctOutput = ".---- ..--- ...-- ----. ---.. --..."; - output = cipher.encode(input); - assertEquals(correctOutput, output); + assertEquals(outputString, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(18)).debug(eq("Working character {}"), anyChar()); + verify(logger, times(15)).debug("Appending letter"); + verify(logger, times(3)).debug("Appending number"); + verify(logger, times(1)).debug("Saving encoded string '{}'", outputString); } + + @Test + public void testEncode_invalid(){ + cipher.inputString = "*"; + + assertThrows(InvalidInputException.class, () -> { + cipher.encode(); + }); + + assertEquals("", cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(1)).debug(eq("Working character {}"), anyChar()); + verify(logger, never()).debug("Appending letter"); + verify(logger, never()).debug("Appending number"); + verify(logger, never()).debug("Saving encoded string '{}'", outputString); + } + @Test public void testDecode(){ - Morse cipher = new Morse(); + cipher.inputString = outputString; - //Test 1 - String input = "... --- ..."; - String correctOutput = "SOS"; - String output = cipher.decode(input); - assertEquals(correctOutput, output); + cipher.decode(); - //Test 2 - input = "-- --- .-. ... . -.-. --- -.. ."; - correctOutput = "MORSECODE"; - output = cipher.decode(input); - assertEquals(correctOutput, output); + assertEquals(inputStringClean, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(18)).debug(eq("Working letter {}"), anyString()); + verify(logger, times(18)).debug(eq("Decoded letter {}"), anyChar()); + verify(logger, times(1)).debug("Saving decoded string '{}'", inputStringClean); + } - //Test 3 - input = ".---- ..--- ...-- ----. ---.. --..."; - correctOutput = "123987"; - output = cipher.decode(input); - assertEquals(correctOutput, output); + @Test + public void testDecode_invalid(){ + cipher.inputString = "A"; + + assertThrows(InvalidInputException.class, () -> { + cipher.decode(); + }); + + assertEquals("", cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(1)).debug(eq("Working letter {}"), anyString()); + verify(logger, never()).debug(eq("Decoded letter {}"), anyChar()); + verify(logger, never()).debug(eq("Saving decoded string '{}'"), anyString()); + } + + @Test + public void testGetters(){ + cipher.inputString = inputString; + cipher.outputString = outputString; + + assertEquals(inputString, cipher.getInputString()); + assertEquals(outputString, cipher.getOutputString()); + } + + @Test + public void testReset(){ + cipher.inputString = inputString; + cipher.outputString = outputString; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + verify(logger, times(1)).debug("Resetting"); + } + + @Test + public void testPracticalEncode(){ + String output = cipher.encode(inputString); + + assertEquals(inputStringClean, cipher.inputString); + assertEquals(outputString, cipher.outputString); + assertEquals(outputString, output); + } + + @Test + public void testPracticalDecode(){ + String output = cipher.decode(outputString); + + assertEquals(outputString, cipher.inputString); + assertEquals(inputStringClean, cipher.outputString); + assertEquals(inputStringClean, output); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPlayfair.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPlayfair.java index c299360..dc26ed2 100644 --- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPlayfair.java +++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPlayfair.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/TestPlayfair.java //Matthew Ellison // Created: 07-30-21 -//Modified: 04-28-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -15,13 +15,15 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; @@ -30,15 +32,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.polysubstitution.Playfair.CharLocation; +@ExtendWith(MockitoExtension.class) public class TestPlayfair{ + @InjectMocks private Playfair cipher; + @Mock private Logger logger; //Fields - private String inputString = "Hide the gold in - the@tree+stump"; - private String inputStringPadded = "Hide the gold in - the@trexe+stump"; - private String inputStringClean = "HIDETHEGOLDINTHETREXESTUMP"; - private String outputString = "Bmod zbx dnab ek - udm@uixmm+ouvif"; - private String outputStringClean = "BMODZBXDNABEKUDMUIXMMOUVIF"; + private String decodedString = "Hide the gold in - the@tree+stump"; + private String decodedStringPadded = "Hide the gold in - the@trexe+stump"; + private String decodedStringClean = "HIDETHEGOLDINTHETREXESTUMP"; + private String encodedString = "Bmod zbx dnab ek - udm@uixmm+ouvif"; + private String encodedStringClean = "BMODZBXDNABEKUDMUIXMMOUVIF"; private String keyword = "Play-fair@Exam ple"; private String keywordClean = "PLAYFIREXMBCDGHKNOQSTUVWZ"; private char[][] grid = new char[][]{ @@ -50,14 +55,6 @@ public class TestPlayfair{ }; - @BeforeEach - public void setup(){ - cipher = new Playfair(); - logger = mock(Logger.class); - Playfair.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new Playfair(); @@ -424,11 +421,11 @@ public class TestPlayfair{ cipher.preserveSymbols = true; cipher.doubled = 'x'; - cipher.setInputString(inputString, true); + cipher.setInputString(decodedString, true); - assertEquals(inputStringPadded, cipher.inputString); + assertEquals(decodedStringPadded, cipher.inputString); verify(logger, times(1)).debug("Setting input string"); - verify(logger, times(1)).debug("Original input string {}", 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"); @@ -441,11 +438,11 @@ public class TestPlayfair{ cipher.preserveSymbols = true; cipher.doubled = 'X'; - cipher.setInputString(inputString, true); + cipher.setInputString(decodedString, true); - assertEquals(inputStringPadded.toUpperCase(), cipher.inputString); + assertEquals(decodedStringPadded.toUpperCase(), cipher.inputString); verify(logger, times(1)).debug("Setting input string"); - verify(logger, times(1)).debug("Original input string {}", 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"); @@ -458,11 +455,11 @@ public class TestPlayfair{ cipher.preserveSymbols = true; cipher.doubled = 'x'; - cipher.setInputString(inputString, true); + cipher.setInputString(decodedString, true); - assertEquals(inputStringPadded.replaceAll("\\s", ""), cipher.inputString); + assertEquals(decodedStringPadded.replaceAll("\\s", ""), cipher.inputString); verify(logger, times(1)).debug("Setting input string"); - verify(logger, times(1)).debug("Original input string {}", 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"); @@ -475,11 +472,11 @@ public class TestPlayfair{ cipher.preserveSymbols = false; cipher.doubled = 'x'; - cipher.setInputString(inputString, true); + cipher.setInputString(decodedString, true); - assertEquals(inputStringPadded.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); + assertEquals(decodedStringPadded.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); verify(logger, times(1)).debug("Setting input string"); - verify(logger, times(1)).debug("Original input string {}", 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"); @@ -511,15 +508,15 @@ public class TestPlayfair{ cipher.preserveSymbols = true; cipher.doubled = 'x'; - cipher.setInputString(outputString, false); + cipher.setInputString(encodedString, false); - assertEquals(outputString, cipher.inputString); + assertEquals(encodedString, cipher.inputString); verify(logger, times(1)).debug("Setting input string"); - verify(logger, times(1)).debug("Original input string {}", outputString); + verify(logger, times(1)).debug("Original input string {}", encodedString); verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); - verify(logger, times(1)).debug("Clean input string '{}'", outputString); + verify(logger, times(1)).debug("Clean input string '{}'", encodedString); } @Test @@ -530,16 +527,16 @@ public class TestPlayfair{ cipher.doubled = 'x'; assertThrows(InvalidCharacterException.class, () -> { - cipher.setInputString(outputString + cipher.replaced, false); + cipher.setInputString(encodedString + cipher.replaced, false); }); assertEquals("", cipher.inputString); verify(logger, times(1)).debug("Setting input string"); - verify(logger, times(1)).debug("Original input string {}", outputString + Character.toString(cipher.replaced)); + verify(logger, times(1)).debug("Original input string {}", encodedString + Character.toString(cipher.replaced)); verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); - verify(logger, never()).debug("Clean input string '{}'", outputString); + verify(logger, never()).debug("Clean input string '{}'", encodedString); } @Test @@ -550,16 +547,16 @@ public class TestPlayfair{ cipher.doubled = 'x'; assertThrows(InvalidInputException.class, () -> { - cipher.setInputString(outputString + "a", false); + cipher.setInputString(encodedString + "a", false); }); - assertEquals(outputString + "a", cipher.inputString); + assertEquals(encodedString + "a", cipher.inputString); verify(logger, times(1)).debug("Setting input string"); - verify(logger, times(1)).debug("Original input string {}", outputString + "a"); + verify(logger, times(1)).debug("Original input string {}", encodedString + "a"); verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); - verify(logger, times(1)).debug("Clean input string '{}'", outputString + "a"); + verify(logger, times(1)).debug("Clean input string '{}'", encodedString + "a"); } @Test @@ -606,48 +603,48 @@ public class TestPlayfair{ public void testSetInputStringEncode(){ cipher.doubled = 'x'; - cipher.setInputStringEncode(inputString); + cipher.setInputStringEncode(decodedString); - assertEquals(inputStringPadded, cipher.inputString); + assertEquals(decodedStringPadded, cipher.inputString); verify(logger, times(1)).debug("Cleaning up input string for encoding"); verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer); verify(logger, times(13)).debug(eq("Starting at character {}"), anyInt()); verify(logger, times(13)).debug(eq("Adding to clean input: {} {} {} {}"), any(StringBuilder.class), anyChar(), any(StringBuilder.class), anyChar()); verify(logger, times(1)).debug("Checking odd characters"); verify(logger, never()).debug("Adding final character to make even"); - verify(logger, times(1)).debug("Cleaned input string '{}'", inputStringPadded); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded); } @Test public void testSetInputStringEncode_odd(){ cipher.doubled = 'x'; - cipher.setInputStringEncode(inputStringPadded + 'a'); + cipher.setInputStringEncode(decodedStringPadded + 'a'); - assertEquals(inputStringPadded + "ax", cipher.inputString); + assertEquals(decodedStringPadded + "ax", cipher.inputString); verify(logger, times(1)).debug("Cleaning up input string for encoding"); verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer); verify(logger, times(14)).debug(eq("Starting at character {}"), anyInt()); verify(logger, times(14)).debug(eq("Adding to clean input: {} {} {} {}"), any(StringBuilder.class), anyChar(), any(StringBuilder.class), anyChar()); verify(logger, times(1)).debug("Checking odd characters"); verify(logger, times(1)).debug("Adding final character to make even"); - verify(logger, times(1)).debug("Cleaned input string '{}'", inputStringPadded + "ax"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded + "ax"); } @Test public void testSetInputString_oddEndSymbol(){ cipher.doubled = 'x'; - cipher.setInputStringEncode(inputStringPadded + "a*"); + cipher.setInputStringEncode(decodedStringPadded + "a*"); - assertEquals(inputStringPadded + "a*x", cipher.inputString); + assertEquals(decodedStringPadded + "a*x", cipher.inputString); verify(logger, times(1)).debug("Cleaning up input string for encoding"); verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer); verify(logger, times(14)).debug(eq("Starting at character {}"), anyInt()); verify(logger, times(14)).debug(eq("Adding to clean input: {} {} {} {}"), any(StringBuilder.class), anyChar(), any(StringBuilder.class), anyChar()); verify(logger, times(1)).debug("Checking odd characters"); verify(logger, times(1)).debug("Adding final character to make even"); - verify(logger, times(1)).debug("Cleaned input string '{}'", inputStringPadded + "a*x"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded + "a*x"); } @Test @@ -655,26 +652,26 @@ public class TestPlayfair{ cipher.doubled = 'x'; cipher.replacer = 'i'; - cipher.setInputStringEncode(inputStringPadded + 'x'); + cipher.setInputStringEncode(decodedStringPadded + 'x'); - assertEquals(inputStringPadded + "xi", cipher.inputString); + assertEquals(decodedStringPadded + "xi", cipher.inputString); verify(logger, times(1)).debug("Cleaning up input string for encoding"); verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer); verify(logger, times(14)).debug(eq("Starting at character {}"), anyInt()); verify(logger, times(14)).debug(eq("Adding to clean input: {} {} {} {}"), any(StringBuilder.class), anyChar(), any(StringBuilder.class), anyChar()); verify(logger, times(1)).debug("Checking odd characters"); verify(logger, times(1)).debug("Adding final character to make even"); - verify(logger, times(1)).debug("Cleaned input string '{}'", inputStringPadded + "xi"); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded + "xi"); } @Test public void testGetPreparedInputString(){ - cipher.inputString = inputStringPadded; + cipher.inputString = decodedStringPadded; String output = cipher.getPreparedInputString(); - assertEquals(inputStringClean, output); + assertEquals(decodedStringClean, output); verify(logger, times(1)).debug("Getting input string ready for encoding"); - verify(logger, times(1)).debug("Prepared string '{}'", inputStringClean); + verify(logger, times(1)).debug("Prepared string '{}'", decodedStringClean); } @Test @@ -768,28 +765,28 @@ public class TestPlayfair{ @Test public void testAddCharactersToCleanString(){ - cipher.inputString = inputStringPadded; + cipher.inputString = decodedStringPadded; - cipher.addCharactersToCleanString(outputStringClean); + cipher.addCharactersToCleanString(encodedStringClean); - assertEquals(outputString, cipher.outputString); + assertEquals(encodedString, cipher.outputString); verify(logger, times(1)).debug("Formatting output string"); verify(logger, times(34)).debug(eq("Working character {}"), anyChar()); verify(logger, times(1)).debug("Appending uppercase"); verify(logger, times(25)).debug("Appending lowercase"); verify(logger, times(8)).debug("Appending symbol"); - verify(logger, times(1)).debug("Formatted output '{}'", outputString); + verify(logger, times(1)).debug("Formatted output '{}'", encodedString); } @Test public void testEncode(){ - cipher.inputString = inputStringPadded; + cipher.inputString = decodedStringPadded; cipher.keyword = keywordClean; cipher.grid = grid; cipher.encode(); - assertEquals(outputString, cipher.outputString); + assertEquals(encodedString, cipher.outputString); verify(logger, times(1)).debug("Encoding"); verify(logger, times(13)).debug(eq("Letters {} {}"), anyChar(), anyChar()); verify(logger, times(2)).debug("Row encoding"); @@ -800,13 +797,13 @@ public class TestPlayfair{ @Test public void testDecode(){ - cipher.inputString = outputString; + cipher.inputString = encodedString; cipher.keyword = keywordClean; cipher.grid = grid; cipher.decode(); - assertEquals(inputStringPadded, cipher.outputString); + assertEquals(decodedStringPadded, cipher.outputString); verify(logger, times(1)).debug("Decoding"); verify(logger, times(13)).debug(eq("Letters {} {}"), anyChar(), anyChar()); verify(logger, times(2)).debug("Row decoding"); @@ -817,8 +814,8 @@ public class TestPlayfair{ @Test public void testReset(){ - cipher.inputString = inputString; - cipher.outputString = outputString; + cipher.inputString = decodedString; + cipher.outputString = encodedString; cipher.keyword = keyword; cipher.grid = grid; @@ -833,13 +830,13 @@ public class TestPlayfair{ @Test public void testGetters(){ - cipher.inputString = inputString; - cipher.outputString = outputString; + cipher.inputString = decodedString; + cipher.outputString = encodedString; cipher.keyword = keyword; cipher.grid = grid; - assertEquals(inputString, cipher.getInputString()); - assertEquals(outputString, cipher.getOutputString()); + assertEquals(decodedString, cipher.getInputString()); + assertEquals(encodedString, cipher.getOutputString()); assertEquals(keyword, cipher.getKeyword()); assertEquals("[P L A Y F]\n[I R E X M]\n[B C D G H]\n[K N O Q S]\n[T U V W Z]", cipher.getGrid()); assertEquals('J', cipher.getReplaced()); @@ -852,12 +849,12 @@ public class TestPlayfair{ public void testPracticalEncode(){ cipher = new Playfair(true, true, true); - String output = cipher.encode(keyword, inputString); + String output = cipher.encode(keyword, decodedString); - assertEquals(inputStringPadded, cipher.inputString); + assertEquals(decodedStringPadded, cipher.inputString); assertEquals(keywordClean, cipher.keyword); - assertEquals(outputString, cipher.outputString); - assertEquals(outputString, output); + assertEquals(encodedString, cipher.outputString); + assertEquals(encodedString, output); assertArrayEquals(grid, cipher.grid); } @@ -865,12 +862,12 @@ public class TestPlayfair{ public void testPracticalEncode_clean(){ cipher = new Playfair(false, false, false); - String output = cipher.encode(keyword, inputString); + String output = cipher.encode(keyword, decodedString); - assertEquals(inputStringClean, cipher.inputString); + assertEquals(decodedStringClean, cipher.inputString); assertEquals(keywordClean, cipher.keyword); - assertEquals(outputStringClean, cipher.outputString); - assertEquals(outputStringClean, output); + assertEquals(encodedStringClean, cipher.outputString); + assertEquals(encodedStringClean, output); assertArrayEquals(grid, cipher.grid); } @@ -878,12 +875,12 @@ public class TestPlayfair{ public void testPracticalDecode(){ cipher = new Playfair(true, true, true); - String output = cipher.decode(keyword, outputString); + String output = cipher.decode(keyword, encodedString); - assertEquals(outputString, cipher.inputString); + assertEquals(encodedString, cipher.inputString); assertEquals(keywordClean, cipher.keyword); - assertEquals(inputStringPadded, cipher.outputString); - assertEquals(inputStringPadded, output); + assertEquals(decodedStringPadded, cipher.outputString); + assertEquals(decodedStringPadded, output); assertArrayEquals(grid, cipher.grid); } @@ -891,12 +888,12 @@ public class TestPlayfair{ public void testPracticalDecode_clean(){ cipher = new Playfair(false, false, false); - String output = cipher.decode(keyword, outputString); + String output = cipher.decode(keyword, encodedString); - assertEquals(outputStringClean, cipher.inputString); + assertEquals(encodedStringClean, cipher.inputString); assertEquals(keywordClean, cipher.keyword); - assertEquals(inputStringClean, cipher.outputString); - assertEquals(inputStringClean, output); + assertEquals(decodedStringClean, cipher.outputString); + assertEquals(decodedStringClean, output); assertArrayEquals(grid, cipher.grid); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPolybiusSquare.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPolybiusSquare.java index 39befd1..64be3ea 100644 --- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPolybiusSquare.java +++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPolybiusSquare.java @@ -1,7 +1,7 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestPolybiusSquare.java //Mattrixwv // Created: 01-04-22 -//Modified: 04-29-23 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; @@ -14,13 +14,15 @@ 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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; @@ -29,15 +31,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare.CharLocation; +@ExtendWith(MockitoExtension.class) public class TestPolybiusSquare{ + @InjectMocks private PolybiusSquare cipher; + @Mock private Logger logger; //Fields - private String inputString = "Message to^encode"; - private String inputStringClean = "M E S S A G E T O E N C O D E"; - private String outputString = "41124545233212 5115^124225152212"; - private String outputStringClean = "41 12 45 45 23 32 12 51 15 12 42 25 15 22 12"; - private String keyword = "keyword"; + private String decodedString = "Message to^encode"; + private String decodedStringClean = "M E S S A G E T O E N C O D E"; + private String encodedString = "41124545233212 5115^124225152212"; + private String encodedStringClean = "41 12 45 45 23 32 12 51 15 12 42 25 15 22 12"; + private String keyword = "ke yw*ord"; private String keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ"; private char[][] grid = new char[][]{ {'K', 'E', 'Y', 'W', 'O'}, @@ -49,14 +54,6 @@ public class TestPolybiusSquare{ private String gridString = "[K E Y W O]\n[R D A B C]\n[F G H I L]\n[M N P Q S]\n[T U V X Z]"; - @BeforeEach - public void setup(){ - cipher = new PolybiusSquare(); - logger = mock(Logger.class); - PolybiusSquare.logger = logger; - } - - @Test public void testConstructor_default(){ cipher = new PolybiusSquare(); @@ -219,16 +216,16 @@ public class TestPolybiusSquare{ cipher.replaced = 'J'; cipher.replacer = 'I'; - cipher.setInputStringEncode(inputString); + cipher.setInputStringEncode(decodedString); - assertEquals(inputString.toUpperCase(), cipher.inputString); - verify(logger, times(1)).debug("Setting input string for encoding '{}'", inputString); + assertEquals(decodedString.toUpperCase(), cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString); verify(logger, times(1)).debug("Checking for digits"); verify(logger, times(1)).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer); - verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase()); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase()); } @Test @@ -238,16 +235,16 @@ public class TestPolybiusSquare{ cipher.replaced = 'J'; cipher.replacer = 'I'; - cipher.setInputStringEncode(inputString); + cipher.setInputStringEncode(decodedString); - assertEquals(inputString.toUpperCase().replaceAll("\\s", ""), cipher.inputString); - verify(logger, times(1)).debug("Setting input string for encoding '{}'", inputString); + assertEquals(decodedString.toUpperCase().replaceAll("\\s", ""), cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString); verify(logger, times(1)).debug("Checking for digits"); verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer); - verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("\\s", "")); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("\\s", "")); } @Test @@ -257,16 +254,16 @@ public class TestPolybiusSquare{ cipher.replaced = 'J'; cipher.replacer = 'I'; - cipher.setInputStringEncode(inputString); + cipher.setInputStringEncode(decodedString); - assertEquals(inputString.toUpperCase().replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); - verify(logger, times(1)).debug("Setting input string for encoding '{}'", inputString); + assertEquals(decodedString.toUpperCase().replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString); verify(logger, times(1)).debug("Checking for digits"); verify(logger, times(1)).debug("Removing case"); verify(logger, never()).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing symbols"); verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer); - verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("[^a-zA-Z\\s]", "")); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("[^a-zA-Z\\s]", "")); } @Test @@ -276,16 +273,16 @@ public class TestPolybiusSquare{ cipher.replaced = 'J'; cipher.replacer = 'I'; - cipher.setInputStringEncode(inputString); + cipher.setInputStringEncode(decodedString); - assertEquals(inputStringClean, cipher.inputString); - verify(logger, times(1)).debug("Setting input string for encoding '{}'", inputString); + assertEquals(decodedStringClean, cipher.inputString); + verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString); verify(logger, times(1)).debug("Checking for digits"); verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing symbols"); verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer); - verify(logger, times(1)).debug("Cleaned input string '{}'", inputStringClean); + verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringClean); } @Test @@ -377,14 +374,14 @@ public class TestPolybiusSquare{ cipher.preserveWhitespace = true; cipher.preserveSymbols = true; - cipher.setInputStringDecode(outputString); + cipher.setInputStringDecode(encodedString); - assertEquals(outputString, cipher.inputString); - verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString); + assertEquals(encodedString, cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString); verify(logger, times(1)).debug("Checking for letters"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); - verify(logger, times(1)).debug("Cleaned input string '{}'", outputString); + verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString); } @Test @@ -392,14 +389,14 @@ public class TestPolybiusSquare{ cipher.preserveWhitespace = false; cipher.preserveSymbols = true; - cipher.setInputStringDecode(outputString); + cipher.setInputStringDecode(encodedString); - assertEquals(outputString.replaceAll("\\s", ""), cipher.inputString); - verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString); + assertEquals(encodedString.replaceAll("\\s", ""), cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString); verify(logger, times(1)).debug("Checking for letters"); verify(logger, times(1)).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); - verify(logger, times(1)).debug("Cleaned input string '{}'", outputString.replaceAll("\\s", "")); + verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.replaceAll("\\s", "")); } @Test @@ -407,14 +404,14 @@ public class TestPolybiusSquare{ cipher.preserveWhitespace = true; cipher.preserveSymbols = false; - cipher.setInputStringDecode(outputString); + cipher.setInputStringDecode(encodedString); - assertEquals(outputString.replaceAll("[^0-9\\s]", ""), cipher.inputString); - verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString); + assertEquals(encodedString.replaceAll("[^0-9\\s]", ""), cipher.inputString); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString); verify(logger, times(1)).debug("Checking for letters"); verify(logger, never()).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing symbols"); - verify(logger, times(1)).debug("Cleaned input string '{}'", outputString.replaceAll("[^0-9\\s]", "")); + verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.replaceAll("[^0-9\\s]", "")); } @Test @@ -423,11 +420,11 @@ public class TestPolybiusSquare{ cipher.preserveSymbols = true; assertThrows(InvalidInputException.class, () -> { - cipher.setInputStringDecode(outputString + "a"); + cipher.setInputStringDecode(encodedString + "a"); }); assertEquals("", cipher.inputString); - verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString + "a"); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString + "a"); verify(logger, times(1)).debug("Checking for letters"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); @@ -440,11 +437,11 @@ public class TestPolybiusSquare{ cipher.preserveSymbols = true; assertThrows(InvalidInputException.class, () -> { - cipher.setInputStringDecode(outputString + "0"); + cipher.setInputStringDecode(encodedString + "0"); }); assertEquals("", cipher.inputString); - verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString + "0"); + verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString + "0"); verify(logger, times(1)).debug("Checking for letters"); verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing symbols"); @@ -504,24 +501,24 @@ public class TestPolybiusSquare{ @Test public void testGetPreparedInputStringEncode(){ - cipher.inputString = inputString; + cipher.inputString = decodedString; String output = cipher.getPreparedInputStringEncode(); - assertEquals(inputStringClean.replaceAll("\\s", ""), output); + assertEquals(decodedStringClean.replaceAll("\\s", ""), output); verify(logger, times(1)).debug("Preparing input string for encoding"); - verify(logger, times(1)).debug("Prepared string '{}'", inputStringClean.replaceAll("\\s", "")); + verify(logger, times(1)).debug("Prepared string '{}'", decodedStringClean.replaceAll("\\s", "")); } @Test public void testGetPreparedInputStringDecode(){ - cipher.inputString = outputString; + cipher.inputString = encodedString; String output = cipher.getPreparedInputStringDecode(); - assertEquals(outputStringClean.replaceAll("\\s", ""), output); + assertEquals(encodedStringClean.replaceAll("\\s", ""), output); verify(logger, times(1)).debug("Preparing input string for decoding"); - verify(logger, times(1)).debug("Prepared string '{}'", outputStringClean.replaceAll("\\s", "")); + verify(logger, times(1)).debug("Prepared string '{}'", encodedStringClean.replaceAll("\\s", "")); } @Test @@ -602,41 +599,41 @@ public class TestPolybiusSquare{ @Test public void testAddCharactersToCleanStringEncode(){ - cipher.inputString = inputString; + cipher.inputString = decodedString; - cipher.addCharactersToCleanStringEncode(outputStringClean.replaceAll("\\D", "")); + cipher.addCharactersToCleanStringEncode(encodedStringClean.replaceAll("\\D", "")); - assertEquals(outputString, cipher.outputString); + assertEquals(encodedString, cipher.outputString); verify(logger, times(1)).debug("Formatting output string for encoding"); verify(logger, times(17)).debug(eq("Working character {}"), anyChar()); verify(logger, times(15)).debug("Adding encoded characters"); verify(logger, times(2)).debug("Adding symbols"); - verify(logger, times(1)).debug("Formatted output '{}'", outputString); + verify(logger, times(1)).debug("Formatted output '{}'", encodedString); } @Test public void testAddCharactersToCleanStringDecode(){ - cipher.inputString = outputString; + cipher.inputString = encodedString; - cipher.addCharactersToCleanStringDecode(inputStringClean.replaceAll("\\s", "")); + cipher.addCharactersToCleanStringDecode(decodedStringClean.replaceAll("\\s", "")); - assertEquals(inputString.toUpperCase(), cipher.outputString); + assertEquals(decodedString.toUpperCase(), cipher.outputString); verify(logger, times(1)).debug("Formatting output string for decoding"); verify(logger, times(17)).debug(eq("Working character {}"), anyChar()); verify(logger, times(15)).debug("Adding decoded characters"); verify(logger, times(2)).debug("Adding symbols"); - verify(logger, times(1)).debug("Formatted output '{}'", inputString.toUpperCase()); + verify(logger, times(1)).debug("Formatted output '{}'", decodedString.toUpperCase()); } @Test public void testEncode(){ - cipher.inputString = inputString.toUpperCase(); + cipher.inputString = decodedString.toUpperCase(); cipher.keyword = keywordClean; cipher.grid = grid; cipher.encode(); - assertEquals(outputString, cipher.outputString); + assertEquals(encodedString, cipher.outputString); verify(logger, times(1)).debug("Encoding"); verify(logger, times(15)).debug(eq("Current working character {}"), anyChar()); verify(logger, times(15)).debug(eq("Location {}, {}"), anyInt(), anyInt()); @@ -644,13 +641,13 @@ public class TestPolybiusSquare{ @Test public void testDecode(){ - cipher.inputString = outputString; + cipher.inputString = encodedString; cipher.keyword = keywordClean; cipher.grid = grid; cipher.decode(); - assertEquals(inputString.toUpperCase(), cipher.outputString); + assertEquals(decodedString.toUpperCase(), cipher.outputString); verify(logger, times(1)).debug("Decoding"); verify(logger, times(15)).debug(eq("Digits to decode {} {}"), anyChar(), anyChar()); verify(logger, times(15)).debug(eq("Decoded letter {}"), anyChar()); @@ -658,14 +655,14 @@ public class TestPolybiusSquare{ @Test public void testGetters(){ - cipher.inputString = inputString; + cipher.inputString = decodedString; cipher.keyword = keywordClean; - cipher.outputString = outputString; + cipher.outputString = encodedString; cipher.grid = grid; - assertEquals(inputString, cipher.getInputString()); + assertEquals(decodedString, cipher.getInputString()); assertEquals(keywordClean, cipher.getKeyword()); - assertEquals(outputString, cipher.getOutputString()); + assertEquals(encodedString, cipher.getOutputString()); assertEquals("[K E Y W O]\n[R D A B C]\n[F G H I L]\n[M N P Q S]\n[T U V X Z]", cipher.getGrid()); assertEquals('J', cipher.getReplaced()); assertEquals('I', cipher.getReplacer()); @@ -673,9 +670,9 @@ public class TestPolybiusSquare{ @Test public void testReset(){ - cipher.inputString = inputString; + cipher.inputString = decodedString; cipher.keyword = keyword; - cipher.outputString = outputString; + cipher.outputString = encodedString; cipher.grid = grid; cipher.reset(); @@ -692,12 +689,12 @@ public class TestPolybiusSquare{ public void testPracticalEncode(){ cipher = new PolybiusSquare(true, true); - String output = cipher.encode(keyword, inputString); + String output = cipher.encode(keyword, decodedString); - assertEquals(inputString.toUpperCase(), cipher.inputString); + assertEquals(decodedString.toUpperCase(), cipher.inputString); assertEquals(keywordClean, cipher.keyword); - assertEquals(outputString, cipher.outputString); - assertEquals(outputString, output); + assertEquals(encodedString, cipher.outputString); + assertEquals(encodedString, output); assertArrayEquals(grid, cipher.grid); } @@ -705,12 +702,12 @@ public class TestPolybiusSquare{ public void testPracticalEncode_clean(){ cipher = new PolybiusSquare(false, false); - String output = cipher.encode(keyword, inputString); + String output = cipher.encode(keyword, decodedString); - assertEquals(inputStringClean, cipher.inputString); + assertEquals(decodedStringClean, cipher.inputString); assertEquals(keywordClean, cipher.keyword); - assertEquals(outputStringClean, cipher.outputString); - assertEquals(outputStringClean, output); + assertEquals(encodedStringClean, cipher.outputString); + assertEquals(encodedStringClean, output); assertArrayEquals(grid, cipher.grid); } @@ -718,12 +715,12 @@ public class TestPolybiusSquare{ public void testPracticalDecode(){ cipher = new PolybiusSquare(true, true); - String output = cipher.decode(keyword, outputString); + String output = cipher.decode(keyword, encodedString); - assertEquals(outputString, cipher.inputString); + assertEquals(encodedString, cipher.inputString); assertEquals(keywordClean, cipher.keyword); - assertEquals(inputString.toUpperCase(), cipher.outputString); - assertEquals(inputString.toUpperCase(), output); + assertEquals(decodedString.toUpperCase(), cipher.outputString); + assertEquals(decodedString.toUpperCase(), output); assertArrayEquals(grid, cipher.grid); } @@ -731,12 +728,12 @@ public class TestPolybiusSquare{ public void testPracticalDecode_clean(){ cipher = new PolybiusSquare(false, false); - String output = cipher.decode(keyword, outputString); + String output = cipher.decode(keyword, encodedString); - assertEquals(outputString.replaceAll("\\s", "").replaceAll("[^0-9]", ""), cipher.inputString); + assertEquals(encodedString.replaceAll("\\s", "").replaceAll("[^0-9]", ""), cipher.inputString); assertEquals(keywordClean, cipher.keyword); - assertEquals(inputStringClean.replaceAll("\\s", ""), cipher.outputString); - assertEquals(inputStringClean.replaceAll("\\s", ""), output); + assertEquals(decodedStringClean.replaceAll("\\s", ""), cipher.outputString); + assertEquals(decodedStringClean.replaceAll("\\s", ""), output); assertArrayEquals(grid, cipher.grid); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestRailFence.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestRailFence.java index 1297b27..72dd0a6 100644 --- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestRailFence.java +++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestRailFence.java @@ -1,537 +1,455 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestRailFence.java //Mattrixwv // Created: 03-21-22 -//Modified: 07-09-22 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +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.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidBaseException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; +@ExtendWith(MockitoExtension.class) public class TestRailFence{ + @InjectMocks + private RailFence cipher; + @Mock + private Logger logger; + //Fields + private String decodedString = "Message to^encode"; + private String decodedStringClean = "MESSAGETOENCODE"; + private String encodedString3 = "Maooesg te^cdsene"; + private String encodedString3Clean = "MAOOESGTECDSENE"; + private String encodedString5 = "Moetese ne^sgcdao"; + private String encodedString5Clean = "MOETESENESGCDAO"; + private StringBuilder[] fence3 = new StringBuilder[]{ + new StringBuilder("Maoo"), + new StringBuilder("esgtecd"), + new StringBuilder("sene") + }; + private StringBuilder[] fence5 = new StringBuilder[]{ + new StringBuilder("Mo"), + new StringBuilder("ete"), + new StringBuilder("sene"), + new StringBuilder("sgcd"), + new StringBuilder("ao") + }; + + @Test - public void testEncode() throws InvalidBaseException, InvalidInputException{ - RailFence cipher = new RailFence(true, true, true); + public void testConstructor_default(){ + cipher = new RailFence(); - //Test lowercase encoding - String inputString = "messagetoencode"; - int numRails = 3; - String correctOutput = "maooesgtecdsene"; - String output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - numRails = 3; - correctOutput = "MAOOESGTECDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test rail length encoding - inputString = "messagetoencode"; - numRails = 5; - correctOutput = "moetesenesgcdao"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - numRails = 3; - correctOutput = "maooesg te cdsene"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - numRails = 3; - correctOutput = "maooesg*te+cdsene"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - numRails = 3; - correctOutput = "Maooesg te^cdsene"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - //Throw in rail length for good measure - inputString = "Message to^encode"; - numRails = 5; - correctOutput = "Moetese ne^sgcdao"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertNull(cipher.fence); } @Test - public void testNocapitalEncode() throws InvalidBaseException, InvalidInputException{ - RailFence cipher = new RailFence(false, true, true); + public void testConstructor_noCapitals(){ + cipher = new RailFence(false, true, true); - //Test lowercase encoding - String inputString = "messagetoencode"; - int numRails = 3; - String correctOutput = "MAOOESGTECDSENE"; - String output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - numRails = 3; - correctOutput = "MAOOESGTECDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test rail length encoding - inputString = "messagetoencode"; - numRails = 5; - correctOutput = "MOETESENESGCDAO"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - numRails = 3; - correctOutput = "MAOOESG TE CDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - numRails = 3; - correctOutput = "MAOOESG*TE+CDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - numRails = 3; - correctOutput = "MAOOESG TE^CDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - //Throw in rail length for good measure - inputString = "Message to^encode"; - numRails = 5; - correctOutput = "MOETESE NE^SGCDAO"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveWhitespace); + assertTrue(cipher.preserveSymbols); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertNull(cipher.fence); } @Test - public void testNoWhitespaceEncode() throws InvalidBaseException, InvalidInputException{ - RailFence cipher = new RailFence(true, false, true); + public void testConstructor_noWhitespace(){ + cipher = new RailFence(true, false, true); - //Test lowercase encoding - String inputString = "messagetoencode"; - int numRails = 3; - String correctOutput = "maooesgtecdsene"; - String output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - numRails = 3; - correctOutput = "MAOOESGTECDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test rail length encoding - inputString = "messagetoencode"; - numRails = 5; - correctOutput = "moetesenesgcdao"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - numRails = 3; - correctOutput = "maooesgtecdsene"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - numRails = 3; - correctOutput = "maooesg*te+cdsene"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - numRails = 3; - correctOutput = "Maooesgte^cdsene"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - //Throw in rail length for good measure - inputString = "Message to^encode"; - numRails = 5; - correctOutput = "Moetesene^sgcdao"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); + assertTrue(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertTrue(cipher.preserveSymbols); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertNull(cipher.fence); } @Test - public void testNoSymbolEncode() throws InvalidBaseException, InvalidInputException{ - RailFence cipher = new RailFence(true, true, false); + public void testConstructor_noSymbols(){ + cipher = new RailFence(true, true, false); - //Test lowercase encoding - String inputString = "messagetoencode"; - int numRails = 3; - String correctOutput = "maooesgtecdsene"; - String output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - numRails = 3; - correctOutput = "MAOOESGTECDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test rail length encoding - inputString = "messagetoencode"; - numRails = 5; - correctOutput = "moetesenesgcdao"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - numRails = 3; - correctOutput = "maooesg te cdsene"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - numRails = 3; - correctOutput = "maooesgtecdsene"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - numRails = 3; - correctOutput = "Maooesg tecdsene"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - //Throw in rail length for good measure - inputString = "Message to^encode"; - numRails = 5; - correctOutput = "Moetese nesgcdao"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); + assertTrue(cipher.preserveCapitals); + assertTrue(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertNull(cipher.fence); } @Test - public void testNoCapitalWhitespaceSymbolEncode() throws InvalidBaseException, InvalidInputException{ - RailFence cipher = new RailFence(false, false, false); + public void testSetInputString(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; - //Test lowercase encoding - String inputString = "messagetoencode"; - int numRails = 3; - String correctOutput = "MAOOESGTECDSENE"; - String output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - numRails = 3; - correctOutput = "MAOOESGTECDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); + cipher.setInputString(decodedString); - //Test rail length encoding - inputString = "messagetoencode"; - numRails = 5; - correctOutput = "MOETESENESGCDAO"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - numRails = 3; - correctOutput = "MAOOESGTECDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to+encode"; - numRails = 3; - correctOutput = "MAOOESGTECDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - numRails = 3; - correctOutput = "MAOOESGTECDSENE"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - //Throw in rail length for good measure - inputString = "Message to^encode"; - numRails = 5; - correctOutput = "MOETESENESGCDAO"; - output = cipher.encode(numRails, inputString); - assertEquals(correctOutput, output); - } - - - @Test - public void testDecode() throws InvalidBaseException, InvalidInputException{ - RailFence cipher = new RailFence(true, true, true); - - //Test lowercase decoding - String inputString = "maooesgtecdsene"; - int numRails = 3; - String correctOutput = "messagetoencode"; - String output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "MAOOESGTECDSENE"; - numRails = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test rail length decoding - inputString = "moetesenesgcdao"; - numRails = 5; - correctOutput = "messagetoencode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "maooesg te cdsene"; - numRails = 3; - correctOutput = "message to encode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "maooesg*te+cdsene"; - numRails = 3; - correctOutput = "message*to+encode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Maooesg te^cdsene"; - numRails = 3; - correctOutput = "Message to^encode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - //Throw in rail length for good measure - inputString = "Moetese ne^sgcdao"; - numRails = 5; - correctOutput = "Message to^encode"; - output = cipher.decode(numRails, 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("Clean input string '{}'", decodedString); } @Test - public void testNoCapitalDecode() throws InvalidBaseException, InvalidInputException{ - RailFence cipher = new RailFence(false, true, true); + public void testSetInputString_noCapitals(){ + cipher.preserveCapitals = false; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; - //Test lowercase decoding - String inputString = "maooesgtecdsene"; - int numRails = 3; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "MAOOESGTECDSENE"; - numRails = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); + cipher.setInputString(decodedString); - //Test rail length decoding - inputString = "moetesenesgcdao"; - numRails = 5; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "maooesg te cdsene"; - numRails = 3; - correctOutput = "MESSAGE TO ENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "maooesg*te+cdsene"; - numRails = 3; - correctOutput = "MESSAGE*TO+ENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Maooesg te^cdsene"; - numRails = 3; - correctOutput = "MESSAGE TO^ENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - //Throw in rail length for good measure - inputString = "Moetese ne^sgcdao"; - numRails = 5; - correctOutput = "MESSAGE TO^ENCODE"; - output = cipher.decode(numRails, 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, never()).debug("Removing whitespace"); + verify(logger, never()).debug("Removing symbols"); + verify(logger, times(1)).debug("Clean input string '{}'", decodedString.toUpperCase()); } @Test - public void testNoWhitespaceDecode() throws InvalidBaseException, InvalidInputException{ - RailFence cipher = new RailFence(true, false, true); + public void testSetInputString_noWhitespace(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = false; + cipher.preserveSymbols = true; - //Test lowercase decoding - String inputString = "maooesgtecdsene"; - int numRails = 3; - String correctOutput = "messagetoencode"; - String output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "MAOOESGTECDSENE"; - numRails = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); + cipher.setInputString(decodedString); - //Test rail length decoding - inputString = "moetesenesgcdao"; - numRails = 5; - correctOutput = "messagetoencode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "maooesg te cdsene"; - numRails = 3; - correctOutput = "messagetoencode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "maooesg*te+cdsene"; - numRails = 3; - correctOutput = "message*to+encode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Maooesg te^cdsene"; - numRails = 3; - correctOutput = "Messageto^encode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - //Throw in rail length for good measure - inputString = "Moetese ne^sgcdao"; - numRails = 5; - correctOutput = "Messageto^encode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); + 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("Clean input string '{}'", decodedString.replaceAll("\\s", "")); } @Test - public void testNoSymbolDecode() throws InvalidBaseException, InvalidInputException{ - RailFence cipher = new RailFence(true, true, false); + public void testSetInputString_noSymbols(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = false; - //Test lowercase decoding - String inputString = "maooesgtecdsene"; - int numRails = 3; - String correctOutput = "messagetoencode"; - String output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "MAOOESGTECDSENE"; - numRails = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); + cipher.setInputString(decodedString); - //Test rail length decoding - inputString = "moetesenesgcdao"; - numRails = 5; - correctOutput = "messagetoencode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "maooesg te cdsene"; - numRails = 3; - correctOutput = "message to encode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "maooesg*te+cdsene"; - numRails = 3; - correctOutput = "messagetoencode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Maooesg te^cdsene"; - numRails = 3; - correctOutput = "Message toencode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - //Throw in rail length for good measure - inputString = "Moetese ne^sgcdao"; - numRails = 5; - correctOutput = "Message toencode"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); + 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("Clean input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", "")); } @Test - public void testNoCapitalWhitespaceSymbolDecode() throws InvalidBaseException, InvalidInputException{ - RailFence cipher = new RailFence(false, false, false); + public void testSetInputString_blank(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; - //Test lowercase decoding - String inputString = "maooesgtecdsene"; - int numRails = 3; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "MAOOESGTECDSENE"; - numRails = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(""); + }); - //Test rail length decoding - inputString = "moetesenesgcdao"; - numRails = 5; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); + 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("Clean input string '{}'", ""); + } - //Test whitespace decoding - inputString = "maooesg te cdsene"; - numRails = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); + @Test + public void testSetInputString_null(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; - //Test symbol decoding - inputString = "maooesg*te+cdsene"; - numRails = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(null); + }); - //Test mixed case, whitespace, symbol decoding - inputString = "Maooesg te^cdsene"; - numRails = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); - //Throw in rail length for good measure - inputString = "Moetese ne^sgcdao"; - numRails = 5; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(numRails, inputString); - assertEquals(correctOutput, output); + 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("Clean input string '{}'"), anyString()); + } + + @Test + public void testSetNumRails(){ + cipher.setNumRails(3); + + assertEquals(3, cipher.fence.length); + verify(logger, times(1)).debug("Creating {} rails", 3); + } + + @Test + public void testSetNumRails_short(){ + assertThrows(InvalidBaseException.class, () -> { + cipher.setNumRails(1); + }); + + assertNull(cipher.fence); + verify(logger, never()).debug(eq("Creating {} rails"), anyInt()); + } + + @Test + public void testGetCleanInputString(){ + cipher.inputString = decodedString; + + String output = cipher.getCleanInputString(); + + assertEquals(decodedString.replaceAll("[^a-zA-Z]", ""), output); + verify(logger, times(1)).debug("Getting input string for encoding"); + } + + @Test + public void testFormatOutput(){ + cipher.inputString = decodedString; + + cipher.formatOutput(encodedString3Clean); + + assertEquals(encodedString3, cipher.outputString); + verify(logger, times(1)).debug("Formatting output string"); + verify(logger, times(17)).debug(eq("Working character {}"), anyChar()); + verify(logger, times(1)).debug("Formatting uppercase"); + verify(logger, times(14)).debug("Formatting lowercase"); + verify(logger, times(2)).debug("Inserting symbol"); + verify(logger, times(1)).debug("Formatted output '{}'", encodedString3); + } + + @Test + public void testGetDecodedStringFromFence(){ + cipher.fence = fence3; + + String output = cipher.getDecodedStringFromFence(); + + assertEquals(decodedString.replaceAll("[^a-zA-Z]", ""), output); + verify(logger, times(1)).debug("Getting decoded string from the fence"); + verify(logger, times(1)).debug(eq("Fence output '{}'"), any(StringBuilder.class)); + } + + @Test + public void testEncode(){ + cipher.inputString = decodedString; + cipher.fence = new StringBuilder[]{ + new StringBuilder(), + new StringBuilder(), + new StringBuilder() + }; + + cipher.encode(); + + assertEquals(encodedString3, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(15)).debug(eq("Working character '{}'"), anyChar()); + verify(logger, times(9)).debug("Moving up"); + verify(logger, times(6)).debug("Moving down"); + verify(logger, times(4)).debug("Swapping to down"); + verify(logger, times(3)).debug("Swapping to up"); + verify(logger, times(1)).debug("Appending rows from the fence"); + } + + @Test + public void testDecode(){ + cipher.inputString = encodedString3; + cipher.fence = new StringBuilder[]{ + new StringBuilder(), + new StringBuilder(), + new StringBuilder() + }; + + cipher.decode(); + + assertEquals(decodedString, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(1)).debug("Number of characters in the top rail {}", 4); + verify(logger, times(1)).debug("Number of characters in the middle rails {}", 8); + verify(logger, times(1)).debug("Number of characters in the bottom rail {}", 4); + verify(logger, times(1)).debug("Adding characters to the rails"); + verify(logger, times(1)).debug("Appending the bottom rail"); + verify(logger, times(1)).debug("Fence output '{}'", decodedString.replaceAll("[^a-zA-Z]", "")); + } + + @Test + public void testGetters(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString3; + cipher.fence = fence3; + + assertEquals(decodedString, cipher.getInputString()); + assertEquals(encodedString3, cipher.getOutputString()); + assertEquals(fence3.length, cipher.getNumRails()); + } + + @Test + public void testReset(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString3; + cipher.fence = fence3; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertNull(cipher.fence); + } + + @Test + public void testPracticalEncoding_3(){ + cipher = new RailFence(true, true, true); + + String output = cipher.encode(3, decodedString); + + assertEquals(decodedString, cipher.inputString); + assertEquals(fence3.length, cipher.fence.length); + assertEquals(fence3[0].toString(), cipher.fence[0].toString()); + assertEquals(fence3[1].toString(), cipher.fence[1].toString()); + assertEquals(fence3[2].toString(), cipher.fence[2].toString()); + assertEquals(encodedString3, cipher.outputString); + assertEquals(encodedString3, output); + } + + @Test + public void testPracticalEncoding_3Clean(){ + cipher = new RailFence(false, false, false); + + String output = cipher.encode(3, decodedString); + + assertEquals(decodedStringClean, cipher.inputString); + assertEquals(fence3.length, cipher.fence.length); + assertEquals(fence3[0].toString().toUpperCase(), cipher.fence[0].toString()); + assertEquals(fence3[1].toString().toUpperCase(), cipher.fence[1].toString()); + assertEquals(fence3[2].toString().toUpperCase(), cipher.fence[2].toString()); + assertEquals(encodedString3Clean, cipher.outputString); + assertEquals(encodedString3Clean, output); + } + + @Test + public void testPracticalEncoding_5(){ + cipher = new RailFence(true, true, true); + + String output = cipher.encode(5, decodedString); + + assertEquals(decodedString, cipher.inputString); + assertEquals(fence5.length, cipher.fence.length); + assertEquals(fence5[0].toString(), cipher.fence[0].toString()); + assertEquals(fence5[1].toString(), cipher.fence[1].toString()); + assertEquals(fence5[2].toString(), cipher.fence[2].toString()); + assertEquals(fence5[3].toString(), cipher.fence[3].toString()); + assertEquals(fence5[4].toString(), cipher.fence[4].toString()); + assertEquals(encodedString5, cipher.outputString); + assertEquals(encodedString5, output); + } + + @Test + public void testPracticalEncoding_5Clean(){ + cipher = new RailFence(false, false, false); + + String output = cipher.encode(5, decodedString); + + assertEquals(decodedStringClean, cipher.inputString); + assertEquals(fence5.length, cipher.fence.length); + assertEquals(fence5[0].toString().toUpperCase(), cipher.fence[0].toString()); + assertEquals(fence5[1].toString().toUpperCase(), cipher.fence[1].toString()); + assertEquals(fence5[2].toString().toUpperCase(), cipher.fence[2].toString()); + assertEquals(fence5[3].toString().toUpperCase(), cipher.fence[3].toString()); + assertEquals(fence5[4].toString().toUpperCase(), cipher.fence[4].toString()); + assertEquals(encodedString5Clean, cipher.outputString); + assertEquals(encodedString5Clean, output); + } + + @Test + public void testPracticalDecoding_3(){ + cipher = new RailFence(true, true, true); + + String output = cipher.decode(3, encodedString3); + + assertEquals(encodedString3, cipher.inputString); + assertEquals(fence3.length, cipher.fence.length); + assertEquals(fence3[0].toString(), cipher.fence[0].toString()); + assertEquals(fence3[1].toString(), cipher.fence[1].toString()); + assertEquals(fence3[2].toString(), cipher.fence[2].toString()); + assertEquals(decodedString, cipher.outputString); + assertEquals(decodedString, output); + } + + @Test + public void testPracticalDecoding_3Clean(){ + cipher = new RailFence(false, false, false); + + String output = cipher.decode(3, encodedString3); + + assertEquals(encodedString3Clean, cipher.inputString); + assertEquals(fence3.length, cipher.fence.length); + assertEquals(fence3[0].toString().toUpperCase(), cipher.fence[0].toString()); + assertEquals(fence3[1].toString().toUpperCase(), cipher.fence[1].toString()); + assertEquals(fence3[2].toString().toUpperCase(), cipher.fence[2].toString()); + assertEquals(decodedStringClean, cipher.outputString); + assertEquals(decodedStringClean, output); + } + + @Test + public void testPracticalDecoding_5(){ + cipher = new RailFence(true, true, true); + + String output = cipher.decode(5, encodedString5); + + assertEquals(encodedString5, cipher.inputString); + assertEquals(fence5.length, cipher.fence.length); + assertEquals(fence5[0].toString(), cipher.fence[0].toString()); + assertEquals(fence5[1].toString(), cipher.fence[1].toString()); + assertEquals(fence5[2].toString(), cipher.fence[2].toString()); + assertEquals(fence5[3].toString(), cipher.fence[3].toString()); + assertEquals(fence5[4].toString(), cipher.fence[4].toString()); + assertEquals(decodedString, cipher.outputString); + assertEquals(decodedString, output); + } + + @Test + public void testPracticalDecoding_5Clean(){ + cipher = new RailFence(false, false, false); + + String output = cipher.decode(5, encodedString5); + + assertEquals(encodedString5Clean, cipher.inputString); + assertEquals(fence5.length, cipher.fence.length); + assertEquals(fence5[0].toString().toUpperCase(), cipher.fence[0].toString()); + assertEquals(fence5[1].toString().toUpperCase(), cipher.fence[1].toString()); + assertEquals(fence5[2].toString().toUpperCase(), cipher.fence[2].toString()); + assertEquals(fence5[3].toString().toUpperCase(), cipher.fence[3].toString()); + assertEquals(fence5[4].toString().toUpperCase(), cipher.fence[4].toString()); + assertEquals(decodedStringClean, cipher.outputString); + assertEquals(decodedStringClean, output); } } diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestTrifid.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestTrifid.java index ba0f273..f423aa2 100644 --- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestTrifid.java +++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestTrifid.java @@ -1,559 +1,806 @@ //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestTrifid.java //Mattrixwv // Created: 03-03-22 -//Modified: 07-09-22 +//Modified: 05-04-23 package com.mattrixwv.cipherstream.polysubstitution; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; 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.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; import com.mattrixwv.cipherstream.exceptions.InvalidBaseException; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; +import com.mattrixwv.cipherstream.polysubstitution.Trifid.CharLocation; +@ExtendWith(MockitoExtension.class) public class TestTrifid{ + @InjectMocks + private Trifid cipher; + @Mock + private Logger logger; + //Fields + private String decodedString = "Message to^encode+"; + private String decodedStringClean = "MESSAGETOENCODE+"; + private String decodedStringCleanAlt = "MESSAGETOENCODE"; + private String encodedString = "Gqdokxy eg^ranmoqr"; + private String encodedStringAlt = "Gqdokpd od^ljvflf+"; + private String encodedString3 = "Gpjqdvd of^odlklf+"; + private String encodedStringClean = "GQDOKXYEGRANMOQR"; + private String encodedStringCleanAlt = "GQDOKPDODLJVFLF"; + private String encodedStringClean3 = "GPJQDVDOFODLKLF+"; + private String keyword = "ke yw*ord"; + private String keywordClean = "KEYWORDABCFGHIJLMNPQSTUVXZ+"; + private String keywordCleanAlt = "KEYWORDABCFGHIJLMNPQSTUVXZ="; + private char[][][] grid = new char[][][]{ + { + {'K', 'E', 'Y'}, + {'W', 'O', 'R'}, + {'D', 'A', 'B'} + }, + { + {'C', 'F', 'G'}, + {'H', 'I', 'J'}, + {'L', 'M', 'N'} + }, + { + {'P', 'Q', 'S'}, + {'T', 'U', 'V'}, + {'X', 'Z', '+'} + } + }; + private String gridString = "[K E Y]\n[W O R]\n[D A B]\n\n[C F G]\n[H I J]\n[L M N]\n\n[P Q S]\n[T U V]\n[X Z +]"; + private char[][][] gridAlt = new char[][][]{ + { + {'K', 'E', 'Y'}, + {'W', 'O', 'R'}, + {'D', 'A', 'B'} + }, + { + {'C', 'F', 'G'}, + {'H', 'I', 'J'}, + {'L', 'M', 'N'} + }, + { + {'P', 'Q', 'S'}, + {'T', 'U', 'V'}, + {'X', 'Z', '='} + } + }; + + @Test - public void testEncode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{ - Trifid cipher = new Trifid(true, true, true, '+'); + public void testConstructor_default(){ + cipher = new Trifid(); - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "gqdokpdodljvflf"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "GQDOKPDODLJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test groupLength encoding - inputString = "messagetoencode"; - keyword = "keyword"; - int groupLength = 3; - correctOutput = "gpjqdvdofodlklf"; - output = cipher.encode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "gqdokpd od ljvflf"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to-encode"; - keyword = "keyword"; - correctOutput = "gqdokpd*od-ljvflf"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "Gqdokpd od^ljvflf"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Throw in groupLength for good measure - inputString = "Message to^encode"; - keyword = "keyword"; - groupLength = 3; - correctOutput = "Gpjqdvd of^odlklf"; - output = cipher.encode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); + assertFalse(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals('+', cipher.fillIn); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(Integer.MAX_VALUE, cipher.groupSize); + assertArrayEquals(new char[3][3][3], cipher.grid); } @Test - public void testNoCapitalEncode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{ - Trifid cipher = new Trifid(false, true, true, '+'); + public void testconstructor_noCapitals(){ + cipher = new Trifid(false, true, true); - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "GQDOKPDODLJVFLF"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "GQDOKPDODLJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test groupLength encoding - inputString = "messagetoencode"; - keyword = "keyword"; - int groupLength = 3; - correctOutput = "GPJQDVDOFODLKLF"; - output = cipher.encode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "GQDOKPD OD LJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to-encode"; - keyword = "keyword"; - correctOutput = "GQDOKPD*OD-LJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "GQDOKPD OD^LJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Throw in groupLength for good measure - inputString = "Message to^encode"; - keyword = "keyword"; - groupLength = 3; - correctOutput = "GPJQDVD OF^ODLKLF"; - output = cipher.encode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveWhitespace); + assertTrue(cipher.preserveSymbols); + assertEquals('+', cipher.fillIn); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(Integer.MAX_VALUE, cipher.groupSize); + assertArrayEquals(new char[3][3][3], cipher.grid); } @Test - public void testNoWhitespaceEncode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{ - Trifid cipher = new Trifid(true, false, true, '+'); + public void testconstructor_noWhitespace(){ + cipher = new Trifid(true, false, true); - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "gqdokpdodljvflf"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "GQDOKPDODLJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test groupLength encoding - inputString = "messagetoencode"; - keyword = "keyword"; - int groupLength = 3; - correctOutput = "gpjqdvdofodlklf"; - output = cipher.encode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "gqdokpdodljvflf"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to-encode"; - keyword = "keyword"; - correctOutput = "gqdokpd*od-ljvflf"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "Gqdokpdod^ljvflf"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Throw in groupLength for good measure - inputString = "Message to^encode"; - keyword = "keyword"; - groupLength = 3; - correctOutput = "Gpjqdvdof^odlklf"; - output = cipher.encode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); + assertTrue(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertTrue(cipher.preserveSymbols); + assertEquals('+', cipher.fillIn); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(Integer.MAX_VALUE, cipher.groupSize); + assertArrayEquals(new char[3][3][3], cipher.grid); } @Test - public void testNoSymbolEncode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{ - Trifid cipher = new Trifid(true, true, false, '+'); + public void testconstructor_noSymbols(){ + cipher = new Trifid(true, true, false); - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "gqdokpdodljvflf"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "GQDOKPDODLJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test groupLength encoding - inputString = "messagetoencode"; - keyword = "keyword"; - int groupLength = 3; - correctOutput = "gpjqdvdofodlklf"; - output = cipher.encode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "gqdokpd od ljvflf"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to-encode"; - keyword = "keyword"; - correctOutput = "gqdokpdodljvflf"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "Gqdokpd odljvflf"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Throw in groupLength for good measure - inputString = "Message to^encode"; - keyword = "keyword"; - groupLength = 3; - correctOutput = "Gpjqdvd ofodlklf"; - output = cipher.encode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); + assertTrue(cipher.preserveCapitals); + assertTrue(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals('+', cipher.fillIn); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(Integer.MAX_VALUE, cipher.groupSize); + assertArrayEquals(new char[3][3][3], cipher.grid); } @Test - public void testNoCapitalWhitespaceSymbolEncode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{ - Trifid cipher = new Trifid(false, false, false, '+'); + public void testconstructor_fillNoCapitals(){ + cipher = new Trifid(false, true, true, '='); - //Test lowercase encoding - String inputString = "messagetoencode"; - String keyword = "keyword"; - String correctOutput = "GQDOKPDODLJVFLF"; - String output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase encoding - inputString = "MESSAGETOENCODE"; - keyword = "keyword"; - correctOutput = "GQDOKPDODLJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test groupLength encoding - inputString = "messagetoencode"; - keyword = "keyword"; - int groupLength = 3; - correctOutput = "GPJQDVDOFODLKLF"; - output = cipher.encode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); - - //Test whitespace encoding - inputString = "message to encode"; - keyword = "keyword"; - correctOutput = "GQDOKPDODLJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol encoding - inputString = "message*to-encode"; - keyword = "keyword"; - correctOutput = "GQDOKPDODLJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol encoding - inputString = "Message to^encode"; - keyword = "keyword"; - correctOutput = "GQDOKPDODLJVFLF"; - output = cipher.encode(keyword, inputString); - assertEquals(correctOutput, output); - //Throw in groupLength for good measure - inputString = "Message to^encode"; - keyword = "keyword"; - groupLength = 3; - correctOutput = "GPJQDVDOFODLKLF"; - output = cipher.encode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); - } - - - @Test - public void testDecode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{ - Trifid cipher = new Trifid(true, true, true, '+'); - - //Test lowercase decoding - String inputString = "gqdokpdodljvflf"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "GQDOKPDODLJVFLF"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test groupLength decoding - inputString = "gpjqdvdofodlklf"; - keyword = "keyword"; - int groupLength = 3; - correctOutput = "messagetoencode"; - output = cipher.decode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "gqdokpd od ljvflf"; - keyword = "keyword"; - correctOutput = "message to encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "gqdokpd*od-ljvflf"; - keyword = "keyword"; - correctOutput = "message*to-encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Gqdokpd od^ljvflf"; - keyword = "keyword"; - correctOutput = "Message to^encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Throw in groupLength for good measure - inputString = "Gpjqdvd of^odlklf"; - keyword = "keyword"; - groupLength = 3; - correctOutput = "Message to^encode"; - output = cipher.decode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); + assertFalse(cipher.preserveCapitals); + assertTrue(cipher.preserveWhitespace); + assertTrue(cipher.preserveSymbols); + assertEquals('=', cipher.fillIn); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(Integer.MAX_VALUE, cipher.groupSize); + assertArrayEquals(new char[3][3][3], cipher.grid); } @Test - public void testNoCapitalDecode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{ - Trifid cipher = new Trifid(false, true, true, '+'); + public void testconstructor_fillNoWhitespace(){ + cipher = new Trifid(true, false, true, '='); - //Test lowercase decoding - String inputString = "gqdokpdodljvflf"; - String keyword = "keyword"; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "GQDOKPDODLJVFLF"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test groupLength decoding - inputString = "gpjqdvdofodlklf"; - keyword = "keyword"; - int groupLength = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "gqdokpd od ljvflf"; - keyword = "keyword"; - correctOutput = "MESSAGE TO ENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "gqdokpd*od-ljvflf"; - keyword = "keyword"; - correctOutput = "MESSAGE*TO-ENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Gqdokpd od^ljvflf"; - keyword = "keyword"; - correctOutput = "MESSAGE TO^ENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Throw in groupLength for good measure - inputString = "Gpjqdvd of^odlklf"; - keyword = "keyword"; - groupLength = 3; - correctOutput = "MESSAGE TO^ENCODE"; - output = cipher.decode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); + assertTrue(cipher.preserveCapitals); + assertFalse(cipher.preserveWhitespace); + assertTrue(cipher.preserveSymbols); + assertEquals('=', cipher.fillIn); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(Integer.MAX_VALUE, cipher.groupSize); + assertArrayEquals(new char[3][3][3], cipher.grid); } @Test - public void testNoWhitespaceDecode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{ - Trifid cipher = new Trifid(true, false, true, '+'); + public void testconstructor_fillNoSymbols(){ + cipher = new Trifid(true, true, false, '='); - //Test lowercase decoding - String inputString = "gqdokpdodljvflf"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "GQDOKPDODLJVFLF"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test groupLength decoding - inputString = "gpjqdvdofodlklf"; - keyword = "keyword"; - int groupLength = 3; - correctOutput = "messagetoencode"; - output = cipher.decode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "gqdokpd od ljvflf"; - keyword = "keyword"; - correctOutput = "messagetoencode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "gqdokpd*od-ljvflf"; - keyword = "keyword"; - correctOutput = "message*to-encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Gqdokpd od^ljvflf"; - keyword = "keyword"; - correctOutput = "Messageto^encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Throw in groupLength for good measure - inputString = "Gpjqdvd of^odlklf"; - keyword = "keyword"; - groupLength = 3; - correctOutput = "Messageto^encode"; - output = cipher.decode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); + assertTrue(cipher.preserveCapitals); + assertTrue(cipher.preserveWhitespace); + assertFalse(cipher.preserveSymbols); + assertEquals('=', cipher.fillIn); + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(Integer.MAX_VALUE, cipher.groupSize); + assertArrayEquals(new char[3][3][3], cipher.grid); } @Test - public void testNoSymbolDeocde() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{ - Trifid cipher = new Trifid(true, true, false, '+'); + public void testSetFillIn(){ + cipher.setFillIn('='); - //Test lowercase decoding - String inputString = "gqdokpdodljvflf"; - String keyword = "keyword"; - String correctOutput = "messagetoencode"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "GQDOKPDODLJVFLF"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test groupLength decoding - inputString = "gpjqdvdofodlklf"; - keyword = "keyword"; - int groupLength = 3; - correctOutput = "messagetoencode"; - output = cipher.decode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); - - //Test whitespace decoding - inputString = "gqdokpd od ljvflf"; - keyword = "keyword"; - correctOutput = "message to encode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test symbol decoding - inputString = "gqdokpd*od-ljvflf"; - keyword = "keyword"; - correctOutput = "messagetoencode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - - //Test mixed case, whitespace, symbol decoding - inputString = "Gqdokpd od^ljvflf"; - keyword = "keyword"; - correctOutput = "Message toencode"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Throw in groupLength for good measure - inputString = "Gpjqdvd of^odlklf"; - keyword = "keyword"; - groupLength = 3; - correctOutput = "Message toencode"; - output = cipher.decode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); + assertEquals('=', cipher.fillIn); + verify(logger, times(1)).debug("Setting fill in {}", '='); } @Test - public void testNoCapitalWhitespaceSymbolDecode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{ - Trifid cipher = new Trifid(false, false, false, '+'); + public void testSetFillIn_nonPrinting(){ + assertThrows(InvalidCharacterException.class, () -> { + cipher.setFillIn('\0'); + }); - //Test lowercase decoding - String inputString = "gqdokpdodljvflf"; - String keyword = "keyword"; - String correctOutput = "MESSAGETOENCODE"; - String output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Test uppercase decoding - inputString = "GQDOKPDODLJVFLF"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals('+', cipher.fillIn); + verify(logger, never()).debug(eq("Setting fill in {}"), anyChar()); + } - //Test groupLength decoding - inputString = "gpjqdvdofodlklf"; - keyword = "keyword"; - int groupLength = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); + @Test + public void testSetFillIn_letter(){ + assertThrows(InvalidCharacterException.class, () -> { + cipher.setFillIn('a'); + }); - //Test whitespace decoding - inputString = "gqdokpd od ljvflf"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + assertEquals('+', cipher.fillIn); + verify(logger, never()).debug(eq("Setting fill in {}"), anyChar()); + } - //Test symbol decoding - inputString = "gqdokpd*od-ljvflf"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); + @Test + public void testSetKeyword(){ + cipher.setKeyword(keyword); - //Test mixed case, whitespace, symbol decoding - inputString = "Gqdokpd od^ljvflf"; - keyword = "keyword"; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, inputString); - assertEquals(correctOutput, output); - //Throw in groupLength for good measure - inputString = "Gpjqdvd of^odlklf"; - keyword = "keyword"; - groupLength = 3; - correctOutput = "MESSAGETOENCODE"; - output = cipher.decode(keyword, groupLength, inputString); - assertEquals(correctOutput, output); + assertEquals(keywordClean, cipher.keyword); + assertArrayEquals(grid, cipher.grid); + verify(logger, times(1)).debug("Original keyword {}", keyword); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Removing all invalid characters"); + verify(logger, times(1)).debug("Appending entire alphabet to keyword"); + verify(logger, times(1)).debug("Removing duplicate characters"); + verify(logger, times(1)).debug("Cleaned keyword {}", keywordClean); + } + + @Test + public void testSetKeyword_blank(){ + char[][][] blankGrid = new char[][][]{ + { + {'A', 'B', 'C'}, + {'D', 'E', 'F'}, + {'G', 'H', 'I'} + }, + { + {'J', 'K', 'L'}, + {'M', 'N', 'O'}, + {'P', 'Q', 'R'} + }, + { + {'S', 'T', 'U'}, + {'V', 'W', 'X'}, + {'Y', 'Z', '+'} + } + }; + cipher.setKeyword(""); + + assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ+", cipher.keyword); + assertArrayEquals(blankGrid, cipher.grid); + verify(logger, times(1)).debug("Original keyword {}", ""); + verify(logger, times(1)).debug("Removing case"); + verify(logger, times(1)).debug("Removing all invalid characters"); + verify(logger, times(1)).debug("Appending entire alphabet to keyword"); + verify(logger, times(1)).debug("Removing duplicate characters"); + verify(logger, times(1)).debug("Cleaned keyword {}", "ABCDEFGHIJKLMNOPQRSTUVWXYZ+"); + } + + @Test + public void testSetKeyword_null(){ + assertThrows(InvalidKeywordException.class, () -> { + cipher.setKeyword(null); + }); + + assertEquals("", cipher.keyword); + assertArrayEquals(new char[3][3][3], cipher.grid); + verify(logger, never()).debug(eq("Original keyword {}"), anyString()); + verify(logger, never()).debug("Removing case"); + verify(logger, never()).debug("Removing all invalid characters"); + verify(logger, never()).debug("Appending entire alphabet to keyword"); + verify(logger, never()).debug("Removing duplicate characters"); + verify(logger, never()).debug(eq("Cleaned keyword {}"), anyString()); + } + + @Test + public void setCreateGrid(){ + cipher.keyword = keywordClean; + + cipher.createGrid(); + + assertArrayEquals(grid, cipher.grid); + verify(logger, times(1)).debug("Creating grid from keyword"); + verify(logger, times(1)).debug("Completed grid\n{}", gridString); + } + + @Test + public void testSetGroupSize(){ + cipher.setGroupSize(1); + + assertEquals(1, cipher.groupSize); + verify(logger, times(1)).debug("Setting group size"); + } + + @Test + public void testSetGroupSize_negative(){ + assertThrows(InvalidBaseException.class, () -> { + cipher.setGroupSize(-1); + }); + + assertEquals(Integer.MAX_VALUE, cipher.groupSize); + verify(logger, never()).debug("Setting group size"); + } + + @Test + public void testSetInputString(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + + cipher.setInputString(decodedString); + + 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); + } + + @Test + public void testSetInputString_noCapitals(){ + cipher.preserveCapitals = false; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + + 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()); + } + + @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", "")); + } + + @Test + public void testSetInputString_noWhitespaceFill(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = false; + cipher.preserveSymbols = true; + cipher.fillIn = ' '; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(decodedString); + }); + + assertEquals("", 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, never()).debug(eq("Cleaned input string '{}'"), 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+]", "")); + } + + @Test + public void testSetInputString_blank(){ + cipher.preserveCapitals = true; + cipher.preserveWhitespace = true; + cipher.preserveSymbols = true; + + assertThrows(InvalidInputException.class, () -> { + cipher.setInputString(""); + }); + + 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); + }); + + 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()); + } + + @Test + public void testGetCleanInputString(){ + cipher.inputString = decodedString; + + String returnedInput = cipher.getCleanInputString(); + + assertEquals(decodedStringClean, returnedInput); + verify(logger, times(1)).debug("Cleaning input string for encoding"); + } + + @Test + public void testFindChar(){ + cipher.grid = grid; + + CharLocation returnedLocation = cipher.findChar('G'); + + assertEquals(cipher.new CharLocation(0, 2, 1).toString(), returnedLocation.toString()); + verify(logger, times(1)).debug("Finding character {} in grid", 'G'); + verify(logger, times(1)).debug("Found at {} {} {}", 1, 0, 2); + } + + @Test + public void testFindChar_invalid(){ + cipher.grid = grid; + + assertThrows(InvalidCharacterException.class, () -> { + cipher.findChar('='); + }); + + verify(logger, times(1)).debug("Finding character {} in grid", '='); + verify(logger, never()).debug(eq("Found at {} {} {}"), anyInt(), anyInt(), anyInt()); + } + + @Test + public void testGetChar(){ + cipher.grid = grid; + + char returnedChar = cipher.getChar(cipher.new CharLocation(0, 1, 2)); + + assertEquals('Q', returnedChar); + verify(logger, times(1)).debug("Getting character at {} {} {}", 2, 0, 1); + } + + @Test + public void testGetChar_largeX(){ + cipher.grid = grid; + + assertThrows(InvalidCharacterException.class, () -> { + cipher.getChar(cipher.new CharLocation(3, 1, 2)); + }); + + verify(logger, never()).debug(eq("Getting character at {} {} {}"), anyInt(), anyInt(), anyInt()); + } + + @Test + public void testGetChar_largeY(){ + cipher.grid = grid; + + assertThrows(InvalidCharacterException.class, () -> { + cipher.getChar(cipher.new CharLocation(0, 3, 2)); + }); + + verify(logger, never()).debug(eq("Getting character at {} {} {}"), anyInt(), anyInt(), anyInt()); + } + + @Test + public void testGetChar_largeZ(){ + cipher.grid = grid; + + assertThrows(InvalidCharacterException.class, () -> { + cipher.getChar(cipher.new CharLocation(0, 1, 3)); + }); + + verify(logger, never()).debug(eq("Getting character at {} {} {}"), anyInt(), anyInt(), anyInt()); + } + + @Test + public void testFormatOutput(){ + cipher.preserveCapitals = true; + cipher.inputString = decodedString; + + cipher.formatOutput(encodedStringClean); + + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Formatting output"); + verify(logger, times(18)).debug(eq("Working character {}"), anyChar()); + verify(logger, times(1)).debug("Formatting uppercase"); + verify(logger, times(14)).debug("Formatting lowercase"); + verify(logger, times(1)).debug("Adding fillIn"); + verify(logger, times(2)).debug("Appending symbol"); + verify(logger, times(1)).debug("Formatted output '{}'", encodedString); + } + + @Test + public void testFormatOutput_noCapitals(){ + cipher.preserveCapitals = false; + cipher.inputString = decodedString.toUpperCase(); + + cipher.formatOutput(encodedStringClean); + + assertEquals(encodedString.toUpperCase(), cipher.outputString); + verify(logger, times(1)).debug("Formatting output"); + verify(logger, times(18)).debug(eq("Working character {}"), anyChar()); + verify(logger, times(15)).debug("Formatting uppercase"); + verify(logger, never()).debug("Formatting lowercase"); + verify(logger, times(1)).debug("Adding fillIn"); + verify(logger, times(2)).debug("Appending symbol"); + verify(logger, times(1)).debug("Formatted output '{}'", encodedString.toUpperCase()); + } + + @Test + public void testEncode(){ + cipher.preserveCapitals = true; + cipher.inputString = decodedString; + cipher.keyword = keywordClean; + cipher.grid = grid; + + cipher.encode(); + + assertEquals(encodedString, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(1)).debug("Converting letters to coordinates"); + verify(logger, times(1)).debug("Splitting locations into groups"); + verify(logger, times(1)).debug("Splitting groups into rows"); + verify(logger, times(1)).debug("Converting split locations into new locations"); + verify(logger, times(1)).debug("Converting new locations into characters"); + } + + @Test + public void testEncode_3(){ + cipher.preserveCapitals = true; + cipher.inputString = decodedString; + cipher.keyword = keywordClean; + cipher.grid = grid; + cipher.groupSize = 3; + + cipher.encode(); + + assertEquals(encodedString3, cipher.outputString); + verify(logger, times(1)).debug("Encoding"); + verify(logger, times(1)).debug("Converting letters to coordinates"); + verify(logger, times(1)).debug("Splitting locations into groups"); + verify(logger, times(1)).debug("Splitting groups into rows"); + verify(logger, times(1)).debug("Converting split locations into new locations"); + verify(logger, times(1)).debug("Converting new locations into characters"); + } + + @Test + public void testDecode(){ + cipher.preserveCapitals = true; + cipher.inputString = encodedString; + cipher.keyword = keywordClean; + cipher.grid = grid; + + cipher.decode(); + + assertEquals(decodedString, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(1)).debug("Converting letters to coordinates"); + verify(logger, times(1)).debug("Splitting locations into groups"); + verify(logger, times(1)).debug("Putting locations into rows"); + verify(logger, times(1)).debug("Converting locations into new locations"); + verify(logger, times(1)).debug("Converting new locations into letters"); + } + + @Test + public void testDecode_3(){ + cipher.preserveCapitals = true; + cipher.inputString = encodedString3; + cipher.keyword = keywordClean; + cipher.grid = grid; + cipher.groupSize = 3; + + cipher.decode(); + + assertEquals(decodedString, cipher.outputString); + verify(logger, times(1)).debug("Decoding"); + verify(logger, times(1)).debug("Converting letters to coordinates"); + verify(logger, times(1)).debug("Splitting locations into groups"); + verify(logger, times(1)).debug("Putting locations into rows"); + verify(logger, times(6)).debug("Converting locations into new locations"); + verify(logger, times(1)).debug("Converting new locations into letters"); + } + + @Test + public void testGetters(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + cipher.keyword = keywordClean; + cipher.groupSize = 3; + cipher.fillIn = '='; + cipher.grid = grid; + + assertEquals(decodedString, cipher.getInputString()); + assertEquals(encodedString, cipher.getOutputString()); + assertEquals(keywordClean, cipher.getKeyword()); + assertEquals(3, cipher.getGroupSize()); + assertEquals('=', cipher.getFillIn()); + assertEquals(gridString, cipher.getGrid()); + } + + @Test + public void testReset(){ + cipher.inputString = decodedString; + cipher.outputString = encodedString; + cipher.keyword = keyword; + cipher.groupSize = 3; + cipher.grid = grid; + + cipher.reset(); + + assertEquals("", cipher.inputString); + assertEquals("", cipher.outputString); + assertEquals("", cipher.keyword); + assertEquals(Integer.MAX_VALUE, cipher.groupSize); + assertArrayEquals(new char[3][3][3], cipher.grid); + } + + @Test + public void testPracticalEncoding(){ + cipher = new Trifid(true, true, true); + + String output = cipher.encode(keyword, decodedString); + + assertEquals(decodedString, cipher.inputString); + assertEquals(keywordClean, cipher.keyword); + assertEquals(encodedString, cipher.outputString); + assertEquals(encodedString, output); + assertArrayEquals(grid, cipher.grid); + } + + @Test + public void testPracticalEncoding_3(){ + cipher = new Trifid(true, true, true); + + String output = cipher.encode(keyword, 3, decodedString); + + assertEquals(decodedString, cipher.inputString); + assertEquals(keywordClean, cipher.keyword); + assertEquals(encodedString3, cipher.outputString); + assertEquals(encodedString3, output); + assertArrayEquals(grid, cipher.grid); + } + + @Test + public void testPracticalEncoding_fill(){ + cipher = new Trifid(true, true, true, '='); + + String output = cipher.encode(keyword, decodedString); + + assertEquals(decodedString, cipher.inputString); + assertEquals(keywordCleanAlt, cipher.keyword); + assertEquals(encodedStringAlt, cipher.outputString); + assertEquals(encodedStringAlt, output); + assertArrayEquals(gridAlt, cipher.grid); + } + + @Test + public void testPracticalEncoding_clean(){ + cipher = new Trifid(false, false, false); + + String output = cipher.encode(keyword, decodedString); + + assertEquals(decodedStringClean, cipher.inputString); + assertEquals(keywordClean, cipher.keyword); + assertEquals(encodedStringClean, cipher.outputString); + assertEquals(encodedStringClean, output); + assertArrayEquals(grid, cipher.grid); + } + + @Test + public void testPracticalEncoding_clean3(){ + cipher = new Trifid(false, false, false); + + String output = cipher.encode(keyword, 3, decodedString); + + assertEquals(decodedStringClean, cipher.inputString); + assertEquals(keywordClean, cipher.keyword); + assertEquals(encodedStringClean3, cipher.outputString); + assertEquals(encodedStringClean3, output); + assertArrayEquals(grid, cipher.grid); + } + + @Test + public void testPracticalEncoding_cleanFill(){ + cipher = new Trifid(false, false, false, '='); + + String output = cipher.encode(keyword, decodedString); + + assertEquals(decodedStringCleanAlt, cipher.inputString); + assertEquals(keywordCleanAlt, cipher.keyword); + assertEquals(encodedStringCleanAlt, cipher.outputString); + assertEquals(encodedStringCleanAlt, output); + assertArrayEquals(gridAlt, cipher.grid); + } + + @Test + public void testPracticalDecoding(){ + cipher = new Trifid(true, true, true); + + String output = cipher.decode(keyword, encodedString); + + assertEquals(encodedString, cipher.inputString); + assertEquals(keywordClean, cipher.keyword); + assertEquals(decodedString, cipher.outputString); + assertEquals(decodedString, output); + assertArrayEquals(grid, cipher.grid); + } + + @Test + public void testPracticalDecoding_3(){ + cipher = new Trifid(true, true, true); + + String output = cipher.decode(keyword, 3, encodedString3); + + assertEquals(encodedString3, cipher.inputString); + assertEquals(keywordClean, cipher.keyword); + assertEquals(decodedString, cipher.outputString); + assertEquals(decodedString, output); + assertArrayEquals(grid, cipher.grid); + } + + @Test + public void testPracticalDecoding_fill(){ + cipher = new Trifid(true, true, true, '='); + + String output = cipher.decode(keyword, encodedStringAlt); + + assertEquals(encodedStringAlt, cipher.inputString); + assertEquals(keywordCleanAlt, cipher.keyword); + assertEquals(decodedString, cipher.outputString); + assertEquals(decodedString, output); + assertArrayEquals(gridAlt, cipher.grid); + } + + @Test + public void testpracticalDecoding_clean(){ + cipher = new Trifid(false, false, false); + + String output = cipher.decode(keyword, encodedString); + + assertEquals(encodedStringClean, cipher.inputString); + assertEquals(keywordClean, cipher.keyword); + assertEquals(decodedStringClean, cipher.outputString); + assertEquals(decodedStringClean, output); + assertArrayEquals(grid, cipher.grid); + } + + @Test + public void testPracticalDecoding_clean3(){ + cipher = new Trifid(false, false, false); + + String output = cipher.decode(keyword, 3, encodedString3); + + assertEquals(encodedStringClean3, cipher.inputString); + assertEquals(keywordClean, cipher.keyword); + assertEquals(decodedStringClean, cipher.outputString); + assertEquals(decodedStringClean, output); + assertArrayEquals(grid, cipher.grid); + } + + @Test + public void testPracticalDecoding_cleanFill(){ + cipher = new Trifid(false, false, false, '='); + + String output = cipher.decode(keyword, encodedStringAlt); + + assertEquals(encodedStringCleanAlt, cipher.inputString); + assertEquals(keywordCleanAlt, cipher.keyword); + assertEquals(decodedStringCleanAlt, cipher.outputString); + assertEquals(decodedStringCleanAlt, output); + assertArrayEquals(gridAlt, cipher.grid); } }