Added more unit test coverage
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.mattrixwv</groupId>
|
||||
<artifactId>cipher-stream-java</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.3.0-SNAPSHOT</version>
|
||||
|
||||
<name>CipherStreamJava</name>
|
||||
<url>http://www.mattrixwv.com</url>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Bifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 07-09-22
|
||||
//Modified: 04-23-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -14,20 +14,20 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Bifid{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Bifid.class);
|
||||
protected static Logger logger = LoggerFactory.getLogger(Bifid.class);
|
||||
|
||||
//Fields
|
||||
private String inputString; //The message that needs to be encoded/decoded
|
||||
private String outputString; //The encoded/decoded message
|
||||
private String keyword; //The keyword used to create the grid
|
||||
private PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
|
||||
private boolean preserveCapitals; //Persist capitals in the output string
|
||||
private boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
private boolean preserveSymbols; //Persist symbols in the output string
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected String keyword; //The keyword used to create the grid
|
||||
protected PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
|
||||
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
private void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Ensure the keyword isn't null
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
@@ -39,7 +39,7 @@ public class Bifid{
|
||||
this.keyword = keyword;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input string isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -74,7 +74,7 @@ public class Bifid{
|
||||
}
|
||||
}
|
||||
//Adds all non-letter characters back to the output string
|
||||
private void formatOutput(String outputString){
|
||||
protected void formatOutput(String outputString){
|
||||
logger.debug("Formatting output");
|
||||
//Keep track of where you are in the output
|
||||
int outputCnt = 0;
|
||||
@@ -100,15 +100,15 @@ public class Bifid{
|
||||
}
|
||||
|
||||
//Save the output
|
||||
logger.debug("Formatted output string '{}'", output);
|
||||
this.outputString = output.toString();
|
||||
logger.debug("Formatted output string '{}'", this.outputString);
|
||||
}
|
||||
//Encodes inputString using a polybius square and stores the result in outputString
|
||||
private void encode() throws InvalidCharacterException, InvalidInputException{
|
||||
protected void encode() throws InvalidCharacterException, InvalidInputException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Get the encoded numbers from a polybius square
|
||||
logger.debug("Encoding polybius");
|
||||
logger.debug("Encoding Polybius");
|
||||
String polybiusMessage = polybiusSquare.encode(keyword, inputString).replaceAll("\\s", "");
|
||||
keyword = polybiusSquare.getKeyword(); //Save the cleaned keyword
|
||||
|
||||
@@ -118,7 +118,7 @@ public class Bifid{
|
||||
boolean firstNum = true;
|
||||
logger.debug("Splitting Polybius Square message");
|
||||
for(char ch : polybiusMessage.toCharArray()){
|
||||
logger.debug("Current character {}", ch);
|
||||
logger.debug("Current character '{}'", ch);
|
||||
|
||||
if(firstNum){
|
||||
row0.append(ch);
|
||||
@@ -140,7 +140,7 @@ public class Bifid{
|
||||
formatOutput(letterResult);
|
||||
}
|
||||
//Decodes inputString using a polybius square and stores the result in outputString
|
||||
private void decode() throws InvalidCharacterException, InvalidInputException{
|
||||
protected void decode() throws InvalidCharacterException, InvalidInputException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Get the decoded number from a polybius square
|
||||
|
||||
@@ -31,7 +31,7 @@ public class LargePolybiusSquare extends PolybiusSquare{
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
}
|
||||
@@ -69,12 +69,12 @@ public class LargePolybiusSquare extends PolybiusSquare{
|
||||
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
|
||||
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
|
||||
if(this.inputString.isBlank() || getPreparedInputStringEncode().isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected String getPreparedInputStringEncoding(){
|
||||
protected String getPreparedInputStringEncode(){
|
||||
logger.debug("Preparing input string for encoding");
|
||||
|
||||
String cleanString = inputString.toUpperCase();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/PolybiusSquare.java
|
||||
//Mattrixwv
|
||||
// Created: 01-04-22
|
||||
//Modified: 07-09-22
|
||||
//Modified: 04-29-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -12,10 +12,11 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class PolybiusSquare{
|
||||
protected static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
|
||||
protected static Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
|
||||
|
||||
//A class representing the location of a character in the grid
|
||||
protected class CharLocation{
|
||||
@@ -43,6 +44,41 @@ public class PolybiusSquare{
|
||||
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||
protected boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||
|
||||
|
||||
//Setting the character to be replaced
|
||||
protected void setReplaced(char replaced) throws InvalidCharacterException{
|
||||
logger.debug("Setting replaced");
|
||||
logger.debug("Original character {}", replaced);
|
||||
|
||||
if(!Character.isAlphabetic(replaced)){
|
||||
throw new InvalidCharacterException("The replaced character must be a letter");
|
||||
}
|
||||
|
||||
logger.debug("Checking replacer");
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
this.replaced = Character.toUpperCase(replaced);
|
||||
logger.debug("Cleaned character {}", this.replaced);
|
||||
}
|
||||
//Setting the character that replaces replaced
|
||||
protected void setReplacer(char replacer) throws InvalidCharacterException{
|
||||
logger.debug("Setting replacer");
|
||||
logger.debug("Original character {}", replacer);
|
||||
|
||||
if(!Character.isAlphabetic(replacer)){
|
||||
throw new InvalidCharacterException("The replacer character must be a letter");
|
||||
}
|
||||
|
||||
logger.debug("Checking replaced");
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replacer letter cannot be the same as the replaced letter");
|
||||
}
|
||||
|
||||
this.replacer = Character.toUpperCase(replacer);
|
||||
logger.debug("Cleaned character {}", this.replacer);
|
||||
}
|
||||
//Create the grid from the keyword
|
||||
protected void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
@@ -57,18 +93,18 @@ public class PolybiusSquare{
|
||||
logger.debug("Created grid\n{}", getGrid());
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Setting input string for encoding '{}'", inputString);
|
||||
|
||||
//Make sure the string doesn't contain any numbers
|
||||
logger.debug("Checking for digits");
|
||||
for(char ch = '0';ch <= '9';++ch){
|
||||
if(inputString.contains(Character.toString(ch))){
|
||||
throw new InvalidCharacterException("Inputs for encoding cannot contain numbers");
|
||||
for(char ch : inputString.toCharArray()){
|
||||
if(Character.isDigit(ch)){
|
||||
throw new InvalidInputException("Inputs for encoding cannot contain numbers");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,13 +143,13 @@ public class PolybiusSquare{
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
|
||||
if(this.inputString.isBlank() || getPreparedInputStringEncode().isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
protected void setInputStringDecoding(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Setting input string for decoding '{}'", inputString);
|
||||
@@ -127,11 +163,11 @@ public class PolybiusSquare{
|
||||
++numberOfDigits;
|
||||
}
|
||||
else if(Character.isAlphabetic(ch)){
|
||||
throw new InvalidCharacterException("Inputs for decoding cannot contains letters");
|
||||
throw new InvalidInputException("Inputs for decoding cannot contains letters");
|
||||
}
|
||||
}
|
||||
if((numberOfDigits % 2) != 0){
|
||||
throw new InvalidCharacterException("There must be an even number of digits in an encoded string");
|
||||
throw new InvalidInputException("There must be an even number of digits in an encoded string");
|
||||
}
|
||||
|
||||
//Remove any whitespace if selected
|
||||
@@ -152,12 +188,12 @@ public class PolybiusSquare{
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank() || getPreparedInputStringDecoding().isBlank()){
|
||||
if(this.inputString.isBlank() || getPreparedInputStringDecode().isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
protected String getPreparedInputStringEncoding(){
|
||||
protected String getPreparedInputStringEncode(){
|
||||
logger.debug("Preparing input string for encoding");
|
||||
|
||||
String cleanString = inputString.toUpperCase();
|
||||
@@ -166,8 +202,8 @@ public class PolybiusSquare{
|
||||
logger.debug("Prepared string '{}'", cleanString);
|
||||
return cleanString;
|
||||
}
|
||||
protected String getPreparedInputStringDecoding(){
|
||||
logger.debug("Prepareing input string for decoding");
|
||||
protected String getPreparedInputStringDecode(){
|
||||
logger.debug("Preparing input string for decoding");
|
||||
|
||||
String cleanString = inputString.replaceAll("\\D", "");
|
||||
|
||||
@@ -177,7 +213,7 @@ public class PolybiusSquare{
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
protected void setKeyword(String keyword){
|
||||
if(keyword == null){
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original keyword {}", keyword);
|
||||
@@ -187,7 +223,7 @@ public class PolybiusSquare{
|
||||
keyword = keyword.toUpperCase();
|
||||
|
||||
//Remove everything except capital letters
|
||||
logger.debug("Removing all non-letters");
|
||||
logger.debug("Removing all non-letter characters");
|
||||
keyword = keyword.replaceAll("[^A-Z]", "");
|
||||
|
||||
//Add all letters in the alphabet to the key
|
||||
@@ -202,7 +238,7 @@ public class PolybiusSquare{
|
||||
StringBuilder uniqueKey = new StringBuilder();
|
||||
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
|
||||
this.keyword = uniqueKey.toString();
|
||||
logger.debug("Cleaned keyword {}", keyword);
|
||||
logger.debug("Cleaned keyword {}", this.keyword);
|
||||
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
@@ -229,11 +265,11 @@ public class PolybiusSquare{
|
||||
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||
logger.debug("Working character {}", inputString.charAt(inputCnt));
|
||||
for(char inputChar : inputString.toCharArray()){
|
||||
logger.debug("Working character {}", inputChar);
|
||||
|
||||
//Add both numbers of any letters to the output
|
||||
if(Character.isAlphabetic(inputString.charAt(inputCnt))){
|
||||
if(Character.isAlphabetic(inputChar)){
|
||||
logger.debug("Adding encoded characters");
|
||||
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
@@ -243,15 +279,15 @@ public class PolybiusSquare{
|
||||
else{
|
||||
logger.debug("Adding symbols");
|
||||
|
||||
fullOutput.append(inputString.charAt(inputCnt));
|
||||
fullOutput.append(inputChar);
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Formatted output '{}'", fullOutput);
|
||||
outputString = fullOutput.toString();
|
||||
logger.debug("Formatted output '{}'", outputString);
|
||||
}
|
||||
protected void addCharactersToCleanStringDecode(String cleanString){
|
||||
logger.debug("Formatting output string to decoding");
|
||||
logger.debug("Formatting output string for decoding");
|
||||
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
@@ -273,20 +309,20 @@ public class PolybiusSquare{
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Formatted output '{}'", fullOutput);
|
||||
outputString = fullOutput.toString();
|
||||
logger.debug("Formatted output '{}'", outputString);
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String encode() throws InvalidInputException{
|
||||
protected void encode() throws InvalidInputException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
String cleanString = getPreparedInputStringEncoding();
|
||||
String cleanString = getPreparedInputStringEncode();
|
||||
for(int cnt = 0;cnt < cleanString.length();++cnt){
|
||||
//Get the next character to be encoded
|
||||
char ch = cleanString.charAt(cnt);
|
||||
|
||||
logger.debug("Working character {}", ch);
|
||||
logger.debug("Current working character {}", ch);
|
||||
|
||||
//Find the letter in the grid
|
||||
CharLocation location = findChar(ch);
|
||||
@@ -299,16 +335,13 @@ public class PolybiusSquare{
|
||||
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanStringEncode(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String decode(){
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
String cleanString = getPreparedInputStringDecoding();
|
||||
String cleanString = getPreparedInputStringDecode();
|
||||
for(int cnt = 0;cnt < cleanString.length();){
|
||||
//Get the digits indicationg the location of the next character
|
||||
char firstDigit = cleanString.charAt(cnt++);
|
||||
@@ -327,9 +360,6 @@ public class PolybiusSquare{
|
||||
|
||||
//Add other characters to the output
|
||||
addCharactersToCleanStringDecode(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public PolybiusSquare() throws InvalidCharacterException{
|
||||
@@ -360,8 +390,9 @@ public class PolybiusSquare{
|
||||
public String encode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputStringEncoding(inputString);
|
||||
return encode();
|
||||
setInputStringEncode(inputString);
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
@@ -370,8 +401,9 @@ public class PolybiusSquare{
|
||||
public String decode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputStringDecoding(inputString);
|
||||
return decode();
|
||||
setInputStringDecode(inputString);
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
@@ -387,31 +419,9 @@ public class PolybiusSquare{
|
||||
public char getReplaced(){
|
||||
return replaced;
|
||||
}
|
||||
public void setReplaced(char replaced) throws InvalidCharacterException{
|
||||
if(!Character.isAlphabetic(replaced)){
|
||||
throw new InvalidCharacterException("The replaced character must be a letter");
|
||||
}
|
||||
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
this.replaced = Character.toUpperCase(replaced);
|
||||
}
|
||||
public char getReplacer(){
|
||||
return replacer;
|
||||
}
|
||||
public void setReplacer(char replacer) throws InvalidCharacterException{
|
||||
if(!Character.isAlphabetic(replacer)){
|
||||
throw new InvalidCharacterException("The replacer character must be a letter");
|
||||
}
|
||||
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replacer letter cannot be the same as the replaced letter");
|
||||
}
|
||||
|
||||
this.replacer = replacer;
|
||||
}
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
|
||||
@@ -1,408 +1,348 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestBifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 07-09-22
|
||||
//Modified: 04-23-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyChar;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class TestBifid{
|
||||
@Test
|
||||
public void testEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, true);
|
||||
private Bifid cipher;
|
||||
private Logger logger;
|
||||
//Test
|
||||
private String inputString = "Message to^encode";
|
||||
private String inputStringClean = "MESSAGETOENCODE";
|
||||
private String outputString = "Mqaokne kc^vdodzd";
|
||||
private String outputStringClean = "MQAOKNEKCVDODZD";
|
||||
private String keyword = "keyword";
|
||||
private String keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ";
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup(){
|
||||
cipher = new Bifid();
|
||||
logger = mock(Logger.class);
|
||||
Bifid.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testConstructor_default(){
|
||||
cipher = new Bifid();
|
||||
|
||||
assertFalse(cipher.preserveCapitals);
|
||||
assertFalse(cipher.preserveWhitespace);
|
||||
assertFalse(cipher.preserveSymbols);
|
||||
assertNotNull(cipher.polybiusSquare);
|
||||
assertFalse(cipher.polybiusSquare.preserveWhitespace);
|
||||
assertFalse(cipher.polybiusSquare.preserveSymbols);
|
||||
assertEquals("", cipher.inputString);
|
||||
assertEquals("", cipher.keyword);
|
||||
assertEquals("", cipher.outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_preservesCapitals(){
|
||||
cipher = new Bifid(true, false, false);
|
||||
|
||||
assertTrue(cipher.preserveCapitals);
|
||||
assertFalse(cipher.preserveWhitespace);
|
||||
assertFalse(cipher.preserveSymbols);
|
||||
assertNotNull(cipher.polybiusSquare);
|
||||
assertFalse(cipher.polybiusSquare.preserveWhitespace);
|
||||
assertFalse(cipher.polybiusSquare.preserveSymbols);
|
||||
assertEquals("", cipher.inputString);
|
||||
assertEquals("", cipher.keyword);
|
||||
assertEquals("", cipher.outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_preservesWhitespace(){
|
||||
cipher = new Bifid(false, true, false);
|
||||
|
||||
assertFalse(cipher.preserveCapitals);
|
||||
assertTrue(cipher.preserveWhitespace);
|
||||
assertFalse(cipher.preserveSymbols);
|
||||
assertNotNull(cipher.polybiusSquare);
|
||||
assertFalse(cipher.polybiusSquare.preserveWhitespace);
|
||||
assertFalse(cipher.polybiusSquare.preserveSymbols);
|
||||
assertEquals("", cipher.inputString);
|
||||
assertEquals("", cipher.keyword);
|
||||
assertEquals("", cipher.outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_preservesSymbols(){
|
||||
cipher = new Bifid(false, false, true);
|
||||
|
||||
assertFalse(cipher.preserveCapitals);
|
||||
assertFalse(cipher.preserveWhitespace);
|
||||
assertTrue(cipher.preserveSymbols);
|
||||
assertNotNull(cipher.polybiusSquare);
|
||||
assertFalse(cipher.polybiusSquare.preserveWhitespace);
|
||||
assertFalse(cipher.polybiusSquare.preserveSymbols);
|
||||
assertEquals("", cipher.inputString);
|
||||
assertEquals("", cipher.keyword);
|
||||
assertEquals("", cipher.outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
|
||||
assertEquals(keyword, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Setting keyword '{}'", keyword);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword_null(){
|
||||
assertThrows(InvalidKeywordException.class, () -> {
|
||||
cipher.setKeyword(null);
|
||||
});
|
||||
|
||||
verify(logger, never()).debug(eq("Setting keyword '{}'"), anyString());
|
||||
verify(logger, never()).debug(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_noCapitals(){
|
||||
cipher.preserveCapitals = false;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_noWhitespace(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
|
||||
assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_noSymbols(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
|
||||
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_blank(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputString("");
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", "");
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_null(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputString(null);
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatOutput(){
|
||||
cipher.inputString = inputString;
|
||||
|
||||
cipher.formatOutput(outputStringClean);
|
||||
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output");
|
||||
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Altering uppercase");
|
||||
verify(logger, times(14)).debug("Altering lowercase");
|
||||
verify(logger, times(2)).debug("Adding symbol");
|
||||
verify(logger, times(1)).debug("Formatted output string '{}'", outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keyword;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(1)).debug("Encoding Polybius");
|
||||
verify(logger, times(1)).debug("Splitting Polybius Square message");
|
||||
verify(logger, times(30)).debug(eq("Current character '{}'"), anyChar());
|
||||
verify(logger, times(1)).debug("Decoding Polybius Square");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = outputString;
|
||||
cipher.keyword = keyword;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputString, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Encoding Polybius Square");
|
||||
verify(logger, times(1)).debug("Splitting Polybius Square message");
|
||||
verify(logger, times(15)).debug(eq("Current characters {} {}"), anyChar(), anyChar());
|
||||
verify(logger, times(1)).debug("Decoding Polybius Square");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.outputString = outputString;
|
||||
|
||||
assertEquals(inputString, cipher.getInputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(outputString, cipher.getOutputString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.outputString = outputString;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
assertEquals("", cipher.keyword);
|
||||
assertEquals("", cipher.outputString);
|
||||
verify(logger, times(1)).debug("Resetting fields");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Bifid(true, true, true);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "mqaoknekcvdodzd";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne kc vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne*kc+vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Mqaokne kc^vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(output, cipher.outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, true, true);
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Bifid(false, false, false);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MQAOKNEKCVDODZD";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNE KC VDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNE*KC+VDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNE KC^VDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
assertEquals(inputStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputStringClean, cipher.outputString);
|
||||
assertEquals(outputStringClean, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, false, true);
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Bifid(true, true, true);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "mqaoknekcvdodzd";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaoknekcvdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne*kc+vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Mqaoknekc^vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputString, cipher.outputString);
|
||||
assertEquals(inputString, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, false);
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new Bifid(false, false, false);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "mqaoknekcvdodzd";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne kc vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaoknekcvdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Mqaokne kcvdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, false, false);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MQAOKNEKCVDODZD";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, true);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "messagetoencode";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message to encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message*to+encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Message to^encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, true, true);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MESSAGETOENCODE";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGE TO ENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGE*TO+ENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGE TO^ENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, false, true);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "messagetoencode";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "messagetoencode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message*to+encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Messageto^encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, false);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "messagetoencode";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message to encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "messagetoencode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Message toencode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, false, false);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MESSAGETOENCODE";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
assertEquals(outputStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputStringClean, cipher.outputString);
|
||||
assertEquals(inputStringClean, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +30,10 @@ public class TestLargePolybiusSquare{
|
||||
private LargePolybiusSquare cipher;
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private String inputString = "Message to^encode";
|
||||
private String inputStringClean = "MESSAGETOENCODE";
|
||||
private String outputString = "35124343222612 4415^123624152112";
|
||||
private String outputStringClean = "31 15 41 41 11 21 15 42 33 15 32 13 33 14 15";
|
||||
private String decodedString = "Message to^encode";
|
||||
private String decodedStringClean = "MESSAGETOENCODE";
|
||||
private String encodedString = "35124343222612 4415^123624152112";
|
||||
private String encodedStringClean = "31 15 41 41 11 21 15 42 33 15 32 13 33 14 15";
|
||||
private String keyword = "keyword";
|
||||
private String keywordClean = "KEYWORDABCFGHIJLMNPQSTUVXZ0123456789";
|
||||
private String keywordBlank = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
@@ -114,13 +114,13 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputStringEncoding(inputString);
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -128,13 +128,13 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputStringEncoding(inputString);
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
|
||||
assertEquals(inputString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -142,13 +142,13 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputStringEncoding(inputString);
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
|
||||
assertEquals(inputString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -156,10 +156,10 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputStringEncoding(inputString);
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
|
||||
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", "M E S S A G E T O E N C O D E");
|
||||
@@ -171,7 +171,7 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringEncoding("*");
|
||||
cipher.setInputStringEncode("*");
|
||||
});
|
||||
|
||||
assertEquals("*", cipher.inputString);
|
||||
@@ -187,7 +187,7 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringEncoding("");
|
||||
cipher.setInputStringEncode("");
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
@@ -203,7 +203,7 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringEncoding(null);
|
||||
cipher.setInputStringEncode(null);
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
@@ -215,12 +215,12 @@ public class TestLargePolybiusSquare{
|
||||
|
||||
@Test
|
||||
public void testGetPreparedInputStringEncoding(){
|
||||
cipher.inputString = inputString.toUpperCase();
|
||||
cipher.inputString = decodedString.toUpperCase();
|
||||
|
||||
String preparedInputString = cipher.getPreparedInputStringEncoding();
|
||||
String preparedInputString = cipher.getPreparedInputStringEncode();
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(inputString.toUpperCase().replaceAll("[^A-Z0-9]", ""), preparedInputString);
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("[^A-Z0-9]", ""), preparedInputString);
|
||||
verify(logger, times(1)).debug("Preparing input string for encoding");
|
||||
verify(logger, times(1)).debug("Prepared input string '{}'", preparedInputString);
|
||||
}
|
||||
@@ -263,16 +263,16 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = inputString.toUpperCase();
|
||||
cipher.inputString = decodedString.toUpperCase();
|
||||
|
||||
cipher.addCharactersToCleanStringEncode(outputString.replaceAll("\\s", "").replaceAll("[^0-9]", ""));
|
||||
cipher.addCharactersToCleanStringEncode(encodedString.replaceAll("\\s", "").replaceAll("[^0-9]", ""));
|
||||
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string");
|
||||
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
|
||||
verify(logger, times(15)).debug("Appending character");
|
||||
verify(logger, times(2)).debug("Appending symbol");
|
||||
verify(logger, times(1)).debug("Saving output string {}", outputString);
|
||||
verify(logger, times(1)).debug("Saving output string {}", encodedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -281,21 +281,21 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = outputString;
|
||||
cipher.inputString = encodedString;
|
||||
|
||||
cipher.addCharactersToCleanStringDecode(inputStringClean);
|
||||
cipher.addCharactersToCleanStringDecode(decodedStringClean);
|
||||
|
||||
verify(logger, times(1)).debug("Formatting output string");
|
||||
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
|
||||
verify(logger, times(15)).debug("Appending character");
|
||||
verify(logger, times(2)).debug("Appending symbol");
|
||||
verify(logger, times(1)).debug("Saving output string {}", inputString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Saving output string {}", decodedString.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.outputString = outputString;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.grid = grid;
|
||||
|
||||
@@ -313,47 +313,47 @@ public class TestLargePolybiusSquare{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new LargePolybiusSquare(true, true);
|
||||
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(outputString, output);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new LargePolybiusSquare(false, false);
|
||||
|
||||
String output = cipher.encode(inputString);
|
||||
String output = cipher.encode(decodedString);
|
||||
|
||||
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
|
||||
assertEquals(keywordBlank, cipher.keyword);
|
||||
assertEquals(outputStringClean, cipher.outputString);
|
||||
assertEquals(outputStringClean, output);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new LargePolybiusSquare(true, true);
|
||||
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputString.toUpperCase(), cipher.outputString);
|
||||
assertEquals(inputString.toUpperCase(), output);
|
||||
assertEquals(decodedString.toUpperCase(), cipher.outputString);
|
||||
assertEquals(decodedString.toUpperCase(), output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new LargePolybiusSquare(false, false);
|
||||
|
||||
String output = cipher.decode(outputStringClean);
|
||||
String output = cipher.decode(encodedStringClean);
|
||||
|
||||
assertEquals(outputStringClean.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(encodedStringClean.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(keywordBlank, cipher.keyword);
|
||||
assertEquals(inputStringClean, cipher.outputString);
|
||||
assertEquals(inputStringClean, output);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
}
|
||||
}
|
||||
@@ -530,7 +530,7 @@ public class TestPlayfair{
|
||||
cipher.doubled = 'x';
|
||||
|
||||
assertThrows(InvalidCharacterException.class, () -> {
|
||||
cipher.setInputString(outputString + Character.toString(cipher.replaced), false);
|
||||
cipher.setInputString(outputString + cipher.replaced, false);
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user