Update unit test coverage

This commit is contained in:
2023-04-22 10:52:16 -04:00
parent 494293c311
commit 59885b8df6
21 changed files with 2784 additions and 2005 deletions

View File

@@ -1,12 +1,10 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java
//Mattrixwv
// Created: 01-26-22
//Modified: 04-14-23
//Modified: 04-21-23
package com.mattrixwv.cipherstream.combination;
import java.util.StringJoiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -14,181 +12,19 @@ import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
import com.mattrixwv.cipherstream.polysubstitution.Columnar;
import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare;
public class ADFGVX{
protected static Logger logger = LoggerFactory.getLogger(ADFGVX.class);
//Internal classes
protected class LargePolybiusSquare extends PolybiusSquare{
protected static final Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class);
@Override
protected void createGrid(){
logger.debug("Creating grid");
for(int row = 0;row < 6;++row){
for(int col = 0;col < 6;++col){
char letter = keyword.charAt((6 * row) + col);
grid[row][col] = letter;
}
}
}
@Override
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
//Change to upper case
inputString = inputString.toUpperCase();
//Remove any whitespace if selected
if(!preserveWhitespace){
logger.debug("Removing whitespace");
inputString = inputString.replaceAll("\\s", "");
}
//Remove any symbols if selected
if(!preserveSymbols){
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
}
if(!preserveWhitespace && !preserveSymbols){
//Add whitespace after every character for the default look
StringJoiner spacedString = new StringJoiner(" ");
for(int cnt = 0;cnt < inputString.length();++cnt){
spacedString.add(Character.toString(inputString.charAt(cnt)));
}
inputString = spacedString.toString();
}
//Save the string
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
@Override
protected String getPreparedInputStringEncoding(){
logger.debug("Preparing input string for encoding");
String cleanString = inputString.toUpperCase();
cleanString = cleanString.replaceAll("[^A-Z0-9]", "");
logger.debug("Cleaned input string '{}'", cleanString);
return cleanString;
}
@Override
protected void setKeyword(String keyword){
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
}
logger.debug("Original keyword '{}'", keyword);
//Change everything to uppercase
keyword = keyword.toUpperCase();
//Remove everything except capital letters and numbers
keyword = keyword.replaceAll("[^A-Z0-9]", "");
//Add all letters in the alphabet to the key
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//Remove all duplicate characters
StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
this.keyword = uniqueKey.toString();
logger.debug("Cleaned keyword '{}'", keyword);
//Create the grid from the sanitized keyword
createGrid();
}
@Override
protected void addCharactersToCleanStringEncode(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Current character {}", inputString.charAt(inputCnt));
//Add both numbers of any letters to the output
if(Character.isAlphabetic(inputString.charAt(inputCnt)) || Character.isDigit(inputString.charAt(inputCnt))){
logger.debug("Appending character");
fullOutput.append(cleanString.charAt(outputCnt++));
fullOutput.append(cleanString.charAt(outputCnt++));
}
//Add any other characters that appear to the output
else{
logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
}
@Override
protected void addCharactersToCleanStringDecode(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Current character {}", inputString.charAt(inputCnt));
//Add the letter to the output and skip the second number
if(Character.isDigit(inputString.charAt(inputCnt)) || Character.isAlphabetic(inputString.charAt(inputCnt))){
logger.debug("Appending character");
fullOutput.append(cleanString.charAt(outputCnt++));
++inputCnt;
}
//Add any other characters that appear to the output
else{
logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
}
@Override
public void reset(){
logger.debug("reseting");
grid = new char[6][6];
inputString = "";
outputString = "";
keyword = "";
}
public LargePolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
super(preserveWhitespace, preserveSymbols);
}
}
//Fields
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The string that is output after encoding/decoding
protected String squareKeyword; //The keyword used in the Polybius Square
protected String outputString; //The string that is output after encoding/decoding
protected String squareKeyword; //The keyword used in the Polybius Square
protected String keyword; //The Keyword used in the Columnar cipher
//Internal ciphers
protected LargePolybiusSquare largePolybiusSquare; //The first step in encoding

View File

