Updating tests

This commit is contained in:
2023-05-05 20:19:27 -04:00
parent b3ca4754ea
commit 9c41f10576
45 changed files with 2729 additions and 2239 deletions

33
pom.xml
View File

@@ -44,25 +44,32 @@
<version>2.0.7</version> <version>2.0.7</version>
</dependency> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<!--Test--> <!--Test-->
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId> <artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version> <version>5.9.3</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<version>5.3.0</version> <version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.simplify4u</groupId>
<artifactId>slf4j2-mock</artifactId>
<version>2.3.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
@@ -120,6 +127,10 @@
<artifactId>maven-deploy-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version> <version>3.1.1</version>
</plugin> </plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.5.0</version>
</plugin>
<!--Site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle--> <!--Site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle-->
<plugin> <plugin>
<artifactId>maven-site-plugin</artifactId> <artifactId>maven-site-plugin</artifactId>
@@ -127,7 +138,7 @@
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-project-info-reports-plugin</artifactId> <artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.4.2</version> <version>3.4.3</version>
</plugin> </plugin>
<!--Versions--> <!--Versions-->
<plugin> <plugin>
@@ -152,7 +163,7 @@
<plugin> <plugin>
<groupId>org.jacoco</groupId> <groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId> <artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.9</version> <version>0.8.10</version>
<executions> <executions>
<execution> <execution>
<id>jacoco-initialize</id> <id>jacoco-initialize</id>

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java
//Mattrixwv //Mattrixwv
// Created: 01-26-22 // Created: 01-26-22
//Modified: 04-21-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.combination; package com.mattrixwv.cipherstream.combination;
@@ -16,16 +16,16 @@ import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare;
public class ADFGVX{ public class ADFGVX{
protected static Logger logger = LoggerFactory.getLogger(ADFGVX.class); private static final Logger logger = LoggerFactory.getLogger(ADFGVX.class);
//Fields //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 inputString; //The string that needs encoded/decoded
protected String outputString; //The string that is output after encoding/decoding protected String outputString; //The string that is output after encoding/decoding
protected String squareKeyword; //The keyword used in the Polybius Square protected String squareKeyword; //The keyword used in the Polybius Square
protected String keyword; //The Keyword used in the Columnar cipher 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 //Internal ciphers
protected LargePolybiusSquare largePolybiusSquare; //The first step in encoding protected LargePolybiusSquare largePolybiusSquare; //The first step in encoding
protected Columnar columnar; //The second 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"); throw new InvalidKeywordException("Square Keyword cannot be null");
} }
logger.debug("squareKeyword = {}", squareKeyword); logger.debug("squareKeyword '{}'", squareKeyword);
this.squareKeyword = squareKeyword; this.squareKeyword = squareKeyword;
} }
//Ensures Columnar keyword constraints //Ensures Columnar keyword constraints
@@ -46,7 +46,7 @@ public class ADFGVX{
throw new InvalidKeywordException("Keyword cannot be null"); throw new InvalidKeywordException("Keyword cannot be null");
} }
logger.debug("keyword = {}", keyword); logger.debug("keyword '{}'", keyword);
this.keyword = keyword; this.keyword = keyword;
} }
//Ensures inputString constraints //Ensures inputString constraints
@@ -210,23 +210,20 @@ public class ADFGVX{
return decode(); return decode();
} }
//Returns the cleaned inputString //Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }
//Returns the outputString
public String getOutputString(){ public String getOutputString(){
return outputString; return outputString;
} }
//Returns the cleaned Polybius Square keyword
public String getSquareKeyword(){ public String getSquareKeyword(){
return squareKeyword; return squareKeyword;
} }
//Returns the cleaned Columnar keyword
public String getKeyword(){ public String getKeyword(){
return keyword; return keyword;
} }
//Makes sure all of the variables are empty //Makes sure all variables are empty
public void reset() throws InvalidCharacterException{ public void reset() throws InvalidCharacterException{
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java
//Mattrixwv //Mattrixwv
// Created: 01-25-22 // Created: 01-25-22
//Modified: 04-14-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.combination; package com.mattrixwv.cipherstream.combination;
@@ -16,16 +16,17 @@ import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
public class ADFGX{ public class ADFGX{
protected static Logger logger = LoggerFactory.getLogger(ADFGX.class); private static final Logger logger = LoggerFactory.getLogger(ADFGX.class);
//Internal fields //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 outputString; //The string that is output after encoding/decoding
protected String squareKeyword; //The keyword used in the Polybius Square protected String squareKeyword; //The keyword used in the Polybius Square
protected String keyword; //The keyword used in the Columnar cipher protected String keyword; //The keyword used in the Columnar cipher
protected boolean preserveCapitals; //Whether to respect capitals in the output string //Settings
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Internal ciphers //Internal ciphers
protected PolybiusSquare polybiusSquare; //The first step in encoding protected PolybiusSquare polybiusSquare; //The first step in encoding
protected Columnar columnar; //The second 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"); throw new InvalidKeywordException("Square keyword cannot be null");
} }
logger.debug("squareKeyword = {}", squareKeyword); logger.debug("Square keyword '{}'", squareKeyword);
this.squareKeyword = squareKeyword; this.squareKeyword = squareKeyword;
} }
//Ensures Columnar keyword constraints //Ensures Columnar keyword constraints
@@ -46,7 +47,7 @@ public class ADFGX{
throw new InvalidKeywordException("Keyword cannot be null"); throw new InvalidKeywordException("Keyword cannot be null");
} }
logger.debug("keyword = {}", keyword); logger.debug("Keyword '{}'", keyword);
this.keyword = keyword; this.keyword = keyword;
} }
//Ensures inputString constraints //Ensures inputString constraints
@@ -74,10 +75,10 @@ public class ADFGX{
this.inputString = inputString; this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank()){ if(this.inputString.isBlank()){
throw new InvalidInputException("Input cannot be blank"); 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 //Format the output string with capitals, symbols, and numbers that are in the input string
protected void formatOutputStringEncode(){ protected void formatOutputStringEncode(){
@@ -209,23 +210,20 @@ public class ADFGX{
return outputString; return outputString;
} }
//Returns the cleaned inputString //Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }
//Returns the outputString
public String getOutputString(){ public String getOutputString(){
return outputString; return outputString;
} }
//Returns the cleaned Polybius Square keyword
public String getSquareKeyword(){ public String getSquareKeyword(){
return squareKeyword; return squareKeyword;
} }
//Returns the cleaned Columnar keyword
public String getKeyword(){ public String getKeyword(){
return keyword; return keyword;
} }
//Makes sure all of the variables are empty //Makes sure all variables are empty
public void reset() throws InvalidCharacterException{ public void reset() throws InvalidCharacterException{
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Affine.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Affine.java
//Mattrixwv //Mattrixwv
// Created: 01-26-22 // Created: 01-26-22
//Modified: 04-15-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -15,16 +15,17 @@ import com.mattrixwv.NumberAlgorithms;
public class Affine{ public class Affine{
protected static Logger logger = LoggerFactory.getLogger(Affine.class); private static final Logger logger = LoggerFactory.getLogger(Affine.class);
//Fields //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 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 key1; //The multiplicative key. Key1 must be relatively prime to 26
protected int key2; //The additive key 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 //Ensures key1 constraints
protected void setKey1(int key1) throws InvalidKeywordException{ protected void setKey1(int key1) throws InvalidKeywordException{
@@ -225,19 +226,16 @@ public class Affine{
return outputString; return outputString;
} }
//Returns the cleaned inputString //Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }
//Returns the outputString
public String getOutputString(){ public String getOutputString(){
return outputString; return outputString;
} }
//Returns the cleaned key1
public int getKey1(){ public int getKey1(){
return key1; return key1;
} }
//Returns the cleaned key2
public int getKey2(){ public int getKey2(){
return key2; return key2;
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java
//Mattrixwv //Mattrixwv
// Created: 07-25-21 // Created: 07-25-21
//Modified: 04-15-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -12,17 +12,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Atbash{ 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 inputString; //Holds the string that needs encoded or decoded
protected String outputString; //The encoded/decoded string protected String outputString; //The encoded/decoded string
protected boolean preserveCapitals; //Whether to respect capitals in the output string //Settings
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveSymbols; //Whether to respect symbols 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 //Encodes inputString and stores in outputString
protected String encode(){ protected void encode(){
logger.debug("Encoding"); logger.debug("Encoding");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
//Step through every element in the inputString and shift it the correct amount //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 //Use either uppercase or lowercase for the base
//(letterbase + 25 - (currentChar - letterBase)) //(letterbase + 25 - (currentChar - letterBase))
if(Character.isUpperCase(currentChar)){ if(Character.isUpperCase(currentChar)){
logger.debug("Encoding uppercase");
output.append((char)(155 - currentChar)); output.append((char)(155 - currentChar));
} }
else{ else{
logger.debug("Encoding lowercase");
output.append((char)(219 - currentChar)); output.append((char)(219 - currentChar));
} }
} }
//Keep any punctuatio/whitespace the way it is //Keep any punctuatio/whitespace the way it is
else{ else{
logger.debug("Appending symbol");
output.append(currentChar); output.append(currentChar);
} }
} }
//Return the output //Return the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString(); outputString = output.toString();
return outputString; logger.debug("Saving output string '{}'", outputString);
} }
//Removes all invalid characters and sets inputString //Removes all invalid characters and sets inputString
protected void setInputString(String inputString) throws InvalidInputException{ protected void setInputString(String inputString) throws InvalidInputException{
@@ -108,22 +114,22 @@ public class Atbash{
//Make sure everything is empty before you begin //Make sure everything is empty before you begin
reset(); reset();
setInputString(inputString); setInputString(inputString);
return encode(); encode();
return outputString;
} }
//Decodes inputString and returns the result //Decodes inputString and returns the result
public String decode(String inputString) throws InvalidInputException{ public String decode(String inputString) throws InvalidInputException{
return encode(inputString); return encode(inputString);
} }
//Returns the cleaned input string //Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }
//Returns the output string
public String getOutputString(){ public String getOutputString(){
return outputString; return outputString;
} }
//Makes sure all of the variables are empty //Makes sure all variables are empty
public void reset(){ public void reset(){
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
//Mattrixwv //Mattrixwv
// Created: 07-25-21 // Created: 07-25-21
//Modified: 04-15-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,7 +13,8 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Autokey extends Vigenere{ 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 //Special rules for setting the strings for encoding
protected void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ protected void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
@@ -57,7 +58,7 @@ public class Autokey extends Vigenere{
} }
//Decodes the inputString //Decodes the inputString
@Override @Override
protected String decode(){ protected void decode(){
logger.debug("Decoding"); logger.debug("Decoding");
//Decode what the key will allow, add that to the key and continue //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 //Save and return the results
outputString = fullOutput.toString(); outputString = fullOutput.toString();
logger.debug("Saving output string '{}'", outputString); 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{ public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset(); reset();
encodeSet(keyword, inputString); encodeSet(keyword, inputString);
return encode(); encode();
return outputString;
} }
//Decodes inputString using the Autokey cipher //Decodes inputString using the Autokey cipher
@Override @Override
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset(); reset();
decodeSet(keyword, inputString); decodeSet(keyword, inputString);
return decode(); decode();
return outputString;
} }
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
//Mattrixwv //Mattrixwv
// Created: 01-12-22 // Created: 01-12-22
//Modified: 04-15-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -17,7 +17,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Baconian{ public class Baconian{
protected static Logger logger = LoggerFactory.getLogger(Baconian.class); private static final Logger logger = LoggerFactory.getLogger(Baconian.class);
//Conversions //Conversions
protected static final ArrayList<String> code = new ArrayList<>(Arrays.asList( protected static final ArrayList<String> code = new ArrayList<>(Arrays.asList(
@@ -26,7 +26,8 @@ public class Baconian{
)); ));
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 outputString; //The encoded/decoded string
protected boolean preserveCapitals; //Whether to respect capitals in the output string protected boolean preserveCapitals; //Persist capitals in the output string
//Sets the input string //Sets the input string
protected void setInputStringEncode(String inputString) throws InvalidInputException{ protected void setInputStringEncode(String inputString) throws InvalidInputException{
@@ -121,7 +122,7 @@ public class Baconian{
logger.debug("Saving output string '{}'", outputString); logger.debug("Saving output string '{}'", outputString);
} }
//Decodes the inputString and stores the result in outputString //Decodes the inputString and stores the result in outputString
protected String decode(){ protected void decode(){
logger.debug("Decoding"); logger.debug("Decoding");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -154,7 +155,6 @@ public class Baconian{
//Save and return the output //Save and return the output
outputString = output.toString(); outputString = output.toString();
logger.debug("Saving output string '{}'", outputString); logger.debug("Saving output string '{}'", outputString);
return outputString;
} }
//Constructor //Constructor
@@ -166,14 +166,7 @@ public class Baconian{
reset(); reset();
this.preserveCapitals = preserveCapitals; 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 //Sets the inputString and encodes the message
public String encode(String inputString) throws InvalidInputException{ public String encode(String inputString) throws InvalidInputException{
reset(); reset();
@@ -185,9 +178,18 @@ public class Baconian{
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{ public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
reset(); reset();
setInputStringDecode(inputString); 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(){ public void reset(){
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/BaseX.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/BaseX.java
//Mattrixwv //Mattrixwv
// Created: 01-08-22 // Created: 01-08-22
//Modified: 04-16-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -16,12 +16,14 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
public class BaseX{ 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 inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string protected String outputString; //The encoded/decoded string
//Settings
protected int base; //The base that the number will be encoded at protected int base; //The base that the number will be encoded at
//Sets the input string //Sets the input string
protected void setInputStringEncode(String inputString) throws InvalidInputException{ protected void setInputStringEncode(String inputString) throws InvalidInputException{
if(inputString == null){ if(inputString == null){
@@ -80,7 +82,7 @@ public class BaseX{
this.base = base; this.base = base;
} }
//Encode inputString, store it in outputString, and return it //Encode inputString, store it in outputString, and return it
protected String encode(){ protected void encode(){
logger.debug("Encoding"); logger.debug("Encoding");
//Encode every character in inputString //Encode every character in inputString
@@ -99,12 +101,9 @@ public class BaseX{
//Save the output //Save the output
outputString = output.toString().toUpperCase(); outputString = output.toString().toUpperCase();
logger.debug("Saving output string '{}'", outputString); logger.debug("Saving output string '{}'", outputString);
//Return the output
return outputString;
} }
//Decode inputString, store it in outputString, and return it //Decode inputString, store it in outputString, and return it
protected String decode() throws InvalidCharacterException{ protected void decode() throws InvalidCharacterException{
logger.debug("Decoding"); logger.debug("Decoding");
//Decode every binary number in the string //Decode every binary number in the string
@@ -128,9 +127,6 @@ public class BaseX{
//Save the output //Save the output
outputString = output.toString(); outputString = output.toString();
logger.debug("Saving output string '{}'", outputString); logger.debug("Saving output string '{}'", outputString);
//Return the output
return outputString;
} }
//Constructor //Constructor
@@ -142,43 +138,47 @@ public class BaseX{
reset(); reset();
setBase(base); 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 //Sets the inputString and encodes the message
public String encode(String inputString) throws InvalidInputException{ public String encode(String inputString) throws InvalidInputException{
reset(); reset();
setInputStringEncode(inputString); setInputStringEncode(inputString);
return encode(); encode();
return outputString;
} }
public String encode(int base, String inputString) throws InvalidBaseException, InvalidInputException{ public String encode(int base, String inputString) throws InvalidBaseException, InvalidInputException{
reset(); reset();
setBase(base); setBase(base);
setInputStringEncode(inputString); setInputStringEncode(inputString);
return encode(); encode();
return outputString;
} }
//Sets the inputString and decodes the message //Sets the inputString and decodes the message
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{ public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
reset(); reset();
setInputStringDecode(inputString); setInputStringDecode(inputString);
return decode(); decode();
return outputString;
} }
public String decode(int base, String inputString) throws InvalidBaseException, InvalidCharacterException, InvalidInputException{ public String decode(int base, String inputString) throws InvalidBaseException, InvalidCharacterException, InvalidInputException{
reset(); reset();
setBase(base); setBase(base);
setInputStringDecode(inputString); 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(){ public void reset(){
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Beaufort.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Beaufort.java
//Mattrixwv //Mattrixwv
// Created: 02-23-22 // Created: 02-23-22
//Modified: 04-16-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,20 +13,21 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Beaufort{ public class Beaufort{
protected static Logger logger = LoggerFactory.getLogger(Beaufort.class); private static final Logger logger = LoggerFactory.getLogger(Beaufort.class);
//Fields //Fields
protected String inputString; //This is the string that needs encoded/decoded 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 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 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 //Settings
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Internal ciphers //Internal ciphers
protected Atbash atbash; //The first step in encoding/decoding the cipher protected Atbash atbash; //The first step in encoding/decoding the cipher
protected Caesar caesar; //The second step in encoding/decoding the cipher protected Caesar caesar; //The second step in encoding/decoding the cipher
protected Vigenere vigenere; //The third step in encoding/decoding the cipher protected Vigenere vigenere; //The third step in encoding/decoding the cipher
//Ensures inputString constraints //Ensures inputString constraints
public void setInputString(String inputString) throws InvalidInputException{ public void setInputString(String inputString) throws InvalidInputException{
//Make sure the input isn't null //Make sure the input isn't null
@@ -138,18 +139,7 @@ public class Beaufort{
vigenere = new Vigenere(preserveCapitals, preserveWhitespace, preserveSymbols); vigenere = new Vigenere(preserveCapitals, preserveWhitespace, preserveSymbols);
reset(); 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 //Encodes inputString using keyword and returns the result
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the parameters //Set the parameters
@@ -170,7 +160,18 @@ public class Beaufort{
decode(); decode();
return outputString; 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(){ public void reset(){
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Caesar.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Caesar.java
//Matthew Ellison //Matthew Ellison
// Created: 07-25-21 // Created: 07-25-21
//Modified: 04-16-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -12,15 +12,16 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Caesar{ public class Caesar{
protected static Logger logger = LoggerFactory.getLogger(Caesar.class); private static final Logger logger = LoggerFactory.getLogger(Caesar.class);
//Fields //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 outputString; //The encoded/decoded string
protected int shift; //The amount that you need to shift each letter protected int shift; //The amount that you need to shift each letter
protected boolean preserveCapitals; //Whether to respect capitals in the output string //Settings
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveSymbols; //Whether to respect symbols 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 //Sets shift and makes sure it is within the propper bounds
protected void setShift(int shiftAmount){ protected void setShift(int shiftAmount){
@@ -63,7 +64,7 @@ public class Caesar{
} }
} }
//Encodes the inputString and stores the result in outputString //Encodes the inputString and stores the result in outputString
protected String encode(){ protected void encode(){
logger.debug("Encoding"); logger.debug("Encoding");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -109,10 +110,9 @@ public class Caesar{
outputString = output.toString(); outputString = output.toString();
logger.debug("Saving encoded string '{}'", outputString); logger.debug("Saving encoded string '{}'", outputString);
return outputString;
} }
//Decodes the inputString and stores the result in outputString //Decodes the inputString and stores the result in outputString
protected String decode(){ protected void decode(){
logger.debug("Decoding"); logger.debug("Decoding");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -162,7 +162,6 @@ public class Caesar{
outputString = output.toString(); outputString = output.toString();
logger.debug("Saving decoded string '{}'", outputString); logger.debug("Saving decoded string '{}'", outputString);
return outputString;
} }
//Constructor //Constructor
@@ -184,29 +183,29 @@ public class Caesar{
reset(); reset();
setShift(shiftAmount); setShift(shiftAmount);
setInputString(inputString); setInputString(inputString);
return encode(); encode();
return outputString;
} }
//Sets the shift and inputString and decodes the message //Sets the shift and inputString and decodes the message
public String decode(int shiftAmount, String inputString) throws InvalidInputException{ public String decode(int shiftAmount, String inputString) throws InvalidInputException{
reset(); reset();
setShift(shiftAmount); setShift(shiftAmount);
setInputString(inputString); setInputString(inputString);
return decode(); decode();
return outputString;
} }
//Returns the inputString //Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }
//Returns shift
public int getShift(){ public int getShift(){
return shift; return shift;
} }
//Returns the outputString
public String getOutputString(){ public String getOutputString(){
return outputString; return outputString;
} }
//Makes sure all of the variables are empty //Makes sure all variables are empty
public void reset(){ public void reset(){
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/OneTimePad.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/OneTimePad.java
//Mattrixwv //Mattrixwv
// Created: 02-23-22 // Created: 02-23-22
//Modified: 07-09-22 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,10 +13,9 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class OneTimePad extends Vigenere{ 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 entropy calculator?
//?Add some kind of "book passage includer"?
//Constructor //Constructor

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Porta.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Porta.java
//Mattrixwv //Mattrixwv
// Created: 02-28-22 // Created: 02-28-22
//Modified: 04-17-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,7 +13,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Porta{ 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 = { private static final String[] tableau = {
"NOPQRSTUVWXYZABCDEFGHIJKLM", //A-B "NOPQRSTUVWXYZABCDEFGHIJKLM", //A-B
@@ -35,9 +35,11 @@ public class Porta{
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 outputString; //The encoded/decoded string
protected String keyword; //The keyword used to encode the input string protected String keyword; //The keyword used to encode the input string
protected boolean preserveCapitals; //Whether to respect capitals in the output string //Settings
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveSymbols; //Whether to respect symbols 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 //Ensure all keyword constraints are followed
protected void setKeyword(String keyword) throws InvalidKeywordException{ protected void setKeyword(String keyword) throws InvalidKeywordException{
@@ -210,19 +212,17 @@ public class Porta{
return outputString; return outputString;
} }
//Returns the inputString //Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }
//Returns the shift
public String getOutputString(){ public String getOutputString(){
return outputString; return outputString;
} }
//Returns the outputString
public String getKeyword(){ public String getKeyword(){
return keyword; return keyword;
} }
//Makes sure all of the fields are empty //Makes sure all fields are empty
public void reset(){ public void reset(){
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Substitution.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Substitution.java
//Mattrixwv //Mattrixwv
// Created: 02-22-22 // Created: 02-22-22
//Modified: 04-18-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,15 +13,16 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Substitution{ public class Substitution{
protected static Logger logger = LoggerFactory.getLogger(Substitution.class); private static final Logger logger = LoggerFactory.getLogger(Substitution.class);
//Fields //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 outputString; //The encoded/decoded string
protected String keyword; //The keyword used to encode/decode the input protected String keyword; //The keyword used to encode/decode the input
protected boolean preserveCapitals; //Whether to respect capitals in the output string //Getters
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveSymbols; //Whether to respect symbols 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 //Ensures key constraints are followed
protected void setKeyword(String key) throws InvalidKeywordException{ protected void setKeyword(String key) throws InvalidKeywordException{
@@ -172,6 +173,7 @@ public class Substitution{
logger.debug("Decoded message '{}'", outputString); logger.debug("Decoded message '{}'", outputString);
} }
//Constructors
public Substitution(){ public Substitution(){
preserveCapitals = false; preserveCapitals = false;
preserveWhitespace = false; preserveWhitespace = false;
@@ -184,6 +186,23 @@ public class Substitution{
this.preserveSymbols = preserveSymbols; this.preserveSymbols = preserveSymbols;
reset(); 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(){ public String getInputString(){
return inputString; return inputString;
} }
@@ -193,18 +212,7 @@ public class Substitution{
public String getKeyword(){ public String getKeyword(){
return keyword; return keyword;
} }
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{ //Makes sure all variables are empty
setKeyword(key);
setInputString(inputString);
encode();
return outputString;
}
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKeyword(key);
setInputString(inputString);
decode();
return outputString;
}
public void reset(){ public void reset(){
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Vigenere.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Vigenere.java
//Matthew Ellison //Matthew Ellison
// Created: 07-25-21 // Created: 07-25-21
//Modified: 04-18-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; package com.mattrixwv.cipherstream.monosubstitution;
@@ -16,16 +16,17 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Vigenere{ public class Vigenere{
protected static Logger logger = LoggerFactory.getLogger(Vigenere.class); private static final Logger logger = LoggerFactory.getLogger(Vigenere.class);
//Fields //Fields
protected String inputString; //This is the string that needs encoded/decoded 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 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 String keyword; //This is the keyword that is resposible for determining the offsets that you change each character by
protected ArrayList<Integer> offset; //Holds the offsets coputed from each character in the keyword protected ArrayList<Integer> offset; //Holds the offsets coputed from each character in the keyword
protected boolean preserveCapitals; //Whether to respect capitals in the output string //Settings
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveSymbols; //Whether to respect symbols 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 //Uses keyword to calculate the offset for the Caesar cipher for each character
protected void setOffset(){ protected void setOffset(){
@@ -102,7 +103,7 @@ public class Vigenere{
} }
} }
//Encodes inputString and stores the result in outputString //Encodes inputString and stores the result in outputString
protected String encode(){ protected void encode(){
logger.debug("Encoding"); logger.debug("Encoding");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -145,10 +146,9 @@ public class Vigenere{
//Save output //Save output
outputString = output.toString(); outputString = output.toString();
logger.debug("Encoded message '{}'", outputString); logger.debug("Encoded message '{}'", outputString);
return outputString;
} }
//Decodes inputString and stores the result in outputString //Decodes inputString and stores the result in outputString
protected String decode(){ protected void decode(){
logger.debug("Decoding"); logger.debug("Decoding");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -191,7 +191,6 @@ public class Vigenere{
//Save output //Save output
outputString = output.toString(); outputString = output.toString();
logger.debug("Decoded message '{}'", outputString); logger.debug("Decoded message '{}'", outputString);
return outputString;
} }
@@ -210,37 +209,39 @@ public class Vigenere{
this.preserveWhitespace = preserveWhitespace; this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols; 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<Integer> getOffsets(){
return offset;
}
//Encodes input using key and returns the result //Encodes input using key and returns the result
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset(); reset();
setKeyword(keyword); setKeyword(keyword);
setInputString(inputString); setInputString(inputString);
return encode(); encode();
return outputString;
} }
//Decodes input using key and returns the result //Decodes input using key and returns the result
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset(); reset();
setKeyword(keyword); setKeyword(keyword);
setInputString(inputString); 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<Integer> getOffsets(){
return offset;
}
//Makes sure all variables are empty
public void reset(){ public void reset(){
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Bifid.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Bifid.java
//Mattrixwv //Mattrixwv
// Created: 03-03-22 // Created: 03-03-22
//Modified: 04-23-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
@@ -14,16 +14,17 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Bifid{ public class Bifid{
protected static Logger logger = LoggerFactory.getLogger(Bifid.class); private static final Logger logger = LoggerFactory.getLogger(Bifid.class);
//Fields //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 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 PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters //Settings
protected boolean preserveCapitals; //Persist capitals in the output string protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace 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 //Strips invalid characters from the keyword and creates the grid
@@ -169,7 +170,7 @@ public class Bifid{
} }
//Constructor //Constructors
public Bifid() throws InvalidCharacterException{ public Bifid() throws InvalidCharacterException{
preserveCapitals = false; preserveCapitals = false;
preserveWhitespace = false; preserveWhitespace = false;
@@ -184,6 +185,7 @@ public class Bifid{
polybiusSquare = new PolybiusSquare(false, false); polybiusSquare = new PolybiusSquare(false, false);
reset(); reset();
} }
//Encodes inputString using keyword and returns the result //Encodes inputString using keyword and returns the result
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{ public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
//Set the parameters //Set the parameters
@@ -207,7 +209,7 @@ public class Bifid{
return outputString; return outputString;
} }
//Gets //Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }
@@ -217,6 +219,7 @@ public class Bifid{
public String getKeyword(){ public String getKeyword(){
return keyword; return keyword;
} }
//Makes sure all variables are empty //Makes sure all variables are empty
public void reset(){ public void reset(){
logger.debug("Resetting fields"); logger.debug("Resetting fields");

View File

@@ -1,7 +1,7 @@
//MattrixwvWebsite/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Columnar.java //MattrixwvWebsite/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Columnar.java
//Mattrixwv //Mattrixwv
// Created: 01-16-22 // Created: 01-16-22
//Modified: 04-26-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
@@ -18,8 +18,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Columnar{ public class Columnar{
protected static Logger logger = LoggerFactory.getLogger(Columnar.class); private static final Logger logger = LoggerFactory.getLogger(Columnar.class);
//Fields //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 outputString; //The encoded/decoded message
@@ -27,11 +26,13 @@ public class Columnar{
protected char characterToAdd; //The character that is added to the end of a string to bring it to the correct length 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<ArrayList<Character>> grid; //The grid used to encode/decode the message protected ArrayList<ArrayList<Character>> grid; //The grid used to encode/decode the message
//Settings
protected boolean preserveCapitals; //Persist capitals in the output string protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace 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
protected boolean removePadding; //Remove the padding letters added to the cipher protected boolean removePadding; //Remove the padding letters added to the cipher
//Strip the inputString of all non-letter characters and change them to capitals //Strip the inputString of all non-letter characters and change them to capitals
protected String getCleanInputString(){ protected String getCleanInputString(){
logger.debug("Cleaning input string"); logger.debug("Cleaning input string");
@@ -467,7 +468,7 @@ public class Columnar{
createOutputStringFromRows(); createOutputStringFromRows();
} }
//Constructor //Constructors
public Columnar() throws InvalidCharacterException{ public Columnar() throws InvalidCharacterException{
preserveCapitals = false; preserveCapitals = false;
preserveWhitespace = false; preserveWhitespace = false;
@@ -492,6 +493,7 @@ public class Columnar{
setCharacterToAdd(characterToAdd); setCharacterToAdd(characterToAdd);
reset(); reset();
} }
//Encodes inputString using keyword and returns the result //Encodes inputString using keyword and returns the result
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{ public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the parameters //Set the parameters
@@ -525,7 +527,8 @@ public class Columnar{
grid = new ArrayList<>(); grid = new ArrayList<>();
charsAdded = 0; charsAdded = 0;
} }
//Gets
//Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Hill.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Hill.java
//Mattrixwv //Mattrixwv
// Created: 01-31-22 // Created: 01-31-22
//Modified: 04-27-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
@@ -19,17 +19,19 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException;
public class Hill{ 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; //Validate and set the key
protected boolean preserveWhitespace;
protected boolean preserveSymbols;
protected String inputString;
protected String outputString;
protected char characterToAdd;
protected ModMatrix key;
protected void setKey(ModMatrix key) throws InvalidKeyException{ protected void setKey(ModMatrix key) throws InvalidKeyException{
logger.debug("Setting key"); logger.debug("Setting key");
@@ -55,9 +57,10 @@ public class Hill{
} }
//Set the key //Set the key
logger.debug("key = {}", key); logger.debug("key\n{}", key);
this.key = new ModMatrix(key); this.key = new ModMatrix(key);
} }
//Validate and set the input string
protected void setInputStringEncode(String inputString) throws InvalidInputException{ protected void setInputStringEncode(String inputString) throws InvalidInputException{
logger.debug("Setting input string for encoding"); 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"); 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(){ protected String getCleanInputString(){
logger.debug("Cleaning inputString"); logger.debug("Cleaning inputString");
@@ -150,6 +154,7 @@ public class Hill{
return cleanInputString; return cleanInputString;
} }
//Validate and set character to add
protected void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{ protected void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
logger.debug("Setting character to add {}", characterToAdd); logger.debug("Setting character to add {}", characterToAdd);
@@ -168,6 +173,7 @@ public class Hill{
logger.debug("Cleaned character {}", characterToAdd); logger.debug("Cleaned character {}", characterToAdd);
this.characterToAdd = characterToAdd; this.characterToAdd = characterToAdd;
} }
//Add capitalization, whitespace, and symbols to the output based on the input
protected String polishOutputString(){ protected String polishOutputString(){
logger.debug("Polishing output string"); logger.debug("Polishing output string");
@@ -197,6 +203,7 @@ public class Hill{
logger.debug("Polished string '{}'", cleanString); logger.debug("Polished string '{}'", cleanString);
return cleanString; return cleanString;
} }
//Get vectors representing the input string
protected ArrayList<ModMatrix> getInputVectors(){ protected ArrayList<ModMatrix> getInputVectors(){
logger.debug("Generating input vectors"); logger.debug("Generating input vectors");
@@ -230,6 +237,7 @@ public class Hill{
//Return the array of vectors //Return the array of vectors
return vectors; return vectors;
} }
//Get the string represented by the vectors that were passed in
protected String getOutputFromVectors(ArrayList<ModMatrix> outputVectors){ protected String getOutputFromVectors(ArrayList<ModMatrix> outputVectors){
logger.debug("Turning vectors into a string"); logger.debug("Turning vectors into a string");
@@ -249,6 +257,7 @@ public class Hill{
logger.debug("Converted string '{}'", convertedString); logger.debug("Converted string '{}'", convertedString);
return convertedString; return convertedString;
} }
//Encode inputString and store the value in outputString
protected void encode(){ protected void encode(){
logger.debug("Encoding"); logger.debug("Encoding");
@@ -272,6 +281,7 @@ public class Hill{
//Add the extra characters back to the output and remove the added characters //Add the extra characters back to the output and remove the added characters
outputString = polishOutputString(); outputString = polishOutputString();
} }
//Decode inputString and store the value in outputString
protected void decode(){ protected void decode(){
logger.debug("Decoding"); logger.debug("Decoding");
@@ -298,6 +308,7 @@ public class Hill{
outputString = polishOutputString(); outputString = polishOutputString();
} }
//Constructors
public Hill() throws InvalidCharacterException{ public Hill() throws InvalidCharacterException{
preserveCapitals = false; preserveCapitals = false;
preserveWhitespace = false; preserveWhitespace = false;
@@ -320,6 +331,7 @@ public class Hill{
reset(); reset();
} }
//Encode inputString using the provided key
public String encode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{ public String encode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{
return encode(new ModMatrix(key, 26), inputString); return encode(new ModMatrix(key, 26), inputString);
} }
@@ -329,6 +341,7 @@ public class Hill{
encode(); encode();
return outputString; return outputString;
} }
//Decode inputString using the provided key
public String decode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{ public String decode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{
return decode(new ModMatrix(key, 26), inputString); return decode(new ModMatrix(key, 26), inputString);
} }
@@ -338,6 +351,8 @@ public class Hill{
decode(); decode();
return outputString; return outputString;
} }
//Makes sure all variables are empty
public void reset(){ public void reset(){
logger.debug("Resetting fields"); logger.debug("Resetting fields");
@@ -345,6 +360,8 @@ public class Hill{
outputString = ""; outputString = "";
key = new ModMatrix(26); key = new ModMatrix(26);
} }
//Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java //CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java
//Mattrixwv //Mattrixwv
// Created: 04-21-23 // Created: 04-21-23
// Modified: 04-21-23 // Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
@@ -16,9 +16,10 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class LargePolybiusSquare extends PolybiusSquare{ 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 @Override
protected void createGrid(){ protected void createGrid(){
logger.debug("Creating grid"); logger.debug("Creating grid");
@@ -30,6 +31,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
} }
} }
} }
//Strips invalid characters from the string that needs encoded/decoded
@Override @Override
protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{ protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){ if(inputString == null){
@@ -73,6 +75,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
throw new InvalidInputException("Input must contain at least 1 letter"); throw new InvalidInputException("Input must contain at least 1 letter");
} }
} }
//Returns the input string ready for encoding
@Override @Override
protected String getPreparedInputStringEncode(){ protected String getPreparedInputStringEncode(){
logger.debug("Preparing input string for encoding"); logger.debug("Preparing input string for encoding");
@@ -84,6 +87,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
return cleanString; return cleanString;
} }
//Strips invalid characters from the keyword and creates the grid
@Override @Override
protected void setKeyword(String keyword) throws InvalidKeywordException{ protected void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){ if(keyword == null){
@@ -112,6 +116,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
//Create the grid from the sanitized keyword //Create the grid from the sanitized keyword
createGrid(); createGrid();
} }
//Adds characters that aren't letters to the output
@Override @Override
protected void addCharactersToCleanStringEncode(String cleanString){ protected void addCharactersToCleanStringEncode(String cleanString){
logger.debug("Formatting output string"); logger.debug("Formatting output string");
@@ -166,6 +171,8 @@ public class LargePolybiusSquare extends PolybiusSquare{
outputString = fullOutput.toString(); outputString = fullOutput.toString();
logger.debug("Saving output string {}", outputString); logger.debug("Saving output string {}", outputString);
} }
//Makes sure all variables are empty
@Override @Override
public void reset(){ public void reset(){
logger.debug("Resetting"); logger.debug("Resetting");
@@ -176,6 +183,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
keyword = ""; keyword = "";
} }
//Constructors
public LargePolybiusSquare() throws InvalidCharacterException{ public LargePolybiusSquare() throws InvalidCharacterException{
super(); super();
} }

View File

@@ -1,94 +1,151 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Morse.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Morse.java
//Matthew Ellison //Mattrixwv
// Created: 07-28-21 // Created: 07-28-21
//Modified: 01-16-22 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; 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{ public class Morse{
//TODO: This needs updated to match new standards private static final Logger logger = LoggerFactory.getLogger(Morse.class);
//Holds the Morse representation of the alphanumeric characters //Code representations
private static final String[] code = { private static final String[] letters = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", //A-L ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", //A-L
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", //M-Z "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." //M-Z
};
private static final String[] numbers = {
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." //0-9 "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." //0-9
}; };
private String inputString; //The string that needs encoded/decoded private static final Map<String, Character> map; //The map to help convert from Morse to letters and numbers
private String outputString; //The encoded/decoded message 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(){ //Validate and set the input string for encoding
StringBuilder output = new StringBuilder(); protected void setInputStringEncode(String inputString){
//Loop through every element in the input string and see what type it is 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()){ 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)){ if(Character.isUpperCase(letter)){
output.append(code[letter - 65]); logger.debug("Appending letter");
output.append(' ');
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)){ else if(Character.isDigit(letter)){
int tempNum = Integer.parseInt(Character.toString(letter)); logger.debug("Appending number");
output.append(code[tempNum + 26]);
output.append(' '); 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 //Save the output
outputString = output.toString(); this.outputString = output.toString();
return outputString; logger.debug("Saving encoded string '{}'", outputString);
} }
//Decodes inputString and stores the result in outputString //Decodes the inputString and stores the result in outputString
private String decode(){ protected void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
//Loop through every element in the input string and encode it
for(String current : inputString.split(" ")){ for(String current : inputString.split(" ")){
boolean found = false; logger.debug("Working letter {}", current);
//Loop through the code and see if the current letter
for(int cnt = 0;(cnt < 26) && (!found);++cnt){ //Get the current letter from the map and append it to the output
//See if current is the same as an element in code if(map.containsKey(current)){
if(current.equals(code[cnt])){ char letter = map.get(current);
//Add 65 to cnt to get the correct capital letter logger.debug("Decoded letter {}", letter);
output.append((char)(cnt + 'A')); output.append(letter);
found = true;
} }
} else{
//Loop through code and see if current is a number throw new InvalidInputException("Invalid characters found in input");
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("<Unknown symbol: " + current + ">");
} }
} }
//Save and return the output //Save the output
outputString = output.toString(); this.outputString = output.toString();
return outputString; logger.debug("Saving decoded string '{}'", 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;
} }
@@ -96,26 +153,34 @@ public class Morse{
public Morse(){ public Morse(){
reset(); 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(){ public String getInputString(){
return inputString; return inputString;
} }
//Returns outputString
public String getOutputString(){ public String getOutputString(){
return outputString; 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 //Makes sure all variables are empty
public void reset(){ public void reset(){
inputString = outputString = ""; logger.debug("Resetting");
inputString = "";
outputString = "";
} }
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java
//Matthew Ellison //Matthew Ellison
// Created: 07-30-21 // Created: 07-30-21
//Modified: 04-28-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
@@ -16,7 +16,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Playfair{ 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 //A class representing the location of a character in the grid
protected class CharLocation{ protected class CharLocation{
@@ -35,16 +35,17 @@ public class Playfair{
} }
//Fields //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 inputString; //The message that needs to be encoded/decoded
protected String outputString; //The encoded/decoded message 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[][] grid; //The grid used to encode/decode the message 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 //Set the doubled character
@@ -482,6 +483,7 @@ public class Playfair{
addCharactersToCleanString(output.toString()); addCharactersToCleanString(output.toString());
} }
//Constructor
public Playfair() throws InvalidCharacterException{ public Playfair() throws InvalidCharacterException{
reset(); reset();
preserveCapitals = false; preserveCapitals = false;
@@ -509,6 +511,7 @@ public class Playfair{
setReplacer(replacer); setReplacer(replacer);
setDoubled(doubled); setDoubled(doubled);
} }
//Sets the keyword and inputString and encodes the message //Sets the keyword and inputString and encodes the message
public String encode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{ public String encode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{
reset(); reset();
@@ -535,7 +538,7 @@ public class Playfair{
outputString = ""; outputString = "";
keyword = ""; keyword = "";
} }
//Gets //Getters
public char getReplaced(){ public char getReplaced(){
return replaced; return replaced;
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/PolybiusSquare.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/PolybiusSquare.java
//Mattrixwv //Mattrixwv
// Created: 01-04-22 // Created: 01-04-22
//Modified: 04-29-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
@@ -16,7 +16,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class PolybiusSquare{ 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 //A class representing the location of a character in the grid
protected class CharLocation{ protected class CharLocation{
@@ -41,8 +41,9 @@ public class PolybiusSquare{
protected char[][] grid; //The grid used to encode/decode the message 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 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 char replacer; //The letter that replaces replaced in the input string or keyword
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string //Settings
protected boolean preserveSymbols; //Whether to respect symbols in the output string protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//Setting the character to be replaced //Setting the character to be replaced
@@ -362,6 +363,7 @@ public class PolybiusSquare{
addCharactersToCleanStringDecode(output.toString()); addCharactersToCleanStringDecode(output.toString());
} }
//Constructors
public PolybiusSquare() throws InvalidCharacterException{ public PolybiusSquare() throws InvalidCharacterException{
reset(); reset();
setReplaced('J'); setReplaced('J');
@@ -383,6 +385,7 @@ public class PolybiusSquare{
this.preserveWhitespace = preserveWhitespace; this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols; this.preserveSymbols = preserveSymbols;
} }
//Sets the keyword and inputString and encodes the message //Sets the keyword and inputString and encodes the message
public String encode(String inputString) throws InvalidCharacterException, InvalidInputException{ public String encode(String inputString) throws InvalidCharacterException, InvalidInputException{
return encode("", inputString); return encode("", inputString);
@@ -415,7 +418,8 @@ public class PolybiusSquare{
outputString = ""; outputString = "";
keyword = ""; keyword = "";
} }
//Gets
//Getters
public char getReplaced(){ public char getReplaced(){
return replaced; return replaced;
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/RailFence.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/RailFence.java
//Mattrixwv //Mattrixwv
// Created: 03-21-22 // Created: 03-21-22
//Modified: 07-09-22 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
@@ -16,17 +16,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
public class RailFence{ public class RailFence{
private static final Logger logger = LoggerFactory.getLogger(RailFence.class); private static final Logger logger = LoggerFactory.getLogger(RailFence.class);
//Fields //Fields
private String inputString; //The message that needs to be encoded/decoded protected String inputString; //The message that needs to be encoded/decoded
private String outputString; //The encoded/decoded message protected String outputString; //The encoded/decoded message
private StringBuilder[] fence; //The fence used for encoding/decoding protected StringBuilder[] fence; //The fence used for encoding/decoding
private boolean preserveCapitals; //Persist capitals in the output string //Settings
private boolean preserveWhitespace; //Persist whitespace in the output string protected boolean preserveCapitals; //Persist capitals in the output string
private boolean preserveSymbols; //Persist symbols 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 //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 //Ensure the input string isn't null
if(inputString == null){ if(inputString == null){
throw new InvalidInputException("Input cannot be 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 //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){ if(numRails < 2){
throw new InvalidBaseException("You must use at least 2 rails"); 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 //Strip the inputString of all non-letter characters
private String getCleanInputString(){ protected String getCleanInputString(){
logger.debug("Getting input string for encoding"); logger.debug("Getting input string for encoding");
return inputString.replaceAll("[^a-zA-Z]", ""); return inputString.replaceAll("[^a-zA-Z]", "");
} }
//Ensures capitals, lowercase, and symbols are displayed in the output string //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"); logger.debug("Formatting output string");
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -105,11 +106,11 @@ public class RailFence{
} }
} }
logger.debug("Formatted output '{}'", output);
this.outputString = output.toString(); this.outputString = output.toString();
logger.debug("Formatted output '{}'", this.outputString);
} }
//Returns the decoded string found in the fence after all characters are placed correctly //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"); logger.debug("Getting decoded string from the fence");
boolean down = true; boolean down = true;
@@ -118,7 +119,7 @@ public class RailFence{
int insideCol = -1; int insideCol = -1;
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
while(true){ 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(rail == 0){
if(outsideCol >= fence[rail].length()){ if(outsideCol >= fence[rail].length()){
break; break;
@@ -161,13 +162,13 @@ public class RailFence{
return output.toString(); return output.toString();
} }
//Encodes inputString using the RailFence cipher and stores the result in outputString //Encodes inputString using the RailFence cipher and stores the result in outputString
private void encode(){ protected void encode(){
logger.debug("Encoding"); logger.debug("Encoding");
boolean up = true; boolean up = true;
int rail = 0; int rail = 0;
for(char ch : getCleanInputString().toCharArray()){ for(char ch : getCleanInputString().toCharArray()){
logger.debug("Working character {}", ch); logger.debug("Working character '{}'", ch);
fence[rail].append(ch); fence[rail].append(ch);
//Advance to the next rail //Advance to the next rail
@@ -204,7 +205,7 @@ public class RailFence{
formatOutput(output.toString()); formatOutput(output.toString());
} }
//Decodes inputString using the RailFence cipher and stores the result in outputString //Decodes inputString using the RailFence cipher and stores the result in outputString
private void decode(){ protected void decode(){
logger.debug("Decoding"); logger.debug("Decoding");
//Determine the number of characters on each rail //Determine the number of characters on each rail
@@ -260,11 +261,13 @@ public class RailFence{
preserveCapitals = false; preserveCapitals = false;
preserveWhitespace = false; preserveWhitespace = false;
preserveSymbols = false; preserveSymbols = false;
reset();
} }
public RailFence(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){ public RailFence(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
this.preserveCapitals = preserveCapitals; this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace; this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols; this.preserveSymbols = preserveSymbols;
reset();
} }
//Encodes inputString using a Rail Fence of length numRails and returns the result //Encodes inputString using a Rail Fence of length numRails and returns the result
@@ -296,7 +299,8 @@ public class RailFence{
outputString = ""; outputString = "";
fence = null; fence = null;
} }
//Gets
//Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Trifid.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Trifid.java
//Mattrixwv //Mattrixwv
// Created: 03-03-22 // Created: 03-03-22
//Modified: 03-03-22 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
@@ -21,10 +21,10 @@ public class Trifid{
private static final Logger logger = LoggerFactory.getLogger(Trifid.class); private static final Logger logger = LoggerFactory.getLogger(Trifid.class);
//A class representing the location of a character in the grid //A class representing the location of a character in the grid
private class CharLocation{ protected class CharLocation{
private int x; protected int x;
private int y; protected int y;
private int z; protected int z;
public CharLocation(int x, int y, int z){ public CharLocation(int x, int y, int z){
this.x = x; this.x = x;
this.y = y; this.y = y;
@@ -45,18 +45,19 @@ public class Trifid{
} }
//Fields //Fields
private String inputString; //The message that needs to be encoded/decoded protected String inputString; //The message that needs to be encoded/decoded
private String outputString; //The encoded/decoded message protected String outputString; //The encoded/decoded message
private String keyword; //The keyword used to create the grid protected String keyword; //The keyword used to create the grid
private int groupSize; //The size of the groups used to break up the input protected int groupSize; //The size of the groups used to break up the input
private char[][][] grid; //The grid used to encode/decode the message protected 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 protected char fillIn; //The character added to the alphabet to meet the 27 character requirement
private boolean preserveCapitals; //Persist capitals in the output string //Settings
private boolean preserveWhitespace; //Persist whitespace in the output string protected boolean preserveCapitals; //Persist capitals in the output string
private boolean preserveSymbols; //Persist symbols 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 //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 //Make sure the character is a printing character
if((fillIn < ' ') || (fillIn > '~')){ if((fillIn < ' ') || (fillIn > '~')){
throw new InvalidCharacterException("Fill in character must be a printing character"); throw new InvalidCharacterException("Fill in character must be a printing character");
@@ -72,7 +73,7 @@ public class Trifid{
this.fillIn = fillIn; this.fillIn = fillIn;
} }
//Strips invalid characters from the keyword and creates the grid //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 //Ensure the keyword isn't null
if(keyword == null){ if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null"); throw new InvalidKeywordException("Keyword cannot be null");
@@ -93,7 +94,7 @@ public class Trifid{
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + fillIn; keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + fillIn;
//Remove all duplicate characters //Remove all duplicate characters
logger.debug("Removing duplicated characters"); logger.debug("Removing duplicate characters");
StringBuilder uniqueKey = new StringBuilder(); StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c)); keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString(); this.keyword = uniqueKey.toString();
@@ -104,7 +105,7 @@ public class Trifid{
createGrid(); createGrid();
} }
//Creates the grid from the keyword //Creates the grid from the keyword
private void createGrid(){ protected void createGrid(){
logger.debug("Creating grid from keyword"); logger.debug("Creating grid from keyword");
for(int layerCnt = 0;layerCnt < grid.length;++layerCnt){ for(int layerCnt = 0;layerCnt < grid.length;++layerCnt){
@@ -120,7 +121,7 @@ public class Trifid{
logger.debug("Completed grid\n{}", getGrid()); logger.debug("Completed grid\n{}", getGrid());
} }
//Ensures groupSize constraints //Ensures groupSize constraints
private void setGroupSize(int groupSize) throws InvalidBaseException{ protected void setGroupSize(int groupSize) throws InvalidBaseException{
if(groupSize <= 0){ if(groupSize <= 0){
throw new InvalidBaseException("Group size must be > 0"); throw new InvalidBaseException("Group size must be > 0");
} }
@@ -130,7 +131,7 @@ public class Trifid{
this.groupSize = groupSize; this.groupSize = groupSize;
} }
//Ensures inputString constraints //Ensures inputString constraints
private void setInputString(String inputString) throws InvalidInputException{ protected void setInputString(String inputString) throws InvalidInputException{
//Ensure the input string isn't null //Ensure the input string isn't null
if(inputString == null){ if(inputString == null){
throw new InvalidInputException("Input cannot be null"); throw new InvalidInputException("Input cannot be null");
@@ -165,12 +166,12 @@ public class Trifid{
} }
} }
//Returns the inputString with only letters //Returns the inputString with only letters
private String getCleanInputString(){ protected String getCleanInputString(){
logger.debug("Cleaning input string for encoding"); logger.debug("Cleaning input string for encoding");
return inputString.toUpperCase().replaceAll("[^A-Z" + fillIn + "]", ""); return inputString.toUpperCase().replaceAll("[^A-Z" + fillIn + "]", "");
} }
//Returns the location of the given character in the grid //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); logger.debug("Finding character {} in grid", letter);
for(int layer = 0;layer < grid.length;++layer){ 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"); throw new InvalidCharacterException("The character '" + letter + "' was not found in the grid");
} }
//Return the character from the location provided //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){ if(location.getX() > 2){
throw new InvalidCharacterException("x cannot be larget than 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()]; return grid[location.getZ()][location.getX()][location.getY()];
} }
//Adds all non-letter characters back to the output string //Adds all non-letter characters back to the output string
private void formatOutput(String outputString){ protected void formatOutput(String outputString){
logger.debug("Formatting output"); logger.debug("Formatting output");
//Keep track of where you are in the output //Keep track of where you are in the output
@@ -222,6 +223,15 @@ public class Trifid{
logger.debug("Formatting lowercase"); logger.debug("Formatting lowercase");
output.append(Character.toLowerCase(outputString.charAt(outputCnt++))); 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{ else{
logger.debug("Appending symbol"); logger.debug("Appending symbol");
output.append(ch); output.append(ch);
@@ -229,15 +239,15 @@ public class Trifid{
} }
//Save the output //Save the output
logger.debug("Formatted output '{}'", output);
this.outputString = output.toString(); this.outputString = output.toString();
logger.debug("Formatted output '{}'", this.outputString);
} }
//Encodes inputString using a polybius square and stores the result in 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"); logger.debug("Encoding");
//Step through every element in the sanitized inputString encoding the letters //Step through every element in the sanitized inputString encoding the letters
logger.debug("Conveting letters to coordinates"); logger.debug("Converting letters to coordinates");
ArrayList<CharLocation> locations = new ArrayList<>(); ArrayList<CharLocation> locations = new ArrayList<>();
for(char ch : getCleanInputString().toCharArray()){ for(char ch : getCleanInputString().toCharArray()){
//Get the location of the char in the grid //Get the location of the char in the grid
@@ -247,10 +257,7 @@ public class Trifid{
//Split the locations up by group //Split the locations up by group
logger.debug("Splitting locations into groups"); logger.debug("Splitting locations into groups");
int numGroups = inputString.length() / groupSize; int numGroups = (int)Math.ceil((double)inputString.length() / (double)groupSize);
if(numGroups == 0){
numGroups = 1;
}
ArrayList<ArrayList<CharLocation>> groups = new ArrayList<>(numGroups); ArrayList<ArrayList<CharLocation>> groups = new ArrayList<>(numGroups);
for(int cnt = 0;cnt < numGroups;++cnt){ for(int cnt = 0;cnt < numGroups;++cnt){
groups.add(new ArrayList<>()); groups.add(new ArrayList<>());
@@ -302,7 +309,7 @@ public class Trifid{
formatOutput(output.toString()); formatOutput(output.toString());
} }
//Decodes inputString using a polybius square and stores the result in outputString //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"); logger.debug("Decoding");
//Step through every element in the sanitized inputString encoding the letters //Step through every element in the sanitized inputString encoding the letters
@@ -316,10 +323,7 @@ public class Trifid{
//Split the locations up by group //Split the locations up by group
logger.debug("Splitting locations into groups"); logger.debug("Splitting locations into groups");
int numGroups = inputString.length() / groupSize; int numGroups = (int)Math.ceil((double)inputString.length() / (double)groupSize);
if(numGroups == 0){
numGroups = 1;
}
ArrayList<ArrayList<CharLocation>> groups = new ArrayList<>(numGroups); ArrayList<ArrayList<CharLocation>> groups = new ArrayList<>(numGroups);
for(int cnt = 0;cnt < numGroups;++cnt){ for(int cnt = 0;cnt < numGroups;++cnt){
groups.add(new ArrayList<>()); groups.add(new ArrayList<>());
@@ -382,18 +386,21 @@ public class Trifid{
preserveWhitespace = false; preserveWhitespace = false;
preserveSymbols = false; preserveSymbols = false;
setFillIn('+'); setFillIn('+');
reset();
} }
public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{ public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
this.preserveCapitals = preserveCapitals; this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace; this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols; this.preserveSymbols = preserveSymbols;
setFillIn('+'); setFillIn('+');
reset();
} }
public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char fillIn) throws InvalidCharacterException{ public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char fillIn) throws InvalidCharacterException{
this.preserveCapitals = preserveCapitals; this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace; this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols; this.preserveSymbols = preserveSymbols;
setFillIn(fillIn); setFillIn(fillIn);
reset();
} }
//Encodes inputString using keyword and groupSize and returns the result //Encodes inputString using keyword and groupSize and returns the result
@@ -437,7 +444,8 @@ public class Trifid{
groupSize = Integer.MAX_VALUE; groupSize = Integer.MAX_VALUE;
grid = new char[3][3][3]; grid = new char[3][3][3];
} }
//Gets
//Getters
public String getInputString(){ public String getInputString(){
return inputString; return inputString;
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/combination/TestADFGVX.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/combination/TestADFGVX.java
//Mattrixwv //Mattrixwv
// Created: 01-26-22 // Created: 01-26-22
//Modified: 04-21-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.combination; 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.anyChar;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
@@ -29,8 +31,11 @@ import com.mattrixwv.cipherstream.polysubstitution.Columnar;
import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare; import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare;
@ExtendWith(MockitoExtension.class)
public class TestADFGVX{ public class TestADFGVX{
@InjectMocks
private ADFGVX cipher; private ADFGVX cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
@@ -41,102 +46,200 @@ public class TestADFGVX{
private String squareKeyword = "SquareKeyword"; private String squareKeyword = "SquareKeyword";
@BeforeEach @Test
public void setup(){ public void testConstructor_default(){
cipher = new ADFGVX(); 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 @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, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setSquareKeyword(null); cipher.setSquareKeyword(null);
}); });
assertEquals("", cipher.squareKeyword);
verify(logger, never()).debug(anyString(), anyString());
cipher.setSquareKeyword(squareKeyword); assertEquals("", cipher.squareKeyword);
assertEquals(squareKeyword, cipher.squareKeyword); verify(logger, never()).debug(eq("squareKeyword '{}'"), anyString());
verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword);
} }
@Test @Test
public void testSetKeyword(){ 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, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null); cipher.setKeyword(null);
}); });
assertEquals("", cipher.keyword);
verify(logger, never()).debug(anyString(), anyString());
String keyword = "keyword"; assertEquals("", cipher.keyword);
cipher.setKeyword(keyword); verify(logger, never()).debug(eq("keyword '{}'"), anyString());
assertEquals(keyword, cipher.keyword);
verify(logger, times(1)).debug("keyword = {}", keyword);
} }
@Test @Test
public void testSetInputString(){ 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.preserveCapitals = true;
cipher.preserveSymbols = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
assertThrows(InvalidInputException.class, () -> { cipher.preserveSymbols = true;
cipher.setInputString("");
});
assertEquals("", cipher.inputString);
verify(logger, times(1)).debug(originalInputString, "");
//No options cipher.setInputString(decodedString);
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);
//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.preserveCapitals = false;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = 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.preserveCapitals = true;
cipher.preserveWhitespace = false; cipher.preserveWhitespace = false;
cipher.preserveSymbols = true; 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.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = false; cipher.preserveSymbols = false;
inputString = "input String*";
cipher.setInputString(inputString); cipher.setInputString(decodedString);
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
verify(logger, times(4)).debug(originalInputString, inputString); 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("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 @Test
@@ -145,6 +248,7 @@ public class TestADFGVX{
cipher.outputString = encodedStringClean; cipher.outputString = encodedStringClean;
cipher.formatOutputStringEncode(); cipher.formatOutputStringEncode();
assertEquals(encodedString, cipher.outputString); assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string to match input string"); verify(logger, times(1)).debug("Formatting output string to match input string");
verify(logger, times(17)).debug(eq("Input character {}"), anyChar()); verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
@@ -160,6 +264,7 @@ public class TestADFGVX{
cipher.inputString = encodedString; cipher.inputString = encodedString;
cipher.formatOutputStringDecode(); cipher.formatOutputStringDecode();
assertEquals(decodedString, cipher.outputString); assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string to match input string"); verify(logger, times(1)).debug("Formatting output string to match input string");
verify(logger, times(17)).debug(eq("Input character {}"), anyChar()); 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"); 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 @Test
public void testGetters(){ public void testGetters(){
cipher.inputString = decodedString; cipher.inputString = decodedString;

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamjava/combination/TestADFGX.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamjava/combination/TestADFGX.java
//Mattrixwv //Mattrixwv
// Created: 01-25-22 // Created: 01-25-22
//Modified: 04-17-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.combination; 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.anyChar;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
@@ -29,8 +31,11 @@ import com.mattrixwv.cipherstream.polysubstitution.Columnar;
import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare; import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
@ExtendWith(MockitoExtension.class)
public class TestADFGX{ public class TestADFGX{
@InjectMocks
private ADFGX cipher; private ADFGX cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
@@ -41,102 +46,199 @@ public class TestADFGX{
private String squareKeyword = "SquareKeyword"; private String squareKeyword = "SquareKeyword";
@BeforeEach @Test
public void setup(){ public void testConstructor_default(){
cipher = new ADFGX(true, true, true); cipher = new ADFGX();
logger = mock(Logger.class);
ADFGX.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.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 @Test
public void testSetSquareKeyword(){ 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, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setSquareKeyword(null); cipher.setSquareKeyword(null);
}); });
assertEquals("", cipher.squareKeyword);
verify(logger, never()).debug(anyString(), anyString());
cipher.setSquareKeyword(squareKeyword); assertEquals("", cipher.squareKeyword);
assertEquals(squareKeyword, cipher.squareKeyword); verify(logger, never()).debug(eq("Square keyword '{}'"), anyString());
verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword);
} }
@Test @Test
public void testSetKeyword(){ 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, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null); cipher.setKeyword(null);
}); });
assertEquals("", cipher.keyword);
verify(logger, never()).debug(anyString(), anyString());
String keyword = "keyword"; assertEquals("", cipher.keyword);
cipher.setKeyword(keyword); verify(logger, never()).debug(eq("Keyword '{}'"), anyString());
assertEquals(keyword, cipher.keyword);
verify(logger, times(1)).debug("keyword = {}", keyword);
} }
@Test @Test
public void testSetInputString(){ 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.preserveCapitals = true;
cipher.preserveSymbols = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
assertThrows(InvalidInputException.class, () -> { cipher.preserveSymbols = true;
cipher.setInputString("");
});
assertEquals("", cipher.inputString);
verify(logger, times(1)).debug(originalInputString, "");
//No options cipher.setInputString(decodedString);
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);
//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.preserveCapitals = false;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = 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.preserveCapitals = true;
cipher.preserveWhitespace = false; cipher.preserveWhitespace = false;
cipher.preserveSymbols = true; 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.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = false; cipher.preserveSymbols = false;
inputString = "input String*";
cipher.setInputString(inputString); cipher.setInputString(decodedString);
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
verify(logger, times(4)).debug(originalInputString, inputString); 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("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 @Test
@@ -145,6 +247,7 @@ public class TestADFGX{
cipher.outputString = encodedStringClean; cipher.outputString = encodedStringClean;
cipher.formatOutputStringEncode(); cipher.formatOutputStringEncode();
assertEquals(encodedString, cipher.outputString); assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string to match input string"); verify(logger, times(1)).debug("Formatting output string to match input string");
verify(logger, times(17)).debug(eq("Input character {}"), anyChar()); verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
@@ -160,6 +263,7 @@ public class TestADFGX{
cipher.inputString = encodedString; cipher.inputString = encodedString;
cipher.formatOutputStringDecode(); cipher.formatOutputStringDecode();
assertEquals(decodedString, cipher.outputString); assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string to match input string"); verify(logger, times(1)).debug("Formatting output string to match input string");
verify(logger, times(17)).debug(eq("Input character {}"), anyChar()); 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"); 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 @Test
public void testGetters(){ public void testGetters(){
cipher.inputString = decodedString; cipher.inputString = decodedString;

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestAffine.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestAffine.java
//Mattrixwv //Mattrixwv
// Created: 01-26-22 // Created: 01-26-22
//Modified: 04-17-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyChar; import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
@ExtendWith(MockitoExtension.class)
public class TestAffine{ public class TestAffine{
@InjectMocks
private Affine cipher; private Affine cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "MEssage to^encode"; private String decodedString = "MEssage to^encode";
@@ -38,17 +42,10 @@ public class TestAffine{
private int key2 = 7; private int key2 = 7;
@BeforeEach
public void setup(){
cipher = new Affine();
logger = mock(Logger.class);
Affine.logger = logger;
}
@Test @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new Affine(); cipher = new Affine();
assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols); assertFalse(cipher.preserveSymbols);
assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveWhitespace);
@@ -61,6 +58,7 @@ public class TestAffine{
@Test @Test
public void testConstructor_preservesCapitals(){ public void testConstructor_preservesCapitals(){
cipher = new Affine(true, false, false); cipher = new Affine(true, false, false);
assertTrue(cipher.preserveCapitals); assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols); assertFalse(cipher.preserveSymbols);
assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveWhitespace);
@@ -73,6 +71,7 @@ public class TestAffine{
@Test @Test
public void testConstructor_preservesWhitespace(){ public void testConstructor_preservesWhitespace(){
cipher = new Affine(false, true, false); cipher = new Affine(false, true, false);
assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols); assertFalse(cipher.preserveSymbols);
assertTrue(cipher.preserveWhitespace); assertTrue(cipher.preserveWhitespace);
@@ -85,6 +84,7 @@ public class TestAffine{
@Test @Test
public void testConstructor_preservesSymbols(){ public void testConstructor_preservesSymbols(){
cipher = new Affine(false, false, true); cipher = new Affine(false, false, true);
assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveSymbols); assertTrue(cipher.preserveSymbols);
assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveWhitespace);
@@ -97,12 +97,10 @@ public class TestAffine{
@Test @Test
public void testKey1(){ public void testKey1(){
cipher.setKey1(key1); cipher.setKey1(key1);
assertEquals(key1, cipher.key1); assertEquals(key1, cipher.key1);
verify(logger, times(1)).debug("Setting key1 {}", key1); verify(logger, times(1)).debug("Setting key1 {}", key1);
verify(logger, times(1)).debug("Cleaned 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 @Test
@@ -110,66 +108,54 @@ public class TestAffine{
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKey1(2); cipher.setKey1(2);
}); });
verify(logger, times(1)).debug("Setting key1 {}", 2); verify(logger, times(1)).debug("Setting key1 {}", 2);
verify(logger, never()).debug("Cleaned 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 @Test
public void testSetKey1_negative(){ public void testSetKey1_negative(){
cipher.setKey1(-27); cipher.setKey1(-27);
assertEquals(25, cipher.key1); assertEquals(25, cipher.key1);
verify(logger, times(1)).debug("Setting key1 {}", -27); verify(logger, times(1)).debug("Setting key1 {}", -27);
verify(logger, times(1)).debug("Cleaned key1 {}", 25); 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 @Test
public void testSetKey1_large(){ public void testSetKey1_large(){
cipher.setKey1(key1 + 26); cipher.setKey1(key1 + 26);
assertEquals(key1, cipher.key1); assertEquals(key1, cipher.key1);
verify(logger, times(1)).debug("Setting key1 {}", key1 + 26); verify(logger, times(1)).debug("Setting key1 {}", key1 + 26);
verify(logger, times(1)).debug("Cleaned 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 @Test
public void testSetKey2(){ public void testSetKey2(){
cipher.setKey2(key2); cipher.setKey2(key2);
assertEquals(key2, cipher.key2); assertEquals(key2, cipher.key2);
verify(logger, times(1)).debug("Setting key2 {}", key2); verify(logger, times(1)).debug("Setting key2 {}", key2);
verify(logger, times(1)).debug("Cleaned 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 @Test
public void testSetKey2_negative(){ public void testSetKey2_negative(){
cipher.setKey2(-27); cipher.setKey2(-27);
assertEquals(25, cipher.key2); assertEquals(25, cipher.key2);
verify(logger, times(1)).debug("Setting key2 {}", -27); verify(logger, times(1)).debug("Setting key2 {}", -27);
verify(logger, times(1)).debug("Cleaned key2 {}", cipher.key2); 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 @Test
public void testSetKey2_large(){ public void testSetKey2_large(){
cipher.setKey2(key2 + 26); cipher.setKey2(key2 + 26);
assertEquals(key2, cipher.key2); assertEquals(key2, cipher.key2);
verify(logger, times(1)).debug("Setting key2 {}", key2 + 26); verify(logger, times(1)).debug("Setting key2 {}", key2 + 26);
verify(logger, times(1)).debug("Cleaned 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 @Test
@@ -182,8 +168,10 @@ public class TestAffine{
assertEquals(decodedString, cipher.inputString); assertEquals(decodedString, cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString); 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(1)).debug("Cleaned input string '{}'", decodedString);
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -197,9 +185,9 @@ public class TestAffine{
assertEquals(decodedString.toLowerCase(), cipher.inputString); assertEquals(decodedString.toLowerCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString); verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, times(1)).debug("Removing case"); 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(1)).debug("Cleaned input string '{}'", decodedString.toLowerCase());
verify(logger, times(2)).debug(anyString(), anyString());
verify(logger, times(1)).debug(anyString());
} }
@Test @Test
@@ -212,10 +200,10 @@ public class TestAffine{
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString); assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString); verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing case");
verify(logger, times(1)).debug("Removing whitespace"); 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(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
verify(logger, times(2)).debug(anyString(), anyString());
verify(logger, times(1)).debug(anyString());
} }
@Test @Test
@@ -228,10 +216,10 @@ public class TestAffine{
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString); 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("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", "")); 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 @Test
@@ -243,8 +231,13 @@ public class TestAffine{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(null); 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 @Test
@@ -256,10 +249,13 @@ public class TestAffine{
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(""); cipher.setInputString("");
}); });
assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", ""); 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(1)).debug("Cleaned input string '{}'", "");
verify(logger, times(2)).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
} }
@Test @Test

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java
//Mattrixwv //Mattrixwv
// Created: 07-25-21 // Created: 07-25-21
//Modified: 04-17-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; 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.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
@ExtendWith(MockitoExtension.class)
public class TestAtbash{ public class TestAtbash{
@InjectMocks
private Atbash cipher; private Atbash cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
@@ -32,17 +39,10 @@ public class TestAtbash{
private String encodedStringClean = "NVHHZTVGLVMXLWV"; private String encodedStringClean = "NVHHZTVGLVMXLWV";
@BeforeEach
public void setup(){
cipher = new Atbash();
logger = mock(Logger.class);
Atbash.logger = logger;
}
@Test @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new Atbash(); cipher = new Atbash();
assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols); assertFalse(cipher.preserveSymbols);
@@ -53,6 +53,7 @@ public class TestAtbash{
@Test @Test
public void testConstructor_preservesCapitals(){ public void testConstructor_preservesCapitals(){
cipher = new Atbash(true, false, false); cipher = new Atbash(true, false, false);
assertTrue(cipher.preserveCapitals); assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols); assertFalse(cipher.preserveSymbols);
@@ -63,6 +64,7 @@ public class TestAtbash{
@Test @Test
public void testConstructor_preservesWhitespace(){ public void testConstructor_preservesWhitespace(){
cipher = new Atbash(false, true, false); cipher = new Atbash(false, true, false);
assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace); assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols); assertFalse(cipher.preserveSymbols);
@@ -73,6 +75,7 @@ public class TestAtbash{
@Test @Test
public void testConstructor_preservesSymbols(){ public void testConstructor_preservesSymbols(){
cipher = new Atbash(false, false, true); cipher = new Atbash(false, false, true);
assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols); assertTrue(cipher.preserveSymbols);
@@ -83,10 +86,16 @@ public class TestAtbash{
@Test @Test
public void testEncode(){ public void testEncode(){
cipher.inputString = decodedString; cipher.inputString = decodedString;
cipher.outputString = encodedString;
assertEquals(decodedString, cipher.inputString); cipher.encode();
assertEquals(encodedString, cipher.outputString); 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 @Test
@@ -99,9 +108,10 @@ public class TestAtbash{
assertEquals(decodedString, cipher.inputString); assertEquals(decodedString, cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString); 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(1)).debug("Cleaned input string '{}'", decodedString);
verify(logger, times(2)).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
} }
@Test @Test
@@ -115,9 +125,9 @@ public class TestAtbash{
assertEquals(decodedString.toUpperCase(), cipher.inputString); assertEquals(decodedString.toUpperCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString); verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase()); verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
verify(logger, times(2)).debug(anyString(), anyString());
verify(logger, times(1)).debug(anyString());
} }
@Test @Test
@@ -130,10 +140,10 @@ public class TestAtbash{
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString); assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString); verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing case");
verify(logger, times(1)).debug("Removing whitespace"); 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("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
verify(logger, times(2)).debug(anyString(), anyString());
verify(logger, times(1)).debug(anyString());
} }
@Test @Test
@@ -146,10 +156,10 @@ public class TestAtbash{
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString); 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("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", "")); 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 @Test
@@ -163,8 +173,11 @@ public class TestAtbash{
}); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(anyString(), anyString()); verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
verify(logger, never()).debug(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 @Test
@@ -180,9 +193,10 @@ public class TestAtbash{
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", ""); 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(1)).debug("Cleaned input string '{}'", "");
verify(logger, times(2)).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
} }
@Test @Test

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAutokey.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAutokey.java
//Mattrixwv //Mattrixwv
// Created: 07-26-21 // Created: 07-26-21
//Modified: 04-17-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyChar; import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
@ExtendWith(MockitoExtension.class)
public class TestAutokey{ public class TestAutokey{
@InjectMocks
private Autokey cipher; private Autokey cipher;
@Mock(name = "com.mattrixwv.cipherstream.monosubstitution.Autokey")
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "MeSsage to^encode"; private String decodedString = "MeSsage to^encode";
@@ -37,17 +39,10 @@ public class TestAutokey{
private ArrayList<Integer> offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3, 12, 4, 18, 18, 0, 6, 4, 19)); private ArrayList<Integer> 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 @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new Autokey(); cipher = new Autokey();
assertFalse(cipher.preserveCapitals); assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols); assertFalse(cipher.preserveSymbols);
@@ -60,6 +55,7 @@ public class TestAutokey{
@Test @Test
public void testConstructor_preservesCapitals(){ public void testConstructor_preservesCapitals(){
cipher = new Autokey(true, false, false); cipher = new Autokey(true, false, false);
assertTrue(cipher.preserveCapitals); assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace); assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols); assertFalse(cipher.preserveSymbols);
@@ -110,7 +106,6 @@ public class TestAutokey{
verify(logger, times(1)).debug("Setting keyword"); verify(logger, times(1)).debug("Setting keyword");
verify(logger, times(1)).debug("Adding input to keyword"); verify(logger, times(1)).debug("Adding input to keyword");
verify(logger, times(1)).debug("Removing last letters in the keyword"); verify(logger, times(1)).debug("Removing last letters in the keyword");
verify(logger, times(4)).debug(anyString());
} }
@Test @Test
@@ -126,7 +121,6 @@ public class TestAutokey{
verify(logger, times(1)).debug("Setting fields for decoding"); verify(logger, times(1)).debug("Setting fields for decoding");
verify(logger, times(1)).debug("Setting keyword"); verify(logger, times(1)).debug("Setting keyword");
verify(logger, times(1)).debug("Setting input string"); verify(logger, times(1)).debug("Setting input string");
verify(logger, times(3)).debug(anyString());
} }
@Test @Test
@@ -134,9 +128,7 @@ public class TestAutokey{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
Autokey.logger = mock(Logger.class);
cipher.decodeSet(keyword, encodedString); cipher.decodeSet(keyword, encodedString);
Autokey.logger = logger;
cipher.decode(); cipher.decode();
@@ -227,40 +219,4 @@ public class TestAutokey{
assertEquals(decodedStringClean, cipher.outputString); assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output); 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);
}
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestBaconian.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestBaconian.java
//Mattrixwv //Mattrixwv
// Created: 01-12-22 // Created: 01-12-22
//Modified: 04-17-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
@ExtendWith(MockitoExtension.class)
public class TestBaconian{ public class TestBaconian{
@InjectMocks
private Baconian cipher; private Baconian cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "Message to-encode"; 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"; 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 @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new Baconian(); cipher = new Baconian();
@@ -72,8 +69,6 @@ public class TestBaconian{
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString); verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing case");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringClean); verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringClean);
verify(logger, never()).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -86,8 +81,6 @@ public class TestBaconian{
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString); verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringCleanLower); verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringCleanLower);
verify(logger, times(1)).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -102,8 +95,6 @@ public class TestBaconian{
verify(logger, times(1)).debug("Setting input string for encoding '{}'", ""); verify(logger, times(1)).debug("Setting input string for encoding '{}'", "");
verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing case");
verify(logger, times(1)).debug("Cleaned input string '{}'", ""); verify(logger, times(1)).debug("Cleaned input string '{}'", "");
verify(logger, never()).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -115,8 +106,9 @@ public class TestBaconian{
}); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(anyString()); verify(logger, never()).debug("Setting input string for encoding '{}'", "");
verify(logger, never()).debug(anyString(), anyString()); verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Cleaned input string '{}'", "");
} }
@Test @Test
@@ -132,8 +124,6 @@ public class TestBaconian{
verify(logger, times(15)).debug(eq("Current 'letter' {}"), anyString()); verify(logger, times(15)).debug(eq("Current 'letter' {}"), anyString());
verify(logger, times(15)).debug("Replacing all non-abAB characters"); verify(logger, times(15)).debug("Replacing all non-abAB characters");
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString); verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString);
verify(logger, times(16)).debug(anyString());
verify(logger, times(17)).debug(anyString(), anyString());
} }
@Test @Test
@@ -149,8 +139,6 @@ public class TestBaconian{
verify(logger, times(15)).debug(eq("Current 'letter' {}"), anyString()); verify(logger, times(15)).debug(eq("Current 'letter' {}"), anyString());
verify(logger, times(15)).debug("Replacing all non-abAB characters"); verify(logger, times(15)).debug("Replacing all non-abAB characters");
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.toLowerCase()); verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.toLowerCase());
verify(logger, times(17)).debug(anyString());
verify(logger, times(17)).debug(anyString(), anyString());
} }
@Test @Test
@@ -168,8 +156,6 @@ public class TestBaconian{
verify(logger, times(1)).debug(eq("Current 'letter' {}"), anyString()); verify(logger, times(1)).debug(eq("Current 'letter' {}"), anyString());
verify(logger, never()).debug("Replacing all non-abAB characters"); verify(logger, never()).debug("Replacing all non-abAB characters");
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
verify(logger, times(1)).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -187,8 +173,6 @@ public class TestBaconian{
verify(logger, times(1)).debug("Current 'letter' {}", "ccccc"); verify(logger, times(1)).debug("Current 'letter' {}", "ccccc");
verify(logger, times(1)).debug("Replacing all non-abAB characters"); verify(logger, times(1)).debug("Replacing all non-abAB characters");
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
verify(logger, times(2)).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -200,14 +184,12 @@ public class TestBaconian{
}); });
assertEquals("", cipher.inputString); 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("Removing case");
verify(logger, never()).debug("Ensuring all 'letters' contain 5 characters"); verify(logger, never()).debug("Ensuring all 'letters' contain 5 characters");
verify(logger, never()).debug(eq("Current 'letter' {}"), anyString()); verify(logger, never()).debug(eq("Current 'letter' {}"), anyString());
verify(logger, never()).debug("Replacing all non-abAB characters"); verify(logger, never()).debug("Replacing all non-abAB characters");
verify(logger, never()).debug("Cleaned input string '{}'", ""); verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
verify(logger, never()).debug(anyString());
verify(logger, never()).debug(anyString(), anyString());
} }
@Test @Test
@@ -219,8 +201,12 @@ public class TestBaconian{
}); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(anyString()); verify(logger, never()).debug(eq("Setting input string for decoding '{}'"), anyString());
verify(logger, never()).debug(anyString(), 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 @Test
@@ -237,8 +223,6 @@ public class TestBaconian{
verify(logger, times(14)).debug("Encoding lowercase"); verify(logger, times(14)).debug("Encoding lowercase");
verify(logger, times(15)).debug(eq("Output letter {}"), anyString()); verify(logger, times(15)).debug(eq("Output letter {}"), anyString());
verify(logger, times(1)).debug("Saving output string '{}'", encodedString); verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
verify(logger, times(16)).debug(anyString());
verify(logger, times(16)).debug(anyString(), anyString());
} }
@Test @Test
@@ -256,8 +240,6 @@ public class TestBaconian{
verify(logger, times(14)).debug("Decoding lowercase"); verify(logger, times(14)).debug("Decoding lowercase");
verify(logger, times(15)).debug(eq("Decoded character {}"), anyChar()); verify(logger, times(15)).debug(eq("Decoded character {}"), anyChar());
verify(logger, times(1)).debug("Saving output string '{}'", decodedStringClean); verify(logger, times(1)).debug("Saving output string '{}'", decodedStringClean);
verify(logger, times(16)).debug(anyString());
verify(logger, times(16)).debug(anyString(), anyString());
} }
@Test @Test

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBaseX.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBaseX.java
//Mattrixwv //Mattrixwv
// Created: 01-08-22 // Created: 01-08-22
//Modified: 04-16-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidBaseException; import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
@@ -25,8 +27,11 @@ import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
@ExtendWith(MockitoExtension.class)
public class TestBaseX{ public class TestBaseX{
@InjectMocks
private BaseX cipher; private BaseX cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "A+B@C d\te\nf"; 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"; 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 @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new BaseX(); cipher = new BaseX();
@@ -68,8 +65,6 @@ public class TestBaseX{
assertEquals(decodedString, cipher.inputString); assertEquals(decodedString, cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString); verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
verify(logger, times(1)).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
} }
@Test @Test
@@ -80,8 +75,6 @@ public class TestBaseX{
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", ""); verify(logger, times(1)).debug("Setting input string for encoding '{}'", "");
verify(logger, times(1)).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
} }
@Test @Test
@@ -164,8 +157,6 @@ public class TestBaseX{
assertEquals(16, cipher.base); assertEquals(16, cipher.base);
verify(logger, times(1)).debug("Setting base {}", 16); verify(logger, times(1)).debug("Setting base {}", 16);
verify(logger, never()).debug(anyString());
verify(logger, never()).debug(anyString(), anyString());
} }
@Test @Test
@@ -176,8 +167,6 @@ public class TestBaseX{
assertEquals(2, cipher.base); assertEquals(2, cipher.base);
verify(logger, never()).debug(eq("Setting base {}"), anyInt()); verify(logger, never()).debug(eq("Setting base {}"), anyInt());
verify(logger, never()).debug(anyString());
verify(logger, never()).debug(anyString(), anyString());
} }
@Test @Test
@@ -188,8 +177,6 @@ public class TestBaseX{
assertEquals(2, cipher.base); assertEquals(2, cipher.base);
verify(logger, never()).debug(eq("Setting base {}"), anyInt()); verify(logger, never()).debug(eq("Setting base {}"), anyInt());
verify(logger, never()).debug(anyString());
verify(logger, never()).debug(anyString(), anyString());
} }
@Test @Test
@@ -256,13 +243,15 @@ public class TestBaseX{
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString); assertEquals("", cipher.outputString);
verify(logger, times(1)).debug("Resetting fields"); verify(logger, times(1)).debug("Resetting fields");
verify(logger, times(1)).debug(anyString());
} }
@Test @Test
public void testPracticalEncoding_2(){ public void testPracticalEncoding_2(){
String output = cipher.encode(2, decodedString); String output = cipher.encode(2, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(2, cipher.base);
assertEquals(encodedString_2, cipher.outputString);
assertEquals(encodedString_2, output); assertEquals(encodedString_2, output);
} }
@@ -270,6 +259,9 @@ public class TestBaseX{
public void testPracticalEncoding_8(){ public void testPracticalEncoding_8(){
String output = cipher.encode(8, decodedString); String output = cipher.encode(8, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(8, cipher.base);
assertEquals(encodedString_8, cipher.outputString);
assertEquals(encodedString_8, output); assertEquals(encodedString_8, output);
} }
@@ -277,6 +269,9 @@ public class TestBaseX{
public void testPracticalEncoding_10(){ public void testPracticalEncoding_10(){
String output = cipher.encode(10, decodedString); String output = cipher.encode(10, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(10, cipher.base);
assertEquals(encodedString_10, cipher.outputString);
assertEquals(encodedString_10, output); assertEquals(encodedString_10, output);
} }
@@ -284,6 +279,9 @@ public class TestBaseX{
public void testPracticalEncoding_16(){ public void testPracticalEncoding_16(){
String output = cipher.encode(16, decodedString); String output = cipher.encode(16, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(16, cipher.base);
assertEquals(encodedString_16, cipher.outputString);
assertEquals(encodedString_16, output); assertEquals(encodedString_16, output);
} }
@@ -291,6 +289,9 @@ public class TestBaseX{
public void testPracticalEncoding_inputOnly(){ public void testPracticalEncoding_inputOnly(){
String output = cipher.encode(decodedString); String output = cipher.encode(decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(2, cipher.base);
assertEquals(encodedString_2, cipher.outputString);
assertEquals(encodedString_2, output); assertEquals(encodedString_2, output);
} }
@@ -298,6 +299,9 @@ public class TestBaseX{
public void testPracticalDecoding_2(){ public void testPracticalDecoding_2(){
String output = cipher.decode(2, encodedString_2); String output = cipher.decode(2, encodedString_2);
assertEquals(encodedString_2, cipher.inputString);
assertEquals(2, cipher.base);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output); assertEquals(decodedString, output);
} }
@@ -305,6 +309,9 @@ public class TestBaseX{
public void testPracticalDecoding_8(){ public void testPracticalDecoding_8(){
String output = cipher.decode(8, encodedString_8); String output = cipher.decode(8, encodedString_8);
assertEquals(encodedString_8, cipher.inputString);
assertEquals(8, cipher.base);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output); assertEquals(decodedString, output);
} }
@@ -312,6 +319,9 @@ public class TestBaseX{
public void testPracticalDecoding_10(){ public void testPracticalDecoding_10(){
String output = cipher.decode(10, encodedString_10); String output = cipher.decode(10, encodedString_10);
assertEquals(encodedString_10, cipher.inputString);
assertEquals(10, cipher.base);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output); assertEquals(decodedString, output);
} }
@@ -319,6 +329,9 @@ public class TestBaseX{
public void testPracticalDecoding_16(){ public void testPracticalDecoding_16(){
String output = cipher.decode(16, encodedString_16); String output = cipher.decode(16, encodedString_16);
assertEquals(encodedString_16, cipher.inputString);
assertEquals(16, cipher.base);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output); assertEquals(decodedString, output);
} }
@@ -326,6 +339,9 @@ public class TestBaseX{
public void testPracticalDecoding_inputOnly(){ public void testPracticalDecoding_inputOnly(){
String output = cipher.decode(encodedString_2); String output = cipher.decode(encodedString_2);
assertEquals(encodedString_2, cipher.inputString);
assertEquals(2, cipher.base);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output); assertEquals(decodedString, output);
} }
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBeaufort.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBeaufort.java
//Mattrixwv //Mattrixwv
// Created: 02-23-22 // Created: 02-23-22
//Modified: 04-17-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
@ExtendWith(MockitoExtension.class)
public class TestBeaufort{ public class TestBeaufort{
@InjectMocks
private Beaufort cipher; private Beaufort cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String decodedStringClean = "MESSAGETOENCODE"; private String decodedStringClean = "MESSAGETOENCODE";
private String encodedString = "Yageolz rq^ujmdag"; private String encodedString = "Yageolz rq^ujmdag";
private String encodedStringClean = "YAGEOLZRQUJMDAG"; private String encodedStringClean = "YAGEOLZRQUJMDAG";
private String keyword = "keyword"; private String keyword = "Ke*y word";
private String keywordDirty = "Ke*y word"; private String keywordClean = "KEYWORD";
@BeforeEach
public void setup(){
cipher = new Beaufort();
logger = mock(Logger.class);
Beaufort.logger = logger;
}
@Test @Test
@@ -142,8 +139,6 @@ public class TestBeaufort{
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString); verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
verify(logger, never()).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -160,8 +155,6 @@ public class TestBeaufort{
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase()); verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
verify(logger, times(1)).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -178,8 +171,6 @@ public class TestBeaufort{
verify(logger, times(1)).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", "")); 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 @Test
@@ -196,8 +187,6 @@ public class TestBeaufort{
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols"); 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("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
verify(logger, times(1)).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -216,8 +205,6 @@ public class TestBeaufort{
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", ""); verify(logger, times(1)).debug("Cleaned input string '{}'", "");
verify(logger, never()).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -232,34 +219,17 @@ public class TestBeaufort{
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString()); verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
verify(logger, never()).debug(anyString());
verify(logger, never()).debug(anyString(), anyString());
} }
@Test @Test
public void testSetKeyword(){ public void testSetKeyword(){
cipher.setKeyword(keyword); 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("Original keyword '{}'", keyword);
verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing all non-letters"); verify(logger, times(1)).debug("Removing all non-letters");
verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase()); verify(logger, times(1)).debug("Cleaned keyword '{}'", keywordClean);
verify(logger, times(2)).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
}
@Test
public void testSetKeyword_dirty(){
cipher.setKeyword(keywordDirty);
assertEquals(keyword.toUpperCase(), cipher.keyword);
verify(logger, times(1)).debug("Original keyword '{}'", keywordDirty);
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing all non-letters");
verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase());
verify(logger, times(2)).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -273,8 +243,6 @@ public class TestBeaufort{
verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing all non-letters"); verify(logger, times(1)).debug("Removing all non-letters");
verify(logger, times(1)).debug("Cleaned keyword '{}'", ""); verify(logger, times(1)).debug("Cleaned keyword '{}'", "");
verify(logger, times(2)).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -288,8 +256,6 @@ public class TestBeaufort{
verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing all non-letters"); verify(logger, times(1)).debug("Removing all non-letters");
verify(logger, times(1)).debug("Cleaned keyword '{}'", "A"); verify(logger, times(1)).debug("Cleaned keyword '{}'", "A");
verify(logger, times(2)).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
} }
@Test @Test
@@ -303,17 +269,13 @@ public class TestBeaufort{
verify(logger, never()).debug("Removing case"); verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing all non-letters"); verify(logger, never()).debug("Removing all non-letters");
verify(logger, never()).debug(eq("Cleaned keyword '{}'"), anyString()); verify(logger, never()).debug(eq("Cleaned keyword '{}'"), anyString());
verify(logger, never()).debug(anyString());
verify(logger, never()).debug(anyString(), anyString());
} }
@Test @Test
public void testEncode(){ public void testEncode(){
cipher = new Beaufort(true, true, true); cipher = new Beaufort(true, true, true);
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.inputString = decodedString; cipher.inputString = decodedString;
logger = mock(Logger.class);
Beaufort.logger = logger;
cipher.encode(); cipher.encode();
@@ -323,17 +285,13 @@ public class TestBeaufort{
verify(logger, times(1)).debug("Shifting all letters by 1"); verify(logger, times(1)).debug("Shifting all letters by 1");
verify(logger, times(1)).debug("Encoding with Vigenere"); verify(logger, times(1)).debug("Encoding with Vigenere");
verify(logger, times(1)).debug("Saving output string '{}'", encodedString); verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
verify(logger, times(4)).debug(anyString());
verify(logger, times(1)).debug(anyString(), anyString());
} }
@Test @Test
public void testDecode(){ public void testDecode(){
cipher = new Beaufort(true, true, true); cipher = new Beaufort(true, true, true);
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.inputString = decodedString; cipher.inputString = decodedString;
logger = mock(Logger.class);
Beaufort.logger = logger;
cipher.decode(); cipher.decode();
@@ -343,8 +301,6 @@ public class TestBeaufort{
verify(logger, times(1)).debug("Shifting all letters by 1"); verify(logger, times(1)).debug("Shifting all letters by 1");
verify(logger, times(1)).debug("Encoding with Vigenere"); verify(logger, times(1)).debug("Encoding with Vigenere");
verify(logger, times(1)).debug("Saving output string '{}'", encodedString); verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
verify(logger, times(4)).debug(anyString());
verify(logger, times(1)).debug(anyString(), anyString());
} }
@Test @Test

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestCaesar.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestCaesar.java
//Matthew Ellison //Matthew Ellison
// Created: 07-25-21 // Created: 07-25-21
//Modified: 04-16-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyChar; import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
@ExtendWith(MockitoExtension.class)
public class TestCaesar{ public class TestCaesar{
@InjectMocks
private Caesar cipher; private Caesar cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "The quick brown fox jumps over - the lAzy dog"; private String decodedString = "The quick brown fox jumps over - the lAzy dog";
@@ -34,15 +39,6 @@ public class TestCaesar{
private int shift = 23; private int shift = 23;
@BeforeEach
public void setup(){
cipher = new Caesar();
logger = mock(Logger.class);
Caesar.logger = logger;
}
@Test @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new Caesar(); cipher = new Caesar();

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestOneTimePad.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestOneTimePad.java
//Mattrixwv //Mattrixwv
// Created: 02-23-22 // Created: 02-23-22
//Modified: 04-17-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@@ -18,15 +17,21 @@ import static org.mockito.Mockito.verify;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
@ExtendWith(MockitoExtension.class)
public class TestOneTimePad{ public class TestOneTimePad{
@InjectMocks
private OneTimePad cipher; private OneTimePad cipher;
@Mock(name = "com.mattrixwv.cipherstream.monosubstitution.OneTimePad")
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
@@ -37,14 +42,6 @@ public class TestOneTimePad{
private ArrayList<Integer> 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)); private ArrayList<Integer> 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 @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new OneTimePad(); cipher = new OneTimePad();

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestPorta.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestPorta.java
//Mattrixwv //Mattrixwv
// Created: 02-28-22 // Created: 02-28-22
//Modified: 04-17-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.anyChar;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
@ExtendWith(MockitoExtension.class)
public class TestPorta{ public class TestPorta{
@InjectMocks
private Porta cipher; private Porta cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
@@ -34,14 +39,7 @@ public class TestPorta{
private String encodedString = "Rtghuos bm^qcwgrw"; private String encodedString = "Rtghuos bm^qcwgrw";
private String encodedStringClean = "RTGHUOSBMQCWGRW"; private String encodedStringClean = "RTGHUOSBMQCWGRW";
private String keyword = "keyword"; private String keyword = "keyword";
private String keywordDirty = "Ke yw*ord";
@BeforeEach
public void setup(){
cipher = new Porta();
logger = mock(Logger.class);
Porta.logger = logger;
}
@Test @Test
@@ -103,6 +101,17 @@ public class TestPorta{
verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase()); 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 @Test
public void testSetKeyword_blank(){ public void testSetKeyword_blank(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
@@ -136,8 +145,10 @@ public class TestPorta{
}); });
assertEquals("", cipher.keyword); assertEquals("", cipher.keyword);
verify(logger, never()).debug(anyString()); verify(logger, never()).debug(eq("Original keyword '{}'"), anyString());
verify(logger, never()).debug(anyString(), anyString()); verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing all non-letters");
verify(logger, never()).debug(eq("Cleaned keyword '{}'"), anyString());
} }
@Test @Test
@@ -244,8 +255,11 @@ public class TestPorta{
}); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, never()).debug(anyString(), anyString()); verify(logger, never()).debug(eq("Original input string {}"), anyString());
verify(logger, never()).debug(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 @Test

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestSubstitution.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestSubstitution.java
//Mattrixwv //Mattrixwv
// Created: 02-22-22 // Created: 02-22-22
//Modified: 04-18-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.anyChar;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
@ExtendWith(MockitoExtension.class)
public class TestSubstitution{ public class TestSubstitution{
@InjectMocks
private Substitution cipher; private Substitution cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
@@ -38,15 +43,9 @@ public class TestSubstitution{
private String encodedStringAlNum = "Oguucig vq^gpeqfg 876"; private String encodedStringAlNum = "Oguucig vq^gpeqfg 876";
private String encodedStringAlNumClean = "OGUUCIGVQGPEQFG"; private String encodedStringAlNumClean = "OGUUCIGVQGPEQFG";
private String keyword = "cdefghijklmnopqrstuvwxyzab"; private String keyword = "cdefghijklmnopqrstuvwxyzab";
private String keywordClean = "CDEFGHIJKLMNOPQRSTUVWXYZAB";
private String keywordAlNum = "cdefghijklmnopqrstuvwxyzab9876543210"; private String keywordAlNum = "cdefghijklmnopqrstuvwxyzab9876543210";
private String keywordAlNumClean = "CDEFGHIJKLMNOPQRSTUVWXYZAB9876543210";
@BeforeEach
public void setup(){
cipher = new Substitution();
logger = mock(Logger.class);
Substitution.logger = logger;
}
@Test @Test
@@ -98,33 +97,33 @@ public class TestSubstitution{
} }
@Test @Test
public void testSetKey(){ public void testSetKeyword(){
cipher.setKeyword(keyword); 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("Original key '{}'", keyword);
verify(logger, times(1)).debug("Removing case"); 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 no duplicate mappings");
verify(logger, times(1)).debug("Ensuring there are only letters in the key"); 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, 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 @Test
public void testSetKey_alNum(){ public void testSetKeyword_alNum(){
cipher.setKeyword(keywordAlNum); 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("Original key '{}'", keywordAlNum);
verify(logger, times(1)).debug("Removing case"); 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 no duplicate mappings");
verify(logger, never()).debug("Ensuring there are only letters in the key"); 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("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 @Test
public void testSetKey_duplicate(){ public void testSetKeyword_duplicate(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("ABA"); cipher.setKeyword("ABA");
}); });
@@ -139,7 +138,7 @@ public class TestSubstitution{
} }
@Test @Test
public void testSetKey_invalidLetter(){ public void testSetKeyword_invalidLetter(){
assertThrows(InvalidKeywordException.class, () ->{ assertThrows(InvalidKeywordException.class, () ->{
cipher.setKeyword("abcdefghijklmnop1rstuvwxyz"); cipher.setKeyword("abcdefghijklmnop1rstuvwxyz");
}); });
@@ -154,7 +153,7 @@ public class TestSubstitution{
} }
@Test @Test
public void testSetKey_invalidAlNum(){ public void testSetKeyword_invalidAlNum(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("abcdefghijklmnop^rstuvwxyz0123456789"); cipher.setKeyword("abcdefghijklmnop^rstuvwxyz0123456789");
}); });
@@ -169,7 +168,7 @@ public class TestSubstitution{
} }
@Test @Test
public void testSetKey_invalidLength(){ public void testSetKeyword_invalidLength(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("AB"); cipher.setKeyword("AB");
}); });
@@ -184,7 +183,7 @@ public class TestSubstitution{
} }
@Test @Test
public void testSetKey_null(){ public void testSetKeyword_null(){
assertThrows(InvalidKeywordException.class, () -> { assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null); cipher.setKeyword(null);
}); });
@@ -304,7 +303,7 @@ public class TestSubstitution{
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.inputString = decodedStringAlNum; cipher.inputString = decodedStringAlNum;
cipher.keyword = keywordAlNum.toUpperCase(); cipher.keyword = keywordAlNumClean;
cipher.encode(); cipher.encode();
@@ -324,7 +323,7 @@ public class TestSubstitution{
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.inputString = encodedStringAlNum; cipher.inputString = encodedStringAlNum;
cipher.keyword = keywordAlNum.toUpperCase(); cipher.keyword = keywordAlNumClean;
cipher.decode(); cipher.decode();
@@ -370,7 +369,7 @@ public class TestSubstitution{
String output = cipher.encode(keyword, decodedString); String output = cipher.encode(keyword, decodedString);
assertEquals(decodedString, cipher.inputString); assertEquals(decodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(encodedString, cipher.outputString); assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output); assertEquals(encodedString, output);
} }
@@ -382,7 +381,7 @@ public class TestSubstitution{
String output = cipher.encode(keyword, decodedString); String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString); assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString); assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output); assertEquals(encodedStringClean, output);
} }
@@ -394,7 +393,7 @@ public class TestSubstitution{
String output = cipher.encode(keywordAlNum, decodedStringAlNum); String output = cipher.encode(keywordAlNum, decodedStringAlNum);
assertEquals(decodedStringAlNum, cipher.inputString); assertEquals(decodedStringAlNum, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword); assertEquals(keywordAlNumClean, cipher.keyword);
assertEquals(encodedStringAlNum, cipher.outputString); assertEquals(encodedStringAlNum, cipher.outputString);
assertEquals(encodedStringAlNum, output); assertEquals(encodedStringAlNum, output);
} }
@@ -406,7 +405,7 @@ public class TestSubstitution{
String output = cipher.encode(keywordAlNum, decodedStringAlNum); String output = cipher.encode(keywordAlNum, decodedStringAlNum);
assertEquals(decodedStringAlNumClean, cipher.inputString); assertEquals(decodedStringAlNumClean, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword); assertEquals(keywordAlNumClean, cipher.keyword);
assertEquals(encodedStringAlNumClean, cipher.outputString); assertEquals(encodedStringAlNumClean, cipher.outputString);
assertEquals(encodedStringAlNumClean, output); assertEquals(encodedStringAlNumClean, output);
} }
@@ -418,7 +417,7 @@ public class TestSubstitution{
String output = cipher.encode(keyword, decodedStringAlNum); String output = cipher.encode(keyword, decodedStringAlNum);
assertEquals(decodedStringAlNum, cipher.inputString); assertEquals(decodedStringAlNum, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(encodedString + " 123", cipher.outputString); assertEquals(encodedString + " 123", cipher.outputString);
assertEquals(encodedString + " 123", output); assertEquals(encodedString + " 123", output);
} }
@@ -430,7 +429,7 @@ public class TestSubstitution{
String output = cipher.decode(keyword, encodedString); String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString); assertEquals(encodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(decodedString, cipher.outputString); assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output); assertEquals(decodedString, output);
} }
@@ -442,7 +441,7 @@ public class TestSubstitution{
String output = cipher.decode(keyword, encodedString); String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString); assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString); assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output); assertEquals(decodedStringClean, output);
} }
@@ -454,7 +453,7 @@ public class TestSubstitution{
String output = cipher.decode(keywordAlNum, encodedStringAlNum); String output = cipher.decode(keywordAlNum, encodedStringAlNum);
assertEquals(encodedStringAlNum, cipher.inputString); assertEquals(encodedStringAlNum, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword); assertEquals(keywordAlNumClean, cipher.keyword);
assertEquals(decodedStringAlNum, cipher.outputString); assertEquals(decodedStringAlNum, cipher.outputString);
assertEquals(decodedStringAlNum, output); assertEquals(decodedStringAlNum, output);
} }
@@ -466,7 +465,7 @@ public class TestSubstitution{
String output = cipher.decode(keywordAlNum, encodedStringAlNum); String output = cipher.decode(keywordAlNum, encodedStringAlNum);
assertEquals(encodedStringAlNumClean, cipher.inputString); assertEquals(encodedStringAlNumClean, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword); assertEquals(keywordAlNumClean, cipher.keyword);
assertEquals(decodedStringAlNumClean, cipher.outputString); assertEquals(decodedStringAlNumClean, cipher.outputString);
assertEquals(decodedStringAlNumClean, output); assertEquals(decodedStringAlNumClean, output);
} }
@@ -478,7 +477,7 @@ public class TestSubstitution{
String output = cipher.decode(keyword, encodedString + " 123"); String output = cipher.decode(keyword, encodedString + " 123");
assertEquals(encodedString + " 123", cipher.inputString); assertEquals(encodedString + " 123", cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(decodedString + " 123", cipher.outputString); assertEquals(decodedString + " 123", cipher.outputString);
assertEquals(decodedString + " 123", output); assertEquals(decodedString + " 123", output);
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestVigenere.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestVigenere.java
//Mattrixwv //Mattrixwv
// Created: 07-25-21 // Created: 07-25-21
//Modified: 04-18-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.monosubstitution; 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.anyChar;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@@ -20,34 +19,33 @@ import static org.mockito.Mockito.verify;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
@ExtendWith(MockitoExtension.class)
public class TestVigenere{ public class TestVigenere{
@InjectMocks
private Vigenere cipher; private Vigenere cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String inputString = "MeSsage to^encode"; private String inputString = "MeSsage to^encode";
private String inputStringClean = "MESSAGETOENCODE"; private String inputStringClean = "MESSAGETOENCODE";
private String outputString = "WiQooxh ds^cjqfgo"; private String outputString = "WiQooxh ds^cjqfgo";
private String outputStringClean = "WIQOOXHDSCJQFGO"; private String outputStringClean = "WIQOOXHDSCJQFGO";
private String keyword = "keyword"; private String keyword = "ke yw*ord";
private String keywordClean = "KEYWORD";
private ArrayList<Integer> offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3)); private ArrayList<Integer> 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 @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new Vigenere(); cipher = new Vigenere();
@@ -102,11 +100,11 @@ public class TestVigenere{
@Test @Test
public void testSetOffset(){ public void testSetOffset(){
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.setOffset(); cipher.setOffset();
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(offset, cipher.offset); assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Setting offset array from keyword"); verify(logger, times(1)).debug("Setting offset array from keyword");
verify(logger, times(1)).debug("Offset {}", offset); verify(logger, times(1)).debug("Offset {}", offset);
@@ -117,7 +115,7 @@ public class TestVigenere{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.offset = offset; cipher.offset = offset;
cipher.setInputString(inputString); cipher.setInputString(inputString);
@@ -135,7 +133,7 @@ public class TestVigenere{
cipher.preserveCapitals = false; cipher.preserveCapitals = false;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.offset = offset; cipher.offset = offset;
cipher.setInputString(inputString); cipher.setInputString(inputString);
@@ -153,7 +151,7 @@ public class TestVigenere{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = false; cipher.preserveWhitespace = false;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.offset = offset; cipher.offset = offset;
cipher.setInputString(inputString); cipher.setInputString(inputString);
@@ -171,7 +169,7 @@ public class TestVigenere{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = false; cipher.preserveSymbols = false;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.offset = offset; cipher.offset = offset;
cipher.setInputString(inputString); cipher.setInputString(inputString);
@@ -189,7 +187,7 @@ public class TestVigenere{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.offset = offset; cipher.offset = offset;
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
@@ -209,7 +207,7 @@ public class TestVigenere{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.offset = offset; cipher.offset = offset;
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
@@ -228,12 +226,12 @@ public class TestVigenere{
public void testSetKeyword(){ public void testSetKeyword(){
cipher.setKeyword(keyword); cipher.setKeyword(keyword);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(offset, cipher.offset); assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Original keyword '{}'", keyword); verify(logger, times(1)).debug("Original keyword '{}'", keyword);
verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing all non-letter characters"); 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 @Test
@@ -270,7 +268,7 @@ public class TestVigenere{
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.inputString = inputString; cipher.inputString = inputString;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.offset = offset; cipher.offset = offset;
cipher.encode(); cipher.encode();
@@ -292,7 +290,7 @@ public class TestVigenere{
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.inputString = outputString; cipher.inputString = outputString;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.offset = offset; cipher.offset = offset;
cipher.decode(); cipher.decode();
@@ -344,7 +342,7 @@ public class TestVigenere{
String output = cipher.encode(keyword, inputString); String output = cipher.encode(keyword, inputString);
assertEquals(inputString, cipher.inputString); assertEquals(inputString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(offset, cipher.offset); assertEquals(offset, cipher.offset);
assertEquals(outputString, cipher.outputString); assertEquals(outputString, cipher.outputString);
assertEquals(outputString, output); assertEquals(outputString, output);
@@ -357,7 +355,7 @@ public class TestVigenere{
String output = cipher.encode(keyword, inputString); String output = cipher.encode(keyword, inputString);
assertEquals(inputStringClean, cipher.inputString); assertEquals(inputStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(offset, cipher.offset); assertEquals(offset, cipher.offset);
assertEquals(outputStringClean, cipher.outputString); assertEquals(outputStringClean, cipher.outputString);
assertEquals(outputStringClean, output); assertEquals(outputStringClean, output);
@@ -370,7 +368,7 @@ public class TestVigenere{
String output = cipher.decode(keyword, outputString); String output = cipher.decode(keyword, outputString);
assertEquals(outputString, cipher.inputString); assertEquals(outputString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(offset, cipher.offset); assertEquals(offset, cipher.offset);
assertEquals(inputString, cipher.outputString); assertEquals(inputString, cipher.outputString);
assertEquals(inputString, output); assertEquals(inputString, output);
@@ -383,7 +381,7 @@ public class TestVigenere{
String output = cipher.decode(keyword, outputString); String output = cipher.decode(keyword, outputString);
assertEquals(outputStringClean, cipher.inputString); assertEquals(outputStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(offset, cipher.offset); assertEquals(offset, cipher.offset);
assertEquals(inputStringClean, cipher.outputString); assertEquals(inputStringClean, cipher.outputString);
assertEquals(inputStringClean, output); assertEquals(inputStringClean, output);

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestBifid.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestBifid.java
//Mattrixwv //Mattrixwv
// Created: 03-03-22 // Created: 03-03-22
//Modified: 04-23-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; 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.anyChar;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
@ExtendWith(MockitoExtension.class)
public class TestBifid{ public class TestBifid{
@InjectMocks
private Bifid cipher; private Bifid cipher;
@Mock
private Logger logger; private Logger logger;
//Test //Test
private String inputString = "Message to^encode"; private String inputString = "Message to^encode";
@@ -38,14 +43,6 @@ public class TestBifid{
private String keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ"; private String keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ";
@BeforeEach
public void setup(){
cipher = new Bifid();
logger = mock(Logger.class);
Bifid.logger = logger;
}
@Test @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new Bifid(); cipher = new Bifid();
@@ -121,7 +118,6 @@ public class TestBifid{
}); });
verify(logger, never()).debug(eq("Setting keyword '{}'"), anyString()); verify(logger, never()).debug(eq("Setting keyword '{}'"), anyString());
verify(logger, never()).debug(anyString(), anyString());
} }
@Test @Test

View File

@@ -1,7 +1,7 @@
//Mattrixwv/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestColumnar.java //Mattrixwv/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestColumnar.java
//Mattrixwv //Mattrixwv
// Created: 01-16-22 // Created: 01-16-22
//Modified: 04-26-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; 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.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@@ -21,8 +20,11 @@ import static org.mockito.Mockito.verify;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
@@ -30,8 +32,11 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
@ExtendWith(MockitoExtension.class)
public class TestColumnar{ public class TestColumnar{
@InjectMocks
private Columnar cipher; private Columnar cipher;
@Mock
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "Message to*encode"; 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 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 encodedStringPadded = "Edxeoxm te*acxgoxsnxsex"; //When padding is left in outputString
private String encodedStringClean = "EDXEOXMTEACXGOXSNXSEX"; private String encodedStringClean = "EDXEOXMTEACXGOXSNXSEX";
private String keyword = "keyword"; private String keyword = "ke yw*ord";
private String keywordClean = "KEYWORD";
private ArrayList<ArrayList<Character>> encodeGrid = new ArrayList<>( private ArrayList<ArrayList<Character>> encodeGrid = new ArrayList<>(
List.of( List.of(
new ArrayList<>( new ArrayList<>(
@@ -77,14 +83,6 @@ public class TestColumnar{
); );
@BeforeEach
public void setup(){
cipher = new Columnar();
logger = mock(Logger.class);
Columnar.logger = logger;
}
@Test @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new Columnar(); cipher = new Columnar();
@@ -257,7 +255,7 @@ public class TestColumnar{
@Test @Test
public void testCreateGridEncode(){ public void testCreateGridEncode(){
cipher.inputString = decodedStringPadded; cipher.inputString = decodedStringPadded;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.createGridEncode(); cipher.createGridEncode();
@@ -268,7 +266,7 @@ public class TestColumnar{
@Test @Test
public void testCreateGridDecode(){ public void testCreateGridDecode(){
cipher.inputString = encodedStringPadingAdded; cipher.inputString = encodedStringPadingAdded;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.createGridDecode(); cipher.createGridDecode();
@@ -281,7 +279,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword; cipher.keyword = keywordClean;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
cipher.setInputStringEncode(decodedString); cipher.setInputStringEncode(decodedString);
@@ -301,7 +299,7 @@ public class TestColumnar{
cipher.preserveCapitals = false; cipher.preserveCapitals = false;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword; cipher.keyword = keywordClean;
cipher.characterToAdd = 'X'; cipher.characterToAdd = 'X';
cipher.setInputStringEncode(decodedString); cipher.setInputStringEncode(decodedString);
@@ -321,7 +319,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = false; cipher.preserveWhitespace = false;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword; cipher.keyword = keywordClean;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
cipher.setInputStringEncode(decodedString); cipher.setInputStringEncode(decodedString);
@@ -341,7 +339,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = false; cipher.preserveSymbols = false;
cipher.keyword = keyword; cipher.keyword = keywordClean;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
cipher.setInputStringEncode(decodedString); cipher.setInputStringEncode(decodedString);
@@ -427,7 +425,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
cipher.setInputStringDecode(encodedString); cipher.setInputStringDecode(encodedString);
@@ -446,7 +444,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
cipher.setInputStringDecode("Message to encod"); cipher.setInputStringDecode("Message to encod");
@@ -465,7 +463,7 @@ public class TestColumnar{
cipher.preserveCapitals = false; cipher.preserveCapitals = false;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.characterToAdd = 'X'; cipher.characterToAdd = 'X';
cipher.setInputStringDecode(encodedString); cipher.setInputStringDecode(encodedString);
@@ -484,7 +482,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = false; cipher.preserveWhitespace = false;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
cipher.setInputStringDecode(encodedString); cipher.setInputStringDecode(encodedString);
@@ -503,7 +501,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = false; cipher.preserveSymbols = false;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
cipher.setInputStringDecode(encodedString); cipher.setInputStringDecode(encodedString);
@@ -522,7 +520,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
@@ -543,7 +541,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
@@ -564,7 +562,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
@@ -584,7 +582,7 @@ public class TestColumnar{
public void testCreateOutputStringFromColumns(){ public void testCreateOutputStringFromColumns(){
cipher.removePadding = true; cipher.removePadding = true;
cipher.inputString = decodedStringPadded; cipher.inputString = decodedStringPadded;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.grid = decodeGrid; cipher.grid = decodeGrid;
cipher.charsAdded = 6; cipher.charsAdded = 6;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
@@ -603,7 +601,7 @@ public class TestColumnar{
public void testCreateOutputStringFromColumns_padding(){ public void testCreateOutputStringFromColumns_padding(){
cipher.removePadding = false; cipher.removePadding = false;
cipher.inputString = decodedStringPadded; cipher.inputString = decodedStringPadded;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.grid = decodeGrid; cipher.grid = decodeGrid;
cipher.charsAdded = 6; cipher.charsAdded = 6;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
@@ -622,7 +620,7 @@ public class TestColumnar{
public void testCreateOutputStringFromRows(){ public void testCreateOutputStringFromRows(){
cipher.removePadding = false; cipher.removePadding = false;
cipher.inputString = encodedStringPadded; cipher.inputString = encodedStringPadded;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.grid = encodeGrid; cipher.grid = encodeGrid;
cipher.charsAdded = 6; cipher.charsAdded = 6;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
@@ -645,7 +643,7 @@ public class TestColumnar{
public void testCreateOuputStringFromRows_removePadding(){ public void testCreateOuputStringFromRows_removePadding(){
cipher.removePadding = true; cipher.removePadding = true;
cipher.inputString = encodedStringPadingAdded; cipher.inputString = encodedStringPadingAdded;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.grid = encodeGrid; cipher.grid = encodeGrid;
cipher.charsAdded = 6; cipher.charsAdded = 6;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
@@ -668,9 +666,9 @@ public class TestColumnar{
public void testSetKeyword(){ public void testSetKeyword(){
cipher.setKeyword(keyword); 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("Original keyword {}", keyword);
verify(logger, times(1)).debug("Cleaned keyword {}", keyword.toUpperCase()); verify(logger, times(1)).debug("Cleaned keyword {}", keywordClean);
} }
@Test @Test
@@ -732,7 +730,7 @@ public class TestColumnar{
public void testGetKeywordAlphaLocations(){ public void testGetKeywordAlphaLocations(){
ArrayList<Integer> alphaLocations = new ArrayList<>(List.of(6, 1, 0, 4, 5, 3, 2)); ArrayList<Integer> alphaLocations = new ArrayList<>(List.of(6, 1, 0, 4, 5, 3, 2));
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
ArrayList<Integer> returnedLocations = cipher.getKeywordAlphaLocations(); ArrayList<Integer> returnedLocations = cipher.getKeywordAlphaLocations();
@@ -745,7 +743,7 @@ public class TestColumnar{
public void testGetKeywordOriginalLocations(){ public void testGetKeywordOriginalLocations(){
ArrayList<Integer> orderedLocations = new ArrayList<>(List.of(2, 1, 6, 5, 3, 4, 0)); ArrayList<Integer> orderedLocations = new ArrayList<>(List.of(2, 1, 6, 5, 3, 4, 0));
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
ArrayList<Integer> returnedLocations = cipher.getKeywordOriginalLocations(); ArrayList<Integer> returnedLocations = cipher.getKeywordOriginalLocations();
@@ -785,7 +783,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.removePadding = true; cipher.removePadding = true;
cipher.inputString = decodedStringPadded; cipher.inputString = decodedStringPadded;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.charsAdded = 6; cipher.charsAdded = 6;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
@@ -804,7 +802,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.removePadding = false; cipher.removePadding = false;
cipher.inputString = decodedStringPadded; cipher.inputString = decodedStringPadded;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.charsAdded = 6; cipher.charsAdded = 6;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
@@ -823,7 +821,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.removePadding = true; cipher.removePadding = true;
cipher.inputString = encodedStringPadingAdded; cipher.inputString = encodedStringPadingAdded;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.charsAdded = 6; cipher.charsAdded = 6;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
@@ -842,7 +840,7 @@ public class TestColumnar{
cipher.preserveCapitals = true; cipher.preserveCapitals = true;
cipher.removePadding = false; cipher.removePadding = false;
cipher.inputString = encodedStringPadded; cipher.inputString = encodedStringPadded;
cipher.keyword = keyword.toUpperCase(); cipher.keyword = keywordClean;
cipher.charsAdded = 6; cipher.charsAdded = 6;
cipher.characterToAdd = 'x'; cipher.characterToAdd = 'x';
@@ -891,7 +889,7 @@ public class TestColumnar{
String output = cipher.encode(keyword, decodedString); String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringPadded, cipher.inputString); assertEquals(decodedStringPadded, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(encodedString, cipher.outputString); assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output); assertEquals(encodedString, output);
} }
@@ -903,7 +901,7 @@ public class TestColumnar{
String output = cipher.encode(keyword, decodedString); String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringPadded, cipher.inputString); assertEquals(decodedStringPadded, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(encodedStringPadded, cipher.outputString); assertEquals(encodedStringPadded, cipher.outputString);
assertEquals(encodedStringPadded, output); assertEquals(encodedStringPadded, output);
} }
@@ -915,7 +913,7 @@ public class TestColumnar{
String output = cipher.encode(keyword, decodedString); String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString); assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString); assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output); assertEquals(encodedStringClean, output);
} }
@@ -927,7 +925,7 @@ public class TestColumnar{
String output = cipher.decode(keyword, encodedString); String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringPadingAdded, cipher.inputString); assertEquals(encodedStringPadingAdded, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(decodedString, cipher.outputString); assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output); assertEquals(decodedString, output);
} }
@@ -939,7 +937,7 @@ public class TestColumnar{
String output = cipher.decode(keyword, encodedStringPadded); String output = cipher.decode(keyword, encodedStringPadded);
assertEquals(encodedStringPadded, cipher.inputString); assertEquals(encodedStringPadded, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(decodedStringPadded, cipher.outputString); assertEquals(decodedStringPadded, cipher.outputString);
assertEquals(decodedStringPadded, output); assertEquals(decodedStringPadded, output);
} }
@@ -951,7 +949,7 @@ public class TestColumnar{
String output = cipher.decode(keyword, encodedString); String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString); assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword); assertEquals(keywordClean, cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString); assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output); assertEquals(decodedStringClean, output);
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamjava/polySubstitution/TestHill.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamjava/polySubstitution/TestHill.java
//Mattrixwv //Mattrixwv
// Created: 01-31-22 // Created: 01-31-22
//Modified: 04-28-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; 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.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import java.util.ArrayList; import java.util.ArrayList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
@@ -31,8 +33,11 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeyException;
import com.mattrixwv.matrix.ModMatrix; import com.mattrixwv.matrix.ModMatrix;
@ExtendWith(MockitoExtension.class)
public class TestHill{ public class TestHill{
@InjectMocks
private Hill cipher; private Hill cipher;
@Mock
private Logger logger; private Logger logger;
//Fields //Fields
private String decodedString = "Message to^encoded"; private String decodedString = "Message to^encoded";
@@ -44,14 +49,6 @@ public class TestHill{
private ModMatrix key = new ModMatrix(keyArray, 26); private ModMatrix key = new ModMatrix(keyArray, 26);
@BeforeEach
public void setup(){
cipher = new Hill();
logger = mock(Logger.class);
Hill.logger = logger;
}
@Test @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new Hill(); cipher = new Hill();
@@ -152,7 +149,7 @@ public class TestHill{
verify(logger, times(1)).debug("Testing mod"); verify(logger, times(1)).debug("Testing mod");
verify(logger, times(1)).debug("Testing square"); verify(logger, times(1)).debug("Testing square");
verify(logger, times(1)).debug("Testing invertable"); verify(logger, times(1)).debug("Testing invertable");
verify(logger, times(1)).debug("key = {}", key); verify(logger, times(1)).debug("key\n{}", key);
} }
@Test @Test
@@ -168,7 +165,7 @@ public class TestHill{
verify(logger, times(1)).debug("Testing mod"); verify(logger, times(1)).debug("Testing mod");
verify(logger, never()).debug("Testing square"); verify(logger, never()).debug("Testing square");
verify(logger, never()).debug("Testing invertable"); 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 @Test
@@ -184,7 +181,7 @@ public class TestHill{
verify(logger, times(1)).debug("Testing mod"); verify(logger, times(1)).debug("Testing mod");
verify(logger, times(1)).debug("Testing square"); verify(logger, times(1)).debug("Testing square");
verify(logger, never()).debug("Testing invertable"); 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 @Test
@@ -200,7 +197,7 @@ public class TestHill{
verify(logger, times(1)).debug("Testing mod"); verify(logger, times(1)).debug("Testing mod");
verify(logger, times(1)).debug("Testing square"); verify(logger, times(1)).debug("Testing square");
verify(logger, times(1)).debug("Testing invertable"); 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 @Test

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java //CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java
//Mattrixwv //Mattrixwv
// Created: 04-21-23 // Created: 04-21-23
//Modified: 04-21-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; 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.anyChar;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException; import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
@ExtendWith(MockitoExtension.class)
public class TestLargePolybiusSquare{ public class TestLargePolybiusSquare{
@InjectMocks
private LargePolybiusSquare cipher; private LargePolybiusSquare cipher;
@Mock(name = "com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare")
private Logger logger; private Logger logger;
//Variables //Variables
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String decodedStringClean = "MESSAGETOENCODE"; private String decodedStringClean = "MESSAGETOENCODE";
private String encodedString = "35124343222612 4415^123624152112"; 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 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 keywordClean = "KEYWORDABCFGHIJLMNPQSTUVXZ0123456789";
private String keywordBlank = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; private String keywordBlank = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private char[][] grid = { 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 @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new LargePolybiusSquare(); cipher = new LargePolybiusSquare();

View File

@@ -1,58 +1,243 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/TestMorse.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/TestMorse.java
//Matthew Ellison //Matthew Ellison
// Created: 07-28-21 // Created: 07-28-21
//Modified: 07-09-22 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals; 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.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{ 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 @Test
public void testEncode(){ public void testEncode(){
Morse cipher = new Morse(); cipher.inputString = inputStringClean;
//Test 1 cipher.encode();
String input = "sos";
String correctOutput = "... --- ...";
String output = cipher.encode(input);
assertEquals(correctOutput, output);
//Test 2 assertEquals(outputString, cipher.outputString);
input = "MORSE, CODE"; verify(logger, times(1)).debug("Encoding");
correctOutput = "-- --- .-. ... . -.-. --- -.. ."; verify(logger, times(18)).debug(eq("Working character {}"), anyChar());
output = cipher.encode(input); verify(logger, times(15)).debug("Appending letter");
assertEquals(correctOutput, output); verify(logger, times(3)).debug("Appending number");
verify(logger, times(1)).debug("Saving encoded string '{}'", outputString);
//Test 3
input = "1.23 987";
correctOutput = ".---- ..--- ...-- ----. ---.. --...";
output = cipher.encode(input);
assertEquals(correctOutput, output);
} }
@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 @Test
public void testDecode(){ public void testDecode(){
Morse cipher = new Morse(); cipher.inputString = outputString;
//Test 1 cipher.decode();
String input = "... --- ...";
String correctOutput = "SOS";
String output = cipher.decode(input);
assertEquals(correctOutput, output);
//Test 2 assertEquals(inputStringClean, cipher.outputString);
input = "-- --- .-. ... . -.-. --- -.. ."; verify(logger, times(1)).debug("Decoding");
correctOutput = "MORSECODE"; verify(logger, times(18)).debug(eq("Working letter {}"), anyString());
output = cipher.decode(input); verify(logger, times(18)).debug(eq("Decoded letter {}"), anyChar());
assertEquals(correctOutput, output); verify(logger, times(1)).debug("Saving decoded string '{}'", inputStringClean);
}
//Test 3 @Test
input = ".---- ..--- ...-- ----. ---.. --..."; public void testDecode_invalid(){
correctOutput = "123987"; cipher.inputString = "A";
output = cipher.decode(input);
assertEquals(correctOutput, output); 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);
} }
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/TestPlayfair.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/TestPlayfair.java
//Matthew Ellison //Matthew Ellison
// Created: 07-30-21 // Created: 07-30-21
//Modified: 04-28-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; 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.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
@@ -30,15 +32,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
import com.mattrixwv.cipherstream.polysubstitution.Playfair.CharLocation; import com.mattrixwv.cipherstream.polysubstitution.Playfair.CharLocation;
@ExtendWith(MockitoExtension.class)
public class TestPlayfair{ public class TestPlayfair{
@InjectMocks
private Playfair cipher; private Playfair cipher;
@Mock
private Logger logger; private Logger logger;
//Fields //Fields
private String inputString = "Hide the gold in - the@tree+stump"; private String decodedString = "Hide the gold in - the@tree+stump";
private String inputStringPadded = "Hide the gold in - the@trexe+stump"; private String decodedStringPadded = "Hide the gold in - the@trexe+stump";
private String inputStringClean = "HIDETHEGOLDINTHETREXESTUMP"; private String decodedStringClean = "HIDETHEGOLDINTHETREXESTUMP";
private String outputString = "Bmod zbx dnab ek - udm@uixmm+ouvif"; private String encodedString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
private String outputStringClean = "BMODZBXDNABEKUDMUIXMMOUVIF"; private String encodedStringClean = "BMODZBXDNABEKUDMUIXMMOUVIF";
private String keyword = "Play-fair@Exam ple"; private String keyword = "Play-fair@Exam ple";
private String keywordClean = "PLAYFIREXMBCDGHKNOQSTUVWZ"; private String keywordClean = "PLAYFIREXMBCDGHKNOQSTUVWZ";
private char[][] grid = new char[][]{ 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 @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new Playfair(); cipher = new Playfair();
@@ -424,11 +421,11 @@ public class TestPlayfair{
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.doubled = 'x'; 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("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 case");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
@@ -441,11 +438,11 @@ public class TestPlayfair{
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.doubled = 'X'; 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("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, times(1)).debug("Removing case");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
@@ -458,11 +455,11 @@ public class TestPlayfair{
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.doubled = 'x'; 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("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 case");
verify(logger, times(1)).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
@@ -475,11 +472,11 @@ public class TestPlayfair{
cipher.preserveSymbols = false; cipher.preserveSymbols = false;
cipher.doubled = 'x'; 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("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 case");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols"); verify(logger, times(1)).debug("Removing symbols");
@@ -511,15 +508,15 @@ public class TestPlayfair{
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.doubled = 'x'; 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("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 case");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); 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 @Test
@@ -530,16 +527,16 @@ public class TestPlayfair{
cipher.doubled = 'x'; cipher.doubled = 'x';
assertThrows(InvalidCharacterException.class, () -> { assertThrows(InvalidCharacterException.class, () -> {
cipher.setInputString(outputString + cipher.replaced, false); cipher.setInputString(encodedString + cipher.replaced, false);
}); });
assertEquals("", cipher.inputString); assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Setting input string"); 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 case");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
verify(logger, never()).debug("Clean input string '{}'", outputString); verify(logger, never()).debug("Clean input string '{}'", encodedString);
} }
@Test @Test
@@ -550,16 +547,16 @@ public class TestPlayfair{
cipher.doubled = 'x'; cipher.doubled = 'x';
assertThrows(InvalidInputException.class, () -> { 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("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 case");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); 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 @Test
@@ -606,48 +603,48 @@ public class TestPlayfair{
public void testSetInputStringEncode(){ public void testSetInputStringEncode(){
cipher.doubled = 'x'; 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("Cleaning up input string for encoding");
verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer); 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("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(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, times(1)).debug("Checking odd characters");
verify(logger, never()).debug("Adding final character to make even"); 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 @Test
public void testSetInputStringEncode_odd(){ public void testSetInputStringEncode_odd(){
cipher.doubled = 'x'; 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("Cleaning up input string for encoding");
verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer); 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("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(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("Checking odd characters");
verify(logger, times(1)).debug("Adding final character to make even"); 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 @Test
public void testSetInputString_oddEndSymbol(){ public void testSetInputString_oddEndSymbol(){
cipher.doubled = 'x'; 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("Cleaning up input string for encoding");
verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer); 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("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(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("Checking odd characters");
verify(logger, times(1)).debug("Adding final character to make even"); 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 @Test
@@ -655,26 +652,26 @@ public class TestPlayfair{
cipher.doubled = 'x'; cipher.doubled = 'x';
cipher.replacer = 'i'; 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("Cleaning up input string for encoding");
verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer); 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("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(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("Checking odd characters");
verify(logger, times(1)).debug("Adding final character to make even"); 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 @Test
public void testGetPreparedInputString(){ public void testGetPreparedInputString(){
cipher.inputString = inputStringPadded; cipher.inputString = decodedStringPadded;
String output = cipher.getPreparedInputString(); 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("Getting input string ready for encoding");
verify(logger, times(1)).debug("Prepared string '{}'", inputStringClean); verify(logger, times(1)).debug("Prepared string '{}'", decodedStringClean);
} }
@Test @Test
@@ -768,28 +765,28 @@ public class TestPlayfair{
@Test @Test
public void testAddCharactersToCleanString(){ 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(1)).debug("Formatting output string");
verify(logger, times(34)).debug(eq("Working character {}"), anyChar()); verify(logger, times(34)).debug(eq("Working character {}"), anyChar());
verify(logger, times(1)).debug("Appending uppercase"); verify(logger, times(1)).debug("Appending uppercase");
verify(logger, times(25)).debug("Appending lowercase"); verify(logger, times(25)).debug("Appending lowercase");
verify(logger, times(8)).debug("Appending symbol"); verify(logger, times(8)).debug("Appending symbol");
verify(logger, times(1)).debug("Formatted output '{}'", outputString); verify(logger, times(1)).debug("Formatted output '{}'", encodedString);
} }
@Test @Test
public void testEncode(){ public void testEncode(){
cipher.inputString = inputStringPadded; cipher.inputString = decodedStringPadded;
cipher.keyword = keywordClean; cipher.keyword = keywordClean;
cipher.grid = grid; cipher.grid = grid;
cipher.encode(); cipher.encode();
assertEquals(outputString, cipher.outputString); assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Encoding"); verify(logger, times(1)).debug("Encoding");
verify(logger, times(13)).debug(eq("Letters {} {}"), anyChar(), anyChar()); verify(logger, times(13)).debug(eq("Letters {} {}"), anyChar(), anyChar());
verify(logger, times(2)).debug("Row encoding"); verify(logger, times(2)).debug("Row encoding");
@@ -800,13 +797,13 @@ public class TestPlayfair{
@Test @Test
public void testDecode(){ public void testDecode(){
cipher.inputString = outputString; cipher.inputString = encodedString;
cipher.keyword = keywordClean; cipher.keyword = keywordClean;
cipher.grid = grid; cipher.grid = grid;
cipher.decode(); cipher.decode();
assertEquals(inputStringPadded, cipher.outputString); assertEquals(decodedStringPadded, cipher.outputString);
verify(logger, times(1)).debug("Decoding"); verify(logger, times(1)).debug("Decoding");
verify(logger, times(13)).debug(eq("Letters {} {}"), anyChar(), anyChar()); verify(logger, times(13)).debug(eq("Letters {} {}"), anyChar(), anyChar());
verify(logger, times(2)).debug("Row decoding"); verify(logger, times(2)).debug("Row decoding");
@@ -817,8 +814,8 @@ public class TestPlayfair{
@Test @Test
public void testReset(){ public void testReset(){
cipher.inputString = inputString; cipher.inputString = decodedString;
cipher.outputString = outputString; cipher.outputString = encodedString;
cipher.keyword = keyword; cipher.keyword = keyword;
cipher.grid = grid; cipher.grid = grid;
@@ -833,13 +830,13 @@ public class TestPlayfair{
@Test @Test
public void testGetters(){ public void testGetters(){
cipher.inputString = inputString; cipher.inputString = decodedString;
cipher.outputString = outputString; cipher.outputString = encodedString;
cipher.keyword = keyword; cipher.keyword = keyword;
cipher.grid = grid; cipher.grid = grid;
assertEquals(inputString, cipher.getInputString()); assertEquals(decodedString, cipher.getInputString());
assertEquals(outputString, cipher.getOutputString()); assertEquals(encodedString, cipher.getOutputString());
assertEquals(keyword, cipher.getKeyword()); 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("[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()); assertEquals('J', cipher.getReplaced());
@@ -852,12 +849,12 @@ public class TestPlayfair{
public void testPracticalEncode(){ public void testPracticalEncode(){
cipher = new Playfair(true, true, true); 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(keywordClean, cipher.keyword);
assertEquals(outputString, cipher.outputString); assertEquals(encodedString, cipher.outputString);
assertEquals(outputString, output); assertEquals(encodedString, output);
assertArrayEquals(grid, cipher.grid); assertArrayEquals(grid, cipher.grid);
} }
@@ -865,12 +862,12 @@ public class TestPlayfair{
public void testPracticalEncode_clean(){ public void testPracticalEncode_clean(){
cipher = new Playfair(false, false, false); 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(keywordClean, cipher.keyword);
assertEquals(outputStringClean, cipher.outputString); assertEquals(encodedStringClean, cipher.outputString);
assertEquals(outputStringClean, output); assertEquals(encodedStringClean, output);
assertArrayEquals(grid, cipher.grid); assertArrayEquals(grid, cipher.grid);
} }
@@ -878,12 +875,12 @@ public class TestPlayfair{
public void testPracticalDecode(){ public void testPracticalDecode(){
cipher = new Playfair(true, true, true); 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(keywordClean, cipher.keyword);
assertEquals(inputStringPadded, cipher.outputString); assertEquals(decodedStringPadded, cipher.outputString);
assertEquals(inputStringPadded, output); assertEquals(decodedStringPadded, output);
assertArrayEquals(grid, cipher.grid); assertArrayEquals(grid, cipher.grid);
} }
@@ -891,12 +888,12 @@ public class TestPlayfair{
public void testPracticalDecode_clean(){ public void testPracticalDecode_clean(){
cipher = new Playfair(false, false, false); 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(keywordClean, cipher.keyword);
assertEquals(inputStringClean, cipher.outputString); assertEquals(decodedStringClean, cipher.outputString);
assertEquals(inputStringClean, output); assertEquals(decodedStringClean, output);
assertArrayEquals(grid, cipher.grid); assertArrayEquals(grid, cipher.grid);
} }
} }

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestPolybiusSquare.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestPolybiusSquare.java
//Mattrixwv //Mattrixwv
// Created: 01-04-22 // Created: 01-04-22
//Modified: 04-29-23 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; 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.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException; import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
@@ -29,15 +31,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare.CharLocation; import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare.CharLocation;
@ExtendWith(MockitoExtension.class)
public class TestPolybiusSquare{ public class TestPolybiusSquare{
@InjectMocks
private PolybiusSquare cipher; private PolybiusSquare cipher;
@Mock
private Logger logger; private Logger logger;
//Fields //Fields
private String inputString = "Message to^encode"; private String decodedString = "Message to^encode";
private String inputStringClean = "M E S S A G E T O E N C O D E"; private String decodedStringClean = "M E S S A G E T O E N C O D E";
private String outputString = "41124545233212 5115^124225152212"; private String encodedString = "41124545233212 5115^124225152212";
private String outputStringClean = "41 12 45 45 23 32 12 51 15 12 42 25 15 22 12"; private String encodedStringClean = "41 12 45 45 23 32 12 51 15 12 42 25 15 22 12";
private String keyword = "keyword"; private String keyword = "ke yw*ord";
private String keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ"; private String keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ";
private char[][] grid = new char[][]{ private char[][] grid = new char[][]{
{'K', 'E', 'Y', 'W', 'O'}, {'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]"; 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 @Test
public void testConstructor_default(){ public void testConstructor_default(){
cipher = new PolybiusSquare(); cipher = new PolybiusSquare();
@@ -219,16 +216,16 @@ public class TestPolybiusSquare{
cipher.replaced = 'J'; cipher.replaced = 'J';
cipher.replacer = 'I'; cipher.replacer = 'I';
cipher.setInputStringEncode(inputString); cipher.setInputStringEncode(decodedString);
assertEquals(inputString.toUpperCase(), cipher.inputString); assertEquals(decodedString.toUpperCase(), cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", 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("Checking for digits");
verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing case");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer); 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 @Test
@@ -238,16 +235,16 @@ public class TestPolybiusSquare{
cipher.replaced = 'J'; cipher.replaced = 'J';
cipher.replacer = 'I'; cipher.replacer = 'I';
cipher.setInputStringEncode(inputString); cipher.setInputStringEncode(decodedString);
assertEquals(inputString.toUpperCase().replaceAll("\\s", ""), cipher.inputString); assertEquals(decodedString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", 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("Checking for digits");
verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer); 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 @Test
@@ -257,16 +254,16 @@ public class TestPolybiusSquare{
cipher.replaced = 'J'; cipher.replaced = 'J';
cipher.replacer = 'I'; cipher.replacer = 'I';
cipher.setInputStringEncode(inputString); cipher.setInputStringEncode(decodedString);
assertEquals(inputString.toUpperCase().replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString); assertEquals(decodedString.toUpperCase().replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", 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("Checking for digits");
verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing case");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols"); verify(logger, times(1)).debug("Removing symbols");
verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer); 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 @Test
@@ -276,16 +273,16 @@ public class TestPolybiusSquare{
cipher.replaced = 'J'; cipher.replaced = 'J';
cipher.replacer = 'I'; cipher.replacer = 'I';
cipher.setInputStringEncode(inputString); cipher.setInputStringEncode(decodedString);
assertEquals(inputStringClean, cipher.inputString); assertEquals(decodedStringClean, cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", 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("Checking for digits");
verify(logger, times(1)).debug("Removing case"); verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols"); verify(logger, times(1)).debug("Removing symbols");
verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer); 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 @Test
@@ -377,14 +374,14 @@ public class TestPolybiusSquare{
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.setInputStringDecode(outputString); cipher.setInputStringDecode(encodedString);
assertEquals(outputString, cipher.inputString); assertEquals(encodedString, cipher.inputString);
verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString); verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString);
verify(logger, times(1)).debug("Checking for letters"); verify(logger, times(1)).debug("Checking for letters");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); 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 @Test
@@ -392,14 +389,14 @@ public class TestPolybiusSquare{
cipher.preserveWhitespace = false; cipher.preserveWhitespace = false;
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
cipher.setInputStringDecode(outputString); cipher.setInputStringDecode(encodedString);
assertEquals(outputString.replaceAll("\\s", ""), cipher.inputString); assertEquals(encodedString.replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString); verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString);
verify(logger, times(1)).debug("Checking for letters"); verify(logger, times(1)).debug("Checking for letters");
verify(logger, times(1)).debug("Removing whitespace"); verify(logger, times(1)).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); 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 @Test
@@ -407,14 +404,14 @@ public class TestPolybiusSquare{
cipher.preserveWhitespace = true; cipher.preserveWhitespace = true;
cipher.preserveSymbols = false; cipher.preserveSymbols = false;
cipher.setInputStringDecode(outputString); cipher.setInputStringDecode(encodedString);
assertEquals(outputString.replaceAll("[^0-9\\s]", ""), cipher.inputString); assertEquals(encodedString.replaceAll("[^0-9\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString); verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString);
verify(logger, times(1)).debug("Checking for letters"); verify(logger, times(1)).debug("Checking for letters");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols"); 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 @Test
@@ -423,11 +420,11 @@ public class TestPolybiusSquare{
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringDecode(outputString + "a"); cipher.setInputStringDecode(encodedString + "a");
}); });
assertEquals("", cipher.inputString); 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, times(1)).debug("Checking for letters");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
@@ -440,11 +437,11 @@ public class TestPolybiusSquare{
cipher.preserveSymbols = true; cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> { assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringDecode(outputString + "0"); cipher.setInputStringDecode(encodedString + "0");
}); });
assertEquals("", cipher.inputString); 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, times(1)).debug("Checking for letters");
verify(logger, never()).debug("Removing whitespace"); verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols"); verify(logger, never()).debug("Removing symbols");
@@ -504,24 +501,24 @@ public class TestPolybiusSquare{
@Test @Test
public void testGetPreparedInputStringEncode(){ public void testGetPreparedInputStringEncode(){
cipher.inputString = inputString; cipher.inputString = decodedString;
String output = cipher.getPreparedInputStringEncode(); 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("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 @Test
public void testGetPreparedInputStringDecode(){ public void testGetPreparedInputStringDecode(){
cipher.inputString = outputString; cipher.inputString = encodedString;
String output = cipher.getPreparedInputStringDecode(); 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("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 @Test
@@ -602,41 +599,41 @@ public class TestPolybiusSquare{
@Test @Test
public void testAddCharactersToCleanStringEncode(){ 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(1)).debug("Formatting output string for encoding");
verify(logger, times(17)).debug(eq("Working character {}"), anyChar()); verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
verify(logger, times(15)).debug("Adding encoded characters"); verify(logger, times(15)).debug("Adding encoded characters");
verify(logger, times(2)).debug("Adding symbols"); verify(logger, times(2)).debug("Adding symbols");
verify(logger, times(1)).debug("Formatted output '{}'", outputString); verify(logger, times(1)).debug("Formatted output '{}'", encodedString);
} }
@Test @Test
public void testAddCharactersToCleanStringDecode(){ 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(1)).debug("Formatting output string for decoding");
verify(logger, times(17)).debug(eq("Working character {}"), anyChar()); verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
verify(logger, times(15)).debug("Adding decoded characters"); verify(logger, times(15)).debug("Adding decoded characters");
verify(logger, times(2)).debug("Adding symbols"); 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 @Test
public void testEncode(){ public void testEncode(){
cipher.inputString = inputString.toUpperCase(); cipher.inputString = decodedString.toUpperCase();
cipher.keyword = keywordClean; cipher.keyword = keywordClean;
cipher.grid = grid; cipher.grid = grid;
cipher.encode(); cipher.encode();
assertEquals(outputString, cipher.outputString); assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Encoding"); verify(logger, times(1)).debug("Encoding");
verify(logger, times(15)).debug(eq("Current working character {}"), anyChar()); verify(logger, times(15)).debug(eq("Current working character {}"), anyChar());
verify(logger, times(15)).debug(eq("Location {}, {}"), anyInt(), anyInt()); verify(logger, times(15)).debug(eq("Location {}, {}"), anyInt(), anyInt());
@@ -644,13 +641,13 @@ public class TestPolybiusSquare{
@Test @Test
public void testDecode(){ public void testDecode(){
cipher.inputString = outputString; cipher.inputString = encodedString;
cipher.keyword = keywordClean; cipher.keyword = keywordClean;
cipher.grid = grid; cipher.grid = grid;
cipher.decode(); cipher.decode();
assertEquals(inputString.toUpperCase(), cipher.outputString); assertEquals(decodedString.toUpperCase(), cipher.outputString);
verify(logger, times(1)).debug("Decoding"); verify(logger, times(1)).debug("Decoding");
verify(logger, times(15)).debug(eq("Digits to decode {} {}"), anyChar(), anyChar()); verify(logger, times(15)).debug(eq("Digits to decode {} {}"), anyChar(), anyChar());
verify(logger, times(15)).debug(eq("Decoded letter {}"), anyChar()); verify(logger, times(15)).debug(eq("Decoded letter {}"), anyChar());
@@ -658,14 +655,14 @@ public class TestPolybiusSquare{
@Test @Test
public void testGetters(){ public void testGetters(){
cipher.inputString = inputString; cipher.inputString = decodedString;
cipher.keyword = keywordClean; cipher.keyword = keywordClean;
cipher.outputString = outputString; cipher.outputString = encodedString;
cipher.grid = grid; cipher.grid = grid;
assertEquals(inputString, cipher.getInputString()); assertEquals(decodedString, cipher.getInputString());
assertEquals(keywordClean, cipher.getKeyword()); 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("[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('J', cipher.getReplaced());
assertEquals('I', cipher.getReplacer()); assertEquals('I', cipher.getReplacer());
@@ -673,9 +670,9 @@ public class TestPolybiusSquare{
@Test @Test
public void testReset(){ public void testReset(){
cipher.inputString = inputString; cipher.inputString = decodedString;
cipher.keyword = keyword; cipher.keyword = keyword;
cipher.outputString = outputString; cipher.outputString = encodedString;
cipher.grid = grid; cipher.grid = grid;
cipher.reset(); cipher.reset();
@@ -692,12 +689,12 @@ public class TestPolybiusSquare{
public void testPracticalEncode(){ public void testPracticalEncode(){
cipher = new PolybiusSquare(true, true); 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(keywordClean, cipher.keyword);
assertEquals(outputString, cipher.outputString); assertEquals(encodedString, cipher.outputString);
assertEquals(outputString, output); assertEquals(encodedString, output);
assertArrayEquals(grid, cipher.grid); assertArrayEquals(grid, cipher.grid);
} }
@@ -705,12 +702,12 @@ public class TestPolybiusSquare{
public void testPracticalEncode_clean(){ public void testPracticalEncode_clean(){
cipher = new PolybiusSquare(false, false); 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(keywordClean, cipher.keyword);
assertEquals(outputStringClean, cipher.outputString); assertEquals(encodedStringClean, cipher.outputString);
assertEquals(outputStringClean, output); assertEquals(encodedStringClean, output);
assertArrayEquals(grid, cipher.grid); assertArrayEquals(grid, cipher.grid);
} }
@@ -718,12 +715,12 @@ public class TestPolybiusSquare{
public void testPracticalDecode(){ public void testPracticalDecode(){
cipher = new PolybiusSquare(true, true); 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(keywordClean, cipher.keyword);
assertEquals(inputString.toUpperCase(), cipher.outputString); assertEquals(decodedString.toUpperCase(), cipher.outputString);
assertEquals(inputString.toUpperCase(), output); assertEquals(decodedString.toUpperCase(), output);
assertArrayEquals(grid, cipher.grid); assertArrayEquals(grid, cipher.grid);
} }
@@ -731,12 +728,12 @@ public class TestPolybiusSquare{
public void testPracticalDecode_clean(){ public void testPracticalDecode_clean(){
cipher = new PolybiusSquare(false, false); 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(keywordClean, cipher.keyword);
assertEquals(inputStringClean.replaceAll("\\s", ""), cipher.outputString); assertEquals(decodedStringClean.replaceAll("\\s", ""), cipher.outputString);
assertEquals(inputStringClean.replaceAll("\\s", ""), output); assertEquals(decodedStringClean.replaceAll("\\s", ""), output);
assertArrayEquals(grid, cipher.grid); assertArrayEquals(grid, cipher.grid);
} }
} }

View File

@@ -1,537 +1,455 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestRailFence.java //CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestRailFence.java
//Mattrixwv //Mattrixwv
// Created: 03-21-22 // Created: 03-21-22
//Modified: 07-09-22 //Modified: 05-04-23
package com.mattrixwv.cipherstream.polysubstitution; package com.mattrixwv.cipherstream.polysubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals; 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.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.InvalidBaseException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException; import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
@ExtendWith(MockitoExtension.class)
public class TestRailFence{ 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 @Test
public void testEncode() throws InvalidBaseException, InvalidInputException{ public void testConstructor_default(){
RailFence cipher = new RailFence(true, true, true); cipher = new RailFence();
//Test lowercase encoding assertFalse(cipher.preserveCapitals);
String inputString = "messagetoencode"; assertFalse(cipher.preserveWhitespace);
int numRails = 3; assertFalse(cipher.preserveSymbols);
String correctOutput = "maooesgtecdsene"; assertEquals("", cipher.inputString);
String output = cipher.encode(numRails, inputString); assertEquals("", cipher.outputString);
assertEquals(correctOutput, output); assertNull(cipher.fence);
//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);
} }
@Test @Test
public void testNocapitalEncode() throws InvalidBaseException, InvalidInputException{ public void testConstructor_noCapitals(){
RailFence cipher = new RailFence(false, true, true); cipher = new RailFence(false, true, true);
//Test lowercase encoding assertFalse(cipher.preserveCapitals);
String inputString = "messagetoencode"; assertTrue(cipher.preserveWhitespace);
int numRails = 3; assertTrue(cipher.preserveSymbols);
String correctOutput = "MAOOESGTECDSENE"; assertEquals("", cipher.inputString);
String output = cipher.encode(numRails, inputString); assertEquals("", cipher.outputString);
assertEquals(correctOutput, output); assertNull(cipher.fence);
//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);
} }
@Test @Test
public void testNoWhitespaceEncode() throws InvalidBaseException, InvalidInputException{ public void testConstructor_noWhitespace(){
RailFence cipher = new RailFence(true, false, true); cipher = new RailFence(true, false, true);
//Test lowercase encoding assertTrue(cipher.preserveCapitals);
String inputString = "messagetoencode"; assertFalse(cipher.preserveWhitespace);
int numRails = 3; assertTrue(cipher.preserveSymbols);
String correctOutput = "maooesgtecdsene"; assertEquals("", cipher.inputString);
String output = cipher.encode(numRails, inputString); assertEquals("", cipher.outputString);
assertEquals(correctOutput, output); assertNull(cipher.fence);
//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);
} }
@Test @Test
public void testNoSymbolEncode() throws InvalidBaseException, InvalidInputException{ public void testConstructor_noSymbols(){
RailFence cipher = new RailFence(true, true, false); cipher = new RailFence(true, true, false);
//Test lowercase encoding assertTrue(cipher.preserveCapitals);
String inputString = "messagetoencode"; assertTrue(cipher.preserveWhitespace);
int numRails = 3; assertFalse(cipher.preserveSymbols);
String correctOutput = "maooesgtecdsene"; assertEquals("", cipher.inputString);
String output = cipher.encode(numRails, inputString); assertEquals("", cipher.outputString);
assertEquals(correctOutput, output); assertNull(cipher.fence);
//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);
} }
@Test @Test
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidBaseException, InvalidInputException{ public void testSetInputString(){
RailFence cipher = new RailFence(false, false, false); cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
//Test lowercase encoding cipher.setInputString(decodedString);
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 assertEquals(decodedString, cipher.inputString);
inputString = "messagetoencode"; verify(logger, times(1)).debug("Original input string '{}'", decodedString);
numRails = 5; verify(logger, never()).debug("Removing case");
correctOutput = "MOETESENESGCDAO"; verify(logger, never()).debug("Removing whitespace");
output = cipher.encode(numRails, inputString); verify(logger, never()).debug("Removing symbols");
assertEquals(correctOutput, output); verify(logger, times(1)).debug("Clean input string '{}'", decodedString);
//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);
} }
@Test @Test
public void testNoCapitalDecode() throws InvalidBaseException, InvalidInputException{ public void testSetInputString_noCapitals(){
RailFence cipher = new RailFence(false, true, true); cipher.preserveCapitals = false;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
//Test lowercase decoding cipher.setInputString(decodedString);
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 assertEquals(decodedString.toUpperCase(), cipher.inputString);
inputString = "moetesenesgcdao"; verify(logger, times(1)).debug("Original input string '{}'", decodedString);
numRails = 5; verify(logger, times(1)).debug("Removing case");
correctOutput = "MESSAGETOENCODE"; verify(logger, never()).debug("Removing whitespace");
output = cipher.decode(numRails, inputString); verify(logger, never()).debug("Removing symbols");
assertEquals(correctOutput, output); verify(logger, times(1)).debug("Clean input string '{}'", decodedString.toUpperCase());
//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);
} }
@Test @Test
public void testNoWhitespaceDecode() throws InvalidBaseException, InvalidInputException{ public void testSetInputString_noWhitespace(){
RailFence cipher = new RailFence(true, false, true); cipher.preserveCapitals = true;
cipher.preserveWhitespace = false;
cipher.preserveSymbols = true;
//Test lowercase decoding cipher.setInputString(decodedString);
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 assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
inputString = "moetesenesgcdao"; verify(logger, times(1)).debug("Original input string '{}'", decodedString);
numRails = 5; verify(logger, never()).debug("Removing case");
correctOutput = "messagetoencode"; verify(logger, times(1)).debug("Removing whitespace");
output = cipher.decode(numRails, inputString); verify(logger, never()).debug("Removing symbols");
assertEquals(correctOutput, output); verify(logger, times(1)).debug("Clean input string '{}'", decodedString.replaceAll("\\s", ""));
//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);
} }
@Test @Test
public void testNoSymbolDecode() throws InvalidBaseException, InvalidInputException{ public void testSetInputString_noSymbols(){
RailFence cipher = new RailFence(true, true, false); cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = false;
//Test lowercase decoding cipher.setInputString(decodedString);
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 assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
inputString = "moetesenesgcdao"; verify(logger, times(1)).debug("Original input string '{}'", decodedString);
numRails = 5; verify(logger, never()).debug("Removing case");
correctOutput = "messagetoencode"; verify(logger, never()).debug("Removing whitespace");
output = cipher.decode(numRails, inputString); verify(logger, times(1)).debug("Removing symbols");
assertEquals(correctOutput, output); verify(logger, times(1)).debug("Clean input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
//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);
} }
@Test @Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidBaseException, InvalidInputException{ public void testSetInputString_blank(){
RailFence cipher = new RailFence(false, false, false); cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
//Test lowercase decoding assertThrows(InvalidInputException.class, () -> {
String inputString = "maooesgtecdsene"; cipher.setInputString("");
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 assertEquals("", cipher.inputString);
inputString = "moetesenesgcdao"; verify(logger, times(1)).debug("Original input string '{}'", "");
numRails = 5; verify(logger, never()).debug("Removing case");
correctOutput = "MESSAGETOENCODE"; verify(logger, never()).debug("Removing whitespace");
output = cipher.decode(numRails, inputString); verify(logger, never()).debug("Removing symbols");
assertEquals(correctOutput, output); verify(logger, times(1)).debug("Clean input string '{}'", "");
}
//Test whitespace decoding @Test
inputString = "maooesg te cdsene"; public void testSetInputString_null(){
numRails = 3; cipher.preserveCapitals = true;
correctOutput = "MESSAGETOENCODE"; cipher.preserveWhitespace = true;
output = cipher.decode(numRails, inputString); cipher.preserveSymbols = true;
assertEquals(correctOutput, output);
//Test symbol decoding assertThrows(InvalidInputException.class, () -> {
inputString = "maooesg*te+cdsene"; cipher.setInputString(null);
numRails = 3; });
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(numRails, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding assertEquals("", cipher.inputString);
inputString = "Maooesg te^cdsene"; verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
numRails = 3; verify(logger, never()).debug("Removing case");
correctOutput = "MESSAGETOENCODE"; verify(logger, never()).debug("Removing whitespace");
output = cipher.decode(numRails, inputString); verify(logger, never()).debug("Removing symbols");
assertEquals(correctOutput, output); verify(logger, never()).debug(eq("Clean input string '{}'"), anyString());
//Throw in rail length for good measure }
inputString = "Moetese ne^sgcdao";
numRails = 5; @Test
correctOutput = "MESSAGETOENCODE"; public void testSetNumRails(){
output = cipher.decode(numRails, inputString); cipher.setNumRails(3);
assertEquals(correctOutput, output);
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);
} }
} }