diff --git a/pom.xml b/pom.xml
index daf037a..a894263 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.mattrixwv
cipher-stream-java
- 1.2.0
+ 1.3.0-SNAPSHOT
CipherStreamJava
http://www.mattrixwv.com
diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Bifid.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Bifid.java
index 8ebbfe6..1e1838a 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: 07-09-22
+//Modified: 04-23-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -14,20 +14,20 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Bifid{
- private static final Logger logger = LoggerFactory.getLogger(Bifid.class);
+ protected static Logger logger = LoggerFactory.getLogger(Bifid.class);
//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 PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
- 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 PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
+ 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 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");
@@ -39,7 +39,7 @@ public class Bifid{
this.keyword = keyword;
}
//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");
@@ -74,7 +74,7 @@ public class Bifid{
}
}
//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
int outputCnt = 0;
@@ -100,15 +100,15 @@ public class Bifid{
}
//Save the output
- logger.debug("Formatted output string '{}'", output);
this.outputString = output.toString();
+ logger.debug("Formatted output string '{}'", this.outputString);
}
//Encodes inputString using a polybius square and stores the result in outputString
- private void encode() throws InvalidCharacterException, InvalidInputException{
+ protected void encode() throws InvalidCharacterException, InvalidInputException{
logger.debug("Encoding");
//Get the encoded numbers from a polybius square
- logger.debug("Encoding polybius");
+ logger.debug("Encoding Polybius");
String polybiusMessage = polybiusSquare.encode(keyword, inputString).replaceAll("\\s", "");
keyword = polybiusSquare.getKeyword(); //Save the cleaned keyword
@@ -118,7 +118,7 @@ public class Bifid{
boolean firstNum = true;
logger.debug("Splitting Polybius Square message");
for(char ch : polybiusMessage.toCharArray()){
- logger.debug("Current character {}", ch);
+ logger.debug("Current character '{}'", ch);
if(firstNum){
row0.append(ch);
@@ -140,7 +140,7 @@ public class Bifid{
formatOutput(letterResult);
}
//Decodes inputString using a polybius square and stores the result in outputString
- private void decode() throws InvalidCharacterException, InvalidInputException{
+ protected void decode() throws InvalidCharacterException, InvalidInputException{
logger.debug("Decoding");
//Get the decoded number from a polybius square
diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java
index 9b58cd4..29885de 100644
--- a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java
+++ b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java
@@ -31,7 +31,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
}
}
@Override
- protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
+ protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
@@ -69,12 +69,12 @@ public class LargePolybiusSquare extends PolybiusSquare{
logger.debug("Cleaned input string '{}'", inputString);
- if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
+ if(this.inputString.isBlank() || getPreparedInputStringEncode().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
@Override
- protected String getPreparedInputStringEncoding(){
+ protected String getPreparedInputStringEncode(){
logger.debug("Preparing input string for encoding");
String cleanString = inputString.toUpperCase();
diff --git a/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java b/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java
index 36213e6..9662669 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: 07-09-22
+//Modified: 04-29-23
package com.mattrixwv.cipherstream.polysubstitution;
@@ -12,10 +12,11 @@ import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
+import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class PolybiusSquare{
- protected static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
+ protected static Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
//A class representing the location of a character in the grid
protected class CharLocation{
@@ -43,6 +44,41 @@ public class PolybiusSquare{
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
+
+ //Setting the character to be replaced
+ protected void setReplaced(char replaced) throws InvalidCharacterException{
+ logger.debug("Setting replaced");
+ logger.debug("Original character {}", replaced);
+
+ if(!Character.isAlphabetic(replaced)){
+ throw new InvalidCharacterException("The replaced character must be a letter");
+ }
+
+ logger.debug("Checking replacer");
+ if(replaced == replacer){
+ throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
+ }
+
+ this.replaced = Character.toUpperCase(replaced);
+ logger.debug("Cleaned character {}", this.replaced);
+ }
+ //Setting the character that replaces replaced
+ protected void setReplacer(char replacer) throws InvalidCharacterException{
+ logger.debug("Setting replacer");
+ logger.debug("Original character {}", replacer);
+
+ if(!Character.isAlphabetic(replacer)){
+ throw new InvalidCharacterException("The replacer character must be a letter");
+ }
+
+ logger.debug("Checking replaced");
+ if(replaced == replacer){
+ throw new InvalidCharacterException("The replacer letter cannot be the same as the replaced letter");
+ }
+
+ this.replacer = Character.toUpperCase(replacer);
+ logger.debug("Cleaned character {}", this.replacer);
+ }
//Create the grid from the keyword
protected void createGrid(){
logger.debug("Creating grid from keyword");
@@ -57,18 +93,18 @@ public class PolybiusSquare{
logger.debug("Created grid\n{}", getGrid());
}
//Strips invalid characters from the string that needs encoded/decoded
- protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
+ protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
- throw new NullPointerException("Input cannot be null");
+ throw new InvalidInputException("Input cannot be null");
}
logger.debug("Setting input string for encoding '{}'", inputString);
//Make sure the string doesn't contain any numbers
logger.debug("Checking for digits");
- for(char ch = '0';ch <= '9';++ch){
- if(inputString.contains(Character.toString(ch))){
- throw new InvalidCharacterException("Inputs for encoding cannot contain numbers");
+ for(char ch : inputString.toCharArray()){
+ if(Character.isDigit(ch)){
+ throw new InvalidInputException("Inputs for encoding cannot contain numbers");
}
}
@@ -107,13 +143,13 @@ public class PolybiusSquare{
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
- if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
+ if(this.inputString.isBlank() || getPreparedInputStringEncode().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
- protected void setInputStringDecoding(String inputString) throws InvalidCharacterException, InvalidInputException{
+ protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
- throw new NullPointerException("Input cannot be null");
+ throw new InvalidInputException("Input cannot be null");
}
logger.debug("Setting input string for decoding '{}'", inputString);
@@ -127,11 +163,11 @@ public class PolybiusSquare{
++numberOfDigits;
}
else if(Character.isAlphabetic(ch)){
- throw new InvalidCharacterException("Inputs for decoding cannot contains letters");
+ throw new InvalidInputException("Inputs for decoding cannot contains letters");
}
}
if((numberOfDigits % 2) != 0){
- throw new InvalidCharacterException("There must be an even number of digits in an encoded string");
+ throw new InvalidInputException("There must be an even number of digits in an encoded string");
}
//Remove any whitespace if selected
@@ -152,12 +188,12 @@ public class PolybiusSquare{
logger.debug("Cleaned input string '{}'", inputString);
this.inputString = inputString;
- if(this.inputString.isBlank() || getPreparedInputStringDecoding().isBlank()){
+ if(this.inputString.isBlank() || getPreparedInputStringDecode().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Returns the input string ready for encoding
- protected String getPreparedInputStringEncoding(){
+ protected String getPreparedInputStringEncode(){
logger.debug("Preparing input string for encoding");
String cleanString = inputString.toUpperCase();
@@ -166,8 +202,8 @@ public class PolybiusSquare{
logger.debug("Prepared string '{}'", cleanString);
return cleanString;
}
- protected String getPreparedInputStringDecoding(){
- logger.debug("Prepareing input string for decoding");
+ protected String getPreparedInputStringDecode(){
+ logger.debug("Preparing input string for decoding");
String cleanString = inputString.replaceAll("\\D", "");
@@ -177,7 +213,7 @@ public class PolybiusSquare{
//Strips invalid characters from the keyword and creates the grid
protected void setKeyword(String keyword){
if(keyword == null){
- throw new NullPointerException("Keyword cannot be null");
+ throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("Original keyword {}", keyword);
@@ -187,7 +223,7 @@ public class PolybiusSquare{
keyword = keyword.toUpperCase();
//Remove everything except capital letters
- logger.debug("Removing all non-letters");
+ logger.debug("Removing all non-letter characters");
keyword = keyword.replaceAll("[^A-Z]", "");
//Add all letters in the alphabet to the key
@@ -202,7 +238,7 @@ public class PolybiusSquare{
StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString();
- logger.debug("Cleaned keyword {}", keyword);
+ logger.debug("Cleaned keyword {}", this.keyword);
//Create the grid from the sanitized keyword
createGrid();
@@ -229,11 +265,11 @@ public class PolybiusSquare{
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
- for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
- logger.debug("Working character {}", inputString.charAt(inputCnt));
+ for(char inputChar : inputString.toCharArray()){
+ logger.debug("Working character {}", inputChar);
//Add both numbers of any letters to the output
- if(Character.isAlphabetic(inputString.charAt(inputCnt))){
+ if(Character.isAlphabetic(inputChar)){
logger.debug("Adding encoded characters");
fullOutput.append(cleanString.charAt(outputCnt++));
@@ -243,15 +279,15 @@ public class PolybiusSquare{
else{
logger.debug("Adding symbols");
- fullOutput.append(inputString.charAt(inputCnt));
+ fullOutput.append(inputChar);
}
}
- logger.debug("Formatted output '{}'", fullOutput);
outputString = fullOutput.toString();
+ logger.debug("Formatted output '{}'", outputString);
}
protected void addCharactersToCleanStringDecode(String cleanString){
- logger.debug("Formatting output string to decoding");
+ logger.debug("Formatting output string for decoding");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
@@ -273,20 +309,20 @@ public class PolybiusSquare{
}
}
- logger.debug("Formatted output '{}'", fullOutput);
outputString = fullOutput.toString();
+ logger.debug("Formatted output '{}'", outputString);
}
//Encodes inputString using the Playfair cipher and stores the result in outputString
- private String encode() throws InvalidInputException{
+ protected void encode() throws InvalidInputException{
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
- String cleanString = getPreparedInputStringEncoding();
+ String cleanString = getPreparedInputStringEncode();
for(int cnt = 0;cnt < cleanString.length();++cnt){
//Get the next character to be encoded
char ch = cleanString.charAt(cnt);
- logger.debug("Working character {}", ch);
+ logger.debug("Current working character {}", ch);
//Find the letter in the grid
CharLocation location = findChar(ch);
@@ -299,16 +335,13 @@ public class PolybiusSquare{
//Add other characters to the output string
addCharactersToCleanStringEncode(output.toString());
-
- //Return the output string
- return outputString;
}
//Decodes inputString using the Playfair cipher and stores the result in outputString
- private String decode(){
+ protected void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
- String cleanString = getPreparedInputStringDecoding();
+ String cleanString = getPreparedInputStringDecode();
for(int cnt = 0;cnt < cleanString.length();){
//Get the digits indicationg the location of the next character
char firstDigit = cleanString.charAt(cnt++);
@@ -327,9 +360,6 @@ public class PolybiusSquare{
//Add other characters to the output
addCharactersToCleanStringDecode(output.toString());
-
- //Return the output string
- return outputString;
}
public PolybiusSquare() throws InvalidCharacterException{
@@ -360,8 +390,9 @@ public class PolybiusSquare{
public String encode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setKeyword(keyword);
- setInputStringEncoding(inputString);
- return encode();
+ setInputStringEncode(inputString);
+ encode();
+ return outputString;
}
//Sets the keyword and inputString and decodes the message
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
@@ -370,8 +401,9 @@ public class PolybiusSquare{
public String decode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setKeyword(keyword);
- setInputStringDecoding(inputString);
- return decode();
+ setInputStringDecode(inputString);
+ decode();
+ return outputString;
}
//Makes sure all variables are empty
@@ -387,31 +419,9 @@ public class PolybiusSquare{
public char getReplaced(){
return replaced;
}
- public void setReplaced(char replaced) throws InvalidCharacterException{
- if(!Character.isAlphabetic(replaced)){
- throw new InvalidCharacterException("The replaced character must be a letter");
- }
-
- if(replaced == replacer){
- throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
- }
-
- this.replaced = Character.toUpperCase(replaced);
- }
public char getReplacer(){
return replacer;
}
- public void setReplacer(char replacer) throws InvalidCharacterException{
- if(!Character.isAlphabetic(replacer)){
- throw new InvalidCharacterException("The replacer character must be a letter");
- }
-
- if(replaced == replacer){
- throw new InvalidCharacterException("The replacer letter cannot be the same as the replaced letter");
- }
-
- this.replacer = replacer;
- }
public String getKeyword(){
return keyword;
}
diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestBifid.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestBifid.java
index b881349..6c92b20 100644
--- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestBifid.java
+++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestBifid.java
@@ -1,408 +1,348 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestBifid.java
//Mattrixwv
// Created: 03-03-22
-//Modified: 07-09-22
+//Modified: 04-23-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.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.anyChar;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
-import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class TestBifid{
- @Test
- public void testEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
- Bifid cipher = new Bifid(true, true, true);
+ private Bifid cipher;
+ private Logger logger;
+ //Test
+ private String inputString = "Message to^encode";
+ private String inputStringClean = "MESSAGETOENCODE";
+ private String outputString = "Mqaokne kc^vdodzd";
+ private String outputStringClean = "MQAOKNEKCVDODZD";
+ private String keyword = "keyword";
+ 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();
+
+ assertFalse(cipher.preserveCapitals);
+ assertFalse(cipher.preserveWhitespace);
+ assertFalse(cipher.preserveSymbols);
+ assertNotNull(cipher.polybiusSquare);
+ assertFalse(cipher.polybiusSquare.preserveWhitespace);
+ assertFalse(cipher.polybiusSquare.preserveSymbols);
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ }
+
+ @Test
+ public void testConstructor_preservesCapitals(){
+ cipher = new Bifid(true, false, false);
+
+ assertTrue(cipher.preserveCapitals);
+ assertFalse(cipher.preserveWhitespace);
+ assertFalse(cipher.preserveSymbols);
+ assertNotNull(cipher.polybiusSquare);
+ assertFalse(cipher.polybiusSquare.preserveWhitespace);
+ assertFalse(cipher.polybiusSquare.preserveSymbols);
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ }
+
+ @Test
+ public void testConstructor_preservesWhitespace(){
+ cipher = new Bifid(false, true, false);
+
+ assertFalse(cipher.preserveCapitals);
+ assertTrue(cipher.preserveWhitespace);
+ assertFalse(cipher.preserveSymbols);
+ assertNotNull(cipher.polybiusSquare);
+ assertFalse(cipher.polybiusSquare.preserveWhitespace);
+ assertFalse(cipher.polybiusSquare.preserveSymbols);
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ }
+
+ @Test
+ public void testConstructor_preservesSymbols(){
+ cipher = new Bifid(false, false, true);
+
+ assertFalse(cipher.preserveCapitals);
+ assertFalse(cipher.preserveWhitespace);
+ assertTrue(cipher.preserveSymbols);
+ assertNotNull(cipher.polybiusSquare);
+ assertFalse(cipher.polybiusSquare.preserveWhitespace);
+ assertFalse(cipher.polybiusSquare.preserveSymbols);
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ }
+
+ @Test
+ public void testSetKeyword(){
+ cipher.setKeyword(keyword);
+
+ assertEquals(keyword, cipher.keyword);
+ verify(logger, times(1)).debug("Setting keyword '{}'", keyword);
+ }
+
+ @Test
+ public void testSetKeyword_null(){
+ assertThrows(InvalidKeywordException.class, () -> {
+ cipher.setKeyword(null);
+ });
+
+ verify(logger, never()).debug(eq("Setting keyword '{}'"), anyString());
+ verify(logger, never()).debug(anyString(), anyString());
+ }
+
+ @Test
+ public void testSetInputString(){
+ cipher.preserveCapitals = true;
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+
+ cipher.setInputString(inputString);
+
+ assertEquals(inputString, cipher.inputString);
+ verify(logger, times(1)).debug("Original input string '{}'", inputString);
+ verify(logger, never()).debug("Removing case");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, times(1)).debug("Cleaned input string '{}'", inputString);
+ }
+
+ @Test
+ public void testSetInputString_noCapitals(){
+ cipher.preserveCapitals = false;
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+
+ cipher.setInputString(inputString);
+
+ assertEquals(inputString.toUpperCase(), cipher.inputString);
+ verify(logger, times(1)).debug("Original input string '{}'", inputString);
+ verify(logger, times(1)).debug("Removing case");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
+ }
+
+ @Test
+ public void testSetInputString_noWhitespace(){
+ cipher.preserveCapitals = true;
+ cipher.preserveWhitespace = false;
+ cipher.preserveSymbols = true;
+
+ cipher.setInputString(inputString);
+
+ assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString);
+ verify(logger, times(1)).debug("Original input string '{}'", inputString);
+ verify(logger, never()).debug("Removing case");
+ verify(logger, times(1)).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("\\s", ""));
+ }
+
+ @Test
+ public void testSetInputString_noSymbols(){
+ cipher.preserveCapitals = true;
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = false;
+
+ cipher.setInputString(inputString);
+
+ assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
+ verify(logger, times(1)).debug("Original input string '{}'", inputString);
+ verify(logger, never()).debug("Removing case");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, times(1)).debug("Removing symbols");
+ verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("[^a-zA-Z\\s]", ""));
+ }
+
+ @Test
+ public void testSetInputString_blank(){
+ cipher.preserveCapitals = true;
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+
+ assertThrows(InvalidInputException.class, () -> {
+ cipher.setInputString("");
+ });
+
+ 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 testFormatOutput(){
+ cipher.inputString = inputString;
+
+ cipher.formatOutput(outputStringClean);
+
+ assertEquals(outputString, cipher.outputString);
+ verify(logger, times(1)).debug("Formatting output");
+ verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
+ verify(logger, times(1)).debug("Altering uppercase");
+ verify(logger, times(14)).debug("Altering lowercase");
+ verify(logger, times(2)).debug("Adding symbol");
+ verify(logger, times(1)).debug("Formatted output string '{}'", outputString);
+ }
+
+ @Test
+ public void testEncode(){
+ cipher.inputString = inputString;
+ cipher.keyword = keyword;
+
+ cipher.encode();
+
+ assertEquals(inputString, cipher.inputString);
+ assertEquals(keywordClean, cipher.keyword);
+ assertEquals(outputString, cipher.outputString);
+ verify(logger, times(1)).debug("Encoding");
+ verify(logger, times(1)).debug("Encoding Polybius");
+ verify(logger, times(1)).debug("Splitting Polybius Square message");
+ verify(logger, times(30)).debug(eq("Current character '{}'"), anyChar());
+ verify(logger, times(1)).debug("Decoding Polybius Square");
+ }
+
+ @Test
+ public void testDecode(){
+ cipher.inputString = outputString;
+ cipher.keyword = keyword;
+
+ cipher.decode();
+
+ assertEquals(outputString, cipher.inputString);
+ assertEquals(keywordClean, cipher.keyword);
+ assertEquals(inputString, cipher.outputString);
+ verify(logger, times(1)).debug("Decoding");
+ verify(logger, times(1)).debug("Encoding Polybius Square");
+ verify(logger, times(1)).debug("Splitting Polybius Square message");
+ verify(logger, times(15)).debug(eq("Current characters {} {}"), anyChar(), anyChar());
+ verify(logger, times(1)).debug("Decoding Polybius Square");
+ }
+
+ @Test
+ public void testGetters(){
+ cipher.inputString = inputString;
+ cipher.keyword = keyword;
+ cipher.outputString = outputString;
+
+ assertEquals(inputString, cipher.getInputString());
+ assertEquals(keyword, cipher.getKeyword());
+ assertEquals(outputString, cipher.getOutputString());
+ }
+
+ @Test
+ public void testReset(){
+ cipher.inputString = inputString;
+ cipher.keyword = keyword;
+ cipher.outputString = outputString;
+
+ cipher.reset();
+
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ verify(logger, times(1)).debug("Resetting fields");
+ }
+
+ @Test
+ public void testPracticalEncoding(){
+ cipher = new Bifid(true, true, true);
- //Test lowercase encoding
- String inputString = "messagetoencode";
- String keyword = "keyword";
- String correctOutput = "mqaoknekcvdodzd";
String output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test uppercase encoding
- inputString = "MESSAGETOENCODE";
- keyword = "keyword";
- correctOutput = "MQAOKNEKCVDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace encoding
- inputString = "message to encode";
- keyword = "keyword";
- correctOutput = "mqaokne kc vdodzd";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol encoding
- inputString = "message*to+encode";
- keyword = "keyword";
- correctOutput = "mqaokne*kc+vdodzd";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test mixed case, whitespace, symbol encoding
- inputString = "Message to^encode";
- keyword = "keyword";
- correctOutput = "Mqaokne kc^vdodzd";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
+ assertEquals(inputString, cipher.inputString);
+ assertEquals(keywordClean, cipher.keyword);
+ assertEquals(outputString, cipher.outputString);
+ assertEquals(output, cipher.outputString);
}
@Test
- public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
- Bifid cipher = new Bifid(false, true, true);
+ public void testPracticalEncoding_clean(){
+ cipher = new Bifid(false, false, false);
- //Test lowercase encoding
- String inputString = "messagetoencode";
- String keyword = "keyword";
- String correctOutput = "MQAOKNEKCVDODZD";
String output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test uppercase encoding
- inputString = "MESSAGETOENCODE";
- keyword = "keyword";
- correctOutput = "MQAOKNEKCVDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace encoding
- inputString = "message to encode";
- keyword = "keyword";
- correctOutput = "MQAOKNE KC VDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol encoding
- inputString = "message*to+encode";
- keyword = "keyword";
- correctOutput = "MQAOKNE*KC+VDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test mixed case, whitespace, symbol encoding
- inputString = "Message to^encode";
- keyword = "keyword";
- correctOutput = "MQAOKNE KC^VDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
+ assertEquals(inputStringClean, cipher.inputString);
+ assertEquals(keywordClean, cipher.keyword);
+ assertEquals(outputStringClean, cipher.outputString);
+ assertEquals(outputStringClean, output);
}
@Test
- public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
- Bifid cipher = new Bifid(true, false, true);
+ public void testPracticalDecoding(){
+ cipher = new Bifid(true, true, true);
- //Test lowercase encoding
- String inputString = "messagetoencode";
- String keyword = "keyword";
- String correctOutput = "mqaoknekcvdodzd";
- String output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test uppercase encoding
- inputString = "MESSAGETOENCODE";
- keyword = "keyword";
- correctOutput = "MQAOKNEKCVDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
+ String output = cipher.decode(keyword, outputString);
- //Test whitespace encoding
- inputString = "message to encode";
- keyword = "keyword";
- correctOutput = "mqaoknekcvdodzd";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol encoding
- inputString = "message*to+encode";
- keyword = "keyword";
- correctOutput = "mqaokne*kc+vdodzd";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test mixed case, whitespace, symbol encoding
- inputString = "Message to^encode";
- keyword = "keyword";
- correctOutput = "Mqaoknekc^vdodzd";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
+ assertEquals(outputString, cipher.inputString);
+ assertEquals(keywordClean, cipher.keyword);
+ assertEquals(inputString, cipher.outputString);
+ assertEquals(inputString, output);
}
@Test
- public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
- Bifid cipher = new Bifid(true, true, false);
+ public void testPracticalDecoding_clean(){
+ cipher = new Bifid(false, false, false);
- //Test lowercase encoding
- String inputString = "messagetoencode";
- String keyword = "keyword";
- String correctOutput = "mqaoknekcvdodzd";
- String output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test uppercase encoding
- inputString = "MESSAGETOENCODE";
- keyword = "keyword";
- correctOutput = "MQAOKNEKCVDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
+ String output = cipher.decode(keyword, outputString);
- //Test whitespace encoding
- inputString = "message to encode";
- keyword = "keyword";
- correctOutput = "mqaokne kc vdodzd";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol encoding
- inputString = "message*to+encode";
- keyword = "keyword";
- correctOutput = "mqaoknekcvdodzd";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test mixed case, whitespace, symbol encoding
- inputString = "Message to^encode";
- keyword = "keyword";
- correctOutput = "Mqaokne kcvdodzd";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- }
-
- @Test
- public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
- Bifid cipher = new Bifid(false, false, false);
-
- //Test lowercase encoding
- String inputString = "messagetoencode";
- String keyword = "keyword";
- String correctOutput = "MQAOKNEKCVDODZD";
- String output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test uppercase encoding
- inputString = "MESSAGETOENCODE";
- keyword = "keyword";
- correctOutput = "MQAOKNEKCVDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace encoding
- inputString = "message to encode";
- keyword = "keyword";
- correctOutput = "MQAOKNEKCVDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol encoding
- inputString = "message*to+encode";
- keyword = "keyword";
- correctOutput = "MQAOKNEKCVDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test mixed case, whitespace, symbol encoding
- inputString = "Message to^encode";
- keyword = "keyword";
- correctOutput = "MQAOKNEKCVDODZD";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- }
-
-
- @Test
- public void testDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
- Bifid cipher = new Bifid(true, true, true);
-
- //Test lowercase decoding
- String inputString = "mqaoknekcvdodzd";
- String keyword = "keyword";
- String correctOutput = "messagetoencode";
- String output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test uppercase decoding
- inputString = "MQAOKNEKCVDODZD";
- keyword = "keyword";
- correctOutput = "MESSAGETOENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace decoding
- inputString = "mqaokne kc vdodzd";
- keyword = "keyword";
- correctOutput = "message to encode";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol decoding
- inputString = "mqaokne*kc+vdodzd";
- keyword = "keyword";
- correctOutput = "message*to+encode";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test mixed case, whitespace, symbol decoding
- inputString = "Mqaokne kc^vdodzd";
- keyword = "keyword";
- correctOutput = "Message to^encode";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- }
-
- @Test
- public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
- Bifid cipher = new Bifid(false, true, true);
-
- //Test lowercase decoding
- String inputString = "mqaoknekcvdodzd";
- String keyword = "keyword";
- String correctOutput = "MESSAGETOENCODE";
- String output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test uppercase decoding
- inputString = "MQAOKNEKCVDODZD";
- keyword = "keyword";
- correctOutput = "MESSAGETOENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace decoding
- inputString = "mqaokne kc vdodzd";
- keyword = "keyword";
- correctOutput = "MESSAGE TO ENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol decoding
- inputString = "mqaokne*kc+vdodzd";
- keyword = "keyword";
- correctOutput = "MESSAGE*TO+ENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test mixed case, whitespace, symbol decoding
- inputString = "Mqaokne kc^vdodzd";
- keyword = "keyword";
- correctOutput = "MESSAGE TO^ENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- }
-
- @Test
- public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
- Bifid cipher = new Bifid(true, false, true);
-
- //Test lowercase decoding
- String inputString = "mqaoknekcvdodzd";
- String keyword = "keyword";
- String correctOutput = "messagetoencode";
- String output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test uppercase decoding
- inputString = "MQAOKNEKCVDODZD";
- keyword = "keyword";
- correctOutput = "MESSAGETOENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace decoding
- inputString = "mqaokne kc vdodzd";
- keyword = "keyword";
- correctOutput = "messagetoencode";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol decoding
- inputString = "mqaokne*kc+vdodzd";
- keyword = "keyword";
- correctOutput = "message*to+encode";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test mixed case, whitespace, symbol decoding
- inputString = "Mqaokne kc^vdodzd";
- keyword = "keyword";
- correctOutput = "Messageto^encode";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- }
-
- @Test
- public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
- Bifid cipher = new Bifid(true, true, false);
-
- //Test lowercase decoding
- String inputString = "mqaoknekcvdodzd";
- String keyword = "keyword";
- String correctOutput = "messagetoencode";
- String output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test uppercase decoding
- inputString = "MQAOKNEKCVDODZD";
- keyword = "keyword";
- correctOutput = "MESSAGETOENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace decoding
- inputString = "mqaokne kc vdodzd";
- keyword = "keyword";
- correctOutput = "message to encode";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol decoding
- inputString = "mqaokne*kc+vdodzd";
- keyword = "keyword";
- correctOutput = "messagetoencode";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test mixed case, whitespace, symbol decoding
- inputString = "Mqaokne kc^vdodzd";
- keyword = "keyword";
- correctOutput = "Message toencode";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- }
-
- @Test
- public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
- Bifid cipher = new Bifid(false, false, false);
-
- //Test lowercase decoding
- String inputString = "mqaoknekcvdodzd";
- String keyword = "keyword";
- String correctOutput = "MESSAGETOENCODE";
- String output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test uppercase decoding
- inputString = "MQAOKNEKCVDODZD";
- keyword = "keyword";
- correctOutput = "MESSAGETOENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace decoding
- inputString = "mqaokne kc vdodzd";
- keyword = "keyword";
- correctOutput = "MESSAGETOENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol decoding
- inputString = "mqaokne*kc+vdodzd";
- keyword = "keyword";
- correctOutput = "MESSAGETOENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test mixed case, whitespace, symbol decoding
- inputString = "Mqaokne kc^vdodzd";
- keyword = "keyword";
- correctOutput = "MESSAGETOENCODE";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
+ assertEquals(outputStringClean, cipher.inputString);
+ assertEquals(keywordClean, cipher.keyword);
+ assertEquals(inputStringClean, cipher.outputString);
+ assertEquals(inputStringClean, output);
}
}
diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java
index eb6d84d..b238757 100644
--- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java
+++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java
@@ -30,10 +30,10 @@ public class TestLargePolybiusSquare{
private LargePolybiusSquare cipher;
private Logger logger;
//Variables
- private String inputString = "Message to^encode";
- private String inputStringClean = "MESSAGETOENCODE";
- private String outputString = "35124343222612 4415^123624152112";
- private String outputStringClean = "31 15 41 41 11 21 15 42 33 15 32 13 33 14 15";
+ 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 keywordClean = "KEYWORDABCFGHIJLMNPQSTUVXZ0123456789";
private String keywordBlank = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
@@ -114,13 +114,13 @@ public class TestLargePolybiusSquare{
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
- cipher.setInputStringEncoding(inputString);
+ cipher.setInputStringEncode(decodedString);
- assertEquals(inputString.toUpperCase(), cipher.inputString);
- verify(logger, times(1)).debug("Original input string '{}'", inputString);
+ assertEquals(decodedString.toUpperCase(), cipher.inputString);
+ verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
- verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
+ verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
}
@Test
@@ -128,13 +128,13 @@ public class TestLargePolybiusSquare{
cipher.preserveWhitespace = false;
cipher.preserveSymbols = true;
- cipher.setInputStringEncoding(inputString);
+ cipher.setInputStringEncode(decodedString);
- assertEquals(inputString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
- verify(logger, times(1)).debug("Original input string '{}'", inputString);
+ assertEquals(decodedString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
+ verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, times(1)).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
- 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
@@ -142,13 +142,13 @@ public class TestLargePolybiusSquare{
cipher.preserveWhitespace = true;
cipher.preserveSymbols = false;
- cipher.setInputStringEncoding(inputString);
+ cipher.setInputStringEncode(decodedString);
- assertEquals(inputString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""), cipher.inputString);
- verify(logger, times(1)).debug("Original input string '{}'", inputString);
+ assertEquals(decodedString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""), cipher.inputString);
+ verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols");
- verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""));
+ verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""));
}
@Test
@@ -156,10 +156,10 @@ public class TestLargePolybiusSquare{
cipher.preserveWhitespace = false;
cipher.preserveSymbols = false;
- cipher.setInputStringEncoding(inputString);
+ cipher.setInputStringEncode(decodedString);
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
- verify(logger, times(1)).debug("Original input string '{}'", inputString);
+ verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, times(1)).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", "M E S S A G E T O E N C O D E");
@@ -171,7 +171,7 @@ public class TestLargePolybiusSquare{
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
- cipher.setInputStringEncoding("*");
+ cipher.setInputStringEncode("*");
});
assertEquals("*", cipher.inputString);
@@ -187,7 +187,7 @@ public class TestLargePolybiusSquare{
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
- cipher.setInputStringEncoding("");
+ cipher.setInputStringEncode("");
});
assertEquals("", cipher.inputString);
@@ -203,7 +203,7 @@ public class TestLargePolybiusSquare{
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
- cipher.setInputStringEncoding(null);
+ cipher.setInputStringEncode(null);
});
assertEquals("", cipher.inputString);
@@ -215,12 +215,12 @@ public class TestLargePolybiusSquare{
@Test
public void testGetPreparedInputStringEncoding(){
- cipher.inputString = inputString.toUpperCase();
+ cipher.inputString = decodedString.toUpperCase();
- String preparedInputString = cipher.getPreparedInputStringEncoding();
+ String preparedInputString = cipher.getPreparedInputStringEncode();
- assertEquals(inputString.toUpperCase(), cipher.inputString);
- assertEquals(inputString.toUpperCase().replaceAll("[^A-Z0-9]", ""), preparedInputString);
+ assertEquals(decodedString.toUpperCase(), cipher.inputString);
+ assertEquals(decodedString.toUpperCase().replaceAll("[^A-Z0-9]", ""), preparedInputString);
verify(logger, times(1)).debug("Preparing input string for encoding");
verify(logger, times(1)).debug("Prepared input string '{}'", preparedInputString);
}
@@ -263,16 +263,16 @@ public class TestLargePolybiusSquare{
cipher.preserveSymbols = true;
cipher.keyword = keywordClean;
cipher.grid = grid;
- cipher.inputString = inputString.toUpperCase();
+ cipher.inputString = decodedString.toUpperCase();
- cipher.addCharactersToCleanStringEncode(outputString.replaceAll("\\s", "").replaceAll("[^0-9]", ""));
+ cipher.addCharactersToCleanStringEncode(encodedString.replaceAll("\\s", "").replaceAll("[^0-9]", ""));
- assertEquals(outputString, cipher.outputString);
+ assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string");
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
verify(logger, times(15)).debug("Appending character");
verify(logger, times(2)).debug("Appending symbol");
- verify(logger, times(1)).debug("Saving output string {}", outputString);
+ verify(logger, times(1)).debug("Saving output string {}", encodedString);
}
@Test
@@ -281,21 +281,21 @@ public class TestLargePolybiusSquare{
cipher.preserveSymbols = true;
cipher.keyword = keywordClean;
cipher.grid = grid;
- cipher.inputString = outputString;
+ cipher.inputString = encodedString;
- cipher.addCharactersToCleanStringDecode(inputStringClean);
+ cipher.addCharactersToCleanStringDecode(decodedStringClean);
verify(logger, times(1)).debug("Formatting output string");
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
verify(logger, times(15)).debug("Appending character");
verify(logger, times(2)).debug("Appending symbol");
- verify(logger, times(1)).debug("Saving output string {}", inputString.toUpperCase());
+ verify(logger, times(1)).debug("Saving output string {}", decodedString.toUpperCase());
}
@Test
public void testReset(){
- cipher.inputString = inputString;
- cipher.outputString = outputString;
+ cipher.inputString = decodedString;
+ cipher.outputString = encodedString;
cipher.keyword = keyword;
cipher.grid = grid;
@@ -313,47 +313,47 @@ public class TestLargePolybiusSquare{
public void testPracticalEncoding(){
cipher = new LargePolybiusSquare(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);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new LargePolybiusSquare(false, false);
- String output = cipher.encode(inputString);
+ String output = cipher.encode(decodedString);
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
assertEquals(keywordBlank, cipher.keyword);
- assertEquals(outputStringClean, cipher.outputString);
- assertEquals(outputStringClean, output);
+ assertEquals(encodedStringClean, cipher.outputString);
+ assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
cipher = new LargePolybiusSquare(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);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new LargePolybiusSquare(false, false);
- String output = cipher.decode(outputStringClean);
+ String output = cipher.decode(encodedStringClean);
- assertEquals(outputStringClean.replaceAll("\\s", ""), cipher.inputString);
+ assertEquals(encodedStringClean.replaceAll("\\s", ""), cipher.inputString);
assertEquals(keywordBlank, cipher.keyword);
- assertEquals(inputStringClean, cipher.outputString);
- assertEquals(inputStringClean, output);
+ assertEquals(decodedStringClean, cipher.outputString);
+ assertEquals(decodedStringClean, output);
}
}
\ No newline at end of file
diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPlayfair.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPlayfair.java
index 5e5f097..c299360 100644
--- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPlayfair.java
+++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPlayfair.java
@@ -530,7 +530,7 @@ public class TestPlayfair{
cipher.doubled = 'x';
assertThrows(InvalidCharacterException.class, () -> {
- cipher.setInputString(outputString + Character.toString(cipher.replaced), false);
+ cipher.setInputString(outputString + cipher.replaced, false);
});
assertEquals("", cipher.inputString);
diff --git a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPolybiusSquare.java b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPolybiusSquare.java
index 092454f..39befd1 100644
--- a/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPolybiusSquare.java
+++ b/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestPolybiusSquare.java
@@ -1,321 +1,742 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestPolybiusSquare.java
//Mattrixwv
// Created: 01-04-22
-//Modified: 07-09-22
+//Modified: 04-29-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.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
+import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
+import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare.CharLocation;
public class TestPolybiusSquare{
- @Test
- public void testEncode() throws InvalidCharacterException, InvalidInputException{
- PolybiusSquare cipher = new PolybiusSquare(true, true);
+ private PolybiusSquare cipher;
+ 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 keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ";
+ private char[][] grid = new char[][]{
+ {'K', 'E', 'Y', 'W', 'O'},
+ {'R', 'D', 'A', 'B', 'C'},
+ {'F', 'G', 'H', 'I', 'L'},
+ {'M', 'N', 'P', 'Q', 'S'},
+ {'T', 'U', 'V', 'X', 'Z'}
+ };
+ 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();
+
+ assertFalse(cipher.preserveWhitespace);
+ assertFalse(cipher.preserveSymbols);
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ assertArrayEquals(new char[5][5], cipher.grid);
+ assertEquals('J', cipher.replaced);
+ assertEquals('I', cipher.replacer);
+ }
+
+ @Test
+ public void testConstructor_noWhitespace(){
+ cipher = new PolybiusSquare(false, true);
+
+ assertFalse(cipher.preserveWhitespace);
+ assertTrue(cipher.preserveSymbols);
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ assertArrayEquals(new char[5][5], cipher.grid);
+ assertEquals('J', cipher.replaced);
+ assertEquals('I', cipher.replacer);
+ }
+
+ @Test
+ public void testConstructor_noSymbols(){
+ cipher = new PolybiusSquare(true, false);
+
+ assertTrue(cipher.preserveWhitespace);
+ assertFalse(cipher.preserveSymbols);
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ assertArrayEquals(new char[5][5], cipher.grid);
+ assertEquals('J', cipher.replaced);
+ assertEquals('I', cipher.replacer);
+ }
+
+ @Test
+ public void testConstructor_newChars_noWhitespace(){
+ cipher = new PolybiusSquare(false, true, 'A', 'B');
+
+ assertFalse(cipher.preserveWhitespace);
+ assertTrue(cipher.preserveSymbols);
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ assertArrayEquals(new char[5][5], cipher.grid);
+ assertEquals('A', cipher.replaced);
+ assertEquals('B', cipher.replacer);
+ }
+
+ @Test
+ public void testConstructor_newChars_noSymbols(){
+ cipher = new PolybiusSquare(true, false, 'A', 'B');
+
+ assertTrue(cipher.preserveWhitespace);
+ assertFalse(cipher.preserveSymbols);
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ assertArrayEquals(new char[5][5], cipher.grid);
+ assertEquals('A', cipher.replaced);
+ assertEquals('B', cipher.replacer);
+ }
+
+ @Test
+ public void testSetReplaced(){
+ cipher.setReplaced('a');
+
+ assertEquals('A', cipher.replaced);
+ verify(logger, times(1)).debug("Setting replaced");
+ verify(logger, times(1)).debug("Original character {}", 'a');
+ verify(logger, times(1)).debug("Checking replacer");
+ verify(logger, times(1)).debug("Cleaned character {}", 'A');
+ }
+
+ @Test
+ public void testSetReplaced_digital(){
+ assertThrows(InvalidCharacterException.class, () -> {
+ cipher.setReplaced('1');
+ });
+
+ assertEquals('J', cipher.replaced);
+ verify(logger, times(1)).debug("Setting replaced");
+ verify(logger, times(1)).debug("Original character {}", '1');
+ verify(logger, never()).debug("Checking replacer");
+ verify(logger, never()).debug(eq("Cleaned character {}"), anyChar());
+ }
+
+ @Test
+ public void testSetReplaced_replacer(){
+ assertThrows(InvalidCharacterException.class, () -> {
+ cipher.setReplaced(cipher.replacer);
+ });
+
+ assertEquals('J', cipher.replaced);
+ verify(logger, times(1)).debug("Setting replaced");
+ verify(logger, times(1)).debug("Original character {}", cipher.replacer);
+ verify(logger, times(1)).debug("Checking replacer");
+ verify(logger, never()).debug(eq("Cleaned character {}"), anyChar());
+ }
+
+ @Test
+ public void testSetReplacer(){
+ cipher.setReplacer('a');
+
+ assertEquals('A', cipher.replacer);
+ verify(logger, times(1)).debug("Setting replacer");
+ verify(logger, times(1)).debug("Original character {}", 'a');
+ verify(logger, times(1)).debug("Checking replaced");
+ verify(logger, times(1)).debug("Cleaned character {}", 'A');
+ }
+
+ @Test
+ public void testSetReplacer_digital(){
+ assertThrows(InvalidCharacterException.class, () -> {
+ cipher.setReplacer('1');
+ });
+
+ assertEquals('I', cipher.replacer);
+ verify(logger, times(1)).debug("Setting replacer");
+ verify(logger, times(1)).debug("Original character {}", '1');
+ verify(logger, never()).debug("Checking replaced");
+ verify(logger, never()).debug(eq("Cleaned character {}"), anyChar());
+ }
+
+ @Test
+ public void testSetReplacer_replaced(){
+ assertThrows(InvalidCharacterException.class, () -> {
+ cipher.setReplacer(cipher.replaced);
+ });
+
+ assertEquals('I', cipher.replacer);
+ verify(logger, times(1)).debug("Setting replacer");
+ verify(logger, times(1)).debug("Original character {}", cipher.replaced);
+ verify(logger, times(1)).debug("Checking replaced");
+ verify(logger, never()).debug(eq("Cleaned character {}"), anyChar());
+ }
+
+ @Test
+ public void testCreateGrid(){
+ cipher.keyword = keywordClean;
+
+ cipher.createGrid();
+
+ assertArrayEquals(grid, cipher.grid);
+ verify(logger, times(1)).debug("Creating grid from keyword");
+ verify(logger, times(1)).debug("Created grid\n{}", gridString);
+ }
+
+ @Test
+ public void testSetInputStringEncode(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+ cipher.replaced = 'J';
+ cipher.replacer = 'I';
+
+ cipher.setInputStringEncode(inputString);
+
+ assertEquals(inputString.toUpperCase(), cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for encoding '{}'", inputString);
+ 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());
+ }
+
+ @Test
+ public void testSetInputStringEncode_noWhitespace(){
+ cipher.preserveWhitespace = false;
+ cipher.preserveSymbols = true;
+ cipher.replaced = 'J';
+ cipher.replacer = 'I';
+
+ cipher.setInputStringEncode(inputString);
+
+ assertEquals(inputString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for encoding '{}'", inputString);
+ 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", ""));
+ }
+
+ @Test
+ public void testSetInputStringEncode_noSymbols(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = false;
+ cipher.replaced = 'J';
+ cipher.replacer = 'I';
+
+ cipher.setInputStringEncode(inputString);
+
+ assertEquals(inputString.toUpperCase().replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for encoding '{}'", inputString);
+ 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]", ""));
+ }
+
+ @Test
+ public void testSetInputStringEncode_noWhitespaceSymbols(){
+ cipher.preserveWhitespace = false;
+ cipher.preserveSymbols = false;
+ cipher.replaced = 'J';
+ cipher.replacer = 'I';
+
+ cipher.setInputStringEncode(inputString);
+
+ assertEquals(inputStringClean, cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for encoding '{}'", inputString);
+ 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);
+ }
+
+ @Test
+ public void testSetInputStringEncode_digits(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+ cipher.replaced = 'J';
+ cipher.replacer = 'I';
+
+ assertThrows(InvalidInputException.class, () -> {
+ cipher.setInputStringEncode("1");
+ });
+
+ assertEquals("", cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for encoding '{}'", "1");
+ verify(logger, times(1)).debug("Checking for digits");
+ verify(logger, never()).debug("Removing case");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, never()).debug(eq("Replacing {} with {}"), anyChar(), anyChar());
+ verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
+ }
+
+ @Test
+ public void testSetInputStringEncode_blank(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+ cipher.replaced = 'J';
+ cipher.replacer = 'I';
+
+ assertThrows(InvalidInputException.class, () -> {
+ cipher.setInputStringEncode("");
+ });
+
+ assertEquals("", cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for encoding '{}'", "");
+ 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 '{}'", "");
+ }
+
+ @Test
+ public void testSetInputStringEncode_blankClean(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+ cipher.replaced = 'J';
+ cipher.replacer = 'I';
+
+ assertThrows(InvalidInputException.class, () -> {
+ cipher.setInputStringEncode("*");
+ });
+
+ assertEquals("*", cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for encoding '{}'", "*");
+ 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 '{}'", "*");
+ }
+
+ @Test
+ public void testSetInputStringEncode_null(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+ cipher.replaced = 'J';
+ cipher.replacer = 'I';
+
+ assertThrows(InvalidInputException.class, () -> {
+ cipher.setInputStringEncode(null);
+ });
+
+ assertEquals("", cipher.inputString);
+ verify(logger, never()).debug(eq("Setting input string for encoding '{}'"), anyString());
+ verify(logger, never()).debug("Checking for digits");
+ verify(logger, never()).debug("Removing case");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, never()).debug(eq("Replacing {} with {}"), anyChar(), anyChar());
+ verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
+ }
+
+ @Test
+ public void testSetInputStringDecode(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+
+ 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 letters");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, times(1)).debug("Cleaned input string '{}'", outputString);
+ }
+
+ @Test
+ public void testSetInputStringDecode_noWhitespace(){
+ cipher.preserveWhitespace = false;
+ cipher.preserveSymbols = true;
+
+ cipher.setInputStringDecode(outputString);
+
+ assertEquals(outputString.replaceAll("\\s", ""), cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString);
+ 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", ""));
+ }
+
+ @Test
+ public void testSetInputStringDecode_noSymbol(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = false;
+
+ cipher.setInputStringDecode(outputString);
+
+ assertEquals(outputString.replaceAll("[^0-9\\s]", ""), cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString);
+ 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]", ""));
+ }
+
+ @Test
+ public void testSetInputStringDecode_alpha(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+
+ assertThrows(InvalidInputException.class, () -> {
+ cipher.setInputStringDecode(outputString + "a");
+ });
+
+ assertEquals("", cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString + "a");
+ verify(logger, times(1)).debug("Checking for letters");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
+ }
+
+ @Test
+ public void testSetInputStringDecode_odd(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+
+ assertThrows(InvalidInputException.class, () -> {
+ cipher.setInputStringDecode(outputString + "0");
+ });
+
+ assertEquals("", cipher.inputString);
+ verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString + "0");
+ verify(logger, times(1)).debug("Checking for letters");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
+ }
+
+ @Test
+ public void testSetInputStringDecode_blank(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+
+ 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 letters");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, times(1)).debug("Cleaned input string '{}'", "");
+ }
+
+ @Test
+ public void testSetInputStringDecode_cleanBlank(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+
+ 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 letters");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, times(1)).debug("Cleaned input string '{}'", "*");
+ }
+
+ @Test
+ public void testSetInputStringDecode_null(){
+ cipher.preserveWhitespace = true;
+ cipher.preserveSymbols = true;
+
+ assertThrows(InvalidInputException.class, () -> {
+ cipher.setInputStringDecode(null);
+ });
+
+ assertEquals("", cipher.inputString);
+ verify(logger, never()).debug(eq("Setting input string for decoding '{}'"), anyString());
+ verify(logger, never()).debug("Checking for letters");
+ verify(logger, never()).debug("Removing whitespace");
+ verify(logger, never()).debug("Removing symbols");
+ verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
+ }
+
+ @Test
+ public void testGetPreparedInputStringEncode(){
+ cipher.inputString = inputString;
+
+ String output = cipher.getPreparedInputStringEncode();
+
+ assertEquals(inputStringClean.replaceAll("\\s", ""), output);
+ verify(logger, times(1)).debug("Preparing input string for encoding");
+ verify(logger, times(1)).debug("Prepared string '{}'", inputStringClean.replaceAll("\\s", ""));
+ }
+
+ @Test
+ public void testGetPreparedInputStringDecode(){
+ cipher.inputString = outputString;
+
+ String output = cipher.getPreparedInputStringDecode();
+
+ assertEquals(outputStringClean.replaceAll("\\s", ""), output);
+ verify(logger, times(1)).debug("Preparing input string for decoding");
+ verify(logger, times(1)).debug("Prepared string '{}'", outputStringClean.replaceAll("\\s", ""));
+ }
+
+ @Test
+ public void testSetKeyword(){
+ cipher.setKeyword(keyword);
+
+ 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 non-letter characters");
+ verify(logger, times(1)).debug("Appending entire alphabet");
+ verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer);
+ verify(logger, times(1)).debug("Cleaned keyword {}", keywordClean);
+ }
+
+ @Test
+ public void testSetKeyword_blank(){
+ char[][] gridBlank = new char[][]{
+ {'A', 'B', 'C', 'D', 'E'},
+ {'F', 'G', 'H', 'I', 'K'},
+ {'L', 'M', 'N', 'O', 'P'},
+ {'Q', 'R', 'S', 'T', 'U'},
+ {'V', 'W', 'X', 'Y', 'Z'}
+ };
+
+ cipher.setKeyword("");
+
+ assertEquals("ABCDEFGHIKLMNOPQRSTUVWXYZ", cipher.keyword);
+ assertArrayEquals(gridBlank, cipher.grid);
+ verify(logger, times(1)).debug("Original keyword {}", "");
+ verify(logger, times(1)).debug("Removing case");
+ verify(logger, times(1)).debug("Removing all non-letter characters");
+ verify(logger, times(1)).debug("Appending entire alphabet");
+ verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer);
+ verify(logger, times(1)).debug("Cleaned keyword {}", "ABCDEFGHIKLMNOPQRSTUVWXYZ");
+ }
+
+ @Test
+ public void testSetKeyword_null(){
+ assertThrows(InvalidKeywordException.class, () -> {
+ cipher.setKeyword(null);
+ });
+
+ assertEquals("", cipher.keyword);
+ assertArrayEquals(new char[5][5], cipher.grid);
+ verify(logger, never()).debug(eq("Original keyword {}"), anyString());
+ verify(logger, never()).debug("Removing case");
+ verify(logger, never()).debug("Removing all non-letter characters");
+ verify(logger, never()).debug("Appending entire alphabet");
+ verify(logger, never()).debug(eq("Replacing {} with {}"), anyChar(), anyChar());
+ verify(logger, never()).debug(eq("Cleaned keyword {}"), anyString());
+ }
+
+ @Test
+ public void testFindChar(){
+ cipher.grid = grid;
+
+ CharLocation returnedLocation = cipher.findChar('A');
+
+ assertEquals(1, returnedLocation.getX());
+ assertEquals(2, returnedLocation.getY());
+ verify(logger, times(1)).debug("Finding {} in grid", 'A');
+ verify(logger, times(1)).debug("Found at {}, {}", 1, 2);
+ }
+
+ @Test
+ public void testFindChar_invalid(){
+ cipher.grid = grid;
+
+ assertThrows(InvalidInputException.class, () -> {
+ cipher.findChar(cipher.replaced);
+ });
+
+ verify(logger, times(1)).debug("Finding {} in grid", cipher.replaced);
+ verify(logger, never()).debug(eq("Found at {}, {}"), anyInt(), anyInt());
+ }
+
+ @Test
+ public void testAddCharactersToCleanStringEncode(){
+ cipher.inputString = inputString;
+
+ cipher.addCharactersToCleanStringEncode(outputStringClean.replaceAll("\\D", ""));
+
+ assertEquals(outputString, 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);
+ }
+
+ @Test
+ public void testAddCharactersToCleanStringDecode(){
+ cipher.inputString = outputString;
+
+ cipher.addCharactersToCleanStringDecode(inputStringClean.replaceAll("\\s", ""));
+
+ assertEquals(inputString.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());
+ }
+
+ @Test
+ public void testEncode(){
+ cipher.inputString = inputString.toUpperCase();
+ cipher.keyword = keywordClean;
+ cipher.grid = grid;
+
+ cipher.encode();
+
+ assertEquals(outputString, 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());
+ }
+
+ @Test
+ public void testDecode(){
+ cipher.inputString = outputString;
+ cipher.keyword = keywordClean;
+ cipher.grid = grid;
+
+ cipher.decode();
+
+ assertEquals(inputString.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());
+ }
+
+ @Test
+ public void testGetters(){
+ cipher.inputString = inputString;
+ cipher.keyword = keywordClean;
+ cipher.outputString = outputString;
+ cipher.grid = grid;
+
+ assertEquals(inputString, cipher.getInputString());
+ assertEquals(keywordClean, cipher.getKeyword());
+ assertEquals(outputString, 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());
+ }
+
+ @Test
+ public void testReset(){
+ cipher.inputString = inputString;
+ cipher.keyword = keyword;
+ cipher.outputString = outputString;
+ cipher.grid = grid;
+
+ cipher.reset();
+
+ assertEquals("", cipher.inputString);
+ assertEquals("", cipher.keyword);
+ assertEquals("", cipher.outputString);
+ assertArrayEquals(new char[5][5], cipher.grid);
+ verify(logger, times(1)).debug("Resetting fields");
+ }
+
+
+ @Test
+ public void testPracticalEncode(){
+ cipher = new PolybiusSquare(true, true);
- //Test simple encoding
- String inputString = "BAT";
- String keyword = "";
- String correctOutput = "121144";
String output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace encoding
- inputString = "B A T";
- keyword = "";
- correctOutput = "12 11 44";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol encoding
- inputString = "B@A+T-";
- keyword = "";
- correctOutput = "12@11+44-";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace, symbol decoding
- inputString = "B A-T";
- keyword = "";
- correctOutput = "12 11-44";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace, symbol decoding with mangled keyword
- inputString = "B A-T";
- keyword = "Z Y+ X-";
- correctOutput = "15 14-52";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
+ assertEquals(inputString.toUpperCase(), cipher.inputString);
+ assertEquals(keywordClean, cipher.keyword);
+ assertEquals(outputString, cipher.outputString);
+ assertEquals(outputString, output);
+ assertArrayEquals(grid, cipher.grid);
}
- @Test
- public void testNoWhitespaceEncode() throws InvalidCharacterException, InvalidInputException{
- PolybiusSquare cipher = new PolybiusSquare(false, true);
- //Test simple encoding
- String inputString = "BAT";
- String keyword = "";
- String correctOutput = "121144";
+ @Test
+ public void testPracticalEncode_clean(){
+ cipher = new PolybiusSquare(false, false);
+
String output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace encoding
- inputString = "B A T";
- keyword = "";
- correctOutput = "121144";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol encoding
- inputString = "B@A+T-";
- keyword = "";
- correctOutput = "12@11+44-";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace, symbol decoding
- inputString = "B A-T";
- keyword = "";
- correctOutput = "1211-44";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace, symbol decoding with mangled keyword
- inputString = "B A-T";
- keyword = "Z Y+ X-";
- correctOutput = "1514-52";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
+ assertEquals(inputStringClean, cipher.inputString);
+ assertEquals(keywordClean, cipher.keyword);
+ assertEquals(outputStringClean, cipher.outputString);
+ assertEquals(outputStringClean, output);
+ assertArrayEquals(grid, cipher.grid);
}
+
@Test
- public void testNoSymbolEncode() throws InvalidCharacterException, InvalidInputException{
- PolybiusSquare cipher = new PolybiusSquare(true, false);
+ public void testPracticalDecode(){
+ cipher = new PolybiusSquare(true, true);
- //Test simple encoding
- String inputString = "BAT";
- String keyword = "";
- String correctOutput = "121144";
- String output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
+ String output = cipher.decode(keyword, outputString);
- //Test whitespace encoding
- inputString = "B A T";
- keyword = "";
- correctOutput = "12 11 44";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol encoding
- inputString = "B@A+T-";
- keyword = "";
- correctOutput = "121144";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace, symbol decoding
- inputString = "B A-T";
- keyword = "";
- correctOutput = "12 1144";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace, symbol decoding with mangled keyword
- inputString = "B A-T";
- keyword = "Z Y+ X-";
- correctOutput = "15 1452";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
+ assertEquals(outputString, cipher.inputString);
+ assertEquals(keywordClean, cipher.keyword);
+ assertEquals(inputString.toUpperCase(), cipher.outputString);
+ assertEquals(inputString.toUpperCase(), output);
+ assertArrayEquals(grid, cipher.grid);
}
+
@Test
- public void testNoWhitespaceSymbolEncode() throws InvalidCharacterException, InvalidInputException{
- PolybiusSquare cipher = new PolybiusSquare(false, false);
+ public void testPracticalDecode_clean(){
+ cipher = new PolybiusSquare(false, false);
- //Test simple encoding
- String inputString = "BAT";
- String keyword = "";
- String correctOutput = "12 11 44";
- String output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
+ String output = cipher.decode(keyword, outputString);
- //Test whitespace encoding
- inputString = "B A T";
- keyword = "";
- correctOutput = "12 11 44";
- assertEquals(correctOutput, output);
- output = cipher.encode(keyword, inputString);
-
- //Test symbol encoding
- inputString = "B@A+T-";
- keyword = "";
- correctOutput = "12 11 44";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace, symbol decoding
- inputString = "B A-T";
- keyword = "";
- correctOutput = "12 11 44";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace, symbol decoding with mangled keyword
- inputString = "B A-T";
- keyword = "Z Y+ X-";
- correctOutput = "15 14 52";
- output = cipher.encode(keyword, inputString);
- assertEquals(correctOutput, output);
- }
- @Test
- public void testDecode() throws InvalidCharacterException, InvalidInputException{
- PolybiusSquare cipher = new PolybiusSquare(true, true);
-
- //Test simple decoding
- String inputString = "121144";
- String keyword = "";
- String correctOutput = "BAT";
- String output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace decoding
- inputString = "12 11 44";
- keyword = "";
- correctOutput = "B A T";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol decoding
- inputString = "12@11+44-";
- keyword = "";
- correctOutput = "B@A+T-";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace, symbol decoding
- inputString = "12 11-44";
- keyword = "";
- correctOutput = "B A-T";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace, symbol decoding with mangled keyword
- inputString = "15 14-52";
- keyword = "Z Y+ X-";
- correctOutput = "B A-T";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- }
- @Test
- public void testNoWhitespaceDecode() throws InvalidCharacterException, InvalidInputException{
- PolybiusSquare cipher = new PolybiusSquare(false, true);
-
- //Test simple decoding
- String inputString = "121144";
- String keyword = "";
- String correctOutput = "BAT";
- String output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace decoding
- inputString = "12 11 44";
- keyword = "";
- correctOutput = "BAT";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol decoding
- inputString = "12@11+44-";
- keyword = "";
- correctOutput = "B@A+T-";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace, symbol decoding
- inputString = "12 11-44";
- keyword = "";
- correctOutput = "BA-T";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace, symbol decoding with mangled keyword
- inputString = "15 14-52";
- keyword = "Z Y+ X-";
- correctOutput = "BA-T";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- }
- @Test
- public void testNoSymbolDeocde() throws InvalidCharacterException, InvalidInputException{
- PolybiusSquare cipher = new PolybiusSquare(true, false);
-
- //Test simple decoding
- String inputString = "121144";
- String keyword = "";
- String correctOutput = "BAT";
- String output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace decoding
- inputString = "12 11 44";
- keyword = "";
- correctOutput = "B A T";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol decoding
- inputString = "12@11+44-";
- keyword = "";
- correctOutput = "BAT";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace, symbol decoding
- inputString = "12 11-44";
- keyword = "";
- correctOutput = "B AT";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace, symbol decoding with mangled keyword
- inputString = "15 14-52";
- keyword = "Z Y+ X-";
- correctOutput = "B AT";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- }
- @Test
- public void testNoWhitespaceSymbolDecode() throws InvalidCharacterException, InvalidInputException{
- PolybiusSquare cipher = new PolybiusSquare(false, false);
-
- //Test simple decoding
- String inputString = "121144";
- String keyword = "";
- String correctOutput = "BAT";
- String output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace decoding
- inputString = "12 11 44";
- keyword = "";
- correctOutput = "BAT";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test symbol decoding
- inputString = "12@11+44-";
- keyword = "";
- correctOutput = "BAT";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
-
- //Test whitespace, symbol decoding
- inputString = "12 11-44";
- keyword = "";
- correctOutput = "BAT";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
- //Test whitespace, symbol decoding with mangled keyword
- inputString = "15 14-52";
- keyword = "Z Y+ X-";
- correctOutput = "BAT";
- output = cipher.decode(keyword, inputString);
- assertEquals(correctOutput, output);
+ assertEquals(outputString.replaceAll("\\s", "").replaceAll("[^0-9]", ""), cipher.inputString);
+ assertEquals(keywordClean, cipher.keyword);
+ assertEquals(inputStringClean.replaceAll("\\s", ""), cipher.outputString);
+ assertEquals(inputStringClean.replaceAll("\\s", ""), output);
+ assertArrayEquals(grid, cipher.grid);
}
}