@@ -13,7 +13,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class OneTimePad extends Vigenere{
private static final Logger logger = LoggerFactory.getLogger(OneTimePad.class);
protected static Logger logger = LoggerFactory.getLogger(OneTimePad.class);
//?Add some kind of entropy calculator?
//?Add some kind of "book passage includer"?

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Porta.java
//Mattrixwv
// Created: 02-28-22
//Modified: 07-09-22
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,7 +13,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Porta{
private static final Logger logger = LoggerFactory.getLogger(Porta.class);
protected static Logger logger = LoggerFactory.getLogger(Porta.class);
private static final String[] tableau = {
"NOPQRSTUVWXYZABCDEFGHIJKLM", //A-B
@@ -32,15 +32,15 @@ public class Porta{
};
//Fields
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private String keyword; //The keyword used to encode the input string
private boolean preserveCapitals; //Whether to respect capitals in the output string
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
private boolean preserveSymbols; //Whether to respect symbols in the output string
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
protected String keyword; //The keyword used to encode the input string
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Ensure all keyword constraints are followed
private void setKeyword(String keyword) throws InvalidKeywordException{
protected void setKeyword(String keyword) throws InvalidKeywordException{
//Make sure the keyword isn't null
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
@@ -66,7 +66,7 @@ public class Porta{
}
}
//Ensure all input constraints are followed
private void setInputString(String inputString) throws InvalidInputException{
protected void setInputString(String inputString) throws InvalidInputException{
//Ensure the input isn't null
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -86,7 +86,7 @@ public class Porta{
inputString = inputString.replaceAll("\\s", "");
}
if(!preserveSymbols){
logger.debug("Removig symbols");
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
@@ -101,7 +101,7 @@ public class Porta{
}
}
//Returns the letter that replaces the passed in letter
private char getReplacer(int keywordCnt, char letter){
protected char getReplacer(int keywordCnt, char letter){
logger.debug("Getting letter that replaces {} at {}", letter, keywordCnt);
char keyLetter = keyword.charAt(keywordCnt % keyword.length());
@@ -129,9 +129,21 @@ public class Porta{
return replacer;
}
//Encodes the inputString and stores the result in outputString
private void encode(){
protected void encode(){
logger.debug("Encoding");
//Encoding is the same as decoding
code();
}
//Decodes the inputString and stores the result in outputString
protected void decode(){
logger.debug("Decoding");
//Decoding is the same as encoding
code();
}
//Codes the inputString and stores the result in outputString
protected void code(){
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount according to the keyword and tableau
@@ -156,15 +168,8 @@ public class Porta{
}
//Save the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
}
//Decodes the inputString and stores the result in outputString
private void decode(){
logger.debug("Decoding");
//Decoding is the same as encoding
encode();
logger.debug("Saving output string '{}'", outputString);
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Substitution.java
//Mattrixwv
// Created: 02-22-22
//Modified: 07-09-22
//Modified: 04-18-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -13,18 +13,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Substitution{
private static final Logger logger = LoggerFactory.getLogger(Substitution.class);
protected static Logger logger = LoggerFactory.getLogger(Substitution.class);
//Fields
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private String key; //The keyword used to encode/decode the input
private boolean preserveCapitals; //Whether to respect capitals in the output string
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
private boolean preserveSymbols; //Whether to respect symbols in the output string
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
protected String keyword; //The keyword used to encode/decode the input
protected boolean preserveCapitals; //Whether to respect capitals in the output string
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
protected boolean preserveSymbols; //Whether to respect symbols in the output string
//Ensures key constraints are followed
private void setKey(String key) throws InvalidKeywordException{
protected void setKeyword(String key) throws InvalidKeywordException{
if(key == null){
throw new InvalidKeywordException("Key cannot be null");
}
@@ -37,8 +37,9 @@ public class Substitution{
//Make sure the key contains no duplicate mappings
logger.debug("Ensuring there are no duplicate mappings");
String tempKey = key.replaceAll("(.)\\1{2}", "");
if(!tempKey.equals(key)){
StringBuilder uniqueKey = new StringBuilder();
key.chars().distinct().forEach(c -> uniqueKey.append((char)c));
if(!key.equals(uniqueKey.toString())){
throw new InvalidKeywordException("The key cannot contain duplicate mappings");
}
@@ -47,16 +48,16 @@ public class Substitution{
logger.debug("Ensuring there are only letters in the key");
//Make sure the key contains all valid characters
tempKey = key.replaceAll("[^A-Z]", "");
String tempKey = key.replaceAll("[^A-Z]", "");
if(!tempKey.equals(key)){
throw new InvalidKeywordException("The key must contain all letters");
}
}
else if(key.length() == 36){
logger.debug("Ensure there are only alpha-numeric characters in the key");
logger.debug("Ensuring there are only alpha-numeric characters in the key");
//Make sure the key contains all valid characters
tempKey = key.replaceAll("[^A-Z0-9]", "");
String tempKey = key.replaceAll("[^A-Z0-9]", "");
if(!tempKey.equals(key)){
throw new InvalidKeywordException("The key must contain all letters and can contain all numbers");
}
@@ -67,10 +68,10 @@ public class Substitution{
//Save the key
logger.debug("Cleaned key '{}'", key);
this.key = key;
this.keyword = key;
}
//Ensure intput constraints are followed
private void setInputString(String inputString) throws InvalidInputException{
protected void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
@@ -104,7 +105,7 @@ public class Substitution{
}
}
//Encodes the inputString and stores the result in outputString
private void encode(){
protected void encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
@@ -115,15 +116,15 @@ public class Substitution{
if(Character.isUpperCase(ch)){
logger.debug("Encoding uppercase");
output.append(Character.toUpperCase(key.charAt(ch - 'A')));
output.append(Character.toUpperCase(keyword.charAt(ch - 'A')));
}
else if(Character.isLowerCase(ch)){
logger.debug("Encoding lowercase");
output.append(Character.toLowerCase(key.charAt(ch - 'a')));
output.append(Character.toLowerCase(keyword.charAt(ch - 'a')));
}
else if(Character.isDigit(ch) && (key.length() == 36)){
else if(Character.isDigit(ch) && (keyword.length() == 36)){
logger.debug("Encoding digit");
output.append(key.charAt('Z' - 'A' + Integer.valueOf(Character.toString(ch)) + 1));
output.append(keyword.charAt('Z' - 'A' + Integer.valueOf(Character.toString(ch)) + 1));
}
else{
logger.debug("Passing symbol through");
@@ -132,11 +133,11 @@ public class Substitution{
}
//Save the output
logger.debug("Encoded message '{}'", output);
this.outputString = output.toString();
logger.debug("Encoded message '{}'", outputString);
}
//Decodes the inputString and stores the result in outputString
private void decode(){
protected void decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
@@ -148,27 +149,27 @@ public class Substitution{
if(Character.isUpperCase(ch)){
logger.debug("Encoding uppercase");
output.append((char)('A' + key.indexOf(Character.toUpperCase(ch))));
output.append((char)('A' + keyword.indexOf(Character.toUpperCase(ch))));
}
else if(Character.isLowerCase(ch)){
logger.debug("Encoding lowercase");
output.append((char)('a' + key.indexOf(Character.toUpperCase(ch))));
output.append((char)('a' + keyword.indexOf(Character.toUpperCase(ch))));
}
else if(Character.isDigit(ch) && (key.length() == 36)){
else if(Character.isDigit(ch) && (keyword.length() == 36)){
logger.debug("Encoding digit");
output.append((char)('0' + (key.indexOf(Character.toUpperCase(ch)) - 26)));
output.append((char)('0' + (keyword.indexOf(Character.toUpperCase(ch)) - 26)));
}
else{
logger.debug("Passing through symbol");
logger.debug("Passing symbol through");
output.append(ch);
}
}
//Save the output
logger.debug("Encoded message '{}'", output);
this.outputString = output.toString();
logger.debug("Decoded message '{}'", outputString);
}
public Substitution(){
@@ -190,16 +191,16 @@ public class Substitution{
return outputString;
}
public String getKeyword(){
return key;
return keyword;
}
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKey(key);
setKeyword(key);
setInputString(inputString);
encode();
return outputString;
}
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKey(key);
setKeyword(key);
setInputString(inputString);
decode();
return outputString;
@@ -209,6 +210,6 @@ public class Substitution{
inputString = "";
outputString = "";
key = "";
keyword = "";
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Vigenere.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-09-22
//Modified: 04-18-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -45,7 +45,7 @@ public class Vigenere{
//Sets inputString
protected void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
throw new InvalidInputException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
@@ -76,7 +76,7 @@ public class Vigenere{
//Sets keyword
protected void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("Original keyword '{}'", keyword);
@@ -119,12 +119,7 @@ public class Vigenere{
letter += offset.get((offsetCnt++) % offset.size());
//Make sure the character is still a letter, if not, wrap around
if(letter < 'A'){
logger.debug("Wrapping around to Z");
letter += 26;
}
else if(letter > 'Z'){
if(letter > 'Z'){
logger.debug("Wrapping around to A");
letter -= 26;
@@ -136,12 +131,7 @@ public class Vigenere{
letter += offset.get((offsetCnt++) % offset.size());
//Make sure the character is still a letter, if not, wrap around
if(letter < 'a'){
logger.debug("Wrapping around to z");
letter += 26;
}
else if(letter > 'z'){
if(letter > 'z'){
logger.debug("Wrapping around to a");
letter -= 26;
@@ -153,8 +143,8 @@ public class Vigenere{
}
//Save output
logger.debug("Encoded message '{}'", output);
outputString = output.toString();
logger.debug("Encoded message '{}'", outputString);
return outputString;
}
//Decodes inputString and stores the result in outputString
@@ -180,11 +170,6 @@ public class Vigenere{
letter += 26;
}
else if(letter > 'Z'){
logger.debug("Wrapping around to A");
letter -= 26;
}
}
else if(Character.isLowerCase(letter)){
logger.debug("Decoding lowercase");
@@ -196,21 +181,16 @@ public class Vigenere{
letter += 26;
}
else if(letter > 'z'){
logger.debug("Wrapping around to a");
letter -= 26;
}
}
//Add letter to output
logger.debug("Encoded letter {}", letter);
logger.debug("Decoded character {}", letter);
output.append(letter);
}
//Save output
logger.debug("Encoded message '{}'", output);
outputString = output.toString();
logger.debug("Decoded message '{}'", outputString);
return outputString;
}

View File

@@ -18,27 +18,27 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Columnar{
private static final Logger logger = LoggerFactory.getLogger(Columnar.class);
protected static final Logger logger = LoggerFactory.getLogger(Columnar.class);
//Fields
private String inputString; //The message that needs to be encoded/decoded
private String outputString; //The encoded/decoded message
private String keyword; //The keyword used to create the grid
private char characterToAdd; //The character that is added to the end of a string to bring it to the correct length
private int charsAdded; //The number of characters that were added to the end of the message
private ArrayList<ArrayList<Character>> grid; //The grid used to encode/decode the message
private boolean preserveCapitals; //Persist capitals in the output string
private boolean preserveWhitespace; //Persist whitespace in the output string
private boolean preserveSymbols; //Persist symbols in the output string
private boolean removePadding; //Remove the padding letters added to the cipher
protected String inputString; //The message that needs to be encoded/decoded
protected String outputString; //The encoded/decoded message
protected String keyword; //The keyword used to create the grid
protected char 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 ArrayList<ArrayList<Character>> grid; //The grid used to encode/decode the message
protected boolean preserveCapitals; //Persist capitals in the output string
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
protected boolean removePadding; //Remove the padding letters added to the cipher
//Strip the inputString of all non-letter characters and change them to capitals
private String getCleanInputString(){
protected String getCleanInputString(){
logger.debug("Cleaning input string");
return inputString.toUpperCase().replaceAll("[^A-Z]", "");
}
//Create the grid from the keyword
private void createGridEncode(){
protected void createGridEncode(){
logger.debug("Creating grid for encoding");
//Add the keyword to the first row in the array
@@ -57,7 +57,7 @@ public class Columnar{
)));
}
}
private void createGridDecode(){
protected void createGridDecode(){
logger.debug("Creating grid for decoding");
//Add the keyword to the first row in the array
@@ -86,7 +86,7 @@ public class Columnar{
}
}
//Strips invalid characters from the string that needs encoded/decoded
private void setInputStringEncode(String inputString) throws InvalidInputException{
protected void setInputStringEncode(String inputString) throws InvalidInputException{
logger.debug("Setting input string for encoding");
//Ensure the input isn't null
@@ -139,7 +139,7 @@ public class Columnar{
throw new InvalidInputException("Input cannot be blank");
}
}
private void setInputStringDecode(String inputString) throws InvalidInputException{
protected void setInputStringDecode(String inputString) throws InvalidInputException{
logger.debug("Setting input string for decoding");
//Ensure the input isn't null
@@ -225,7 +225,7 @@ public class Columnar{
}
}
//Creates the output string from the grid
private void createOutputStringFromColumns(){
protected void createOutputStringFromColumns(){
logger.debug("Creating output string for encoding");
//Get the current rows of any characters that you added
@@ -274,7 +274,7 @@ public class Columnar{
logger.debug("Output string '{}'", output);
outputString = output.toString();
}
private void createOutputStringFromRows(){
protected void createOutputStringFromRows(){
logger.debug("Creating output string for decoding");
//Turn the grid into a string
@@ -338,7 +338,7 @@ public class Columnar{
outputString = output.toString();
}
//Strips invalid characters from the keyword and creates the grid
private void setKeyword(String keyword) throws InvalidKeywordException{
protected void setKeyword(String keyword) throws InvalidKeywordException{
//Ensure the keyword isn't null
if(keyword == null){
throw new NullPointerException("Keyword cannot be null");
@@ -357,7 +357,7 @@ public class Columnar{
}
}
//Set the character that is added to the end of the string
private void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
protected void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
if(!Character.isAlphabetic(characterToAdd)){
throw new InvalidCharacterException("Character to add must be a letter");
}
@@ -374,7 +374,7 @@ public class Columnar{
logger.debug("Character to add for padding {}", characterToAdd);
}
//Returns a list of integers that represents the location of the characters of the keyword in alphabetic order
private ArrayList<Integer> getKeywordAlphaLocations(){
protected ArrayList<Integer> getKeywordAlphaLocations(){
logger.debug("Creating an array of keyword letter locations");
ArrayList<Integer> orderedLocations = new ArrayList<>();
@@ -392,7 +392,7 @@ public class Columnar{
logger.debug("Array of keyword letters {}", orderedLocations);
return orderedLocations;
}
private ArrayList<Integer> getKeywordOriginalLocations(){
protected ArrayList<Integer> getKeywordOriginalLocations(){
logger.debug("Creating array of original keyword locations");
//Figure out the order the columns are in
@@ -413,7 +413,7 @@ public class Columnar{
return originalOrder;
}
//Rearanges the grid based on the list of numbers given
private void rearangeGrid(ArrayList<Integer> listOrder){
protected void rearangeGrid(ArrayList<Integer> listOrder){
logger.debug("Rearanging grid");
//Create a new grid and make sure it is the same size as the original grid
@@ -435,7 +435,7 @@ public class Columnar{
grid = newGrid;
}
//Encodes inputString using the Columnar cipher and stores the result in outputString
private void encode(){
protected void encode(){
logger.debug("Encoding");
//Create the grid
@@ -448,7 +448,7 @@ public class Columnar{
createOutputStringFromColumns();
}
//Decodes inputString using the Columnar cipher and stores the result in outputString
private void decode(){
protected void decode(){
logger.debug("Decoding");
//Create the grid

View File

@@ -0,0 +1,185 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java
//Mattrixwv
// Created: 04-21-23
// Modified: 04-21-23
package com.mattrixwv.cipherstream.polysubstitution;
import java.util.StringJoiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class LargePolybiusSquare extends PolybiusSquare{
protected static Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class);
@Override
protected void createGrid(){
logger.debug("Creating grid");
for(int row = 0;row < 6;++row){
for(int col = 0;col < 6;++col){
char letter = keyword.charAt((6 * row) + col);
grid[row][col] = letter;
}
}
}
@Override
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
logger.debug("Original input string '{}'", inputString);
//Change to upper case
inputString = inputString.toUpperCase();
//Remove any whitespace if selected
if(!preserveWhitespace){
logger.debug("Removing whitespace");
inputString = inputString.replaceAll("\\s", "");
}
//Remove any symbols if selected
if(!preserveSymbols){
logger.debug("Removing symbols");
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
}
if(!preserveWhitespace && !preserveSymbols){
//Add whitespace after every character for the default look
StringJoiner spacedString = new StringJoiner(" ");
for(int cnt = 0;cnt < inputString.length();++cnt){
spacedString.add(Character.toString(inputString.charAt(cnt)));
}
inputString = spacedString.toString();
}
//Save the string
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
@Override
protected String getPreparedInputStringEncoding(){
logger.debug("Preparing input string for encoding");
String cleanString = inputString.toUpperCase();
cleanString = cleanString.replaceAll("[^A-Z0-9]", "");
logger.debug("Prepared input string '{}'", cleanString);
return cleanString;
}
@Override
protected void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
}
logger.debug("Original keyword '{}'", keyword);
//Change everything to uppercase
keyword = keyword.toUpperCase();
//Remove everything except capital letters and numbers
keyword = keyword.replaceAll("[^A-Z0-9]", "");
//Add all letters in the alphabet to the key
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//Remove all duplicate characters
StringBuilder uniqueKey = new StringBuilder();
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
keyword = uniqueKey.toString();
logger.debug("Cleaned keyword '{}'", keyword);
this.keyword = keyword;
//Create the grid from the sanitized keyword
createGrid();
}
@Override
protected void addCharactersToCleanStringEncode(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Current character {}", inputString.charAt(inputCnt));
//Add both numbers of any letters to the output
if(Character.isAlphabetic(inputString.charAt(inputCnt)) || Character.isDigit(inputString.charAt(inputCnt))){
logger.debug("Appending character");
fullOutput.append(cleanString.charAt(outputCnt++));
fullOutput.append(cleanString.charAt(outputCnt++));
}
//Add any other characters that appear to the output
else{
logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
logger.debug("Saving output string {}", outputString);
}
@Override
protected void addCharactersToCleanStringDecode(String cleanString){
logger.debug("Formatting output string");
int outputCnt = 0;
StringBuilder fullOutput = new StringBuilder();
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
logger.debug("Current character {}", inputString.charAt(inputCnt));
//Add the letter to the output and skip the second number
if(Character.isDigit(inputString.charAt(inputCnt)) || Character.isAlphabetic(inputString.charAt(inputCnt))){
logger.debug("Appending character");
fullOutput.append(cleanString.charAt(outputCnt++));
++inputCnt;
}
//Add any other characters that appear to the output
else{
logger.debug("Appending symbol");
fullOutput.append(inputString.charAt(inputCnt));
}
}
outputString = fullOutput.toString();
logger.debug("Saving output string {}", outputString);
}
@Override
public void reset(){
logger.debug("Resetting");
grid = new char[6][6];
inputString = "";
outputString = "";
keyword = "";
}
public LargePolybiusSquare() throws InvalidCharacterException{
super();
}
public LargePolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
super(preserveWhitespace, preserveSymbols);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/combination/TestADFGVX.java
//Mattrixwv
// Created: 01-26-22
//Modified: 04-14-23
//Modified: 04-21-23
package com.mattrixwv.cipherstream.combination;
@@ -23,20 +23,20 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.mattrixwv.cipherstream.combination.ADFGVX.LargePolybiusSquare;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
import com.mattrixwv.cipherstream.polysubstitution.Columnar;
import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare;
public class TestADFGVX{
private ADFGVX cipher;
private Logger logger;
//Variables
private String encodedString = "Message to^encode";
private String encodedStringClean = "MESSAGETOENCODE";
private String decodedString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
private String decodedStringClean = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
private String decodedString = "Message to^encode";
private String decodedStringClean = "MESSAGETOENCODE";
private String encodedString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
private String encodedStringClean = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
private String keyword = "keyword";
private String squareKeyword = "SquareKeyword";
@@ -141,25 +141,10 @@ public class TestADFGVX{
@Test
public void testFormateOutputStringEncode(){
cipher.inputString = encodedString;
cipher.outputString = decodedStringClean;
cipher.inputString = decodedString;
cipher.outputString = encodedStringClean;
cipher.formatOutputStringEncode();
assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string to match input string");
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
verify(logger, times(1)).debug("Converting output to uppercase");
verify(logger, times(14)).debug("Converting output to lowercase");
verify(logger, times(2)).debug("Appending symbol to output");
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
}
@Test
public void testFormatOutputStringDecode(){
cipher.outputString = encodedStringClean;
cipher.inputString = decodedString;
cipher.formatOutputStringDecode();
assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string to match input string");
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
@@ -170,14 +155,29 @@ public class TestADFGVX{
}
@Test
public void testEncode(){
public void testFormatOutputStringDecode(){
cipher.outputString = decodedStringClean;
cipher.inputString = encodedString;
cipher.formatOutputStringDecode();
assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string to match input string");
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
verify(logger, times(1)).debug("Converting output to uppercase");
verify(logger, times(14)).debug("Converting output to lowercase");
verify(logger, times(2)).debug("Appending symbol to output");
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
}
@Test
public void testEncode(){
cipher.inputString = decodedString;
cipher.keyword = keyword;
cipher.squareKeyword = squareKeyword;
cipher.encode();
assertEquals(decodedString, cipher.outputString);
assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Encoding using Polybius Square");
verify(logger, times(1)).debug("Replacing coordinates with letters");
verify(logger, times(1)).debug("Encoding using columnar");
@@ -185,20 +185,20 @@ public class TestADFGVX{
@Test
public void testDecode(){
cipher.inputString = decodedString;
cipher.inputString = encodedString;
cipher.keyword = keyword;
cipher.squareKeyword = squareKeyword;
cipher.decode();
assertEquals(encodedString, cipher.outputString);
assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Decoding using columnar");
verify(logger, times(1)).debug("Replacing letters with coordinates");
verify(logger, times(1)).debug("Decoding using Polybius Square");
}
@Test
public void testConstructors(){
public void testConstructor_default(){
cipher = new ADFGVX();
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
@@ -209,7 +209,10 @@ public class TestADFGVX{
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);
@@ -220,27 +223,45 @@ public class TestADFGVX{
assertEquals("", cipher.squareKeyword);
assertNotNull(cipher.largePolybiusSquare);
assertNotNull(cipher.columnar);
}
@Test
public void testConstructor_preservesWhitespace(){
cipher = new ADFGVX(false, true, false);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols);
assertTrue(cipher.preserveWhitespace);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.squareKeyword);
assertEquals("", cipher.squareKeyword);
assertNotNull(cipher.largePolybiusSquare);
assertNotNull(cipher.columnar);
}
@Test
public void testConstructor_preservesSymbols(){
cipher = new ADFGVX(false, false, true);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveSymbols);
assertFalse(cipher.preserveWhitespace);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.squareKeyword);
assertEquals("", cipher.squareKeyword);
assertNotNull(cipher.largePolybiusSquare);
assertNotNull(cipher.columnar);
}
@Test
public void testGetters(){
cipher.inputString = encodedString;
cipher.outputString = decodedString;
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.squareKeyword = squareKeyword;
cipher.keyword = keyword;
assertEquals(encodedString, cipher.getInputString());
assertEquals(decodedString, cipher.getOutputString());
assertEquals(decodedString, cipher.getInputString());
assertEquals(encodedString, cipher.getOutputString());
assertEquals(squareKeyword, cipher.getSquareKeyword());
assertEquals(keyword, cipher.getKeyword());
}
@@ -249,8 +270,8 @@ public class TestADFGVX{
public void testReset(){
LargePolybiusSquare ps = cipher.largePolybiusSquare;
Columnar columnar = cipher.columnar;
cipher.inputString = encodedString;
cipher.outputString = decodedString;
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.squareKeyword = squareKeyword;
cipher.keyword = keyword;
@@ -267,27 +288,49 @@ public class TestADFGVX{
@Test
public void testPracticalEncoding(){
//Test as original
cipher = new ADFGVX(true, true, true);
String output = cipher.encode(squareKeyword, keyword, encodedString);
assertEquals(decodedString, output);
//Test fully cleaned
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(keyword, cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new ADFGVX(false, false, false);
output = cipher.encode(squareKeyword, keyword, encodedString);
assertEquals(decodedStringClean, output);
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword, cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
//Test as original
cipher = new ADFGVX(true, true, true);
String output = cipher.decode(squareKeyword, keyword, decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(keyword, cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new ADFGVX(false, false, false);
output = cipher.decode(squareKeyword, keyword, decodedString);
assertEquals(encodedStringClean, output);
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword, cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamjava/combination/TestADFGX.java
//Mattrixwv
// Created: 01-25-22
//Modified: 04-14-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.combination;
@@ -198,7 +198,7 @@ public class TestADFGX{
}
@Test
public void testConstructors(){
public void testConstructor_default(){
cipher = new ADFGX();
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
@@ -209,7 +209,10 @@ public class TestADFGX{
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);
@@ -220,16 +223,34 @@ public class TestADFGX{
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
@@ -267,27 +288,49 @@ public class TestADFGX{
@Test
public void testPracticalEncoding(){
//Test as original
cipher = new ADFGX(true, true, true);
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new ADFGX(false, false, false);
output = cipher.encode(squareKeyword, keyword, decodedString);
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
//Test as original
cipher = new ADFGX(true, true, true);
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(decodedString, output);
//Test fully cleaned
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new ADFGX(false, false, false);
output = cipher.decode(squareKeyword, keyword, encodedString);
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestAffine.java
//Mattrixwv
// Created: 01-26-22
//Modified: 04-15-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -30,9 +30,9 @@ public class TestAffine{
private Affine cipher;
private Logger logger;
//Variables
private String decodedString = "Message to^encode";
private String decodedString = "MEssage to^encode";
private String decodedStringClean = "messagetoencode";
private String encodedString = "Pbtthlb yz^burzwb";
private String encodedString = "PBtthlb yz^burzwb";
private String encodedStringClean = "pbtthlbyzburzwb";
private int key1 = 5;
private int key2 = 7;
@@ -59,7 +59,7 @@ public class TestAffine{
}
@Test
public void testConstructor_preserves(){
public void testConstructor_preservesCapitals(){
cipher = new Affine(true, false, false);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols);
@@ -68,16 +68,30 @@ public class TestAffine{
assertEquals("", cipher.outputString);
assertEquals(0, cipher.key1);
assertEquals(0, cipher.key2);
}
@Test
public void testConstructor_preservesWhitespace(){
cipher = new Affine(false, true, false);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols);
assertTrue(cipher.preserveWhitespace);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(0, cipher.key1);
assertEquals(0, cipher.key2);
}
@Test
public void testConstructor_preservesSymbols(){
cipher = new Affine(false, false, true);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveSymbols);
assertFalse(cipher.preserveWhitespace);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(0, cipher.key1);
assertEquals(0, cipher.key2);
}
@Test
@@ -316,27 +330,53 @@ public class TestAffine{
@Test
public void testPracticalEncoding(){
//Test as original
cipher = new Affine(true, true, true);
String output = cipher.encode(key1, key2, decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
String output = cipher.encode(key1, key2, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(key1, cipher.key1);
assertEquals(key2, cipher.key2);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Affine(false, false, false);
output = cipher.encode(key1, key2, decodedString);
String output = cipher.encode(key1, key2, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(key1, cipher.key1);
assertEquals(key2, cipher.key2);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
//Test as original
cipher = new Affine(true, true, true);
String output = cipher.decode(key1, key2, encodedString);
assertEquals(decodedString, output);
//Test fully cleaned
String output = cipher.decode(key1, key2, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(key1, cipher.key1);
assertEquals(key2, cipher.key2);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testpracticalDecoding_clean(){
cipher = new Affine(false, false, false);
output = cipher.decode(key1, key2, encodedString);
String output = cipher.decode(key1, key2, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(key1, cipher.key1);
assertEquals(key2, cipher.key2);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java
//Mattrixwv
// Created: 07-25-21
//Modified: 04-15-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -51,23 +51,33 @@ public class TestAtbash{
}
@Test
public void testConstructor_preserves(){
public void testConstructor_preservesCapitals(){
cipher = new Atbash(true, false, false);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
}
@Test
public void testConstructor_preservesWhitespace(){
cipher = new Atbash(false, true, false);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
}
@Test
public void testConstructor_preservesSymbols(){
cipher = new Atbash(false, false, true);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
}
@Test
@@ -198,27 +208,45 @@ public class TestAtbash{
@Test
public void testPracticalEncoding(){
//Test as original
cipher = new Atbash(true, true, true);
String output = cipher.encode(decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
String output = cipher.encode(decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Atbash(false, false, false);
output = cipher.encode(decodedString);
String output = cipher.encode(decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
//Test as original
cipher = new Atbash(true, true, true);
String output = cipher.decode(encodedString);
assertEquals(decodedString, output);
//Test fully cleaned
String output = cipher.decode(encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new Atbash(false, false, false);
output = cipher.decode(encodedString);
String output = cipher.decode(encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAutokey.java
//Mattrixwv
// Created: 07-26-21
//Modified: 04-15-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -58,7 +58,7 @@ public class TestAutokey{
}
@Test
public void testConstructor_preserves(){
public void testConstructor_preservesCapitals(){
cipher = new Autokey(true, false, false);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
@@ -67,16 +67,32 @@ public class TestAutokey{
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(0, cipher.offset.size());
}
@Test
public void testConstructor_preservesWhitespace(){
cipher = new Autokey(false, true, false);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(0, cipher.offset.size());
}
@Test
public void testConstructor_preservesSymbols(){
cipher = new Autokey(false, false, true);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(0, cipher.offset.size());
}
@Test
@@ -166,27 +182,49 @@ public class TestAutokey{
@Test
public void testPracticalEncoding(){
//Test as original
cipher = new Autokey(true, true, true);
String output = cipher.encode(keyword, decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals((keyword + decodedStringClean.substring(0, 8)).toUpperCase(), cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Autokey(false, false, false);
output = cipher.encode(keyword, decodedString);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals((keyword + decodedStringClean.substring(0, 8)).toUpperCase(), cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
//Test as original
cipher = new Autokey(true, true, true);
String output = cipher.decode(keyword, encodedString);
assertEquals(decodedString, output);
//Test fully cleaned
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals((keyword + decodedStringClean.substring(0, 14)).toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testpracticalDecoding_clean(){
cipher = new Autokey(false, false, false);
output = cipher.decode(keyword, encodedString);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals((keyword + decodedStringClean.substring(0, 14)).toUpperCase(), cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestBaconian.java
//Mattrixwv
// Created: 01-12-22
//Modified: 04-16-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -31,7 +31,7 @@ public class TestBaconian{
private Logger logger;
//Variables
private String decodedString = "Message to-encode";
private String decodedStringCleanUpper = "Messagetoencode";
private String decodedStringClean = "Messagetoencode";
private String decodedStringCleanLower = "messagetoencode";
private String encodedString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
@@ -54,7 +54,7 @@ public class TestBaconian{
}
@Test
public void testConstructor_preserves(){
public void testConstructor_preservesCapitals(){
cipher = new Baconian(true);
assertTrue(cipher.preserveCapitals);
@@ -68,10 +68,10 @@ public class TestBaconian{
cipher.setInputStringEncode(decodedString);
assertEquals(decodedStringCleanUpper, cipher.inputString);
assertEquals(decodedStringClean, cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
verify(logger, never()).debug("Removing case");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringCleanUpper);
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringClean);
verify(logger, never()).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
}
@@ -226,7 +226,7 @@ public class TestBaconian{
@Test
public void testEncode(){
cipher.preserveCapitals = true;
cipher.inputString = decodedStringCleanUpper;
cipher.inputString = decodedStringClean;
cipher.encode();
@@ -248,14 +248,14 @@ public class TestBaconian{
cipher.decode();
assertEquals(decodedStringCleanUpper, cipher.outputString);
assertEquals(decodedStringClean, cipher.outputString);
verify(logger, times(1)).debug("Decoding");
verify(logger, times(15)).debug(eq("Working letter {}"), anyString());
verify(logger, times(15)).debug(eq("Location of letter {}"), anyInt());
verify(logger, times(1)).debug("Decoding uppercase");
verify(logger, times(14)).debug("Decoding lowercase");
verify(logger, times(15)).debug(eq("Decoded character {}"), anyChar());
verify(logger, times(1)).debug("Saving output string '{}'", decodedStringCleanUpper);
verify(logger, times(1)).debug("Saving output string '{}'", decodedStringClean);
verify(logger, times(16)).debug(anyString());
verify(logger, times(16)).debug(anyString(), anyString());
}
@@ -283,37 +283,67 @@ public class TestBaconian{
@Test
public void testPracticalEncoding(){
//Test as original
cipher.preserveCapitals = true;
String output = cipher.encode(decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
String output = cipher.encode(decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher.preserveCapitals = false;
output = cipher.encode(decodedString);
String output = cipher.encode(decodedString);
assertEquals(decodedStringCleanLower, cipher.inputString);
assertEquals(encodedString.toLowerCase(), cipher.outputString);
assertEquals(encodedString.toLowerCase(), output);
}
@Test
public void testPracticalDecoding(){
//Test as original
cipher.preserveCapitals = true;
String output = cipher.decode(encodedString);
assertEquals(decodedStringCleanUpper, output);
//Test fully cleaned
assertEquals(encodedString, cipher.inputString);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher.preserveCapitals = false;
output = cipher.decode(encodedString);
String output = cipher.decode(encodedString);
assertEquals(encodedString.toLowerCase(), cipher.inputString);
assertEquals(decodedStringCleanLower, cipher.outputString);
assertEquals(decodedStringCleanLower, output);
}
//Test uppercase input
@Test
public void testPracticalDecoding_upper(){
cipher.preserveCapitals = false;
output = cipher.decode(encodedString.toUpperCase());
String output = cipher.decode(encodedString.toUpperCase());
assertEquals(encodedString.toLowerCase(), cipher.inputString);
assertEquals(decodedStringCleanLower, cipher.outputString);
assertEquals(decodedStringCleanLower, output);
}
//Test lowercase input
@Test
public void testPracticalDecoding_lower(){
cipher.preserveCapitals = false;
output = cipher.decode(encodedString.toLowerCase());
String output = cipher.decode(encodedString.toLowerCase());
assertEquals(encodedString.toLowerCase(), cipher.inputString);
assertEquals(decodedStringCleanLower, cipher.outputString);
assertEquals(decodedStringCleanLower, output);
}
}

View File

@@ -287,6 +287,13 @@ public class TestBaseX{
assertEquals(encodedString_16, output);
}
@Test
public void testPracticalEncoding_inputOnly(){
String output = cipher.encode(decodedString);
assertEquals(encodedString_2, output);
}
@Test
public void testPracticalDecoding_2(){
String output = cipher.decode(2, encodedString_2);
@@ -314,4 +321,11 @@ public class TestBaseX{
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_inputOnly(){
String output = cipher.decode(encodedString_2);
assertEquals(decodedString, output);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBeaufort.java
//Mattrixwv
// Created: 02-23-22
//Modified: 04-16-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -353,9 +353,9 @@ public class TestBeaufort{
cipher.outputString = encodedString;
cipher.keyword = keyword;
assertEquals(decodedString, cipher.inputString);
assertEquals(encodedString, cipher.outputString);
assertEquals(keyword, cipher.keyword);
assertEquals(decodedString, cipher.getInputString());
assertEquals(encodedString, cipher.getOutputString());
assertEquals(keyword, cipher.getKeyword());
}
@Test
@@ -373,27 +373,45 @@ public class TestBeaufort{
@Test
public void testPracticalEncoding(){
//Test as original
cipher = new Beaufort(true, true, true);
String output = cipher.encode(keyword, decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Beaufort(false, false, false);
output = cipher.encode(keyword, decodedString);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
//Test as original
cipher = new Beaufort(true, true, true);
String output = cipher.decode(keyword, encodedString);
assertEquals(decodedString, output);
//Test fully cleaned
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new Beaufort(false, false, false);
output = cipher.decode(keyword, encodedString);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -27,10 +27,10 @@ public class TestCaesar{
private Caesar cipher;
private Logger logger;
//Variables
private String inputString = "The quick brown fox jumps over - the lAzy dog";
private String inputStringClean = "thequickbrownfoxjumpsoverthelazydog";
private String outputString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb iXwv ald";
private String outputStringClean = "qebnrfzhyoltkclugrjmplsboqebixwvald";
private String decodedString = "The quick brown fox jumps over - the lAzy dog";
private String decodedStringClean = "thequickbrownfoxjumpsoverthelazydog";
private String encodedString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb iXwv ald";
private String encodedStringClean = "qebnrfzhyoltkclugrjmplsboqebixwvald";
private int shift = 23;
@@ -124,14 +124,14 @@ public class TestCaesar{
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.setInputString(inputString);
cipher.setInputString(decodedString);
assertEquals(inputString, cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
assertEquals(decodedString, cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString);
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
}
@Test
@@ -140,14 +140,14 @@ public class TestCaesar{
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.setInputString(inputString);
cipher.setInputString(decodedString);
assertEquals(inputString.toLowerCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
assertEquals(decodedString.toLowerCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, times(1)).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toLowerCase());
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toLowerCase());
}
@Test
@@ -156,14 +156,14 @@ public class TestCaesar{
cipher.preserveWhitespace = false;
cipher.preserveSymbols = true;
cipher.setInputString(inputString);
cipher.setInputString(decodedString);
assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing case");
verify(logger, times(1)).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("\\s", ""));
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
}
@Test
@@ -172,14 +172,14 @@ public class TestCaesar{
cipher.preserveWhitespace = true;
cipher.preserveSymbols = false;
cipher.setInputString(inputString);
cipher.setInputString(decodedString);
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("[^a-zA-Z\\s]", ""));
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
}
@Test
@@ -220,12 +220,12 @@ public class TestCaesar{
@Test
public void testEncode(){
cipher.inputString = inputString;
cipher.inputString = decodedString;
cipher.shift = shift;
cipher.encode();
assertEquals(outputString, cipher.outputString);
assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Encoding");
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Encoding uppercase");
@@ -235,17 +235,17 @@ public class TestCaesar{
verify(logger, never()).debug("Wrapping around to z");
verify(logger, times(31)).debug("Wrapping around to a");
verify(logger, times(45)).debug(eq("Encoded character {}"), anyChar());
verify(logger, times(1)).debug("Saving encoded string '{}'", outputString);
verify(logger, times(1)).debug("Saving encoded string '{}'", encodedString);
}
@Test
public void testEncode_negative(){
cipher.inputString = inputString;
cipher.inputString = decodedString;
cipher.shift = shift - 26;
cipher.encode();
assertEquals(outputString, cipher.outputString);
assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Encoding");
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Encoding uppercase");
@@ -255,17 +255,17 @@ public class TestCaesar{
verify(logger, times(2)).debug("Wrapping around to z");
verify(logger, never()).debug("Wrapping around to a");
verify(logger, times(45)).debug(eq("Encoded character {}"), anyChar());
verify(logger, times(1)).debug("Saving encoded string '{}'", outputString);
verify(logger, times(1)).debug("Saving encoded string '{}'", encodedString);
}
@Test
public void testDecode(){
cipher.inputString = outputString;
cipher.inputString = encodedString;
cipher.shift = shift;
cipher.decode();
assertEquals(inputString, cipher.outputString);
assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Decoding");
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Decoding uppercase");
@@ -275,17 +275,17 @@ public class TestCaesar{
verify(logger, times(31)).debug("Wrapping around to z");
verify(logger, never()).debug("Wrapping around to a");
verify(logger, times(45)).debug(eq("Decoded character {}"), anyChar());
verify(logger, times(1)).debug("Saving decoded string '{}'", inputString);
verify(logger, times(1)).debug("Saving decoded string '{}'", decodedString);
}
@Test
public void testDecode_negative(){
cipher.inputString = outputString;
cipher.inputString = encodedString;
cipher.shift = shift - 26;
cipher.decode();
assertEquals(inputString, cipher.outputString);
assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Decoding");
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Decoding uppercase");
@@ -295,24 +295,24 @@ public class TestCaesar{
verify(logger, never()).debug("Wrapping around to z");
verify(logger, times(2)).debug("Wrapping around to a");
verify(logger, times(45)).debug(eq("Decoded character {}"), anyChar());
verify(logger, times(1)).debug("Saving decoded string '{}'", inputString);
verify(logger, times(1)).debug("Saving decoded string '{}'", decodedString);
}
@Test
public void testGetters(){
cipher.inputString = inputString;
cipher.outputString = outputString;
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.shift = shift;
assertEquals(inputString, cipher.getInputString());
assertEquals(outputString, cipher.getOutputString());
assertEquals(decodedString, cipher.getInputString());
assertEquals(encodedString, cipher.getOutputString());
assertEquals(shift, cipher.getShift());
}
@Test
public void testReset(){
cipher.inputString = inputString;
cipher.outputString = outputString;
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.shift = shift;
cipher.reset();
@@ -326,80 +326,71 @@ public class TestCaesar{
@Test
public void testPracticalEncoding(){
cipher = new Caesar(true, true, true);
logger = mock(Logger.class);
Caesar.logger = logger;
String output = cipher.encode(shift, inputString);
assertEquals(inputString, cipher.inputString);
String output = cipher.encode(shift, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(shift, cipher.shift);
assertEquals(outputString, cipher.outputString);
assertEquals(outputString, output);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Caesar(false, false, false);
logger = mock(Logger.class);
Caesar.logger = logger;
String output = cipher.encode(shift, inputString);
assertEquals(inputStringClean, cipher.inputString);
String output = cipher.encode(shift, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(shift, cipher.shift);
assertEquals(outputStringClean, cipher.outputString);
assertEquals(outputStringClean, output);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalEncoding_negative(){
cipher = new Caesar(true, true, true);
logger = mock(Logger.class);
Caesar.logger = logger;
String output = cipher.encode(shift, inputString);
assertEquals(inputString, cipher.inputString);
String output = cipher.encode(shift, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(shift, cipher.shift);
assertEquals(outputString, cipher.outputString);
assertEquals(outputString, output);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalDecoding(){
cipher = new Caesar(true, true, true);
logger = mock(Logger.class);
Caesar.logger = logger;
String output = cipher.decode(shift, outputString);
String output = cipher.decode(shift, encodedString);
assertEquals(outputString, cipher.inputString);
assertEquals(encodedString, cipher.inputString);
assertEquals(shift, cipher.shift);
assertEquals(inputString, cipher.outputString);
assertEquals(inputString, output);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new Caesar(false, false, false);
logger = mock(Logger.class);
Caesar.logger = logger;
String output = cipher.decode(shift, outputString);
String output = cipher.decode(shift, encodedString);
assertEquals(outputStringClean, cipher.inputString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(shift, cipher.shift);
assertEquals(inputStringClean, cipher.outputString);
assertEquals(inputStringClean, output);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
@Test
public void testPracticalDecoding_negative(){
cipher = new Caesar(true, true, true);
logger = mock(Logger.class);
Caesar.logger = logger;
String output = cipher.decode(shift - 26, outputString);
String output = cipher.decode(shift - 26, encodedString);
assertEquals(outputString, cipher.inputString);
assertEquals(encodedString, cipher.inputString);
assertEquals(shift - 26, cipher.shift);
assertEquals(inputString, cipher.outputString);
assertEquals(inputString, output);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
}

View File

@@ -1,407 +1,241 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestOneTimePad.java
//Mattrixwv
// Created: 02-23-22
//Modified: 07-09-22
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class TestOneTimePad{
@Test
public void testEncode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, true, true);
private OneTimePad cipher;
private Logger logger;
//Variables
private String decodedString = "Message to^encode";
private String decodedStringClean = "MESSAGETOENCODE";
private String encodedString = "Wiqooxh mv^egkgws";
private String encodedStringClean = "WIQOOXHMVEGKGWS";
private String keyword = "keywordThatIsTotallyRandom";
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));
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "wiqooxhmvegkgws";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxh mv egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxh*mv+egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol encoding
inputString = "Message to^encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Wiqooxh mv^egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(false, true, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "WIQOOXHMVEGKGWS";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXH MV EGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXH*MV+EGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol encoding
inputString = "Message to^encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXH MV^EGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, false, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "wiqooxhmvegkgws";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxhmvegkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxh*mv+egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol encoding
inputString = "Message to^encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Wiqooxhmv^egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, true, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "wiqooxhmvegkgws";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxh mv egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxhmvegkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol encoding
inputString = "Message to^encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Wiqooxh mvegkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(false, false, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "WIQOOXHMVEGKGWS";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol encoding
inputString = "Message to^encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
@BeforeEach
public void setup(){
cipher = new OneTimePad();
logger = mock(Logger.class);
OneTimePad.logger = logger;
}
@Test
public void testDecode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, true, true);
public void testConstructor_default(){
cipher = new OneTimePad();
//Test lowercase decoding
String inputString = "wiqooxhmvegkgws";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHMVEGKGWS";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh mv egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "message to encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*mv+egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "message*to+encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Wiqooxh mv^egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Message to^encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
}
@Test
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(false, true, true);
public void testConstructor_preservesCapitals(){
cipher = new OneTimePad(true, false, false);
//Test lowercase decoding
String inputString = "wiqooxhmvegkgws";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHMVEGKGWS";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh mv egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGE TO ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*mv+egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGE*TO+ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Wiqooxh mv^egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGE TO^ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
}
@Test
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, false, true);
public void testConstructor_preservesWhitespace(){
cipher = new OneTimePad(false, true, false);
//Test lowercase decoding
String inputString = "wiqooxhmvegkgws";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHMVEGKGWS";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh mv egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "messagetoencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*mv+egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "message*to+encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Wiqooxh mv^egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Messageto^encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
}
@Test
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, true, false);
public void testConstructor_preservesSymbols(){
cipher = new OneTimePad(false, false, true);
//Test lowercase decoding
String inputString = "wiqooxhmvegkgws";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHMVEGKGWS";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh mv egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "message to encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*mv+egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "messagetoencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Wiqooxh mv^egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Message toencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(false, false, false);
public void testEncode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
//Test lowercase decoding
String inputString = "wiqooxhmvegkgws";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHMVEGKGWS";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
String output = cipher.encode(keyword, decodedString);
//Test whitespace decoding
inputString = "wiqooxh mv egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(decodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Encoding");
}
//Test symbol decoding
inputString = "wiqooxh*mv+egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
@Test
public void testEncode_clean(){
cipher.preserveCapitals = false;
cipher.preserveWhitespace = false;
cipher.preserveSymbols = false;
//Test mixed case, whtiespace, symbol decoding
inputString = "Wiqooxh mv^egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Encoding");
}
@Test
public void testEncode_short(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
assertThrows(InvalidKeywordException.class, () -> {
cipher.encode("keyword", decodedString);
}, "Key must be at least as long as the input");
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
verify(logger, never()).debug(anyString());
}
@Test
public void testDecode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Decoding");
}
@Test
public void testDecode_clean(){
cipher.preserveCapitals = false;
cipher.preserveWhitespace = false;
cipher.preserveSymbols = false;
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Decoding");
}
@Test
public void testDecode_short(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.decode("keyword", encodedString);
});
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
verify(logger, never()).debug(anyString());
}
@Test
public void testPracticalEncoding(){
cipher = new OneTimePad(true, true, true);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new OneTimePad(false, false, false);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
cipher = new OneTimePad(true, true, true);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new OneTimePad(false, false, false);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,467 +1,485 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestSubstitution.java
//Mattrixwv
// Created: 02-22-22
//Modified: 07-09-22
//Modified: 04-18-23
package com.mattrixwv.cipherstream.monosubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class TestSubstitution{
@Test
public void testEncode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, true, true);
private Substitution cipher;
private Logger logger;
//Variables
private String decodedString = "Message to^encode";
private String decodedStringClean = "MESSAGETOENCODE";
private String decodedStringAlNum = "Message to^encode 123";
private String decodedStringAlNumClean = "MESSAGETOENCODE";
private String encodedString = "Oguucig vq^gpeqfg";
private String encodedStringClean = "OGUUCIGVQGPEQFG";
private String encodedStringAlNum = "Oguucig vq^gpeqfg 876";
private String encodedStringAlNumClean = "OGUUCIGVQGPEQFG";
private String keyword = "cdefghijklmnopqrstuvwxyzab";
private String keywordAlNum = "cdefghijklmnopqrstuvwxyzab9876543210";
//Test lowercase encoding
String inputString = "messagetoencode";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "oguucigvqgpeqfg";
String output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucig vq gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucig*vq+gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Oguucig vq^gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number encoding with long key
inputString = "Message to&encode 123";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Oguucig vq&gpeqfg 876";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(false, true, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "OGUUCIGVQGPEQFG";
String output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIG VQ GPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIG*VQ+GPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIG VQ^GPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number encoding with long key
inputString = "Message to&encode 123";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "OGUUCIG VQ&GPEQFG 876";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, false, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "oguucigvqgpeqfg";
String output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucigvqgpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucig*vq+gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Oguucigvq^gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number encoding with long key
inputString = "Message to&encode 123";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Oguucigvq&gpeqfg876";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, true, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "oguucigvqgpeqfg";
String output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucig vq gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucigvqgpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Oguucig vqgpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number encoding with long key
inputString = "Message to&encode 123";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Oguucig vqgpeqfg ";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(false, false, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "OGUUCIGVQGPEQFG";
String output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number encoding with long key
inputString = "Message to&encode 123";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
@BeforeEach
public void setup(){
cipher = new Substitution();
logger = mock(Logger.class);
Substitution.logger = logger;
}
@Test
public void testDecode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, true, true);
public void testConstructor_default(){
cipher = new Substitution();
//Test lowercase decoding
String inputString = "oguucigvqgpeqfg";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "messagetoencode";
String output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "OGUUCIGVQGPEQFG";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "oguucig vq gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "message to encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "oguucig*vq+gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "message*to+encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Oguucig vq^gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Message to^encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number decoding with long key
inputString = "Oguucig vq&gpeqfg 876";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Message to&encode 123";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
}
@Test
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(false, true, true);
public void testConstructor_preservesCapitals(){
cipher = new Substitution(true, false, false);
//Test lowercase decoding
String inputString = "oguucigvqgpeqfg";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "OGUUCIGVQGPEQFG";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "oguucig vq gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGE TO ENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "oguucig*vq+gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGE*TO+ENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Oguucig vq^gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGE TO^ENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number decoding with long key
inputString = "Oguucig vq&gpeqfg 876";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "MESSAGE TO&ENCODE 123";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
}
@Test
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, false, true);
public void testConstructor_preservesWhitespace(){
cipher = new Substitution(false, true, false);
//Test lowercase decoding
String inputString = "oguucigvqgpeqfg";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "messagetoencode";
String output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "OGUUCIGVQGPEQFG";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "oguucig vq gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "messagetoencode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "oguucig*vq+gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "message*to+encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Oguucig vq^gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Messageto^encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number decoding with long key
inputString = "Oguucig vq&gpeqfg 876";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Messageto&encode123";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
}
@Test
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, true, false);
public void testConstructor_preservesSymbols(){
cipher = new Substitution(false, false, true);
//Test lowercase decoding
String inputString = "oguucigvqgpeqfg";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "messagetoencode";
String output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "OGUUCIGVQGPEQFG";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "oguucig vq gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "message to encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "oguucig*vq+gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "messagetoencode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Oguucig vq^gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Message toencode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number decoding with long key
inputString = "Oguucig vq&gpeqfg 876";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Message toencode ";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(false, false, false);
public void testSetKey(){
cipher.setKeyword(keyword);
//Test lowercase decoding
String inputString = "oguucigvqgpeqfg";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "OGUUCIGVQGPEQFG";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertEquals(keyword.toUpperCase(), cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", keyword);
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, times(1)).debug("Ensuring there are only letters in the key");
verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, times(1)).debug("Cleaned key '{}'", keyword.toUpperCase());
}
//Test whitespace decoding
inputString = "oguucig vq gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
@Test
public void testSetKey_alNum(){
cipher.setKeyword(keywordAlNum);
//Test symbol decoding
inputString = "oguucig*vq+gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", keywordAlNum);
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, never()).debug("Ensuring there are only letters in the key");
verify(logger, times(1)).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, times(1)).debug("Cleaned key '{}'", keywordAlNum.toUpperCase());
}
//Test mixed case, whitespace, symbol decoding
inputString = "Oguucig vq^gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number decoding with long key
inputString = "Oguucig vq&gpeqfg 876";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
@Test
public void testSetKey_duplicate(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("ABA");
});
assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", "ABA");
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, never()).debug("Ensuring there are only letters in the key");
verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, never()).debug(eq("Cleaned key '{}'"), anyString());
}
@Test
public void testSetKey_invalidLetter(){
assertThrows(InvalidKeywordException.class, () ->{
cipher.setKeyword("abcdefghijklmnop1rstuvwxyz");
});
assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", "abcdefghijklmnop1rstuvwxyz");
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, times(1)).debug("Ensuring there are only letters in the key");
verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, never()).debug(eq("Cleaned key '{}'"), anyString());
}
@Test
public void testSetKey_invalidAlNum(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("abcdefghijklmnop^rstuvwxyz0123456789");
});
assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", "abcdefghijklmnop^rstuvwxyz0123456789");
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, never()).debug("Ensuring there are only letters in the key");
verify(logger, times(1)).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, never()).debug(eq("Cleaned key '{}'"), anyString());
}
@Test
public void testSetKey_invalidLength(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("AB");
}, "The key must contain all letters and can contain all numbers");
assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", "AB");
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, never()).debug("Ensuring there are only letters in the key");
verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, never()).debug(eq("Cleaned key '{}'"), anyString());
}
@Test
public void testSetKey_null(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null);
}, "Key cannot be null");
assertEquals("", cipher.keyword);
verify(logger, never()).debug(eq("Original key '{}'"), anyString());
verify(logger, never()).debug("Removing case");
verify(logger, never()).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 alpha-numeric characters in the key");
verify(logger, never()).debug(eq("Cleaned key '{}'"), anyString());
}
@Test
public void testSetInputString(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.setInputString(decodedString);
assertEquals(decodedString, cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
}
@Test
public void testSetInputString_noCapitals(){
cipher.preserveCapitals = false;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.setInputString(decodedString);
assertEquals(decodedString.toUpperCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, times(1)).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
}
@Test
public void testSetInputString_noWhitespace(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = false;
cipher.preserveSymbols = true;
cipher.setInputString(decodedString);
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing case");
verify(logger, times(1)).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
}
@Test
public void testSetInputString_noSymbols(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = false;
cipher.setInputString(decodedString);
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
}
@Test
public void testSetInputString_blank(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputString("");
}, "Input must contain at least 1 letter");
assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", "");
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
}
@Test
public void testSetInputString_null(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(null);
}, "Input cannot be null");
assertEquals("", cipher.inputString);
verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
}
@Test
public void testEncode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.inputString = decodedStringAlNum;
cipher.keyword = keywordAlNum.toUpperCase();
cipher.encode();
assertEquals(encodedStringAlNum, cipher.outputString);
verify(logger, times(1)).debug("Encoding");
verify(logger, times(21)).debug(eq("Working character {}"), anyChar());
verify(logger, times(1)).debug("Encoding uppercase");
verify(logger, times(14)).debug("Encoding lowercase");
verify(logger, times(3)).debug("Encoding digit");
verify(logger, times(3)).debug("Passing symbol through");
verify(logger, times(1)).debug("Encoded message '{}'", encodedStringAlNum);
}
@Test
public void testDecode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.inputString = encodedStringAlNum;
cipher.keyword = keywordAlNum.toUpperCase();
cipher.decode();
assertEquals(decodedStringAlNum, cipher.outputString);
verify(logger, times(1)).debug("Decoding");
verify(logger, times(21)).debug(eq("Working character {}"), anyChar());
verify(logger, times(1)).debug("Encoding uppercase");
verify(logger, times(14)).debug("Encoding lowercase");
verify(logger, times(3)).debug("Encoding digit");
verify(logger, times(3)).debug("Passing symbol through");
verify(logger, times(1)).debug("Decoded message '{}'", decodedStringAlNum);
}
@Test
public void testGetters(){
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.keyword = keyword;
assertEquals(decodedString, cipher.getInputString());
assertEquals(encodedString, cipher.getOutputString());
assertEquals(keyword, cipher.getKeyword());
}
@Test
public void testReset(){
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.keyword = keyword;
cipher.reset();
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Resetting fields");
}
@Test
public void testPracticalEncoding(){
cipher = new Substitution(true, true, true);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Substitution(false, false, false);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalEncoding_alNum(){
cipher = new Substitution(true, true, true);
String output = cipher.encode(keywordAlNum, decodedStringAlNum);
assertEquals(decodedStringAlNum, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword);
assertEquals(encodedStringAlNum, cipher.outputString);
assertEquals(encodedStringAlNum, output);
}
@Test
public void testPracticalEncoding_alNumClean(){
cipher = new Substitution(false, false, false);
String output = cipher.encode(keywordAlNum, decodedStringAlNum);
assertEquals(decodedStringAlNumClean, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword);
assertEquals(encodedStringAlNumClean, cipher.outputString);
assertEquals(encodedStringAlNumClean, output);
}
@Test
public void testPrecticalEncoding_noAlNumKey(){
cipher = new Substitution(true, true, true);
String output = cipher.encode(keyword, decodedStringAlNum);
assertEquals(decodedStringAlNum, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedString + " 123", cipher.outputString);
assertEquals(encodedString + " 123", output);
}
@Test
public void testPracticalDecoding(){
cipher = new Substitution(true, true, true);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new Substitution(false, false, false);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
@Test
public void testPracticalDecoding_alNum(){
cipher = new Substitution(true, true, true);
String output = cipher.decode(keywordAlNum, encodedStringAlNum);
assertEquals(encodedStringAlNum, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword);
assertEquals(decodedStringAlNum, cipher.outputString);
assertEquals(decodedStringAlNum, output);
}
@Test
public void testPracticalDecoding_alNumClean(){
cipher = new Substitution(false, false, false);
String output = cipher.decode(keywordAlNum, encodedStringAlNum);
assertEquals(encodedStringAlNumClean, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword);
assertEquals(decodedStringAlNumClean, cipher.outputString);
assertEquals(decodedStringAlNumClean, output);
}
@Test
public void testPracticalDecoding_noAlNumKey(){
cipher = new Substitution(true, true, true);
String output = cipher.decode(keyword, encodedString + " 123");
assertEquals(encodedString + " 123", cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString + " 123", cipher.outputString);
assertEquals(decodedString + " 123", output);
}
}

View File

@@ -1,407 +1,391 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestVigenere.java
//Mattrixwv
// Created: 07-25-21
//Modified: 07-09-22
//Modified: 04-18-23
package com.mattrixwv.cipherstream.monosubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class TestVigenere{
@Test
public void testEncode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, true, true);
private Vigenere cipher;
private Logger logger;
//Variables
private String inputString = "MeSsage to^encode";
private String inputStringClean = "MESSAGETOENCODE";
private String outputString = "WiQooxh ds^cjqfgo";
private String outputStringClean = "WIQOOXHDSCJQFGO";
private String keyword = "keyword";
private 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
public void testConstructor_default(){
cipher = new Vigenere();
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
}
@Test
public void testConstructor_preservesCapitals(){
cipher = new Vigenere(true, false, false);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
}
@Test
public void testConstructor_preservesWhitespace(){
cipher = new Vigenere(false, true, false);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
}
@Test
public void testConstructor_preservesSymbols(){
cipher = new Vigenere(false, false, true);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
}
@Test
public void testSetOffset(){
cipher.keyword = keyword.toUpperCase();
cipher.setOffset();
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Setting offset array from keyword");
verify(logger, times(1)).debug("Offset {}", offset);
}
@Test
public void testSetInputString(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
cipher.setInputString(inputString);
assertEquals(inputString, cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString);
}
@Test
public void testSetInputString_noCapitals(){
cipher.preserveCapitals = false;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
cipher.setInputString(inputString);
assertEquals(inputString.toUpperCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, times(1)).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
}
@Test
public void testSetInputString_noWhitespace(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = false;
cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
cipher.setInputString(inputString);
assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, never()).debug("Removing case");
verify(logger, times(1)).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("\\s", ""));
}
@Test
public void testSetInputString_noSymbols(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = false;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
cipher.setInputString(inputString);
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("[^a-zA-Z\\s]", ""));
}
@Test
public void testSetInputString_blank(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputString("");
}, "Input cannot be null");
assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", "");
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
}
@Test
public void testSetInputString_null(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(null);
}, "Input must contain at least 1 letter");
assertEquals("", cipher.inputString);
verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
}
@Test
public void testSetKeyword(){
cipher.setKeyword(keyword);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Original keyword '{}'", keyword);
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing all non-letter characters");
verify(logger, times(1)).debug("Clean keyword '{}'", keyword.toUpperCase());
}
@Test
public void testSetKeyword_blank(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("");
}, "Keyword must contain at least 2 letters");
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
verify(logger, times(1)).debug("Original keyword '{}'", "");
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing all non-letter characters");
verify(logger, times(1)).debug("Clean keyword '{}'", "");
}
@Test
public void testSetKeyword_null(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null);
}, "Keyword must contain at least 2 letters");
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
verify(logger, never()).debug(eq("Original keyword '{}'"), anyString());
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing all non-letter characters");
verify(logger, never()).debug(eq("Clean keyword '{}'"), anyString());
}
@Test
public void testEncode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.inputString = inputString;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
cipher.encode();
assertEquals(outputString, cipher.outputString);
verify(logger, times(1)).debug("Encoding");
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Encoding uppercase");
verify(logger, times(1)).debug("Wrapping around to A");
verify(logger, times(13)).debug("Encoding lowercase");
verify(logger, times(5)).debug("Wrapping around to a");
verify(logger, times(17)).debug(eq("Encoded character {}"), anyChar());
verify(logger, times(1)).debug("Encoded message '{}'", outputString);
}
@Test
public void testDecode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.inputString = outputString;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
cipher.decode();
assertEquals(inputString, cipher.outputString);
verify(logger, times(1)).debug("Decoding");
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Decoding uppercase");
verify(logger, times(1)).debug("Wrapping around to Z");
verify(logger, times(13)).debug("Decoding lowercase");
verify(logger, times(5)).debug("Wrapping around to z");
verify(logger, times(17)).debug(eq("Decoded character {}"), anyChar());
verify(logger, times(1)).debug("Decoded message '{}'", inputString);
}
@Test
public void testGetters(){
cipher.inputString = inputString;
cipher.outputString = outputString;
cipher.keyword = keyword;
cipher.offset = offset;
assertEquals(inputString, cipher.getInputString());
assertEquals(outputString, cipher.getOutputString());
assertEquals(keyword, cipher.getKeyword());
assertEquals(offset, cipher.getOffsets());
}
@Test
public void testReset(){
cipher.inputString = inputString;
cipher.outputString = outputString;
cipher.keyword = keyword;
cipher.offset = offset;
cipher.reset();
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
verify(logger, times(1)).debug("Resetting fields");
}
@Test
public void testPracticalEncoding(){
cipher = new Vigenere(true, true, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keyword";
String correctOutput = "wiqooxhdscjqfgo";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keyword";
correctOutput = "wiqooxh ds cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keyword";
correctOutput = "wiqooxh*ds+cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
keyword = "keyword";
correctOutput = "Wiqooxh ds^cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(inputString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
assertEquals(outputString, cipher.outputString);
assertEquals(outputString, output);
}
@Test
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(false, true, true);
public void testPracticalEncoding_clean(){
cipher = new Vigenere(false, false, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keyword";
String correctOutput = "WIQOOXHDSCJQFGO";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keyword";
correctOutput = "WIQOOXH DS CJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keyword";
correctOutput = "WIQOOXH*DS+CJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
keyword = "keyword";
correctOutput = "WIQOOXH DS^CJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(inputStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
assertEquals(outputStringClean, cipher.outputString);
assertEquals(outputStringClean, output);
}
@Test
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, false, true);
public void testPracticalDecoding(){
cipher = new Vigenere(true, true, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keyword";
String correctOutput = "wiqooxhdscjqfgo";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
String output = cipher.decode(keyword, outputString);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keyword";
correctOutput = "wiqooxhdscjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keyword";
correctOutput = "wiqooxh*ds+cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
keyword = "keyword";
correctOutput = "Wiqooxhds^cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(outputString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
assertEquals(inputString, cipher.outputString);
assertEquals(inputString, output);
}
@Test
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, true, false);
public void testPracticalDecoding_clean(){
cipher = new Vigenere(false, false, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keyword";
String correctOutput = "wiqooxhdscjqfgo";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
String output = cipher.decode(keyword, outputString);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keyword";
correctOutput = "wiqooxh ds cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keyword";
correctOutput = "wiqooxhdscjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
keyword = "keyword";
correctOutput = "Wiqooxh dscjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(false, false, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keyword";
String correctOutput = "WIQOOXHDSCJQFGO";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testDecode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, true, true);
//Test lowercase decoding
String inputString = "wiqooxhdscjqfgo";
String keyword = "keyword";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHDSCJQFGO";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh ds cjqfgo";
keyword = "keyword";
correctOutput = "message to encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*ds+cjqfgo";
keyword = "keyword";
correctOutput = "message*to+encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Wiqooxh ds^cjqfgo";
keyword = "keyword";
correctOutput = "Message to^encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(false, true, true);
//Test lowercase decoding
String inputString = "wiqooxhdscjqfgo";
String keyword = "keyword";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHDSCJQFGO";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh ds cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGE TO ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*ds+cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGE*TO+ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Wiqooxh ds^cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGE TO^ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, false, true);
//Test lowercase decoding
String inputString = "wiqooxhdscjqfgo";
String keyword = "keyword";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHDSCJQFGO";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh ds cjqfgo";
keyword = "keyword";
correctOutput = "messagetoencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*ds+cjqfgo";
keyword = "keyword";
correctOutput = "message*to+encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Wiqooxh ds^cjqfgo";
keyword = "keyword";
correctOutput = "Messageto^encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, true, false);
//Test lowercase decoding
String inputString = "wiqooxhdscjqfgo";
String keyword = "keyword";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHDSCJQFGO";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh ds cjqfgo";
keyword = "keyword";
correctOutput = "message to encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*ds+cjqfgo";
keyword = "keyword";
correctOutput = "messagetoencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Wiqooxh ds^cjqfgo";
keyword = "keyword";
correctOutput = "Message toencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(false, false, false);
//Test lowercase decoding
String inputString = "wiqooxhdscjqfgo";
String keyword = "keyword";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHDSCJQFGO";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh ds cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*ds+cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Wiqooxh ds^cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(outputStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
assertEquals(inputStringClean, cipher.outputString);
assertEquals(inputStringClean, output);
}
}

View File

@@ -0,0 +1,359 @@
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java
//Mattrixwv
// Created: 04-21-23
//Modified: 04-21-23
package com.mattrixwv.cipherstream.polysubstitution;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class TestLargePolybiusSquare{
private LargePolybiusSquare cipher;
private Logger logger;
//Variables
private String inputString = "Message to^encode";
private String inputStringClean = "MESSAGETOENCODE";
private String outputString = "35124343222612 4415^123624152112";
private String outputStringClean = "31 15 41 41 11 21 15 42 33 15 32 13 33 14 15";
private String keyword = "keyword";
private String keywordClean = "KEYWORDABCFGHIJLMNPQSTUVXZ0123456789";
private String keywordBlank = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private char[][] grid = {
{'K', 'E', 'Y', 'W', 'O', 'R'},
{'D', 'A', 'B', 'C', 'F', 'G'},
{'H', 'I', 'J', 'L', 'M', 'N'},
{'P', 'Q', 'S', 'T', 'U', 'V'},
{'X', 'Z', '0', '1', '2', '3'},
{'4', '5', '6', '7', '8', '9'}
};
private char[][] gridClean = {
{'A', 'B', 'C', 'D', 'E', 'F'},
{'G', 'H', 'I', 'J', 'K', 'L'},
{'M', 'N', 'O', 'P', 'Q', 'R'},
{'S', 'T', 'U', 'V', 'W', 'X'},
{'Y', 'Z', '0', '1', '2', '3'},
{'4', '5', '6', '7', '8', '9'},
};
@BeforeEach
public void setup(){
cipher = new LargePolybiusSquare();
logger = mock(Logger.class);
LargePolybiusSquare.logger = logger;
}
@Test
public void testConstructor_default(){
cipher = new LargePolybiusSquare();
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertArrayEquals(new char[6][6], cipher.grid);
}
@Test
public void testConstructor_preserveWhitespace(){
cipher = new LargePolybiusSquare(true, false);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertArrayEquals(new char[6][6], cipher.grid);
}
@Test
public void testConstructor_preserveSymbols(){
cipher = new LargePolybiusSquare(false, true);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertArrayEquals(new char[6][6], cipher.grid);
}
@Test
public void testCreateGrid(){
cipher.keyword = keywordClean;
cipher.createGrid();
assertArrayEquals(grid, cipher.grid);
verify(logger, times(1)).debug("Creating grid");
}
@Test
public void testSetInputStringEncoding(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.setInputStringEncoding(inputString);
assertEquals(inputString.toUpperCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
}
@Test
public void testSetInputStringEncoding_noWhitespace(){
cipher.preserveWhitespace = false;
cipher.preserveSymbols = true;
cipher.setInputStringEncoding(inputString);
assertEquals(inputString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, times(1)).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("\\s", ""));
}
@Test
public void testSetInputStringEncoding_noSymbols(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = false;
cipher.setInputStringEncoding(inputString);
assertEquals(inputString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, never()).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""));
}
@Test
public void testSetInputStringEncoding_noWhitespaceNoSymbols(){
cipher.preserveWhitespace = false;
cipher.preserveSymbols = false;
cipher.setInputStringEncoding(inputString);
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, times(1)).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", "M E S S A G E T O E N C O D E");
}
@Test
public void testSetInputStringEncoding_preparedBlank(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncoding("*");
});
assertEquals("*", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", "*");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", "*");
}
@Test
public void testSetInputStringEncoding_blank(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncoding("");
});
assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", "");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
}
@Test
public void testSetInputStringEncoding_null(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncoding(null);
});
assertEquals("", cipher.inputString);
verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
}
@Test
public void testGetPreparedInputStringEncoding(){
cipher.inputString = inputString.toUpperCase();
String preparedInputString = cipher.getPreparedInputStringEncoding();
assertEquals(inputString.toUpperCase(), cipher.inputString);
assertEquals(inputString.toUpperCase().replaceAll("[^A-Z0-9]", ""), preparedInputString);
verify(logger, times(1)).debug("Preparing input string for encoding");
verify(logger, times(1)).debug("Prepared input string '{}'", preparedInputString);
}
@Test
public void testSetKeyword(){
cipher.setKeyword(keyword);
assertEquals(keywordClean, cipher.keyword);
assertArrayEquals(grid, cipher.grid);
verify(logger, times(1)).debug("Original keyword '{}'", keyword);
verify(logger, times(1)).debug("Cleaned keyword '{}'", keywordClean);
}
@Test
public void testSetKeyword_blank(){
cipher.setKeyword("");
assertEquals(keywordBlank, cipher.keyword);
assertArrayEquals(gridClean, cipher.grid);
verify(logger, times(1)).debug("Original keyword '{}'", "");
verify(logger, times(1)).debug("Cleaned keyword '{}'", keywordBlank);
}
@Test
public void testSetKeyword_null(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null);
});
assertEquals("", cipher.keyword);
assertArrayEquals(new char[6][6], cipher.grid);
verify(logger, never()).debug(eq("Original keyword '{}'"), anyString());
verify(logger, never()).debug(eq("Cleaned keyword '{}'"), anyString());
}
@Test
public void testAddCharactersToCleanStringEncode(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.keyword = keywordClean;
cipher.grid = grid;
cipher.inputString = inputString.toUpperCase();
cipher.addCharactersToCleanStringEncode(outputString.replaceAll("\\s", "").replaceAll("[^0-9]", ""));
assertEquals(outputString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string");
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
verify(logger, times(15)).debug("Appending character");
verify(logger, times(2)).debug("Appending symbol");
verify(logger, times(1)).debug("Saving output string {}", outputString);
}
@Test
public void testAddCharactersToCleanStringDecode(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.keyword = keywordClean;
cipher.grid = grid;
cipher.inputString = outputString;
cipher.addCharactersToCleanStringDecode(inputStringClean);
verify(logger, times(1)).debug("Formatting output string");
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
verify(logger, times(15)).debug("Appending character");
verify(logger, times(2)).debug("Appending symbol");
verify(logger, times(1)).debug("Saving output string {}", inputString.toUpperCase());
}
@Test
public void testReset(){
cipher.inputString = inputString;
cipher.outputString = outputString;
cipher.keyword = keyword;
cipher.grid = grid;
cipher.reset();
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertArrayEquals(new char[6][6], cipher.grid);
verify(logger, times(1)).debug("Resetting");
}
@Test
public void testPracticalEncoding(){
cipher = new LargePolybiusSquare(true, true);
String output = cipher.encode(keyword, inputString);
assertEquals(inputString.toUpperCase(), cipher.inputString);
assertEquals(keywordClean, cipher.keyword);
assertEquals(outputString, cipher.outputString);
assertEquals(outputString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new LargePolybiusSquare(false, false);
String output = cipher.encode(inputString);
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
assertEquals(keywordBlank, cipher.keyword);
assertEquals(outputStringClean, cipher.outputString);
assertEquals(outputStringClean, output);
}
@Test
public void testPracticalDecoding(){
cipher = new LargePolybiusSquare(true, true);
String output = cipher.decode(keyword, outputString);
assertEquals(outputString, cipher.inputString);
assertEquals(keywordClean, cipher.keyword);
assertEquals(inputString.toUpperCase(), cipher.outputString);
assertEquals(inputString.toUpperCase(), output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new LargePolybiusSquare(false, false);
String output = cipher.decode(outputStringClean);
assertEquals(outputStringClean.replaceAll("\\s", ""), cipher.inputString);
assertEquals(keywordBlank, cipher.keyword);
assertEquals(inputStringClean, cipher.outputString);
assertEquals(inputStringClean, output);
}
}