Updated test coverage
This commit is contained in:
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@@ -38,6 +38,6 @@
|
|||||||
],
|
],
|
||||||
"sonarlint.connectedMode.project": {
|
"sonarlint.connectedMode.project": {
|
||||||
"connectionId": "mattrixwvSonarqube",
|
"connectionId": "mattrixwvSonarqube",
|
||||||
"projectKey": "mattrixwv_cipherstreamjava_AYGcdy79opaC7KAbzMEs"
|
"projectKey": "CipherStreamJava"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
7
pom.xml
7
pom.xml
@@ -63,12 +63,7 @@
|
|||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
<version>5.3.0</version>
|
<version>5.3.0</version>
|
||||||
</dependency>
|
<scope>test</scope>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mockito</groupId>
|
|
||||||
<artifactId>mockito-junit-jupiter</artifactId>
|
|
||||||
<version>5.3.0</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java
|
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGVX.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 01-26-22
|
// Created: 01-26-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-14-23
|
||||||
package com.mattrixwv.cipherstream.combination;
|
package com.mattrixwv.cipherstream.combination;
|
||||||
|
|
||||||
|
|
||||||
@@ -18,11 +18,11 @@ import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
|
|||||||
|
|
||||||
|
|
||||||
public class ADFGVX{
|
public class ADFGVX{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ADFGVX.class);
|
protected static Logger logger = LoggerFactory.getLogger(ADFGVX.class);
|
||||||
|
|
||||||
//Internal classes
|
//Internal classes
|
||||||
private class LargePolybiusSquare extends PolybiusSquare{
|
protected class LargePolybiusSquare extends PolybiusSquare{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class);
|
protected static final Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void createGrid(){
|
protected void createGrid(){
|
||||||
@@ -183,38 +183,38 @@ public class ADFGVX{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Fields
|
//Fields
|
||||||
private boolean preserveCapitals; //Whether to respect capitals in the output string
|
protected boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||||
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||||
private boolean preserveSymbols; //Whether to respect symbols in the output string
|
protected boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||||
private String inputString; //The string that needs encoded/decoded
|
protected String inputString; //The string that needs encoded/decoded
|
||||||
private String outputString; //The string that is output after encoding/decoding
|
protected String outputString; //The string that is output after encoding/decoding
|
||||||
private String squareKeyword; //The keyword used in the Polybius Square
|
protected String squareKeyword; //The keyword used in the Polybius Square
|
||||||
private String keyword; //The Keyword used in the Columnar cipher
|
protected String keyword; //The Keyword used in the Columnar cipher
|
||||||
//Internal ciphers
|
//Internal ciphers
|
||||||
private LargePolybiusSquare largePolybiusSquare; //The first step in encoding
|
protected LargePolybiusSquare largePolybiusSquare; //The first step in encoding
|
||||||
private Columnar columnar; //The second step in encoding
|
protected Columnar columnar; //The second step in encoding
|
||||||
|
|
||||||
|
|
||||||
//Ensures Polybius keyword constraints
|
//Ensures Polybius keyword constraints
|
||||||
private void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{
|
protected void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{
|
||||||
if(squareKeyword == null){
|
if(squareKeyword == null){
|
||||||
throw new InvalidKeywordException("Square Keyword cannot be null");
|
throw new InvalidKeywordException("Square Keyword cannot be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Square Keyword '{}'", squareKeyword);
|
logger.debug("squareKeyword = {}", squareKeyword);
|
||||||
this.squareKeyword = squareKeyword;
|
this.squareKeyword = squareKeyword;
|
||||||
}
|
}
|
||||||
//Ensures Columnar keyword constraints
|
//Ensures Columnar keyword constraints
|
||||||
private void setKeyword(String keyword) throws InvalidKeywordException{
|
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||||
if(keyword == null){
|
if(keyword == null){
|
||||||
throw new InvalidKeywordException("Keyword cannot be null");
|
throw new InvalidKeywordException("Keyword cannot be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Keyword '{}'", keyword);
|
logger.debug("keyword = {}", keyword);
|
||||||
this.keyword = keyword;
|
this.keyword = keyword;
|
||||||
}
|
}
|
||||||
//Ensures inputString constraints
|
//Ensures inputString constraints
|
||||||
private void setInputString(String inputString) throws InvalidInputException{
|
protected void setInputString(String inputString) throws InvalidInputException{
|
||||||
if(inputString == null){
|
if(inputString == null){
|
||||||
throw new InvalidInputException("Input cannot be null");
|
throw new InvalidInputException("Input cannot be null");
|
||||||
}
|
}
|
||||||
@@ -246,31 +246,37 @@ public class ADFGVX{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Format the output string with capitals, symbols, and numbers that are in the input string
|
//Format the output string with capitals, symbols, and numbers that are in the input string
|
||||||
private void formatOutputStringEncode(){
|
protected void formatOutputStringEncode(){
|
||||||
logger.debug("Formatting output string");
|
logger.debug("Formatting output string to match input string");
|
||||||
|
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
int outputLocation = 0;
|
int outputLocation = 0;
|
||||||
for(char ch : inputString.toCharArray()){
|
for(char ch : inputString.toCharArray()){
|
||||||
logger.debug("Input string {}", ch);
|
logger.debug("Input character {}", ch);
|
||||||
if(Character.isUpperCase(ch)){
|
if(Character.isUpperCase(ch)){
|
||||||
|
logger.debug("Converting output to uppercase");
|
||||||
|
|
||||||
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
|
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
|
||||||
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
|
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
|
||||||
}
|
}
|
||||||
else if(Character.isLowerCase(ch)){
|
else if(Character.isLowerCase(ch)){
|
||||||
|
logger.debug("Converting output to lowercase");
|
||||||
|
|
||||||
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
|
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
|
||||||
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
|
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
logger.debug("Appending symbol to output");
|
||||||
|
|
||||||
output.append(ch);
|
output.append(ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Saving output string '{}'", output);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
|
logger.debug("Saving output string '{}'", outputString);
|
||||||
}
|
}
|
||||||
private void formatOutputStringDecode(){
|
protected void formatOutputStringDecode(){
|
||||||
logger.debug("Formatting output string");
|
logger.debug("Formatting output string to match input string");
|
||||||
|
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
int outputLocation = 0;
|
int outputLocation = 0;
|
||||||
@@ -278,23 +284,29 @@ public class ADFGVX{
|
|||||||
char ch = inputString.charAt(inputLocation);
|
char ch = inputString.charAt(inputLocation);
|
||||||
logger.debug("Input character {}", ch);
|
logger.debug("Input character {}", ch);
|
||||||
if(Character.isUpperCase(ch)){
|
if(Character.isUpperCase(ch)){
|
||||||
|
logger.debug("Converting output to uppercase");
|
||||||
|
|
||||||
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
|
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
|
||||||
++inputLocation;
|
++inputLocation;
|
||||||
}
|
}
|
||||||
else if(Character.isLowerCase(ch)){
|
else if(Character.isLowerCase(ch)){
|
||||||
|
logger.debug("Converting output to lowercase");
|
||||||
|
|
||||||
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
|
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
|
||||||
++inputLocation;
|
++inputLocation;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
logger.debug("Appending symbol to output");
|
||||||
|
|
||||||
output.append(ch);
|
output.append(ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Saving output string '{}'", output);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
|
logger.debug("Saving output string '{}'", outputString);
|
||||||
}
|
}
|
||||||
//Encodes the inputString and stores the result in outputString
|
//Encodes the inputString and stores the result in outputString
|
||||||
private String encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{
|
protected String encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{
|
||||||
//Encode the input with polybius
|
//Encode the input with polybius
|
||||||
logger.debug("Encoding using Polybius Square");
|
logger.debug("Encoding using Polybius Square");
|
||||||
String polybiusOutput = largePolybiusSquare.encode(squareKeyword, inputString);
|
String polybiusOutput = largePolybiusSquare.encode(squareKeyword, inputString);
|
||||||
@@ -303,7 +315,7 @@ public class ADFGVX{
|
|||||||
polybiusOutput = polybiusOutput.replace("1", "A").replace("2", "D").replace("3", "F").replace("4", "G").replace("5", "V").replace("6", "X");
|
polybiusOutput = polybiusOutput.replace("1", "A").replace("2", "D").replace("3", "F").replace("4", "G").replace("5", "V").replace("6", "X");
|
||||||
|
|
||||||
//Encode polybius's output with columnar
|
//Encode polybius's output with columnar
|
||||||
logger.debug("Encoding with columnar");
|
logger.debug("Encoding using columnar");
|
||||||
String columnarOutput = columnar.encode(keyword, polybiusOutput);
|
String columnarOutput = columnar.encode(keyword, polybiusOutput);
|
||||||
outputString = columnarOutput;
|
outputString = columnarOutput;
|
||||||
|
|
||||||
@@ -313,16 +325,16 @@ public class ADFGVX{
|
|||||||
return outputString;
|
return outputString;
|
||||||
}
|
}
|
||||||
//Decodes the inputString and stores the result in outputString
|
//Decodes the inputString and stores the result in outputString
|
||||||
private String decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{
|
protected String decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{
|
||||||
//Decode the input with columnar
|
//Decode the input with columnar
|
||||||
logger.debug("Decoding columnar");
|
logger.debug("Decoding using columnar");
|
||||||
String columnarOutput = columnar.decode(keyword, inputString);
|
String columnarOutput = columnar.decode(keyword, inputString);
|
||||||
|
|
||||||
//Change the symbols to the correct ones for polybius
|
//Change the symbols to the correct ones for polybius
|
||||||
logger.debug("Replacing characters with coordinates");
|
logger.debug("Replacing letters with coordinates");
|
||||||
columnarOutput = columnarOutput.replace("A", "1").replace("D", "2").replace("F", "3").replace("G", "4").replace("V", "5").replace("X", "6");
|
columnarOutput = columnarOutput.replace("A", "1").replace("D", "2").replace("F", "3").replace("G", "4").replace("V", "5").replace("X", "6");
|
||||||
//Decode with polybius
|
//Decode with polybius
|
||||||
logger.debug("Decoding Polybius Square");
|
logger.debug("Decoding using Polybius Square");
|
||||||
String polybiusOutput = largePolybiusSquare.decode(squareKeyword, columnarOutput);
|
String polybiusOutput = largePolybiusSquare.decode(squareKeyword, columnarOutput);
|
||||||
outputString = polybiusOutput;
|
outputString = polybiusOutput;
|
||||||
|
|
||||||
@@ -380,7 +392,7 @@ public class ADFGVX{
|
|||||||
}
|
}
|
||||||
//Makes sure all of the variables are empty
|
//Makes sure all of the variables are empty
|
||||||
public void reset() throws InvalidCharacterException{
|
public void reset() throws InvalidCharacterException{
|
||||||
logger.debug("Reseting fields");
|
logger.debug("Resetting fields");
|
||||||
|
|
||||||
largePolybiusSquare = new LargePolybiusSquare(false, false);
|
largePolybiusSquare = new LargePolybiusSquare(false, false);
|
||||||
columnar = new Columnar(false, false, false, true, 'B');
|
columnar = new Columnar(false, false, false, true, 'B');
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java
|
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 01-25-22
|
// Created: 01-25-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-14-23
|
||||||
package com.mattrixwv.cipherstream.combination;
|
package com.mattrixwv.cipherstream.combination;
|
||||||
|
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ public class ADFGX{
|
|||||||
throw new InvalidInputException("Input cannot be null");
|
throw new InvalidInputException("Input cannot be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("original input string '{}'", inputString);
|
logger.debug("Original input string '{}'", inputString);
|
||||||
if(!preserveCapitals){
|
if(!preserveCapitals){
|
||||||
logger.debug("Removing capitals");
|
logger.debug("Removing capitals");
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ public class ADFGX{
|
|||||||
if(this.inputString.isBlank()){
|
if(this.inputString.isBlank()){
|
||||||
throw new InvalidInputException("Input cannot be blank");
|
throw new InvalidInputException("Input cannot be blank");
|
||||||
}
|
}
|
||||||
logger.debug("cleaned input string '{}'", inputString);
|
logger.debug("Cleaned input string '{}'", inputString);
|
||||||
}
|
}
|
||||||
//Format the output string with capitals, symbols, and numbers that are in the input string
|
//Format the output string with capitals, symbols, and numbers that are in the input string
|
||||||
protected void formatOutputStringEncode(){
|
protected void formatOutputStringEncode(){
|
||||||
@@ -106,8 +106,8 @@ public class ADFGX{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Saving output string '{}'", outputString);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
|
logger.debug("Saving output string '{}'", outputString);
|
||||||
}
|
}
|
||||||
protected void formatOutputStringDecode(){
|
protected void formatOutputStringDecode(){
|
||||||
logger.debug("Formatting output string to match input string");
|
logger.debug("Formatting output string to match input string");
|
||||||
@@ -135,8 +135,8 @@ public class ADFGX{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Saving output string '{}'", output);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
|
logger.debug("Saving output string '{}'", outputString);
|
||||||
}
|
}
|
||||||
//Encodes the inputString and stores the result in outputString
|
//Encodes the inputString and stores the result in outputString
|
||||||
protected void encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{
|
protected void encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Affine.java
|
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Affine.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 01-26-22
|
// Created: 01-26-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-15-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
@@ -15,24 +15,26 @@ import com.mattrixwv.NumberAlgorithms;
|
|||||||
|
|
||||||
|
|
||||||
public class Affine{
|
public class Affine{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Affine.class);
|
protected static Logger logger = LoggerFactory.getLogger(Affine.class);
|
||||||
|
|
||||||
//Fields
|
//Fields
|
||||||
private boolean preserveCapitals; //Whether to respect capitals in the output string
|
protected boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||||
private boolean preserveSymbols; //Whether to respect symbols in the output string
|
protected boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||||
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||||
private String inputString; //The string that needs encoded/decoded
|
protected String inputString; //The string that needs encoded/decoded
|
||||||
private String outputString; //The string that is output after encoding/decoding
|
protected String outputString; //The string that is output after encoding/decoding
|
||||||
private int key1; //The multiplicative key. Key1 must be relatively prime to 26
|
protected int key1; //The multiplicative key. Key1 must be relatively prime to 26
|
||||||
private int key2; //The additive key
|
protected int key2; //The additive key
|
||||||
|
|
||||||
//Ensures key1 constraints
|
//Ensures key1 constraints
|
||||||
private void setKey1(int key1) throws InvalidKeywordException{
|
protected void setKey1(int key1) throws InvalidKeywordException{
|
||||||
logger.debug("Setting key1 {}", key1);
|
logger.debug("Setting key1 {}", key1);
|
||||||
|
|
||||||
|
//Mod 26 to ensure no overflow
|
||||||
|
key1 %= 26;
|
||||||
|
|
||||||
//If the key is negative change it to possitive
|
//If the key is negative change it to possitive
|
||||||
if(key1 < 0){
|
if(key1 < 0){
|
||||||
key1 %= 26;
|
|
||||||
key1 += 26;
|
key1 += 26;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,12 +49,14 @@ public class Affine{
|
|||||||
logger.debug("Cleaned key1 {}", key1);
|
logger.debug("Cleaned key1 {}", key1);
|
||||||
}
|
}
|
||||||
//Ensures key2 constraints
|
//Ensures key2 constraints
|
||||||
private void setKey2(int key2){
|
protected void setKey2(int key2){
|
||||||
logger.debug("Setting key2 {}", key2);
|
logger.debug("Setting key2 {}", key2);
|
||||||
|
|
||||||
|
//Mod 26 to ensure no overflow
|
||||||
|
key2 %= 26;
|
||||||
|
|
||||||
//If the key is negative change it to possitive
|
//If the key is negative change it to possitive
|
||||||
if(key2 < 0){
|
if(key2 < 0){
|
||||||
key2 %= 26;
|
|
||||||
key2 += 26;
|
key2 += 26;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +66,7 @@ public class Affine{
|
|||||||
logger.debug("Cleaned key2 {}", key2);
|
logger.debug("Cleaned key2 {}", key2);
|
||||||
}
|
}
|
||||||
//Ensures inputString constraints
|
//Ensures inputString constraints
|
||||||
private void setInputString(String inputString) throws InvalidInputException{
|
protected void setInputString(String inputString) throws InvalidInputException{
|
||||||
if(inputString == null){
|
if(inputString == null){
|
||||||
throw new InvalidInputException("Input must not be null");
|
throw new InvalidInputException("Input must not be null");
|
||||||
}
|
}
|
||||||
@@ -94,7 +98,7 @@ public class Affine{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Encodes the inputString and stores the result in outputString
|
//Encodes the inputString and stores the result in outputString
|
||||||
private String encode(){
|
protected void encode(){
|
||||||
logger.debug("Encoding");
|
logger.debug("Encoding");
|
||||||
|
|
||||||
//Step through every character in the input and encode it if needed
|
//Step through every character in the input and encode it if needed
|
||||||
@@ -108,7 +112,7 @@ public class Affine{
|
|||||||
//Encode the number
|
//Encode the number
|
||||||
letter = ((key1 * letter) + key2) % 26;
|
letter = ((key1 * letter) + key2) % 26;
|
||||||
//Change the new number back to a character and append it to the output
|
//Change the new number back to a character and append it to the output
|
||||||
char newChar = (char)(letter + 65);
|
char newChar = (char)(letter + 'A');
|
||||||
output.append(newChar);
|
output.append(newChar);
|
||||||
|
|
||||||
logger.debug("Encoded char {}", newChar);
|
logger.debug("Encoded char {}", newChar);
|
||||||
@@ -119,7 +123,7 @@ public class Affine{
|
|||||||
//Encode the number
|
//Encode the number
|
||||||
letter = ((key1 * letter) + key2) % 26;
|
letter = ((key1 * letter) + key2) % 26;
|
||||||
//Change the new number back to a character and append it to the output
|
//Change the new number back to a character and append it to the output
|
||||||
char newChar = (char)(letter + 97);
|
char newChar = (char)(letter + 'a');
|
||||||
output.append(newChar);
|
output.append(newChar);
|
||||||
|
|
||||||
logger.debug("Encoded char {}", newChar);
|
logger.debug("Encoded char {}", newChar);
|
||||||
@@ -131,12 +135,11 @@ public class Affine{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Save and return the output
|
//Save and return the output
|
||||||
logger.debug("Saving output string '{}'", output);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
return outputString;
|
logger.debug("Saving output string '{}'", outputString);
|
||||||
}
|
}
|
||||||
//Decodes the inputString and stores the result in outputString
|
//Decodes the inputString and stores the result in outputString
|
||||||
private String decode(){
|
protected void decode(){
|
||||||
logger.debug("Decoding");
|
logger.debug("Decoding");
|
||||||
|
|
||||||
//Find the multiplicative inverse of key1
|
//Find the multiplicative inverse of key1
|
||||||
@@ -186,9 +189,8 @@ public class Affine{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Save and return the output
|
//Save and return the output
|
||||||
logger.debug("Saving output string '{}'", output);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
return outputString;
|
logger.debug("Saving output string '{}'", outputString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -211,14 +213,16 @@ public class Affine{
|
|||||||
setKey1(key1);
|
setKey1(key1);
|
||||||
setKey2(key2);
|
setKey2(key2);
|
||||||
setInputString(inputString);
|
setInputString(inputString);
|
||||||
return encode();
|
encode();
|
||||||
|
return outputString;
|
||||||
}
|
}
|
||||||
//Decodes inputString using key1 and key2 and returns the result
|
//Decodes inputString using key1 and key2 and returns the result
|
||||||
public String decode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
|
public String decode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||||
setKey1(key1);
|
setKey1(key1);
|
||||||
setKey2(key2);
|
setKey2(key2);
|
||||||
setInputString(inputString);
|
setInputString(inputString);
|
||||||
return decode();
|
decode();
|
||||||
|
return outputString;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Returns the cleaned inputString
|
//Returns the cleaned inputString
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java
|
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 07-25-21
|
// Created: 07-25-21
|
||||||
//Modified: 07-09-22
|
//Modified: 04-15-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
@@ -12,17 +12,17 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
|||||||
|
|
||||||
|
|
||||||
public class Atbash{
|
public class Atbash{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Atbash.class);
|
protected static Logger logger = LoggerFactory.getLogger(Atbash.class);
|
||||||
|
|
||||||
private String inputString; //Holds the string that needs encoded or decoded
|
protected String inputString; //Holds the string that needs encoded or decoded
|
||||||
private String outputString; //The encoded/decoded string
|
protected String outputString; //The encoded/decoded string
|
||||||
private boolean preserveCapitals; //Whether to respect capitals in the output string
|
protected boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||||
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||||
private boolean preserveSymbols; //Whether to respect symbols in the output string
|
protected boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||||
|
|
||||||
|
|
||||||
//Encodes inputString and stores in outputString
|
//Encodes inputString and stores in outputString
|
||||||
private String encode(){
|
protected String encode(){
|
||||||
logger.debug("Encoding");
|
logger.debug("Encoding");
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
//Step through every element in the inputString and shift it the correct amount
|
//Step through every element in the inputString and shift it the correct amount
|
||||||
@@ -52,9 +52,9 @@ public class Atbash{
|
|||||||
return outputString;
|
return outputString;
|
||||||
}
|
}
|
||||||
//Removes all invalid characters and sets inputString
|
//Removes all invalid characters and sets inputString
|
||||||
private void setInputString(String inputString) throws InvalidInputException{
|
protected void setInputString(String inputString) throws InvalidInputException{
|
||||||
if(inputString == null){
|
if(inputString == null){
|
||||||
throw new NullPointerException("Input cannot be null");
|
throw new InvalidInputException("Input cannot be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Original input string '{}'", inputString);
|
logger.debug("Original input string '{}'", inputString);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
|
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 07-25-21
|
// Created: 07-25-21
|
||||||
//Modified: 07-09-22
|
//Modified: 04-15-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
@@ -13,10 +13,10 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
|||||||
|
|
||||||
|
|
||||||
public class Autokey extends Vigenere{
|
public class Autokey extends Vigenere{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Autokey.class);
|
protected static Logger logger = LoggerFactory.getLogger(Autokey.class);
|
||||||
|
|
||||||
//Special rules for setting the strings for encoding
|
//Special rules for setting the strings for encoding
|
||||||
private void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
protected void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||||
logger.debug("Setting fields for encoding");
|
logger.debug("Setting fields for encoding");
|
||||||
|
|
||||||
//Set the input
|
//Set the input
|
||||||
@@ -44,7 +44,7 @@ public class Autokey extends Vigenere{
|
|||||||
setOffset();
|
setOffset();
|
||||||
}
|
}
|
||||||
//Setting the strings for decoding
|
//Setting the strings for decoding
|
||||||
private void decodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
protected void decodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||||
logger.debug("Setting fields for decoding");
|
logger.debug("Setting fields for decoding");
|
||||||
|
|
||||||
//Remove all unneccessary elements from the key
|
//Remove all unneccessary elements from the key
|
||||||
@@ -88,11 +88,6 @@ public class Autokey extends Vigenere{
|
|||||||
|
|
||||||
letter += 26;
|
letter += 26;
|
||||||
}
|
}
|
||||||
else if(letter > 'Z'){
|
|
||||||
logger.debug("Wrapping around to A");
|
|
||||||
|
|
||||||
letter -= 26;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if(Character.isLowerCase(letter)){
|
else if(Character.isLowerCase(letter)){
|
||||||
logger.debug("Appending lowercase");
|
logger.debug("Appending lowercase");
|
||||||
@@ -103,11 +98,6 @@ public class Autokey extends Vigenere{
|
|||||||
|
|
||||||
letter += 26;
|
letter += 26;
|
||||||
}
|
}
|
||||||
else if(letter > 'z'){
|
|
||||||
logger.debug("Wrapping around to a");
|
|
||||||
|
|
||||||
letter -= 26;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Decoded letter {}", letter);
|
logger.debug("Decoded letter {}", letter);
|
||||||
@@ -117,8 +107,8 @@ public class Autokey extends Vigenere{
|
|||||||
fullOutput.append(currentOutput);
|
fullOutput.append(currentOutput);
|
||||||
|
|
||||||
//Save and return the results
|
//Save and return the results
|
||||||
logger.debug("Saving output string '{}'", fullOutput);
|
|
||||||
outputString = fullOutput.toString();
|
outputString = fullOutput.toString();
|
||||||
|
logger.debug("Saving output string '{}'", outputString);
|
||||||
return outputString;
|
return outputString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
|
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 01-12-22
|
// Created: 01-12-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-15-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
@@ -17,21 +17,21 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
|||||||
|
|
||||||
|
|
||||||
public class Baconian{
|
public class Baconian{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Baconian.class);
|
protected static Logger logger = LoggerFactory.getLogger(Baconian.class);
|
||||||
|
|
||||||
//Conversions
|
//Conversions
|
||||||
private static final ArrayList<String> code = new ArrayList<>(Arrays.asList(
|
protected static final ArrayList<String> code = new ArrayList<>(Arrays.asList(
|
||||||
"aaaaa", "aaaab", "aaaba", "aaabb", "aabaa", "aabab", "aabba","aabbb", "abaaa", "abaaa", "abaab", "ababa", "ababb", //A-M
|
"aaaaa", "aaaab", "aaaba", "aaabb", "aabaa", "aabab", "aabba","aabbb", "abaaa", "abaaa", "abaab", "ababa", "ababb", //A-M
|
||||||
"abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z
|
"abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z
|
||||||
));
|
));
|
||||||
private String inputString; //The string that needs encoded/decoded
|
protected String inputString; //The string that needs encoded/decoded
|
||||||
private String outputString; //The encoded/decoded string
|
protected String outputString; //The encoded/decoded string
|
||||||
private boolean preserveCapitals; //Whether to respect capitals in the output string
|
protected boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||||
|
|
||||||
//Sets the input string
|
//Sets the input string
|
||||||
private void setInputStringEncode(String inputString) throws InvalidInputException{
|
protected void setInputStringEncode(String inputString) throws InvalidInputException{
|
||||||
if(inputString == null){
|
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);
|
logger.debug("Setting input string for encoding '{}'", inputString);
|
||||||
@@ -52,9 +52,12 @@ public class Baconian{
|
|||||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||||
if(inputString == null){
|
if(inputString == null){
|
||||||
throw new NullPointerException("Input cannot be null");
|
throw new InvalidInputException("Input cannot be null");
|
||||||
|
}
|
||||||
|
else if(inputString.isBlank()){
|
||||||
|
throw new InvalidInputException("Input cannot be empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Setting input string for decoding '{}'", inputString);
|
logger.debug("Setting input string for decoding '{}'", inputString);
|
||||||
@@ -86,13 +89,9 @@ public class Baconian{
|
|||||||
this.inputString = inputString;
|
this.inputString = inputString;
|
||||||
|
|
||||||
logger.debug("Cleaned input string '{}'", inputString);
|
logger.debug("Cleaned input string '{}'", inputString);
|
||||||
|
|
||||||
if(this.inputString.isBlank()){
|
|
||||||
throw new InvalidInputException("Input cannot be empty");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//Encodes the inputString and stores the result in outputString
|
//Encodes the inputString and stores the result in outputString
|
||||||
private String encode(){
|
protected void encode(){
|
||||||
logger.debug("Encoding");
|
logger.debug("Encoding");
|
||||||
StringJoiner output = new StringJoiner(" ");
|
StringJoiner output = new StringJoiner(" ");
|
||||||
//Go through every character in the inputString and encode it
|
//Go through every character in the inputString and encode it
|
||||||
@@ -113,17 +112,16 @@ public class Baconian{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Add the encoded character to the output
|
//Add the encoded character to the output
|
||||||
logger.debug("Output 'letter' {}", binary);
|
logger.debug("Output letter {}", binary);
|
||||||
output.add(binary);
|
output.add(binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Save and return the output
|
//Save and return the output
|
||||||
logger.debug("Saving output string '{}'", output);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
return outputString;
|
logger.debug("Saving output string '{}'", outputString);
|
||||||
}
|
}
|
||||||
//Decodes the inputString and stores the result in outputString
|
//Decodes the inputString and stores the result in outputString
|
||||||
private String decode(){
|
protected String decode(){
|
||||||
logger.debug("Decoding");
|
logger.debug("Decoding");
|
||||||
|
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
@@ -154,8 +152,8 @@ public class Baconian{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Save and return the output
|
//Save and return the output
|
||||||
logger.debug("Saving output string '{}'", output);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
|
logger.debug("Saving output string '{}'", outputString);
|
||||||
return outputString;
|
return outputString;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,7 +178,8 @@ public class Baconian{
|
|||||||
public String encode(String inputString) throws InvalidInputException{
|
public String encode(String inputString) throws InvalidInputException{
|
||||||
reset();
|
reset();
|
||||||
setInputStringEncode(inputString);
|
setInputStringEncode(inputString);
|
||||||
return encode();
|
encode();
|
||||||
|
return outputString;
|
||||||
}
|
}
|
||||||
//Sets the inputString and decodes the message
|
//Sets the inputString and decodes the message
|
||||||
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/BaseX.java
|
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/BaseX.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 01-08-22
|
// Created: 01-08-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-16-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
@@ -16,16 +16,16 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
|
|||||||
|
|
||||||
|
|
||||||
public class BaseX{
|
public class BaseX{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(BaseX.class);
|
protected static Logger logger = LoggerFactory.getLogger(BaseX.class);
|
||||||
|
|
||||||
private String inputString; //The string that needs encoded/decoded
|
protected String inputString; //The string that needs encoded/decoded
|
||||||
private String outputString; //The encoded/decoded string
|
protected String outputString; //The encoded/decoded string
|
||||||
private int base; //The base that the number will be encoded at
|
protected int base; //The base that the number will be encoded at
|
||||||
|
|
||||||
//Sets the input string
|
//Sets the input string
|
||||||
private void setInputStringEncode(String inputString) throws InvalidInputException{
|
protected void setInputStringEncode(String inputString) throws InvalidInputException{
|
||||||
if(inputString == null){
|
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);
|
logger.debug("Setting input string for encoding '{}'", inputString);
|
||||||
@@ -36,9 +36,9 @@ public class BaseX{
|
|||||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||||
if(inputString == null){
|
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);
|
logger.debug("Setting input string for decoding '{}'", inputString);
|
||||||
@@ -67,9 +67,12 @@ public class BaseX{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Sets the numeric base
|
//Sets the numeric base
|
||||||
private void setBase(int base) throws InvalidBaseException{
|
protected void setBase(int base) throws InvalidBaseException{
|
||||||
if(base <= 0){
|
if(base < Character.MIN_RADIX){
|
||||||
throw new InvalidBaseException("Base cannot be a negative number");
|
throw new InvalidBaseException("Base cannot be less than " + Character.MIN_RADIX);
|
||||||
|
}
|
||||||
|
else if(base > Character.MAX_RADIX){
|
||||||
|
throw new InvalidBaseException("Base cannot be larger than " + Character.MAX_RADIX);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Setting base {}", base);
|
logger.debug("Setting base {}", base);
|
||||||
@@ -77,7 +80,7 @@ public class BaseX{
|
|||||||
this.base = base;
|
this.base = base;
|
||||||
}
|
}
|
||||||
//Encode inputString, store it in outputString, and return it
|
//Encode inputString, store it in outputString, and return it
|
||||||
private String encode(){
|
protected String encode(){
|
||||||
logger.debug("Encoding");
|
logger.debug("Encoding");
|
||||||
|
|
||||||
//Encode every character in inputString
|
//Encode every character in inputString
|
||||||
@@ -101,7 +104,7 @@ public class BaseX{
|
|||||||
return outputString;
|
return outputString;
|
||||||
}
|
}
|
||||||
//Decode inputString, store it in outputString, and return it
|
//Decode inputString, store it in outputString, and return it
|
||||||
private String decode() throws InvalidCharacterException{
|
protected String decode() throws InvalidCharacterException{
|
||||||
logger.debug("Decoding");
|
logger.debug("Decoding");
|
||||||
|
|
||||||
//Decode every binary number in the string
|
//Decode every binary number in the string
|
||||||
@@ -114,8 +117,8 @@ public class BaseX{
|
|||||||
logger.debug("Decoded number {}", num);
|
logger.debug("Decoded number {}", num);
|
||||||
|
|
||||||
//Make sure it is in a valid range
|
//Make sure it is in a valid range
|
||||||
if((num < 0) && (num > 255)){
|
if((num < 0) || (num > 255)){
|
||||||
throw new InvalidCharacterException("The base" + base + " string '" + baseXString + "' is not a valid character");
|
throw new InvalidCharacterException("The base" + base + " string '" + baseXString + "' is not a valid ASCII character");
|
||||||
}
|
}
|
||||||
|
|
||||||
//Convert the int to a char and save it
|
//Convert the int to a char and save it
|
||||||
@@ -123,8 +126,8 @@ public class BaseX{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Save the output
|
//Save the output
|
||||||
logger.debug("Saving output string '{}'", output);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
|
logger.debug("Saving output string '{}'", outputString);
|
||||||
|
|
||||||
//Return the output
|
//Return the output
|
||||||
return outputString;
|
return outputString;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Beaufort.java
|
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Beaufort.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 02-23-22
|
// Created: 02-23-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-16-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
@@ -13,19 +13,19 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
|||||||
|
|
||||||
|
|
||||||
public class Beaufort{
|
public class Beaufort{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Beaufort.class);
|
protected static Logger logger = LoggerFactory.getLogger(Beaufort.class);
|
||||||
|
|
||||||
//Fields
|
//Fields
|
||||||
private String inputString; //This is the string that needs encoded/decoded
|
protected String inputString; //This is the string that needs encoded/decoded
|
||||||
private String outputString; //This is the string that is output after encoding/decoding
|
protected String outputString; //This is the string that is output after encoding/decoding
|
||||||
private String keyword; //This is the keyword that is responsible for determining the offsets that you change each character by
|
protected String keyword; //This is the keyword that is responsible for determining the offsets that you change each character by
|
||||||
private boolean preserveCapitals; //Whether to respect capitals in the output string
|
protected boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||||
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||||
private boolean preserveSymbols; //Whether to respect symbols in the output string
|
protected boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||||
//Internal ciphers
|
//Internal ciphers
|
||||||
private Atbash atbash; //The first step in encoding/decoding the cipher
|
protected Atbash atbash; //The first step in encoding/decoding the cipher
|
||||||
private Caesar caesar; //The second step in encoding/decoding the cipher
|
protected Caesar caesar; //The second step in encoding/decoding the cipher
|
||||||
private Vigenere vigenere; //The third step in encoding/decoding the cipher
|
protected Vigenere vigenere; //The third step in encoding/decoding the cipher
|
||||||
|
|
||||||
//Ensures inputString constraints
|
//Ensures inputString constraints
|
||||||
public void setInputString(String inputString) throws InvalidInputException{
|
public void setInputString(String inputString) throws InvalidInputException{
|
||||||
@@ -88,9 +88,20 @@ public class Beaufort{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Encodes the inputString and stores the result in outputString
|
//Encodes the inputString and stores the result in outputString
|
||||||
public void encode() throws InvalidKeywordException, InvalidInputException{
|
protected void encode() throws InvalidKeywordException, InvalidInputException{
|
||||||
logger.debug("Encoding");
|
logger.debug("Encoding");
|
||||||
|
|
||||||
|
code();
|
||||||
|
}
|
||||||
|
//Decodes the inputString and stores the result in outputString
|
||||||
|
protected void decode() throws InvalidKeywordException, InvalidInputException{
|
||||||
|
logger.debug("Decoding");
|
||||||
|
|
||||||
|
//Decoding is just encoding again
|
||||||
|
code();
|
||||||
|
}
|
||||||
|
//Codes input and saves to output
|
||||||
|
protected void code(){
|
||||||
//Reverse the string
|
//Reverse the string
|
||||||
logger.debug("Encoding with Atbash");
|
logger.debug("Encoding with Atbash");
|
||||||
String atbashString = atbash.encode(inputString);
|
String atbashString = atbash.encode(inputString);
|
||||||
@@ -106,13 +117,6 @@ public class Beaufort{
|
|||||||
logger.debug("Saving output string '{}'", vigenereString);
|
logger.debug("Saving output string '{}'", vigenereString);
|
||||||
this.outputString = vigenereString;
|
this.outputString = vigenereString;
|
||||||
}
|
}
|
||||||
//Decodes the inputString and stores the result in outputString
|
|
||||||
public void decode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
logger.debug("Decoding");
|
|
||||||
|
|
||||||
//Decoding is just encoding again
|
|
||||||
encode();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Constructor
|
//Constructor
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Caesar.java
|
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Caesar.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 07-25-21
|
// Created: 07-25-21
|
||||||
//Modified: 07-09-22
|
//Modified: 04-16-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
@@ -12,18 +12,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
|||||||
|
|
||||||
|
|
||||||
public class Caesar{
|
public class Caesar{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Caesar.class);
|
protected static Logger logger = LoggerFactory.getLogger(Caesar.class);
|
||||||
|
|
||||||
//Fields
|
//Fields
|
||||||
private String inputString; //The string that needs encoded/decoded
|
protected String inputString; //The string that needs encoded/decoded
|
||||||
private String outputString; //The encoded/decoded string
|
protected String outputString; //The encoded/decoded string
|
||||||
private int shift; //The amount that you need to shift each letter
|
protected int shift; //The amount that you need to shift each letter
|
||||||
private boolean preserveCapitals; //Whether to respect capitals in the output string
|
protected boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||||
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||||
private boolean preserveSymbols; //Whether to respect symbols in the output string
|
protected boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||||
|
|
||||||
//Sets shift and makes sure it is within the propper bounds
|
//Sets shift and makes sure it is within the propper bounds
|
||||||
private void setShift(int shiftAmount){
|
protected void setShift(int shiftAmount){
|
||||||
logger.debug("Setting shift {}", shiftAmount);
|
logger.debug("Setting shift {}", shiftAmount);
|
||||||
|
|
||||||
//If you shift more than 26 you will just be wrapping back around again
|
//If you shift more than 26 you will just be wrapping back around again
|
||||||
@@ -32,9 +32,9 @@ public class Caesar{
|
|||||||
logger.debug("Cleaned shift {}", shift);
|
logger.debug("Cleaned shift {}", shift);
|
||||||
}
|
}
|
||||||
//Sets the input string
|
//Sets the input string
|
||||||
private void setInputString(String inputString) throws InvalidInputException{
|
protected void setInputString(String inputString) throws InvalidInputException{
|
||||||
if(inputString == null){
|
if(inputString == null){
|
||||||
throw new NullPointerException("Input cannot be null");
|
throw new InvalidInputException("Input cannot be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Original input string '{}'", inputString);
|
logger.debug("Original input string '{}'", inputString);
|
||||||
@@ -63,7 +63,7 @@ public class Caesar{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Encodes the inputString and stores the result in outputString
|
//Encodes the inputString and stores the result in outputString
|
||||||
private String encode(){
|
protected String encode(){
|
||||||
logger.debug("Encoding");
|
logger.debug("Encoding");
|
||||||
|
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
@@ -107,12 +107,12 @@ public class Caesar{
|
|||||||
output.append(currentChar);
|
output.append(currentChar);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Saving encoded string '{}'", output);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
|
logger.debug("Saving encoded string '{}'", outputString);
|
||||||
return outputString;
|
return outputString;
|
||||||
}
|
}
|
||||||
//Decodes the inputString and stores the result in outputString
|
//Decodes the inputString and stores the result in outputString
|
||||||
private String decode(){
|
protected String decode(){
|
||||||
logger.debug("Decoding");
|
logger.debug("Decoding");
|
||||||
|
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
@@ -156,11 +156,12 @@ public class Caesar{
|
|||||||
}
|
}
|
||||||
//If it is whitespace, number, or punctuation just let it pass through
|
//If it is whitespace, number, or punctuation just let it pass through
|
||||||
//Add it to the output string
|
//Add it to the output string
|
||||||
|
logger.debug("Decoded character {}", currentChar);
|
||||||
output.append(currentChar);
|
output.append(currentChar);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Saving decoded string '{}'", output);
|
|
||||||
outputString = output.toString();
|
outputString = output.toString();
|
||||||
|
logger.debug("Saving decoded string '{}'", outputString);
|
||||||
return outputString;
|
return outputString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
|||||||
|
|
||||||
|
|
||||||
public class Vigenere{
|
public class Vigenere{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Vigenere.class);
|
protected static Logger logger = LoggerFactory.getLogger(Vigenere.class);
|
||||||
|
|
||||||
//Fields
|
//Fields
|
||||||
protected String inputString; //This is the string that needs encoded/decoded
|
protected String inputString; //This is the string that needs encoded/decoded
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
|||||||
|
|
||||||
|
|
||||||
public class PolybiusSquare{
|
public class PolybiusSquare{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
|
protected static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
|
||||||
|
|
||||||
//A class representing the location of a character in the grid
|
//A class representing the location of a character in the grid
|
||||||
protected class CharLocation{
|
protected class CharLocation{
|
||||||
|
|||||||
@@ -1,450 +1,293 @@
|
|||||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/combination/TestADFGVX.java
|
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/combination/TestADFGVX.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 01-26-22
|
// Created: 01-26-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-14-23
|
||||||
package com.mattrixwv.cipherstream.combination;
|
package com.mattrixwv.cipherstream.combination;
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||||
|
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.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
import com.mattrixwv.cipherstream.combination.ADFGVX.LargePolybiusSquare;
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||||
|
import com.mattrixwv.cipherstream.polysubstitution.Columnar;
|
||||||
|
|
||||||
|
|
||||||
public class TestADFGVX{
|
public class TestADFGVX{
|
||||||
@Test
|
private ADFGVX cipher;
|
||||||
public void testEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
|
private Logger logger;
|
||||||
ADFGVX cipher = new ADFGVX(true, true, true);
|
//Variables
|
||||||
|
private String encodedString = "Message to^encode";
|
||||||
|
private String encodedStringClean = "MESSAGETOENCODE";
|
||||||
|
private String decodedString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
|
||||||
|
private String decodedStringClean = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
|
||||||
|
private String keyword = "keyword";
|
||||||
|
private String squareKeyword = "SquareKeyword";
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String squareKeyword = "SquareKeyword";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "axgvdavfxgagfaafagaaxdxfgdagda";
|
|
||||||
String output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
@BeforeEach
|
||||||
inputString = "message to encode";
|
public void setup(){
|
||||||
squareKeyword = "SquareKeyword";
|
cipher = new ADFGVX();
|
||||||
keyword = "keyword";
|
logger = mock(Logger.class);
|
||||||
correctOutput = "axgvdavfxgagfa afag aaxdxfgdagda";
|
ADFGVX.logger = logger;
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "axgvdavfxgagfa*afag+aaxdxfgdagda";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to-encode";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AXgvdavfxgagfa afag-aaxdxfgdagda";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
|
|
||||||
ADFGX cipher = new ADFGX(false, true, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String squareKeyword = "SquareKeyword";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
|
||||||
String output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAGAGADFAGAXXD AXDX ADAFAFXDDGDF";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode-";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAGAGADFAGAXXD*AXDX+ADAFAFXDDGDF-";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAGAGADFAGAXXD AXDX^ADAFAFXDDGDF";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoWhitespaceEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
|
|
||||||
ADFGX cipher = new ADFGX(true, false, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String squareKeyword = "SquareKeyword";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "aagagadfagaxxdaxdxadafafxddgdf";
|
|
||||||
String output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "aagagadfagaxxdaxdxadafafxddgdf";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode-";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "aagagadfagaxxd*axdx+adafafxddgdf-";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAgagadfagaxxdaxdx^adafafxddgdf";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoSymbolEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
|
|
||||||
ADFGX cipher = new ADFGX(true, true, false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String squareKeyword = "SquareKeyword";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "aagagadfagaxxdaxdxadafafxddgdf";
|
|
||||||
String output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "aagagadfagaxxd axdx adafafxddgdf";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode-";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "aagagadfagaxxdaxdxadafafxddgdf";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAgagadfagaxxd axdxadafafxddgdf";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
|
|
||||||
ADFGX cipher = new ADFGX(false, false, false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String squareKeyword = "SquareKeyword";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
|
||||||
String output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode-";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
|
||||||
output = cipher.encode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
|
public void testSquareKeyword(){
|
||||||
ADFGVX cipher = new ADFGVX(true, true, true);
|
assertThrows(InvalidKeywordException.class, () -> {
|
||||||
|
cipher.setSquareKeyword(null);
|
||||||
|
});
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher.setSquareKeyword(squareKeyword);
|
||||||
String inputString = "axgvdavfxgagfaafagaaxdxfgdagda";
|
assertEquals(squareKeyword, cipher.squareKeyword);
|
||||||
String squareKeyword = "SquareKeyword";
|
verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword);
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "messagetoencode";
|
|
||||||
String output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "axgvdavfxgagfa afag aaxdxfgdagda";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "message to encode";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "message*to+encode-";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Message to^encode";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
|
public void testSetKeyword(){
|
||||||
ADFGVX cipher = new ADFGVX(false, true, true);
|
assertThrows(InvalidKeywordException.class, () -> {
|
||||||
|
cipher.setKeyword(null);
|
||||||
|
});
|
||||||
|
assertEquals("", cipher.keyword);
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
|
||||||
//Test lowercase decoding
|
|
||||||
String inputString = "axgvdavfxgagfaafagaaxdxfgdagda";
|
|
||||||
String squareKeyword = "SquareKeyword";
|
|
||||||
String keyword = "keyword";
|
String keyword = "keyword";
|
||||||
String correctOutput = "MESSAGETOENCODE";
|
cipher.setKeyword(keyword);
|
||||||
String output = cipher.decode(squareKeyword, keyword, inputString);
|
assertEquals(keyword, cipher.keyword);
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(1)).debug("keyword = {}", keyword);
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "axgvdavfxgagfa afag aaxdxfgdagda";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGE TO ENCODE";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGE*TO+ENCODE-";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGE TO^ENCODE";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoWhitespaceDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
|
public void testSetInputString(){
|
||||||
ADFGVX cipher = new ADFGVX(true, false, true);
|
//Null input
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputString(null);
|
||||||
|
});
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
|
||||||
//Test lowercase decoding
|
String originalInputString = "Original input string '{}'";
|
||||||
String inputString = "axgvdavfxgagfaafagaaxdxfgdagda";
|
String cleanedInputString = "Cleaned input string '{}'";
|
||||||
String squareKeyword = "SquareKeyword";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "messagetoencode";
|
|
||||||
String output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
//Blank input
|
||||||
inputString = "axgvdavfxgagfa afag aaxdxfgdagda";
|
cipher.preserveCapitals = true;
|
||||||
squareKeyword = "SquareKeyword";
|
cipher.preserveSymbols = true;
|
||||||
keyword = "keyword";
|
cipher.preserveWhitespace = true;
|
||||||
correctOutput = "messagetoencode";
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
cipher.setInputString("");
|
||||||
assertEquals(correctOutput, output);
|
});
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug(originalInputString, "");
|
||||||
|
|
||||||
//Test symbol decoding
|
//No options
|
||||||
inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-";
|
String inputString = "input String*";
|
||||||
squareKeyword = "SquareKeyword";
|
cipher.setInputString(inputString);
|
||||||
keyword = "keyword";
|
assertEquals(inputString, cipher.inputString);
|
||||||
correctOutput = "message*to+encode-";
|
verify(logger, times(1)).debug(originalInputString, inputString);
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
verify(logger, times(1)).debug(cleanedInputString, inputString);
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
//capitals
|
||||||
inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
|
cipher.preserveCapitals = false;
|
||||||
squareKeyword = "SquareKeyword";
|
cipher.preserveWhitespace = true;
|
||||||
keyword = "keyword";
|
cipher.preserveSymbols = true;
|
||||||
correctOutput = "Messageto^encode";
|
inputString = "input String*";
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
cipher.setInputString(inputString);
|
||||||
assertEquals(correctOutput, output);
|
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||||
|
verify(logger, times(2)).debug(originalInputString, inputString);
|
||||||
|
verify(logger, times(1)).debug("Removing capitals");
|
||||||
|
verify(logger, times(1)).debug(cleanedInputString, inputString.toUpperCase());
|
||||||
|
|
||||||
|
//whitespace
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveWhitespace = false;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
inputString = "input String*";
|
||||||
|
cipher.setInputString(inputString);
|
||||||
|
assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString);
|
||||||
|
verify(logger, times(3)).debug(originalInputString, inputString);
|
||||||
|
verify(logger, times(1)).debug("Removing whitespace");
|
||||||
|
verify(logger, times(1)).debug(cleanedInputString, inputString.replaceAll("\\s", ""));
|
||||||
|
|
||||||
|
//symbols
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
cipher.preserveSymbols = false;
|
||||||
|
inputString = "input String*";
|
||||||
|
cipher.setInputString(inputString);
|
||||||
|
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||||
|
verify(logger, times(4)).debug(originalInputString, inputString);
|
||||||
|
verify(logger, times(1)).debug("Removing symbols");
|
||||||
|
verify(logger, times(1)).debug(cleanedInputString, inputString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoSymbolDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
|
public void testFormateOutputStringEncode(){
|
||||||
ADFGVX cipher = new ADFGVX(true, true, false);
|
cipher.inputString = encodedString;
|
||||||
|
cipher.outputString = decodedStringClean;
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher.formatOutputStringEncode();
|
||||||
String inputString = "axgvdavfxgagfaafagaaxdxfgdagda";
|
assertEquals(decodedString, cipher.outputString);
|
||||||
String squareKeyword = "SquareKeyword";
|
verify(logger, times(1)).debug("Formatting output string to match input string");
|
||||||
String keyword = "keyword";
|
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
|
||||||
String correctOutput = "messagetoencode";
|
verify(logger, times(1)).debug("Converting output to uppercase");
|
||||||
String output = cipher.decode(squareKeyword, keyword, inputString);
|
verify(logger, times(14)).debug("Converting output to lowercase");
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(2)).debug("Appending symbol to output");
|
||||||
//Test uppercase decoding
|
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
|
||||||
inputString = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "axgvdavfxgagfa afag aaxdxfgdagda";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "message to encode";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Message toencode";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
|
public void testFormatOutputStringDecode(){
|
||||||
ADFGVX cipher = new ADFGVX(false, false, false);
|
cipher.outputString = encodedStringClean;
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher.formatOutputStringDecode();
|
||||||
String inputString = "axgvdavfxgagfaafagaaxdxfgdagda";
|
assertEquals(encodedString, cipher.outputString);
|
||||||
String squareKeyword = "SquareKeyword";
|
verify(logger, times(1)).debug("Formatting output string to match input string");
|
||||||
String keyword = "keyword";
|
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
|
||||||
String correctOutput = "MESSAGETOENCODE";
|
verify(logger, times(1)).debug("Converting output to uppercase");
|
||||||
String output = cipher.decode(squareKeyword, keyword, inputString);
|
verify(logger, times(14)).debug("Converting output to lowercase");
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(2)).debug("Appending symbol to output");
|
||||||
//Test uppercase decoding
|
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||||
inputString = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
|
}
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
@Test
|
||||||
inputString = "axgvdavfxgagfa afag aaxdxfgdagda";
|
public void testEncode(){
|
||||||
squareKeyword = "SquareKeyword";
|
cipher.inputString = encodedString;
|
||||||
keyword = "keyword";
|
cipher.keyword = keyword;
|
||||||
correctOutput = "MESSAGETOENCODE";
|
cipher.squareKeyword = squareKeyword;
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
cipher.encode();
|
||||||
inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-";
|
|
||||||
squareKeyword = "SquareKeyword";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
assertEquals(decodedString, cipher.outputString);
|
||||||
inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
|
verify(logger, times(1)).debug("Encoding using Polybius Square");
|
||||||
squareKeyword = "SquareKeyword";
|
verify(logger, times(1)).debug("Replacing coordinates with letters");
|
||||||
keyword = "keyword";
|
verify(logger, times(1)).debug("Encoding using columnar");
|
||||||
correctOutput = "MESSAGETOENCODE";
|
}
|
||||||
output = cipher.decode(squareKeyword, keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
@Test
|
||||||
|
public void testDecode(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.keyword = keyword;
|
||||||
|
cipher.squareKeyword = squareKeyword;
|
||||||
|
|
||||||
|
cipher.decode();
|
||||||
|
|
||||||
|
assertEquals(encodedString, 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(){
|
||||||
|
cipher = new ADFGVX();
|
||||||
|
assertFalse(cipher.preserveCapitals);
|
||||||
|
assertFalse(cipher.preserveWhitespace);
|
||||||
|
assertFalse(cipher.preserveSymbols);
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
assertNotNull(cipher.largePolybiusSquare);
|
||||||
|
assertNotNull(cipher.columnar);
|
||||||
|
|
||||||
|
cipher = new ADFGVX(true, false, false);
|
||||||
|
assertTrue(cipher.preserveCapitals);
|
||||||
|
assertFalse(cipher.preserveSymbols);
|
||||||
|
assertFalse(cipher.preserveWhitespace);
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
assertNotNull(cipher.largePolybiusSquare);
|
||||||
|
assertNotNull(cipher.columnar);
|
||||||
|
|
||||||
|
cipher = new ADFGVX(false, true, false);
|
||||||
|
assertFalse(cipher.preserveCapitals);
|
||||||
|
assertFalse(cipher.preserveSymbols);
|
||||||
|
assertTrue(cipher.preserveWhitespace);
|
||||||
|
|
||||||
|
cipher = new ADFGVX(false, false, true);
|
||||||
|
assertFalse(cipher.preserveCapitals);
|
||||||
|
assertTrue(cipher.preserveSymbols);
|
||||||
|
assertFalse(cipher.preserveWhitespace);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetters(){
|
||||||
|
cipher.inputString = encodedString;
|
||||||
|
cipher.outputString = decodedString;
|
||||||
|
cipher.squareKeyword = squareKeyword;
|
||||||
|
cipher.keyword = keyword;
|
||||||
|
|
||||||
|
assertEquals(encodedString, cipher.getInputString());
|
||||||
|
assertEquals(decodedString, cipher.getOutputString());
|
||||||
|
assertEquals(squareKeyword, cipher.getSquareKeyword());
|
||||||
|
assertEquals(keyword, cipher.getKeyword());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReset(){
|
||||||
|
LargePolybiusSquare ps = cipher.largePolybiusSquare;
|
||||||
|
Columnar columnar = cipher.columnar;
|
||||||
|
cipher.inputString = encodedString;
|
||||||
|
cipher.outputString = decodedString;
|
||||||
|
cipher.squareKeyword = squareKeyword;
|
||||||
|
cipher.keyword = keyword;
|
||||||
|
|
||||||
|
cipher.reset();
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
assertEquals("", cipher.keyword);
|
||||||
|
assertNotSame(ps, cipher.largePolybiusSquare);
|
||||||
|
assertNotSame(columnar, cipher.columnar);
|
||||||
|
verify(logger, times(1)).debug("Resetting fields");
|
||||||
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
cipher = new ADFGVX(false, false, false);
|
||||||
|
output = cipher.encode(squareKeyword, keyword, encodedString);
|
||||||
|
assertEquals(decodedStringClean, 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
|
||||||
|
cipher = new ADFGVX(false, false, false);
|
||||||
|
output = cipher.decode(squareKeyword, keyword, decodedString);
|
||||||
|
assertEquals(encodedStringClean, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,14 @@ package com.mattrixwv.cipherstream.combination;
|
|||||||
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyChar;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
@@ -15,23 +21,29 @@ import static org.mockito.Mockito.verify;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||||
|
import com.mattrixwv.cipherstream.polysubstitution.Columnar;
|
||||||
|
import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
|
||||||
|
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
|
||||||
public class TestADFGX{
|
public class TestADFGX{
|
||||||
private ADFGX cipher;
|
private ADFGX cipher;
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
|
//Variables
|
||||||
|
private String decodedString = "Message to^encode";
|
||||||
|
private String decodedStringClean = "MESSAGETOENCODE";
|
||||||
|
private String encodedString = "AAgagadfagaxxd axdx^adafafxddgdf";
|
||||||
|
private String encodedStringClean = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
||||||
|
private String keyword = "keyword";
|
||||||
|
private String squareKeyword = "SquareKeyword";
|
||||||
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setup(){
|
public void setup(){
|
||||||
cipher = new ADFGX();
|
cipher = new ADFGX(true, true, true);
|
||||||
logger = mock(Logger.class);
|
logger = mock(Logger.class);
|
||||||
ADFGX.logger = logger;
|
ADFGX.logger = logger;
|
||||||
}
|
}
|
||||||
@@ -45,7 +57,6 @@ public class TestADFGX{
|
|||||||
assertEquals("", cipher.squareKeyword);
|
assertEquals("", cipher.squareKeyword);
|
||||||
verify(logger, never()).debug(anyString(), anyString());
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
|
||||||
String squareKeyword = "squareKeyword";
|
|
||||||
cipher.setSquareKeyword(squareKeyword);
|
cipher.setSquareKeyword(squareKeyword);
|
||||||
assertEquals(squareKeyword, cipher.squareKeyword);
|
assertEquals(squareKeyword, cipher.squareKeyword);
|
||||||
verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword);
|
verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword);
|
||||||
@@ -74,8 +85,8 @@ public class TestADFGX{
|
|||||||
assertEquals("", cipher.inputString);
|
assertEquals("", cipher.inputString);
|
||||||
verify(logger, never()).debug(anyString(), anyString());
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
|
||||||
String originalInputString = "original input string '{}'";
|
String originalInputString = "Original input string '{}'";
|
||||||
String cleanedInputString = "cleaned input string '{}'";
|
String cleanedInputString = "Cleaned input string '{}'";
|
||||||
|
|
||||||
//Blank input
|
//Blank input
|
||||||
cipher.preserveCapitals = true;
|
cipher.preserveCapitals = true;
|
||||||
@@ -130,70 +141,153 @@ public class TestADFGX{
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFormatOutputStringEncode(){
|
public void testFormatOutputStringEncode(){
|
||||||
//TODO:
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedStringClean;
|
||||||
|
|
||||||
|
cipher.formatOutputStringEncode();
|
||||||
|
assertEquals(encodedString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Formatting output string to match input string");
|
||||||
|
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
|
||||||
|
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 '{}'", encodedString);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatOutputStringDecode(){
|
public void testFormatOutputStringDecode(){
|
||||||
//TODO:
|
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
|
@Test
|
||||||
public void testEncodePrivate(){
|
public void testEncode(){
|
||||||
//TODO:
|
cipher.inputString = decodedString;
|
||||||
|
cipher.keyword = keyword;
|
||||||
|
cipher.squareKeyword = squareKeyword;
|
||||||
|
|
||||||
|
cipher.encode();
|
||||||
|
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecodePrivate(){
|
public void testDecode(){
|
||||||
//TODO:
|
cipher.inputString = encodedString;
|
||||||
|
cipher.keyword = keyword;
|
||||||
|
cipher.squareKeyword = squareKeyword;
|
||||||
|
|
||||||
|
cipher.decode();
|
||||||
|
|
||||||
|
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
|
@Test
|
||||||
public void testConstructors(){
|
public void testConstructors(){
|
||||||
//TODO:
|
cipher = new ADFGX();
|
||||||
|
assertFalse(cipher.preserveCapitals);
|
||||||
|
assertFalse(cipher.preserveWhitespace);
|
||||||
|
assertFalse(cipher.preserveSymbols);
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
assertNotNull(cipher.polybiusSquare);
|
||||||
|
assertNotNull(cipher.columnar);
|
||||||
|
|
||||||
|
cipher = new ADFGX(true, false, false);
|
||||||
|
assertTrue(cipher.preserveCapitals);
|
||||||
|
assertFalse(cipher.preserveSymbols);
|
||||||
|
assertFalse(cipher.preserveWhitespace);
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
assertNotNull(cipher.polybiusSquare);
|
||||||
|
assertNotNull(cipher.columnar);
|
||||||
|
|
||||||
|
cipher = new ADFGX(false, true, false);
|
||||||
|
assertFalse(cipher.preserveCapitals);
|
||||||
|
assertFalse(cipher.preserveSymbols);
|
||||||
|
assertTrue(cipher.preserveWhitespace);
|
||||||
|
|
||||||
|
cipher = new ADFGX(false, false, true);
|
||||||
|
assertFalse(cipher.preserveCapitals);
|
||||||
|
assertTrue(cipher.preserveSymbols);
|
||||||
|
assertFalse(cipher.preserveWhitespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetters(){
|
public void testGetters(){
|
||||||
//TODO:
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
cipher.squareKeyword = squareKeyword;
|
||||||
|
cipher.keyword = keyword;
|
||||||
|
|
||||||
|
assertEquals(decodedString, cipher.getInputString());
|
||||||
|
assertEquals(encodedString, cipher.getOutputString());
|
||||||
|
assertEquals(squareKeyword, cipher.getSquareKeyword());
|
||||||
|
assertEquals(keyword, cipher.getKeyword());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReset(){
|
public void testReset(){
|
||||||
//TODO:
|
PolybiusSquare polybius = cipher.polybiusSquare;
|
||||||
|
Columnar columnar = cipher.columnar;
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
cipher.squareKeyword = squareKeyword;
|
||||||
|
cipher.keyword = keyword;
|
||||||
|
|
||||||
|
cipher.reset();
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals("", cipher.squareKeyword);
|
||||||
|
assertEquals("", cipher.keyword);
|
||||||
|
assertNotSame(polybius, cipher.polybiusSquare);
|
||||||
|
assertNotSame(columnar, cipher.columnar);
|
||||||
|
verify(logger, times(1)).debug("Resetting fields");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPracticalEncoding(){
|
public void testPracticalEncoding(){
|
||||||
//Test as original
|
//Test as original
|
||||||
cipher.preserveCapitals = true;
|
cipher = new ADFGX(true, true, true);
|
||||||
cipher.preserveWhitespace = true;
|
String output = cipher.encode(squareKeyword, keyword, decodedString);
|
||||||
cipher.preserveSymbols = true;
|
assertEquals(encodedString, output);
|
||||||
String output = cipher.encode("SquareKeyword", "keyword", "Message to^encode");
|
|
||||||
assertEquals("AAgagadfagaxxd axdx^adafafxddgdf", output);
|
|
||||||
|
|
||||||
//Test fully cleaned
|
//Test fully cleaned
|
||||||
cipher.preserveCapitals = false;
|
cipher = new ADFGX(false, false, false);
|
||||||
cipher.preserveWhitespace = false;
|
output = cipher.encode(squareKeyword, keyword, decodedString);
|
||||||
cipher.preserveSymbols = false;
|
assertEquals(encodedStringClean, output);
|
||||||
output = cipher.encode("SquareKeyword", "keyword", "Message to^encode");
|
|
||||||
assertEquals("AAGAGADFAGAXXDAXDXADAFAFXDDGDF", output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPracticalDecoding(){
|
public void testPracticalDecoding(){
|
||||||
//Test as original
|
//Test as original
|
||||||
cipher.preserveCapitals = true;
|
cipher = new ADFGX(true, true, true);
|
||||||
cipher.preserveWhitespace = true;
|
String output = cipher.decode(squareKeyword, keyword, encodedString);
|
||||||
cipher.preserveSymbols = true;
|
assertEquals(decodedString, output);
|
||||||
String output = cipher.decode("SquareKeyword", "keyword", "AAgagadfagaxxd axdx^adafafxddgdf");
|
|
||||||
assertEquals("Message to^encode", output);
|
|
||||||
|
|
||||||
//Test fully cleaned
|
//Test fully cleaned
|
||||||
cipher.preserveCapitals = false;
|
cipher = new ADFGX(false, false, false);
|
||||||
cipher.preserveWhitespace = false;
|
output = cipher.decode(squareKeyword, keyword, encodedString);
|
||||||
cipher.preserveSymbols = false;
|
assertEquals(decodedStringClean, output);
|
||||||
output = cipher.decode("SquareKeyword", "keyword", "AAgagadfagaxxd axdx^adafafxddgdf");
|
|
||||||
assertEquals("MESSAGETOENCODE", output);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,449 +1,342 @@
|
|||||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestAffine.java
|
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestAffine.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 01-26-22
|
// Created: 01-26-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-15-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyChar;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||||
|
|
||||||
|
|
||||||
public class TestAffine{
|
public class TestAffine{
|
||||||
@Test
|
private Affine cipher;
|
||||||
public void testEncode() throws InvalidKeywordException, InvalidInputException{
|
private Logger logger;
|
||||||
Affine cipher = new Affine(true, true, true);
|
//Variables
|
||||||
|
private String decodedString = "Message to^encode";
|
||||||
|
private String decodedStringClean = "messagetoencode";
|
||||||
|
private String encodedString = "Pbtthlb yz^burzwb";
|
||||||
|
private String encodedStringClean = "pbtthlbyzburzwb";
|
||||||
|
private int key1 = 5;
|
||||||
|
private int key2 = 7;
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
int key1 = 5;
|
|
||||||
int key2 = 7;
|
|
||||||
String correctOutput = "pbtthlbyzburzwb";
|
|
||||||
String output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "PBTTHLBYZBURZWB";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
@BeforeEach
|
||||||
inputString = "message to encode";
|
public void setup(){
|
||||||
key1 = 5;
|
cipher = new Affine();
|
||||||
key2 = 7;
|
logger = mock(Logger.class);
|
||||||
correctOutput = "pbtthlb yz burzwb";
|
Affine.logger = logger;
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode-";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlb*yz+burzwb-";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "Pbtthlb yz^burzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Affine cipher = new Affine(false, true, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
int key1 = 5;
|
|
||||||
int key2 = 7;
|
|
||||||
String correctOutput = "pbtthlbyzburzwb";
|
|
||||||
String output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlbyzburzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlb yz burzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode-";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlb*yz+burzwb-";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlb yz^burzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Affine cipher = new Affine(true, false, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
int key1 = 5;
|
|
||||||
int key2 = 7;
|
|
||||||
String correctOutput = "pbtthlbyzburzwb";
|
|
||||||
String output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "PBTTHLBYZBURZWB";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlbyzburzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode-";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlb*yz+burzwb-";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "Pbtthlbyz^burzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Affine cipher = new Affine(true, true, false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
int key1 = 5;
|
|
||||||
int key2 = 7;
|
|
||||||
String correctOutput = "pbtthlbyzburzwb";
|
|
||||||
String output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "PBTTHLBYZBURZWB";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlb yz burzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode-";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlbyzburzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "Pbtthlb yzburzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Affine cipher = new Affine(false, false, false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
int key1 = 5;
|
|
||||||
int key2 = 7;
|
|
||||||
String correctOutput = "pbtthlbyzburzwb";
|
|
||||||
String output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlbyzburzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlbyzburzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode-";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlbyzburzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "pbtthlbyzburzwb";
|
|
||||||
output = cipher.encode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testConstructor_default(){
|
||||||
Affine cipher = new Affine(true, true, true);
|
cipher = new Affine();
|
||||||
|
assertFalse(cipher.preserveCapitals);
|
||||||
//Test lowercase decoding
|
assertFalse(cipher.preserveSymbols);
|
||||||
String inputString = "pbtthlbyzburzwb";
|
assertFalse(cipher.preserveWhitespace);
|
||||||
int key1 = 5;
|
assertEquals("", cipher.inputString);
|
||||||
int key2 = 7;
|
assertEquals("", cipher.outputString);
|
||||||
String correctOutput = "messagetoencode";
|
assertEquals(0, cipher.key1);
|
||||||
String output = cipher.decode(key1, key2, inputString);
|
assertEquals(0, cipher.key2);
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppsercase decoding
|
|
||||||
inputString = "PBTTHLBYZBURZWB";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "pbtthlb yz burzwb";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "message to encode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "pbtthlb*yz+burzwb-";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "message*to+encode-";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Pbtthlb yz^burzwb";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "Message to^encode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testConstructor_preserves(){
|
||||||
Affine cipher = new Affine(false, true, true);
|
cipher = new Affine(true, false, false);
|
||||||
|
assertTrue(cipher.preserveCapitals);
|
||||||
|
assertFalse(cipher.preserveSymbols);
|
||||||
|
assertFalse(cipher.preserveWhitespace);
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals(0, cipher.key1);
|
||||||
|
assertEquals(0, cipher.key2);
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher = new Affine(false, true, false);
|
||||||
String inputString = "pbtthlbyzburzwb";
|
assertFalse(cipher.preserveCapitals);
|
||||||
int key1 = 5;
|
assertFalse(cipher.preserveSymbols);
|
||||||
int key2 = 7;
|
assertTrue(cipher.preserveWhitespace);
|
||||||
String correctOutput = "messagetoencode";
|
|
||||||
String output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppsercase decoding
|
|
||||||
inputString = "PBTTHLBYZBURZWB";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
cipher = new Affine(false, false, true);
|
||||||
inputString = "pbtthlb yz burzwb";
|
assertFalse(cipher.preserveCapitals);
|
||||||
key1 = 5;
|
assertTrue(cipher.preserveSymbols);
|
||||||
key2 = 7;
|
assertFalse(cipher.preserveWhitespace);
|
||||||
correctOutput = "message to encode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "pbtthlb*yz+burzwb-";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "message*to+encode-";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Pbtthlb yz^burzwb";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "message to^encode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testKey1(){
|
||||||
Affine cipher = new Affine(true, false, true);
|
cipher.setKey1(key1);
|
||||||
|
assertEquals(key1, cipher.key1);
|
||||||
//Test lowercase decoding
|
verify(logger, times(1)).debug("Setting key1 {}", key1);
|
||||||
String inputString = "pbtthlbyzburzwb";
|
verify(logger, times(1)).debug("Cleaned key1 {}", key1);
|
||||||
int key1 = 5;
|
verify(logger, times(2)).debug(anyString(), anyInt());
|
||||||
int key2 = 7;
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
String correctOutput = "messagetoencode";
|
verify(logger, never()).debug(anyString());
|
||||||
String output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppsercase decoding
|
|
||||||
inputString = "PBTTHLBYZBURZWB";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "pbtthlb yz burzwb";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "pbtthlb*yz+burzwb-";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "message*to+encode-";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Pbtthlb yz^burzwb";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "Messageto^encode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testSetKey1_notPrime(){
|
||||||
Affine cipher = new Affine(true, true, false);
|
assertThrows(InvalidKeywordException.class, () -> {
|
||||||
|
cipher.setKey1(2);
|
||||||
//Test lowercase decoding
|
});
|
||||||
String inputString = "pbtthlbyzburzwb";
|
verify(logger, times(1)).debug("Setting key1 {}", 2);
|
||||||
int key1 = 5;
|
verify(logger, never()).debug("Cleaned key1 {}", 2);
|
||||||
int key2 = 7;
|
verify(logger, times(1)).debug(anyString(), anyInt());
|
||||||
String correctOutput = "messagetoencode";
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
String output = cipher.decode(key1, key2, inputString);
|
verify(logger, never()).debug(anyString());
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppsercase decoding
|
|
||||||
inputString = "PBTTHLBYZBURZWB";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "pbtthlb yz burzwb";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "message to encode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "pbtthlb*yz+burzwb-";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Pbtthlb yz^burzwb";
|
|
||||||
key1 = 5;
|
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "Message toencode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testSetKey1_negative(){
|
||||||
Affine cipher = new Affine(false, false, false);
|
cipher.setKey1(-27);
|
||||||
|
assertEquals(25, cipher.key1);
|
||||||
|
verify(logger, times(1)).debug("Setting key1 {}", -27);
|
||||||
|
verify(logger, times(1)).debug("Cleaned key1 {}", 25);
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyInt());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
//Test lowercase decoding
|
@Test
|
||||||
String inputString = "pbtthlbyzburzwb";
|
public void testSetKey1_large(){
|
||||||
int key1 = 5;
|
cipher.setKey1(key1 + 26);
|
||||||
int key2 = 7;
|
assertEquals(key1, cipher.key1);
|
||||||
String correctOutput = "messagetoencode";
|
verify(logger, times(1)).debug("Setting key1 {}", key1 + 26);
|
||||||
String output = cipher.decode(key1, key2, inputString);
|
verify(logger, times(1)).debug("Cleaned key1 {}", key1);
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(2)).debug(anyString(), anyInt());
|
||||||
//Test uppsercase decoding
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
inputString = "PBTTHLBYZBURZWB";
|
verify(logger, never()).debug(anyString());
|
||||||
key1 = 5;
|
}
|
||||||
key2 = 7;
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.decode(key1, key2, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
@Test
|
||||||
inputString = "pbtthlb yz burzwb";
|
public void testSetKey2(){
|
||||||
key1 = 5;
|
cipher.setKey2(key2);
|
||||||
key2 = 7;
|
assertEquals(key2, cipher.key2);
|
||||||
correctOutput = "messagetoencode";
|
verify(logger, times(1)).debug("Setting key2 {}", key2);
|
||||||
output = cipher.decode(key1, key2, inputString);
|
verify(logger, times(1)).debug("Cleaned key2 {}", key2);
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(2)).debug(anyString(), anyInt());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
//Test symbol decoding
|
@Test
|
||||||
inputString = "pbtthlb*yz+burzwb-";
|
public void testSetKey2_negative(){
|
||||||
key1 = 5;
|
cipher.setKey2(-27);
|
||||||
key2 = 7;
|
assertEquals(25, cipher.key2);
|
||||||
correctOutput = "messagetoencode";
|
verify(logger, times(1)).debug("Setting key2 {}", -27);
|
||||||
output = cipher.decode(key1, key2, inputString);
|
verify(logger, times(1)).debug("Cleaned key2 {}", cipher.key2);
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(2)).debug(anyString(), anyInt());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
@Test
|
||||||
inputString = "Pbtthlb yz^burzwb";
|
public void testSetKey2_large(){
|
||||||
key1 = 5;
|
cipher.setKey2(key2 + 26);
|
||||||
key2 = 7;
|
assertEquals(key2, cipher.key2);
|
||||||
correctOutput = "messagetoencode";
|
verify(logger, times(1)).debug("Setting key2 {}", key2 + 26);
|
||||||
output = cipher.decode(key1, key2, inputString);
|
verify(logger, times(1)).debug("Cleaned key2 {}", key2);
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(2)).debug(anyString(), anyInt());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
|
||||||
|
cipher.setInputString(decodedString);
|
||||||
|
|
||||||
|
assertEquals(decodedString, cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString_noCapitals(){
|
||||||
|
cipher.preserveCapitals = false;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
|
||||||
|
cipher.setInputString(decodedString);
|
||||||
|
|
||||||
|
assertEquals(decodedString.toLowerCase(), cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
|
verify(logger, times(1)).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toLowerCase());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString_noWhitespace(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.preserveWhitespace = false;
|
||||||
|
|
||||||
|
cipher.setInputString(decodedString);
|
||||||
|
|
||||||
|
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
|
verify(logger, times(1)).debug("Removing whitespace");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString_noSymbols(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveSymbols = false;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
|
||||||
|
cipher.setInputString(decodedString);
|
||||||
|
|
||||||
|
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
|
verify(logger, times(1)).debug("Removing symbols");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString_null(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputString(null);
|
||||||
|
});
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString_blank(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputString("");
|
||||||
|
});
|
||||||
|
verify(logger, times(1)).debug("Original input string '{}'", "");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncode(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.key1 = key1;
|
||||||
|
cipher.key2 = key2;
|
||||||
|
|
||||||
|
cipher.encode();
|
||||||
|
|
||||||
|
assertEquals(encodedString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Encoding");
|
||||||
|
verify(logger, times(17)).debug(eq("Current char {}"), anyChar());
|
||||||
|
verify(logger, times(15)).debug(eq("Encoded char {}"), anyChar());
|
||||||
|
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecode(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
cipher.inputString = encodedString;
|
||||||
|
cipher.key1 = key1;
|
||||||
|
cipher.key2 = key2;
|
||||||
|
|
||||||
|
cipher.decode();
|
||||||
|
|
||||||
|
assertEquals(decodedString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Decoding");
|
||||||
|
verify(logger, times(1)).debug("Key1 inverse {}", 21);
|
||||||
|
verify(logger, times(17)).debug(eq("Current char {}"), anyChar());
|
||||||
|
verify(logger, times(15)).debug(eq("Decoded char {}"), anyChar());
|
||||||
|
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetters(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
cipher.key1 = key1;
|
||||||
|
cipher.key2 = key2;
|
||||||
|
|
||||||
|
assertEquals(decodedString, cipher.getInputString());
|
||||||
|
assertEquals(encodedString, cipher.getOutputString());
|
||||||
|
assertEquals(key1, cipher.getKey1());
|
||||||
|
assertEquals(key2, cipher.getKey2());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReset(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
cipher.key1 = key1;
|
||||||
|
cipher.key2 = key2;
|
||||||
|
|
||||||
|
cipher.reset();
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals(0, cipher.key1);
|
||||||
|
assertEquals(0, cipher.key2);
|
||||||
|
verify(logger, times(1)).debug("Resetting fields");
|
||||||
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
cipher = new Affine(false, false, false);
|
||||||
|
output = cipher.encode(key1, key2, decodedString);
|
||||||
|
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
|
||||||
|
cipher = new Affine(false, false, false);
|
||||||
|
output = cipher.decode(key1, key2, encodedString);
|
||||||
|
assertEquals(decodedStringClean, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,356 +1,224 @@
|
|||||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java
|
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 07-25-21
|
// Created: 07-25-21
|
||||||
//Modified: 07-09-22
|
//Modified: 04-15-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.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 org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||||
|
|
||||||
|
|
||||||
public class TestAtbash{
|
public class TestAtbash{
|
||||||
@Test
|
private Atbash cipher;
|
||||||
public void testEncode() throws InvalidInputException{
|
private Logger logger;
|
||||||
Atbash cipher = new Atbash(true, true, true);
|
//Variables
|
||||||
|
private String decodedString = "Message to^encode";
|
||||||
|
private String decodedStringClean = "MESSAGETOENCODE";
|
||||||
|
private String encodedString = "Nvhhztv gl^vmxlwv";
|
||||||
|
private String encodedStringClean = "NVHHZTVGLVMXLWV";
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String correctOutput = "nvhhztvglvmxlwv";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
correctOutput = "NVHHZTVGLVMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
@BeforeEach
|
||||||
inputString = "message to encode";
|
public void setup(){
|
||||||
correctOutput = "nvhhztv gl vmxlwv";
|
cipher = new Atbash();
|
||||||
output = cipher.encode(inputString);
|
logger = mock(Logger.class);
|
||||||
assertEquals(correctOutput, output);
|
Atbash.logger = logger;
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
correctOutput = "nvhhztv*gl+vmxlwv";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
correctOutput = "Nvhhztv gl^vmxlwv";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalEncode() throws InvalidInputException{
|
|
||||||
Atbash cipher = new Atbash(false, true, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String correctOutput = "NVHHZTVGLVMXLWV";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
correctOutput = "NVHHZTVGLVMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
correctOutput = "NVHHZTV GL VMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
correctOutput = "NVHHZTV*GL+VMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
correctOutput = "NVHHZTV GL^VMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoWhitespaceEncode() throws InvalidInputException{
|
|
||||||
Atbash cipher = new Atbash(true, false, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String correctOutput = "nvhhztvglvmxlwv";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
correctOutput = "NVHHZTVGLVMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
correctOutput = "nvhhztvglvmxlwv";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
correctOutput = "nvhhztv*gl+vmxlwv";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
correctOutput = "Nvhhztvgl^vmxlwv";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoSymbolEncode() throws InvalidInputException{
|
|
||||||
Atbash cipher = new Atbash(true, true, false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String correctOutput = "nvhhztvglvmxlwv";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
correctOutput = "NVHHZTVGLVMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
correctOutput = "nvhhztv gl vmxlwv";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
correctOutput = "nvhhztvglvmxlwv";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
correctOutput = "Nvhhztv glvmxlwv";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidInputException{
|
|
||||||
Atbash cipher = new Atbash(false, false, false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String correctOutput = "NVHHZTVGLVMXLWV";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
correctOutput = "NVHHZTVGLVMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
correctOutput = "NVHHZTVGLVMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
correctOutput = "NVHHZTVGLVMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
correctOutput = "NVHHZTVGLVMXLWV";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecode() throws InvalidInputException{
|
public void testConstructor_default(){
|
||||||
Atbash cipher = new Atbash(true, true, true);
|
cipher = new Atbash();
|
||||||
|
assertFalse(cipher.preserveCapitals);
|
||||||
//Test lowercase decoding
|
assertFalse(cipher.preserveWhitespace);
|
||||||
String inputString = "nvhhztvglvmxlwv";
|
assertFalse(cipher.preserveSymbols);
|
||||||
String correctOutput = "messagetoencode";
|
assertEquals("", cipher.inputString);
|
||||||
String output = cipher.encode(inputString);
|
assertEquals("", cipher.outputString);
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "NVHHZTVGLVMXLWV";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "nvhhztv gl vmxlwv";
|
|
||||||
correctOutput = "message to encode";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "nvhhztv*gl+vmxlwv";
|
|
||||||
correctOutput = "message*to+encode";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "Nvhhztv gl^vmxlwv";
|
|
||||||
correctOutput = "Message to^encode";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalDecode() throws InvalidInputException{
|
public void testConstructor_preserves(){
|
||||||
Atbash cipher = new Atbash(false, true, true);
|
cipher = new Atbash(true, false, false);
|
||||||
|
assertTrue(cipher.preserveCapitals);
|
||||||
|
assertFalse(cipher.preserveWhitespace);
|
||||||
|
assertFalse(cipher.preserveSymbols);
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher = new Atbash(false, true, false);
|
||||||
String inputString = "nvhhztvglvmxlwv";
|
assertFalse(cipher.preserveCapitals);
|
||||||
String correctOutput = "MESSAGETOENCODE";
|
assertTrue(cipher.preserveWhitespace);
|
||||||
String output = cipher.encode(inputString);
|
assertFalse(cipher.preserveSymbols);
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "NVHHZTVGLVMXLWV";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
cipher = new Atbash(false, false, true);
|
||||||
inputString = "nvhhztv gl vmxlwv";
|
assertFalse(cipher.preserveCapitals);
|
||||||
correctOutput = "MESSAGE TO ENCODE";
|
assertFalse(cipher.preserveWhitespace);
|
||||||
output = cipher.encode(inputString);
|
assertTrue(cipher.preserveSymbols);
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "nvhhztv*gl+vmxlwv";
|
|
||||||
correctOutput = "MESSAGE*TO+ENCODE";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "Nvhhztv gl^vmxlwv";
|
|
||||||
correctOutput = "MESSAGE TO^ENCODE";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoWhitespaceDecode() throws InvalidInputException{
|
public void testEncode(){
|
||||||
Atbash cipher = new Atbash(true, false, true);
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals(decodedString, cipher.inputString);
|
||||||
String inputString = "nvhhztvglvmxlwv";
|
assertEquals(encodedString, cipher.outputString);
|
||||||
String correctOutput = "messagetoencode";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "NVHHZTVGLVMXLWV";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "nvhhztv gl vmxlwv";
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "nvhhztv*gl+vmxlwv";
|
|
||||||
correctOutput = "message*to+encode";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "Nvhhztv gl^vmxlwv";
|
|
||||||
correctOutput = "Messageto^encode";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoSymbolDecode() throws InvalidInputException{
|
public void testSetInputString(){
|
||||||
Atbash cipher = new Atbash(true, true, false);
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher.setInputString(decodedString);
|
||||||
String inputString = "nvhhztvglvmxlwv";
|
|
||||||
String correctOutput = "messagetoencode";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "NVHHZTVGLVMXLWV";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
assertEquals(decodedString, cipher.inputString);
|
||||||
inputString = "nvhhztv gl vmxlwv";
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
correctOutput = "message to encode";
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||||
output = cipher.encode(inputString);
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, never()).debug(anyString());
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "nvhhztv*gl+vmxlwv";
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "Nvhhztv gl^vmxlwv";
|
|
||||||
correctOutput = "Message toencode";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidInputException{
|
public void testSetInputString_noCapitals(){
|
||||||
Atbash cipher = new Atbash(false, false, false);
|
cipher.preserveCapitals = false;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher.setInputString(decodedString);
|
||||||
String inputString = "nvhhztvglvmxlwv";
|
|
||||||
String correctOutput = "MESSAGETOENCODE";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "NVHHZTVGLVMXLWV";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||||
inputString = "nvhhztv gl vmxlwv";
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
correctOutput = "MESSAGETOENCODE";
|
verify(logger, times(1)).debug("Removing case");
|
||||||
output = cipher.encode(inputString);
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
//Test symbol decoding
|
@Test
|
||||||
inputString = "nvhhztv*gl+vmxlwv";
|
public void testSetInputString_noWhitespace(){
|
||||||
correctOutput = "MESSAGETOENCODE";
|
cipher.preserveCapitals = true;
|
||||||
output = cipher.encode(inputString);
|
cipher.preserveSymbols = true;
|
||||||
assertEquals(correctOutput, output);
|
cipher.preserveWhitespace = false;
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
cipher.setInputString(decodedString);
|
||||||
inputString = "Nvhhztv gl^vmxlwv";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||||
output = cipher.encode(inputString);
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(1)).debug("Removing whitespace");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString_noSymbols(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveSymbols = false;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
|
||||||
|
cipher.setInputString(decodedString);
|
||||||
|
|
||||||
|
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
|
verify(logger, times(1)).debug("Removing symbols");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString_null(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputString(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString_blank(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputString("");
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Original input string '{}'", "");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetters(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
|
||||||
|
assertEquals(decodedString, cipher.getInputString());
|
||||||
|
assertEquals(encodedString, cipher.getOutputString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReset(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
|
||||||
|
cipher.reset();
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Resetting fields");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalEncoding(){
|
||||||
|
//Test as original
|
||||||
|
cipher = new Atbash(true, true, true);
|
||||||
|
String output = cipher.encode(decodedString);
|
||||||
|
assertEquals(encodedString, output);
|
||||||
|
|
||||||
|
//Test fully cleaned
|
||||||
|
cipher = new Atbash(false, false, false);
|
||||||
|
output = cipher.encode(decodedString);
|
||||||
|
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
|
||||||
|
cipher = new Atbash(false, false, false);
|
||||||
|
output = cipher.decode(encodedString);
|
||||||
|
assertEquals(decodedStringClean, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,413 +1,198 @@
|
|||||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAutokey.java
|
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAutokey.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 07-26-21
|
// Created: 07-26-21
|
||||||
//Modified: 07-09-22
|
//Modified: 04-15-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyChar;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||||
|
|
||||||
|
|
||||||
public class TestAutokey{
|
public class TestAutokey{
|
||||||
@Test
|
private Autokey cipher;
|
||||||
public void testEncode() throws InvalidKeywordException, InvalidInputException{
|
private Logger logger;
|
||||||
Autokey cipher = new Autokey(true, true, true);
|
//Variables
|
||||||
|
private String decodedString = "MeSsage to^encode";
|
||||||
|
private String decodedStringClean = "MESSAGETOENCODE";
|
||||||
|
private String encodedString = "WiQooxh fs^wfcuhx";
|
||||||
|
private String encodedStringClean = "WIQOOXHFSWFCUHX";
|
||||||
|
private String keyword = "keyword";
|
||||||
|
private ArrayList<Integer> offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3, 12, 4, 18, 18, 0, 6, 4, 19));
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "wiqooxhfswfcuhx";
|
|
||||||
String output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXHFSWFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
@BeforeEach
|
||||||
inputString = "message to encode";
|
public void setup(){
|
||||||
keyword = "keyword";
|
cipher = new Autokey();
|
||||||
correctOutput = "wiqooxh fs wfcuhx";
|
logger = mock(Logger.class);
|
||||||
output = cipher.encode(keyword, inputString);
|
Autokey.logger = logger;
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "wiqooxh*fs+wfcuhx";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Wiqooxh fs^wfcuhx";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Autokey cipher = new Autokey(false, true, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "WIQOOXHFSWFCUHX";
|
|
||||||
String output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXHFSWFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXH FS WFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXH*FS+WFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXH FS^WFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Autokey cipher = new Autokey(true, false, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "wiqooxhfswfcuhx";
|
|
||||||
String output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXHFSWFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "wiqooxhfswfcuhx";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "wiqooxh*fs+wfcuhx";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Wiqooxhfs^wfcuhx";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Autokey cipher = new Autokey(true, true, false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "wiqooxhfswfcuhx";
|
|
||||||
String output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXHFSWFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "wiqooxh fs wfcuhx";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "wiqooxhfswfcuhx";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Wiqooxh fswfcuhx";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Autokey cipher = new Autokey(false, false, false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "WIQOOXHFSWFCUHX";
|
|
||||||
String output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXHFSWFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXHFSWFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXHFSWFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "WIQOOXHFSWFCUHX";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testConstructor_default(){
|
||||||
Autokey cipher = new Autokey(true, true, true);
|
cipher = new Autokey();
|
||||||
|
assertFalse(cipher.preserveCapitals);
|
||||||
//Test lowercase decoding
|
assertFalse(cipher.preserveWhitespace);
|
||||||
String inputString = "wiqooxhfswfcuhx";
|
assertFalse(cipher.preserveSymbols);
|
||||||
String keyword = "keyword";
|
assertEquals("", cipher.inputString);
|
||||||
String correctOutput = "messagetoencode";
|
assertEquals("", cipher.outputString);
|
||||||
String output = cipher.decode(keyword, inputString);
|
assertEquals("", cipher.keyword);
|
||||||
assertEquals(correctOutput, output);
|
assertEquals(0, cipher.offset.size());
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "WIQOOXHFSWFCUHX";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "wiqooxh fs wfcuhx";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "message to encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "wiqooxh*fs+wfcuhx";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "message*to+encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Wiqooxh fs^wfcuhx";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Message to^encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testConstructor_preserves(){
|
||||||
Autokey cipher = new Autokey(false, true, true);
|
cipher = new Autokey(true, false, false);
|
||||||
|
assertTrue(cipher.preserveCapitals);
|
||||||
|
assertFalse(cipher.preserveWhitespace);
|
||||||
|
assertFalse(cipher.preserveSymbols);
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals("", cipher.keyword);
|
||||||
|
assertEquals(0, cipher.offset.size());
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher = new Autokey(false, true, false);
|
||||||
String inputString = "WIQOOXHFSWFCUHX";
|
assertFalse(cipher.preserveCapitals);
|
||||||
String keyword = "keyword";
|
assertTrue(cipher.preserveWhitespace);
|
||||||
String correctOutput = "MESSAGETOENCODE";
|
assertFalse(cipher.preserveSymbols);
|
||||||
String output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "WIQOOXHFSWFCUHX";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
cipher = new Autokey(false, false, true);
|
||||||
inputString = "WIQOOXH FS WFCUHX";
|
assertFalse(cipher.preserveCapitals);
|
||||||
keyword = "keyword";
|
assertFalse(cipher.preserveWhitespace);
|
||||||
correctOutput = "MESSAGE TO ENCODE";
|
assertTrue(cipher.preserveSymbols);
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "WIQOOXH*FS+WFCUHX";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGE*TO+ENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "WIQOOXH FS^WFCUHX";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGE TO^ENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testEncodeSet(){
|
||||||
Autokey cipher = new Autokey(true, false, true);
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher.encodeSet(keyword, decodedString);
|
||||||
String inputString = "wiqooxhfswfcuhx";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "messagetoencode";
|
|
||||||
String output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "WIQOOXHFSWFCUHX";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
assertEquals(decodedString, cipher.inputString);
|
||||||
inputString = "wiqooxh fs wfcuhx";
|
assertEquals((keyword + decodedString.replaceAll("\\s", "").replaceAll("[^a-zA-Z\\s]", "").substring(0, 8)).toUpperCase(), cipher.keyword);
|
||||||
keyword = "keyword";
|
assertEquals(offset, cipher.offset);
|
||||||
correctOutput = "messagetoencode";
|
verify(logger, times(1)).debug("Setting fields for encoding");
|
||||||
output = cipher.decode(keyword, inputString);
|
verify(logger, times(1)).debug("Setting keyword");
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(1)).debug("Adding input to keyword");
|
||||||
|
verify(logger, times(1)).debug("Removing last letters in the keyword");
|
||||||
//Test symbol decoding
|
verify(logger, times(4)).debug(anyString());
|
||||||
inputString = "wiqooxh*fs+wfcuhx";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "message*to+encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Wiqooxh fs^wfcuhx";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Messageto^encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testDecodeSet(){
|
||||||
Autokey cipher = new Autokey(true, true, false);
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher.decodeSet(keyword, decodedString);
|
||||||
String inputString = "wiqooxhfswfcuhx";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "messagetoencode";
|
|
||||||
String output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "WIQOOXHFSWFCUHX";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||||
inputString = "wiqooxh fs wfcuhx";
|
assertEquals(decodedString, cipher.inputString);
|
||||||
keyword = "keyword";
|
verify(logger, times(1)).debug("Setting fields for decoding");
|
||||||
correctOutput = "message to encode";
|
verify(logger, times(1)).debug("Setting keyword");
|
||||||
output = cipher.decode(keyword, inputString);
|
verify(logger, times(1)).debug("Setting input string");
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(3)).debug(anyString());
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "wiqooxh*fs+wfcuhx";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Wiqooxh fs^wfcuhx";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Message toencode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testDecode(){
|
||||||
Autokey cipher = new Autokey(false, false, false);
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
Autokey.logger = mock(Logger.class);
|
||||||
|
cipher.decodeSet(keyword, encodedString);
|
||||||
|
Autokey.logger = logger;
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher.decode();
|
||||||
String inputString = "WIQOOXHFSWFCUHX";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "MESSAGETOENCODE";
|
|
||||||
String output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "WIQOOXHFSWFCUHX";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
assertEquals(decodedString, cipher.outputString);
|
||||||
inputString = "WIQOOXH FS WFCUHX";
|
verify(logger, times(1)).debug("Decoding");
|
||||||
keyword = "keyword";
|
verify(logger, times(2)).debug("Appending partial output to keyword");
|
||||||
correctOutput = "MESSAGETOENCODE";
|
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||||
output = cipher.decode(keyword, inputString);
|
verify(logger, times(2)).debug("Appending uppercase");
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(1)).debug("Wrapping around to Z");
|
||||||
|
verify(logger, times(13)).debug("Appending lowercase");
|
||||||
|
verify(logger, times(3)).debug("Wrapping around to z");
|
||||||
|
verify(logger, times(17)).debug(eq("Decoded letter {}"), anyChar());
|
||||||
|
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
|
||||||
|
}
|
||||||
|
|
||||||
//Test symbol decoding
|
@Test
|
||||||
inputString = "WIQOOXH*FS+WFCUHX";
|
public void testGetters(){
|
||||||
keyword = "keyword";
|
cipher.inputString = decodedString;
|
||||||
correctOutput = "MESSAGETOENCODE";
|
cipher.outputString = encodedString;
|
||||||
output = cipher.decode(keyword, inputString);
|
cipher.keyword = keyword;
|
||||||
assertEquals(correctOutput, output);
|
cipher.offset.add(1);
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
assertEquals(decodedString, cipher.getInputString());
|
||||||
inputString = "WIQOOXH FS^WFCUHX";
|
assertEquals(encodedString, cipher.getOutputString());
|
||||||
keyword = "keyword";
|
assertEquals(keyword, cipher.getKeyword());
|
||||||
correctOutput = "MESSAGETOENCODE";
|
assertEquals(new ArrayList<>(List.of(1)), cipher.getOffsets());
|
||||||
output = cipher.decode(keyword, inputString);
|
}
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
@Test
|
||||||
|
public void testReset(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
cipher.keyword = keyword;
|
||||||
|
cipher.offset.add(1);
|
||||||
|
|
||||||
|
cipher.reset();
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals("", cipher.keyword);
|
||||||
|
assertEquals(0, cipher.offset.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
cipher = new Autokey(false, false, false);
|
||||||
|
output = cipher.encode(keyword, decodedString);
|
||||||
|
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
|
||||||
|
cipher = new Autokey(false, false, false);
|
||||||
|
output = cipher.decode(keyword, encodedString);
|
||||||
|
assertEquals(decodedStringClean, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testKeyword() throws InvalidKeywordException{
|
public void testSetKeyword() throws InvalidKeywordException{
|
||||||
Autokey cipher = new Autokey();
|
Autokey cipher = new Autokey();
|
||||||
|
|
||||||
//Test keyword with whitespace
|
//Test keyword with whitespace
|
||||||
|
|||||||
@@ -1,103 +1,319 @@
|
|||||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestBaconian.java
|
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestBaconian.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 01-12-22
|
// Created: 01-12-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-16-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyChar;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||||
|
|
||||||
|
|
||||||
public class TestBaconian{
|
public class TestBaconian{
|
||||||
@Test
|
private Baconian cipher;
|
||||||
public void testEncode() throws InvalidInputException{
|
private Logger logger;
|
||||||
Baconian cipher = new Baconian(true);
|
//Variables
|
||||||
|
private String decodedString = "Message to-encode";
|
||||||
|
private String decodedStringCleanUpper = "Messagetoencode";
|
||||||
|
private String decodedStringCleanLower = "messagetoencode";
|
||||||
|
private String encodedString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String correctOutput = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
correctOutput = "ABABB AABAA BAAAB BAAAB AAAAA AABBA AABAA BAABA ABBAB AABAA ABBAA AAABA ABBAB AAABB AABAA";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
@BeforeEach
|
||||||
inputString = "Message to-encode";
|
public void setup(){
|
||||||
correctOutput = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
cipher = new Baconian();
|
||||||
output = cipher.encode(inputString);
|
logger = mock(Logger.class);
|
||||||
assertEquals(correctOutput, output);
|
Baconian.logger = logger;
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testEncodeNoCapital() throws InvalidInputException{
|
|
||||||
Baconian cipher = new Baconian(false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String correctOutput = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
correctOutput = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "Message to-encode";
|
|
||||||
correctOutput = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecode() throws InvalidCharacterException, InvalidInputException{
|
public void testConstructor_default(){
|
||||||
Baconian cipher = new Baconian(true);
|
cipher = new Baconian();
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertFalse(cipher.preserveCapitals);
|
||||||
String inputString = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
assertEquals("", cipher.inputString);
|
||||||
String correctOutput = "messagetoencode";
|
assertEquals("", cipher.outputString);
|
||||||
String output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "ABABB AABAA BAAAB BAAAB AAAAA AABBA AABAA BAABA ABBAB AABAA ABBAA AAABA ABBAB AAABB AABAA";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
|
||||||
correctOutput = "Messagetoencode";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecodeNoCapital() throws InvalidCharacterException, InvalidInputException{
|
public void testConstructor_preserves(){
|
||||||
Baconian cipher = new Baconian(false);
|
cipher = new Baconian(true);
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertTrue(cipher.preserveCapitals);
|
||||||
String inputString = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
assertEquals("", cipher.inputString);
|
||||||
String correctOutput = "messagetoencode";
|
assertEquals("", cipher.outputString);
|
||||||
String output = cipher.decode(inputString);
|
}
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "ABABB AABAA BAAAB BAAAB AAAAA AABBA AABAA BAABA ABBAB AABAA ABBAA AAABA ABBAB AAABB AABAA";
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
@Test
|
||||||
inputString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
public void testSetInputStringEncode(){
|
||||||
correctOutput = "messagetoencode";
|
cipher.preserveCapitals = true;
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
cipher.setInputStringEncode(decodedString);
|
||||||
|
|
||||||
|
assertEquals(decodedStringCleanUpper, 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, never()).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringEncode_noCapitals(){
|
||||||
|
cipher.preserveCapitals = false;
|
||||||
|
|
||||||
|
cipher.setInputStringEncode(decodedString);
|
||||||
|
|
||||||
|
assertEquals(decodedStringCleanLower, cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
|
||||||
|
verify(logger, times(1)).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringCleanLower);
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringEncode_blank(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputStringEncode("");
|
||||||
|
}, "Input must contain at least 1 letter");
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Setting input string for encoding '{}'", "");
|
||||||
|
verify(logger, never()).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringEncode_null(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputStringEncode(null);
|
||||||
|
}, "Input cannot be null");
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringDecode(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
|
||||||
|
cipher.setInputStringDecode(encodedString);
|
||||||
|
|
||||||
|
assertEquals(encodedString, cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString);
|
||||||
|
verify(logger, never()).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Ensuring all 'letters' contain 5 characters");
|
||||||
|
verify(logger, times(15)).debug(eq("Current 'letter' {}"), anyString());
|
||||||
|
verify(logger, times(15)).debug("Replacing all non-abAB characters");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString);
|
||||||
|
verify(logger, times(16)).debug(anyString());
|
||||||
|
verify(logger, times(17)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringDecode_noCapitals(){
|
||||||
|
cipher.preserveCapitals = false;
|
||||||
|
|
||||||
|
cipher.setInputStringDecode(encodedString);
|
||||||
|
|
||||||
|
assertEquals(encodedString.toLowerCase(), cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString);
|
||||||
|
verify(logger, times(1)).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Ensuring all 'letters' contain 5 characters");
|
||||||
|
verify(logger, times(15)).debug(eq("Current 'letter' {}"), anyString());
|
||||||
|
verify(logger, times(15)).debug("Replacing all non-abAB characters");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.toLowerCase());
|
||||||
|
verify(logger, times(17)).debug(anyString());
|
||||||
|
verify(logger, times(17)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringDecode_invalidLength(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
|
||||||
|
assertThrows(InvalidCharacterException.class, () -> {
|
||||||
|
cipher.setInputStringDecode("a");
|
||||||
|
}, "All Baconian letters contain exactly 5 characters: a");
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Setting input string for decoding '{}'", "a");
|
||||||
|
verify(logger, never()).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Ensuring all 'letters' contain 5 characters");
|
||||||
|
verify(logger, times(1)).debug(eq("Current 'letter' {}"), anyString());
|
||||||
|
verify(logger, never()).debug("Replacing all non-abAB characters");
|
||||||
|
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringDecode_invalidChars(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
|
||||||
|
assertThrows(InvalidCharacterException.class, () -> {
|
||||||
|
cipher.setInputStringDecode("ccccc");
|
||||||
|
}, "Baconian letters contain only a's and b's: ccccc");
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Setting input string for decoding '{}'", "ccccc");
|
||||||
|
verify(logger, never()).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Ensuring all 'letters' contain 5 characters");
|
||||||
|
verify(logger, times(1)).debug("Current 'letter' {}", "ccccc");
|
||||||
|
verify(logger, times(1)).debug("Replacing all non-abAB characters");
|
||||||
|
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringDecode_blank(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputStringDecode("");
|
||||||
|
}, "Input cannot be empty");
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, never()).debug("Setting input string for decoding '{}'", "");
|
||||||
|
verify(logger, never()).debug("Removing case");
|
||||||
|
verify(logger, never()).debug("Ensuring all 'letters' contain 5 characters");
|
||||||
|
verify(logger, never()).debug(eq("Current 'letter' {}"), anyString());
|
||||||
|
verify(logger, never()).debug("Replacing all non-abAB characters");
|
||||||
|
verify(logger, never()).debug("Cleaned input string '{}'", "");
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringDecode_null(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputStringDecode(null);
|
||||||
|
}, "Input cannot be null");
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncode(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.inputString = decodedStringCleanUpper;
|
||||||
|
|
||||||
|
cipher.encode();
|
||||||
|
|
||||||
|
assertEquals(encodedString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Encoding");
|
||||||
|
verify(logger, times(15)).debug(eq("Working character {}"), anyChar());
|
||||||
|
verify(logger, times(1)).debug("Encoding uppercase");
|
||||||
|
verify(logger, times(14)).debug("Encoding lowercase");
|
||||||
|
verify(logger, times(15)).debug(eq("Output letter {}"), anyString());
|
||||||
|
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||||
|
verify(logger, times(16)).debug(anyString());
|
||||||
|
verify(logger, times(16)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecode(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.inputString = encodedString;
|
||||||
|
|
||||||
|
cipher.decode();
|
||||||
|
|
||||||
|
assertEquals(decodedStringCleanUpper, 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(16)).debug(anyString());
|
||||||
|
verify(logger, times(16)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetters(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
|
||||||
|
assertEquals(decodedString, cipher.getInputString());
|
||||||
|
assertEquals(encodedString, cipher.getOutputString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReset(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
|
||||||
|
cipher.reset();
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Resetting fields");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalEncoding(){
|
||||||
|
//Test as original
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
String output = cipher.encode(decodedString);
|
||||||
|
assertEquals(encodedString, output);
|
||||||
|
|
||||||
|
//Test fully cleaned
|
||||||
|
cipher.preserveCapitals = false;
|
||||||
|
output = cipher.encode(decodedString);
|
||||||
|
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
|
||||||
|
cipher.preserveCapitals = false;
|
||||||
|
output = cipher.decode(encodedString);
|
||||||
|
assertEquals(decodedStringCleanLower, output);
|
||||||
|
|
||||||
|
//Test uppercase input
|
||||||
|
cipher.preserveCapitals = false;
|
||||||
|
output = cipher.decode(encodedString.toUpperCase());
|
||||||
|
assertEquals(decodedStringCleanLower, output);
|
||||||
|
|
||||||
|
//Test lowercase input
|
||||||
|
cipher.preserveCapitals = false;
|
||||||
|
output = cipher.decode(encodedString.toLowerCase());
|
||||||
|
assertEquals(decodedStringCleanLower, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,24 @@
|
|||||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBaseX.java
|
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBaseX.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 01-08-22
|
// Created: 01-08-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-16-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyChar;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||||
@@ -15,270 +26,292 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
|||||||
|
|
||||||
|
|
||||||
public class TestBaseX{
|
public class TestBaseX{
|
||||||
@Test
|
private BaseX cipher;
|
||||||
public void testBinaryEncode() throws InvalidBaseException, InvalidInputException{
|
private Logger logger;
|
||||||
BaseX cipher = new BaseX();
|
//Variables
|
||||||
|
private String decodedString = "A+B@C d\te\nf";
|
||||||
|
private String encodedString_2 = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
|
||||||
|
private String encodedString_8 = "101 53 102 100 103 40 144 11 145 12 146";
|
||||||
|
private String encodedString_10 = "65 43 66 64 67 32 100 9 101 10 102";
|
||||||
|
private String encodedString_16 = "41 2B 42 40 43 20 64 9 65 A 66";
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "a";
|
|
||||||
String correctOutput = "1100001";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "A";
|
|
||||||
correctOutput = "1000001";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
@BeforeEach
|
||||||
inputString = "A B\tC\n";
|
public void setup(){
|
||||||
correctOutput = "1000001 100000 1000010 1001 1000011 1010";
|
cipher = new BaseX();
|
||||||
output = cipher.encode(inputString);
|
logger = mock(Logger.class);
|
||||||
assertEquals(correctOutput, output);
|
BaseX.logger = logger;
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "A@B-C+";
|
|
||||||
correctOutput = "1000001 1000000 1000010 101101 1000011 101011";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "A+B@C d\te\nf";
|
|
||||||
correctOutput = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testOctalEncode() throws InvalidBaseException, InvalidInputException{
|
|
||||||
BaseX cipher = new BaseX(8);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "a";
|
|
||||||
String correctOutput = "141";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "A";
|
|
||||||
correctOutput = "101";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "A B\tC\n";
|
|
||||||
correctOutput = "101 40 102 11 103 12";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "A@B-C+";
|
|
||||||
correctOutput = "101 100 102 55 103 53";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "A+B@C d\te\nf";
|
|
||||||
correctOutput = "101 53 102 100 103 40 144 11 145 12 146";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testDecimalEncode() throws InvalidBaseException, InvalidInputException{
|
|
||||||
BaseX cipher = new BaseX(10);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "a";
|
|
||||||
String correctOutput = "97";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "A";
|
|
||||||
correctOutput = "65";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "A B\tC\n";
|
|
||||||
correctOutput = "65 32 66 9 67 10";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "A@B-C+";
|
|
||||||
correctOutput = "65 64 66 45 67 43";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "A+B@C d\te\nf";
|
|
||||||
correctOutput = "65 43 66 64 67 32 100 9 101 10 102";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testHexEncode() throws InvalidBaseException, InvalidInputException{
|
|
||||||
BaseX cipher = new BaseX(16);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String correctOutput = "61";
|
|
||||||
String inputString = "a";
|
|
||||||
String output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "A";
|
|
||||||
correctOutput = "41";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "A B\tC\n";
|
|
||||||
correctOutput = "41 20 42 9 43 A";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "A@B-C+";
|
|
||||||
correctOutput = "41 40 42 2D 43 2B";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
inputString = "A+B@C d\te\nf";
|
|
||||||
correctOutput = "41 2B 42 40 43 20 64 9 65 A 66";
|
|
||||||
output = cipher.encode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBinaryDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{
|
public void testConstructor_default(){
|
||||||
BaseX cipher = new BaseX();
|
cipher = new BaseX();
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String inputString = "1100001";
|
assertEquals("", cipher.outputString);
|
||||||
String correctOutput = "a";
|
assertEquals(2, cipher.base);
|
||||||
String output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "1000001";
|
|
||||||
correctOutput = "A";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "1000001 100000 1000010 1001 1000011 1010";
|
|
||||||
correctOutput = "A B\tC\n";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "1000001 1000000 1000010 101101 1000011 101011";
|
|
||||||
correctOutput = "A@B-C+";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
|
|
||||||
correctOutput = "A+B@C d\te\nf";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOctalDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{
|
public void testConstructor_base(){
|
||||||
BaseX cipher = new BaseX(8);
|
cipher = new BaseX(8);
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String inputString = "141";
|
assertEquals("", cipher.outputString);
|
||||||
String correctOutput = "a";
|
assertEquals(8, cipher.base);
|
||||||
String output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "101";
|
|
||||||
correctOutput = "A";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "101 40 102 11 103 12";
|
|
||||||
correctOutput = "A B\tC\n";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "101 100 102 55 103 53";
|
|
||||||
correctOutput = "A@B-C+";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "101 53 102 100 103 40 144 11 145 12 146";
|
|
||||||
correctOutput = "A+B@C d\te\nf";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecimalDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{
|
public void testSetInputStringEncode(){
|
||||||
BaseX cipher = new BaseX(10);
|
cipher.setInputStringEncode(decodedString);
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals(decodedString, cipher.inputString);
|
||||||
String inputString = "97";
|
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
|
||||||
String correctOutput = "a";
|
verify(logger, times(1)).debug(anyString(), anyString());
|
||||||
String output = cipher.decode(inputString);
|
verify(logger, never()).debug(anyString());
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "65";
|
|
||||||
correctOutput = "A";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
inputString = "65 32 66 9 67 10";
|
|
||||||
correctOutput = "A B\tC\n";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "65 64 66 45 67 43";
|
|
||||||
correctOutput = "A@B-C+";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
inputString = "65 43 66 64 67 32 100 9 101 10 102";
|
|
||||||
correctOutput = "A+B@C d\te\nf";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHexDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{
|
public void testSetInputStringEncode_blank(){
|
||||||
BaseX cipher = new BaseX(16);
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputStringEncode("");
|
||||||
|
}, "Input must contain at least 1 letter");
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String inputString = "61";
|
verify(logger, times(1)).debug("Setting input string for encoding '{}'", "");
|
||||||
String correctOutput = "a";
|
verify(logger, times(1)).debug(anyString(), anyString());
|
||||||
String output = cipher.decode(inputString);
|
verify(logger, never()).debug(anyString());
|
||||||
assertEquals(correctOutput, output);
|
}
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "41";
|
|
||||||
correctOutput = "A";
|
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
@Test
|
||||||
inputString = "41 20 42 9 43 A";
|
public void testSetInputStringEncode_null(){
|
||||||
correctOutput = "A B\tC\n";
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
output = cipher.decode(inputString);
|
cipher.setInputStringEncode(null);
|
||||||
assertEquals(correctOutput, output);
|
}, "Input cannot be null");
|
||||||
|
|
||||||
//Test symbol decoding
|
assertEquals("", cipher.inputString);
|
||||||
inputString = "41 40 42 2D 43 2B";
|
verify(logger, never()).debug("Setting input string for encoding '{}'", decodedString);
|
||||||
correctOutput = "A@B-C+";
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
output = cipher.decode(inputString);
|
verify(logger, never()).debug(anyString());
|
||||||
assertEquals(correctOutput, output);
|
}
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
@Test
|
||||||
inputString = "41 2B 42 40 43 20 64 9 65 A 66";
|
public void testSetInputStringDecode(){
|
||||||
correctOutput = "A+B@C d\te\nf";
|
cipher.base = 16;
|
||||||
output = cipher.decode(inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
cipher.setInputStringDecode(encodedString_16);
|
||||||
|
|
||||||
|
assertEquals(encodedString_16, cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString_16);
|
||||||
|
verify(logger, times(1)).debug("Creating string of valid 'numbers'");
|
||||||
|
verify(logger, times(16)).debug(eq("Current number {}, converted {}"), anyInt(), anyString());
|
||||||
|
verify(logger, times(1)).debug("Checking for invalid characters");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString_16);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringDecode_invalid(){
|
||||||
|
cipher.base = 16;
|
||||||
|
|
||||||
|
assertThrows(InvalidCharacterException.class, () -> {
|
||||||
|
cipher.setInputStringDecode("G");
|
||||||
|
}, "inputString cannot contain anything except numbers 0-15, and whitespace");
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Setting input string for decoding '{}'", "G");
|
||||||
|
verify(logger, times(1)).debug("Creating string of valid 'numbers'");
|
||||||
|
verify(logger, times(16)).debug(eq("Current number {}, converted {}"), anyInt(), anyString());
|
||||||
|
verify(logger, times(1)).debug("Checking for invalid characters");
|
||||||
|
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringDecode_blank(){
|
||||||
|
cipher.base = 16;
|
||||||
|
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputStringDecode("");
|
||||||
|
}, "Input must contain at least 1 letter");
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Setting input string for decoding '{}'", "");
|
||||||
|
verify(logger, times(1)).debug("Creating string of valid 'numbers'");
|
||||||
|
verify(logger, times(16)).debug(eq("Current number {}, converted {}"), anyInt(), anyString());
|
||||||
|
verify(logger, times(1)).debug("Checking for invalid characters");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputStringDecode_null(){
|
||||||
|
cipher.base = 16;
|
||||||
|
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputStringDecode(null);
|
||||||
|
}, "Input cannot be null");
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, never()).debug(eq("Setting input string for decoding '{}'"), anyString());
|
||||||
|
verify(logger, never()).debug("Creating string of valid 'numbers'");
|
||||||
|
verify(logger, never()).debug(eq("Current number {}, converted {}"), anyInt(), anyString());
|
||||||
|
verify(logger, never()).debug("Checking for invalid characters");
|
||||||
|
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetBase(){
|
||||||
|
cipher.setBase(16);
|
||||||
|
|
||||||
|
assertEquals(16, cipher.base);
|
||||||
|
verify(logger, times(1)).debug("Setting base {}", 16);
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetBase_min(){
|
||||||
|
assertThrows(InvalidBaseException.class, () -> {
|
||||||
|
cipher.setBase(Character.MIN_RADIX - 1);
|
||||||
|
}, "Base cannot be less than " + Character.MIN_RADIX);
|
||||||
|
|
||||||
|
assertEquals(2, cipher.base);
|
||||||
|
verify(logger, never()).debug(eq("Setting base {}"), anyInt());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetBase_max(){
|
||||||
|
assertThrows(InvalidBaseException.class, () -> {
|
||||||
|
cipher.setBase(Character.MAX_RADIX + 1);
|
||||||
|
}, "Base cannot be larger than " + Character.MAX_RADIX);
|
||||||
|
|
||||||
|
assertEquals(2, cipher.base);
|
||||||
|
verify(logger, never()).debug(eq("Setting base {}"), anyInt());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncode(){
|
||||||
|
cipher.base = 16;
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
|
||||||
|
cipher.encode();
|
||||||
|
|
||||||
|
assertEquals(encodedString_16, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Encoding");
|
||||||
|
verify(logger, times(11)).debug(eq("Working number {}"), anyChar());
|
||||||
|
verify(logger, times(11)).debug(eq("Converted number {}"), anyString());
|
||||||
|
verify(logger, times(1)).debug("Saving output string '{}'", encodedString_16);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecode(){
|
||||||
|
cipher.base = 16;
|
||||||
|
cipher.inputString = encodedString_16;
|
||||||
|
|
||||||
|
cipher.decode();
|
||||||
|
|
||||||
|
assertEquals(decodedString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Decoding");
|
||||||
|
verify(logger, times(11)).debug(eq("Current number {}"), anyString());
|
||||||
|
verify(logger, times(11)).debug(eq("Decoded number {}"), anyInt());
|
||||||
|
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecode_invalidCharacter(){
|
||||||
|
cipher.base = 16;
|
||||||
|
cipher.inputString = "FFF";
|
||||||
|
|
||||||
|
assertThrows(InvalidCharacterException.class, () -> {
|
||||||
|
cipher.decode();
|
||||||
|
}, "The base 16 string FFF is not a valid ASCII character");
|
||||||
|
|
||||||
|
verify(logger, times(1)).debug("Decoding");
|
||||||
|
verify(logger, times(1)).debug("Current number {}", "FFF");
|
||||||
|
verify(logger, times(1)).debug("Decoded number {}", 4095);
|
||||||
|
verify(logger, never()).debug(eq("Saving output string '{}'"), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetters(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString_2;
|
||||||
|
cipher.base = 8;
|
||||||
|
|
||||||
|
assertEquals(decodedString, cipher.getInputString());
|
||||||
|
assertEquals(encodedString_2, cipher.getOutputString());
|
||||||
|
assertEquals(8, cipher.getBase());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReset(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString_2;
|
||||||
|
|
||||||
|
cipher.reset();
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Resetting fields");
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalEncoding_2(){
|
||||||
|
String output = cipher.encode(2, decodedString);
|
||||||
|
|
||||||
|
assertEquals(encodedString_2, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalEncoding_8(){
|
||||||
|
String output = cipher.encode(8, decodedString);
|
||||||
|
|
||||||
|
assertEquals(encodedString_8, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalEncoding_10(){
|
||||||
|
String output = cipher.encode(10, decodedString);
|
||||||
|
|
||||||
|
assertEquals(encodedString_10, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalEncoding_16(){
|
||||||
|
String output = cipher.encode(16, decodedString);
|
||||||
|
|
||||||
|
assertEquals(encodedString_16, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalDecoding_2(){
|
||||||
|
String output = cipher.decode(2, encodedString_2);
|
||||||
|
|
||||||
|
assertEquals(decodedString, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalDecoding_8(){
|
||||||
|
String output = cipher.decode(8, encodedString_8);
|
||||||
|
|
||||||
|
assertEquals(decodedString, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalDecoding_10(){
|
||||||
|
String output = cipher.decode(10, encodedString_10);
|
||||||
|
|
||||||
|
assertEquals(decodedString, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalDecoding_16(){
|
||||||
|
String output = cipher.decode(16, encodedString_16);
|
||||||
|
|
||||||
|
assertEquals(decodedString, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,407 +1,399 @@
|
|||||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBeaufort.java
|
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBeaufort.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 02-23-22
|
// Created: 02-23-22
|
||||||
//Modified: 07-09-22
|
//Modified: 04-16-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||||
|
|
||||||
|
|
||||||
public class TestBeaufort{
|
public class TestBeaufort{
|
||||||
@Test
|
private Beaufort cipher;
|
||||||
public void testEncode() throws InvalidKeywordException, InvalidInputException{
|
private Logger logger;
|
||||||
Beaufort cipher = new Beaufort(true, true, true);
|
//Variables
|
||||||
|
private String decodedString = "Message to^encode";
|
||||||
|
private String decodedStringClean = "MESSAGETOENCODE";
|
||||||
|
private String encodedString = "Yageolz rq^ujmdag";
|
||||||
|
private String encodedStringClean = "YAGEOLZRQUJMDAG";
|
||||||
|
private String keyword = "keyword";
|
||||||
|
private String keywordDirty = "Ke*y word";
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "yageolzrqujmdag";
|
|
||||||
String output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZRQUJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
@BeforeEach
|
||||||
inputString = "message to encode";
|
public void setup(){
|
||||||
keyword = "keyword";
|
cipher = new Beaufort();
|
||||||
correctOutput = "yageolz rq ujmdag";
|
logger = mock(Logger.class);
|
||||||
output = cipher.encode(keyword, inputString);
|
Beaufort.logger = logger;
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "yageolz*rq+ujmdag";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Yageolz rq^ujmdag";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Beaufort cipher = new Beaufort(false, true, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "YAGEOLZRQUJMDAG";
|
|
||||||
String output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZRQUJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZ RQ UJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZ*RQ+UJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZ RQ^UJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Beaufort cipher = new Beaufort(true, false, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "yageolzrqujmdag";
|
|
||||||
String output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZRQUJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "yageolzrqujmdag";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "yageolz*rq+ujmdag";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Yageolzrq^ujmdag";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Beaufort cipher = new Beaufort(true, true, false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "yageolzrqujmdag";
|
|
||||||
String output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZRQUJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "yageolz rq ujmdag";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "yageolzrqujmdag";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Yageolz rqujmdag";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalwhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{
|
|
||||||
Beaufort cipher = new Beaufort(false, false, false);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String inputString = "messagetoencode";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "YAGEOLZRQUJMDAG";
|
|
||||||
String output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
inputString = "MESSAGETOENCODE";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZRQUJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
inputString = "message to encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZRQUJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
inputString = "message*to+encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZRQUJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol encoding
|
|
||||||
inputString = "Message to^encode";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "YAGEOLZRQUJMDAG";
|
|
||||||
output = cipher.encode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testConstructor_default(){
|
||||||
Beaufort cipher = new Beaufort(true, true, true);
|
cipher = new Beaufort();
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String inputString = "yageolzrqujmdag";
|
assertEquals("", cipher.outputString);
|
||||||
String keyword = "keyword";
|
assertEquals("", cipher.keyword);
|
||||||
String correctOutput = "messagetoencode";
|
assertFalse(cipher.preserveCapitals);
|
||||||
String output = cipher.decode(keyword, inputString);
|
assertFalse(cipher.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertFalse(cipher.preserveSymbols);
|
||||||
//Test uppercase decoding
|
assertFalse(cipher.atbash.preserveCapitals);
|
||||||
inputString = "YAGEOLZRQUJMDAG";
|
assertFalse(cipher.atbash.preserveWhitespace);
|
||||||
keyword = "keyword";
|
assertFalse(cipher.atbash.preserveSymbols);
|
||||||
correctOutput = "MESSAGETOENCODE";
|
assertFalse(cipher.caesar.preserveCapitals);
|
||||||
output = cipher.decode(keyword, inputString);
|
assertFalse(cipher.caesar.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertFalse(cipher.caesar.preserveSymbols);
|
||||||
|
assertFalse(cipher.vigenere.preserveCapitals);
|
||||||
//Test whitespace decoding
|
assertFalse(cipher.vigenere.preserveWhitespace);
|
||||||
inputString = "yageolz rq ujmdag";
|
assertFalse(cipher.vigenere.preserveSymbols);
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "message to encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "yageolz*rq+ujmdag";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "message*to+encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Yageolz rq^ujmdag";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Message to^encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testConstructor_preservesCapitals(){
|
||||||
Beaufort cipher = new Beaufort(false, true, true);
|
cipher = new Beaufort(true, false, false);
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String inputString = "yageolzrqujmdag";
|
assertEquals("", cipher.outputString);
|
||||||
String keyword = "keyword";
|
assertEquals("", cipher.keyword);
|
||||||
String correctOutput = "MESSAGETOENCODE";
|
assertTrue(cipher.preserveCapitals);
|
||||||
String output = cipher.decode(keyword, inputString);
|
assertFalse(cipher.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertFalse(cipher.preserveSymbols);
|
||||||
//Test uppercase decoding
|
assertTrue(cipher.atbash.preserveCapitals);
|
||||||
inputString = "YAGEOLZRQUJMDAG";
|
assertFalse(cipher.atbash.preserveWhitespace);
|
||||||
keyword = "keyword";
|
assertFalse(cipher.atbash.preserveSymbols);
|
||||||
correctOutput = "MESSAGETOENCODE";
|
assertTrue(cipher.caesar.preserveCapitals);
|
||||||
output = cipher.decode(keyword, inputString);
|
assertFalse(cipher.caesar.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertFalse(cipher.caesar.preserveSymbols);
|
||||||
|
assertTrue(cipher.vigenere.preserveCapitals);
|
||||||
//Test whitespace decoding
|
assertFalse(cipher.vigenere.preserveWhitespace);
|
||||||
inputString = "yageolz rq ujmdag";
|
assertFalse(cipher.vigenere.preserveSymbols);
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGE TO ENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "yageolz*rq+ujmdag";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGE*TO+ENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Yageolz rq^ujmdag";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGE TO^ENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testConstructor_preservesWhitespace(){
|
||||||
Beaufort cipher = new Beaufort(true, false, true);
|
cipher = new Beaufort(false, true, false);
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String inputString = "yageolzrqujmdag";
|
assertEquals("", cipher.outputString);
|
||||||
String keyword = "keyword";
|
assertEquals("", cipher.keyword);
|
||||||
String correctOutput = "messagetoencode";
|
assertFalse(cipher.preserveCapitals);
|
||||||
String output = cipher.decode(keyword, inputString);
|
assertTrue(cipher.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertFalse(cipher.preserveSymbols);
|
||||||
//Test uppercase decoding
|
assertFalse(cipher.atbash.preserveCapitals);
|
||||||
inputString = "YAGEOLZRQUJMDAG";
|
assertTrue(cipher.atbash.preserveWhitespace);
|
||||||
keyword = "keyword";
|
assertFalse(cipher.atbash.preserveSymbols);
|
||||||
correctOutput = "MESSAGETOENCODE";
|
assertFalse(cipher.caesar.preserveCapitals);
|
||||||
output = cipher.decode(keyword, inputString);
|
assertTrue(cipher.caesar.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertFalse(cipher.caesar.preserveSymbols);
|
||||||
|
assertFalse(cipher.vigenere.preserveCapitals);
|
||||||
//Test whitespace decoding
|
assertTrue(cipher.vigenere.preserveWhitespace);
|
||||||
inputString = "yageolz rq ujmdag";
|
assertFalse(cipher.vigenere.preserveSymbols);
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "yageolz*rq+ujmdag";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "message*to+encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Yageolz rq^ujmdag";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Messageto^encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testConstructor_preservesSymbols(){
|
||||||
Beaufort cipher = new Beaufort(true, true, false);
|
cipher = new Beaufort(false, false, true);
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String inputString = "yageolzrqujmdag";
|
assertEquals("", cipher.outputString);
|
||||||
String keyword = "keyword";
|
assertEquals("", cipher.keyword);
|
||||||
String correctOutput = "messagetoencode";
|
assertFalse(cipher.preserveCapitals);
|
||||||
String output = cipher.decode(keyword, inputString);
|
assertFalse(cipher.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertTrue(cipher.preserveSymbols);
|
||||||
//Test uppercase decoding
|
assertFalse(cipher.atbash.preserveCapitals);
|
||||||
inputString = "YAGEOLZRQUJMDAG";
|
assertFalse(cipher.atbash.preserveWhitespace);
|
||||||
keyword = "keyword";
|
assertTrue(cipher.atbash.preserveSymbols);
|
||||||
correctOutput = "MESSAGETOENCODE";
|
assertFalse(cipher.caesar.preserveCapitals);
|
||||||
output = cipher.decode(keyword, inputString);
|
assertFalse(cipher.caesar.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertTrue(cipher.caesar.preserveSymbols);
|
||||||
|
assertFalse(cipher.vigenere.preserveCapitals);
|
||||||
//Test whitespace decoding
|
assertFalse(cipher.vigenere.preserveWhitespace);
|
||||||
inputString = "yageolz rq ujmdag";
|
assertTrue(cipher.vigenere.preserveSymbols);
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "message to encode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
inputString = "yageolz*rq+ujmdag";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "messagetoencode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
|
||||||
inputString = "Yageolz rq^ujmdag";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "Message toencode";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
|
public void testSetInputString(){
|
||||||
Beaufort cipher = new Beaufort(false, false, false);
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
|
||||||
//Test lowercase decoding
|
cipher.setInputString(decodedString);
|
||||||
String inputString = "yageolzrqujmdag";
|
|
||||||
String keyword = "keyword";
|
|
||||||
String correctOutput = "MESSAGETOENCODE";
|
|
||||||
String output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
inputString = "YAGEOLZRQUJMDAG";
|
|
||||||
keyword = "keyword";
|
|
||||||
correctOutput = "MESSAGETOENCODE";
|
|
||||||
output = cipher.decode(keyword, inputString);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
assertEquals(decodedString, cipher.inputString);
|
||||||
inputString = "yageolz rq ujmdag";
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
keyword = "keyword";
|
verify(logger, never()).debug("Removing case");
|
||||||
correctOutput = "MESSAGETOENCODE";
|
verify(logger, never()).debug("Removing whitespace");
|
||||||
output = cipher.decode(keyword, inputString);
|
verify(logger, never()).debug("Removing symbols");
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
//Test symbol decoding
|
@Test
|
||||||
inputString = "yageolz*rq+ujmdag";
|
public void testSetInputString_noCapitals(){
|
||||||
keyword = "keyword";
|
cipher.preserveCapitals = false;
|
||||||
correctOutput = "MESSAGETOENCODE";
|
cipher.preserveWhitespace = true;
|
||||||
output = cipher.decode(keyword, inputString);
|
cipher.preserveSymbols = true;
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, symbol decoding
|
cipher.setInputString(decodedString);
|
||||||
inputString = "Yageolz rq^ujmdag";
|
|
||||||
keyword = "keyword";
|
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||||
correctOutput = "MESSAGETOENCODE";
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
output = cipher.decode(keyword, inputString);
|
verify(logger, times(1)).debug("Removing case");
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, never()).debug("Removing whitespace");
|
||||||
|
verify(logger, never()).debug("Removing symbols");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
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", ""));
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString_noSymbols(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
cipher.preserveSymbols = false;
|
||||||
|
|
||||||
|
cipher.setInputString(decodedString);
|
||||||
|
|
||||||
|
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||||
|
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||||
|
verify(logger, never()).debug("Removing case");
|
||||||
|
verify(logger, never()).debug("Removing whitespace");
|
||||||
|
verify(logger, times(1)).debug("Removing symbols");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||||
|
verify(logger, times(1)).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 '{}'", "");
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString_null(){
|
||||||
|
assertThrows(InvalidInputException.class, () -> {
|
||||||
|
cipher.setInputString(null);
|
||||||
|
}, "Input must conatin 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());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetKeyword(){
|
||||||
|
cipher.setKeyword(keyword);
|
||||||
|
|
||||||
|
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||||
|
verify(logger, times(1)).debug("Original keyword '{}'", keyword);
|
||||||
|
verify(logger, times(1)).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Removing all non-letters");
|
||||||
|
verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase());
|
||||||
|
verify(logger, times(2)).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetKeyword_dirty(){
|
||||||
|
cipher.setKeyword(keywordDirty);
|
||||||
|
|
||||||
|
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||||
|
verify(logger, times(1)).debug("Original keyword '{}'", keywordDirty);
|
||||||
|
verify(logger, times(1)).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Removing all non-letters");
|
||||||
|
verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase());
|
||||||
|
verify(logger, times(2)).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetKeyword_blank(){
|
||||||
|
assertThrows(InvalidKeywordException.class, () -> {
|
||||||
|
cipher.setKeyword("");
|
||||||
|
}, "Keyword must contain at least 2 letters");
|
||||||
|
|
||||||
|
assertEquals("", cipher.keyword);
|
||||||
|
verify(logger, times(1)).debug("Original keyword '{}'", "");
|
||||||
|
verify(logger, times(1)).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Removing all non-letters");
|
||||||
|
verify(logger, times(1)).debug("Cleaned keyword '{}'", "");
|
||||||
|
verify(logger, times(2)).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetKeyword_short(){
|
||||||
|
assertThrows(InvalidKeywordException.class, () -> {
|
||||||
|
cipher.setKeyword("A");
|
||||||
|
}, "Keyword must contain at least 2 letters");
|
||||||
|
|
||||||
|
assertEquals("A", cipher.keyword);
|
||||||
|
verify(logger, times(1)).debug("Original keyword '{}'", "A");
|
||||||
|
verify(logger, times(1)).debug("Removing case");
|
||||||
|
verify(logger, times(1)).debug("Removing all non-letters");
|
||||||
|
verify(logger, times(1)).debug("Cleaned keyword '{}'", "A");
|
||||||
|
verify(logger, times(2)).debug(anyString());
|
||||||
|
verify(logger, times(2)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetKeyword_null(){
|
||||||
|
assertThrows(InvalidKeywordException.class, () -> {
|
||||||
|
cipher.setKeyword(null);
|
||||||
|
}, "Keyword cannot be null");
|
||||||
|
|
||||||
|
assertEquals("", cipher.keyword);
|
||||||
|
verify(logger, never()).debug(eq("Original keyword '{}'"), anyString());
|
||||||
|
verify(logger, never()).debug("Removing case");
|
||||||
|
verify(logger, never()).debug("Removing all non-letters");
|
||||||
|
verify(logger, never()).debug(eq("Cleaned keyword '{}'"), anyString());
|
||||||
|
verify(logger, never()).debug(anyString());
|
||||||
|
verify(logger, never()).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncode(){
|
||||||
|
cipher = new Beaufort(true, true, true);
|
||||||
|
cipher.keyword = keyword.toUpperCase();
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
logger = mock(Logger.class);
|
||||||
|
Beaufort.logger = logger;
|
||||||
|
|
||||||
|
cipher.encode();
|
||||||
|
|
||||||
|
assertEquals(encodedString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Encoding");
|
||||||
|
verify(logger, times(1)).debug("Encoding with Atbash");
|
||||||
|
verify(logger, times(1)).debug("Shifting all letters by 1");
|
||||||
|
verify(logger, times(1)).debug("Encoding with Vigenere");
|
||||||
|
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||||
|
verify(logger, times(4)).debug(anyString());
|
||||||
|
verify(logger, times(1)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecode(){
|
||||||
|
cipher = new Beaufort(true, true, true);
|
||||||
|
cipher.keyword = keyword.toUpperCase();
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
logger = mock(Logger.class);
|
||||||
|
Beaufort.logger = logger;
|
||||||
|
|
||||||
|
cipher.decode();
|
||||||
|
|
||||||
|
assertEquals(encodedString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Decoding");
|
||||||
|
verify(logger, times(1)).debug("Encoding with Atbash");
|
||||||
|
verify(logger, times(1)).debug("Shifting all letters by 1");
|
||||||
|
verify(logger, times(1)).debug("Encoding with Vigenere");
|
||||||
|
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||||
|
verify(logger, times(4)).debug(anyString());
|
||||||
|
verify(logger, times(1)).debug(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetters(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
cipher.keyword = keyword;
|
||||||
|
|
||||||
|
assertEquals(decodedString, cipher.inputString);
|
||||||
|
assertEquals(encodedString, cipher.outputString);
|
||||||
|
assertEquals(keyword, cipher.keyword);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReset(){
|
||||||
|
cipher.inputString = decodedString;
|
||||||
|
cipher.outputString = encodedString;
|
||||||
|
cipher.keyword = keyword;
|
||||||
|
|
||||||
|
cipher.reset();
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals("", cipher.keyword);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
cipher = new Beaufort(false, false, false);
|
||||||
|
output = cipher.encode(keyword, decodedString);
|
||||||
|
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
|
||||||
|
cipher = new Beaufort(false, false, false);
|
||||||
|
output = cipher.decode(keyword, encodedString);
|
||||||
|
assertEquals(decodedStringClean, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,628 +1,405 @@
|
|||||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestCaesar.java
|
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestCaesar.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 07-25-21
|
// Created: 07-25-21
|
||||||
//Modified: 07-09-22
|
//Modified: 04-16-23
|
||||||
package com.mattrixwv.cipherstream.monosubstitution;
|
package com.mattrixwv.cipherstream.monosubstitution;
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyChar;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||||
|
|
||||||
|
|
||||||
public class TestCaesar{
|
public class TestCaesar{
|
||||||
@Test
|
private Caesar cipher;
|
||||||
public void testEncode() throws InvalidInputException{
|
private Logger logger;
|
||||||
Caesar cipher = new Caesar(true, true, true);
|
//Variables
|
||||||
|
private String inputString = "The quick brown fox jumps over - the lAzy dog";
|
||||||
//Test lowercase encode
|
private String inputStringClean = "thequickbrownfoxjumpsoverthelazydog";
|
||||||
String input = "abc";
|
private String outputString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb iXwv ald";
|
||||||
int shift = 3;
|
private String outputStringClean = "qebnrfzhyoltkclugrjmplsboqebixwvald";
|
||||||
String correctOutput = "def";
|
private int shift = 23;
|
||||||
String output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
input = "ABC";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "DEF";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test out of bounds shift encoding
|
|
||||||
input = "abc";
|
|
||||||
shift = 29;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test out of bounds shift encoding negative
|
|
||||||
input = "abc";
|
|
||||||
shift = -23;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
//Test whitespace encoding
|
public void setup(){
|
||||||
input = "abc def";
|
cipher = new Caesar();
|
||||||
shift = 3;
|
logger = mock(Logger.class);
|
||||||
correctOutput = "def ghi";
|
Caesar.logger = logger;
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
input = "abc-def@";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "def-ghi@";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
input = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
shift = 23;
|
|
||||||
correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test mixed case, whitespace, and symbol encoding with negative shift
|
|
||||||
input = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
shift = -3;
|
|
||||||
correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalEncode() throws InvalidInputException{
|
|
||||||
Caesar cipher = new Caesar(false, true, true);
|
|
||||||
|
|
||||||
//Test lowercase encode
|
|
||||||
String input = "abc";
|
|
||||||
int shift = 3;
|
|
||||||
String correctOutput = "def";
|
|
||||||
String output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
input = "ABC";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test out of bounds shift encoding
|
|
||||||
input = "abc";
|
|
||||||
shift = 29;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test out of bounds shift encoding negative
|
|
||||||
input = "abc";
|
|
||||||
shift = -23;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
input = "abc def";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "def ghi";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
input = "abc-def@";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "def-ghi@";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
input = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
shift = 23;
|
|
||||||
correctOutput = "qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test mixed case, whitespace, and symbol encoding with negative shift
|
|
||||||
input = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
shift = -3;
|
|
||||||
correctOutput = "qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoWhitespaceEncode() throws InvalidInputException{
|
|
||||||
Caesar cipher = new Caesar(true, false, true);
|
|
||||||
|
|
||||||
//Test lowercase encoding
|
|
||||||
String input = "abc";
|
|
||||||
int shift = 3;
|
|
||||||
String correctOutput = "def";
|
|
||||||
String output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
input = "ABC";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "DEF";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test out of bounds shift encoding
|
|
||||||
input = "abc";
|
|
||||||
shift = 29;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test out of bounds shift encoding negative
|
|
||||||
input = "abc";
|
|
||||||
shift = -23;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
input = "abc def";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "defghi";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
input = "abc-def@";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "def-ghi@";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Testing mixed case, whitespace, and symbol encoding
|
|
||||||
input = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
shift = 23;
|
|
||||||
correctOutput = "Qebnrfzhyoltkclugrjmplsbo-qebixwvald";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Testing mixed case, whitespace, and symbol encoding
|
|
||||||
input = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
shift = -3;
|
|
||||||
correctOutput = "Qebnrfzhyoltkclugrjmplsbo-qebixwvald";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoSymbolEncode() throws InvalidInputException{
|
|
||||||
Caesar cipher = new Caesar(true, true, false);
|
|
||||||
|
|
||||||
//Test lowercase encode
|
|
||||||
String input = "abc";
|
|
||||||
int shift = 3;
|
|
||||||
String correctOutput = "def";
|
|
||||||
String output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
input = "ABC";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "DEF";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test out of bounds shift encoding
|
|
||||||
input = "abc";
|
|
||||||
shift = 29;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test out of bounds shift encoding negative
|
|
||||||
input = "abc";
|
|
||||||
shift = -23;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
input = "abc def";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "def ghi";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
input = "abc-def@";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "defghi";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
input = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
shift = 23;
|
|
||||||
correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//test mixed case, whitespace, and symbol encoding with negative shift
|
|
||||||
input = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
shift = -3;
|
|
||||||
correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidInputException{
|
|
||||||
Caesar cipher = new Caesar(false, false, false);
|
|
||||||
|
|
||||||
//Test lowercase encode
|
|
||||||
String input = "abc";
|
|
||||||
int shift = 3;
|
|
||||||
String correctOutput = "def";
|
|
||||||
String output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase encoding
|
|
||||||
input = "ABC";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test out of bounds shift encoding
|
|
||||||
input = "abc";
|
|
||||||
shift = 29;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test out of bounds shift encoding negative
|
|
||||||
input = "abc";
|
|
||||||
shift = 29;
|
|
||||||
correctOutput = "def";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test whitespace encoding
|
|
||||||
input = "abc def";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "defghi";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test symbol encoding
|
|
||||||
input = "abc-def@";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "defghi";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol encoding
|
|
||||||
input = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
shift = 23;
|
|
||||||
correctOutput = "qebnrfzhyoltkclugrjmplsboqebixwvald";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test mixed case, whitespace, and symbol encoding with negative shift
|
|
||||||
input = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
shift = -3;
|
|
||||||
correctOutput = "qebnrfzhyoltkclugrjmplsboqebixwvald";
|
|
||||||
output = cipher.encode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecode() throws InvalidInputException{
|
public void testConstructor_default(){
|
||||||
Caesar cipher = new Caesar(true, true, true);
|
cipher = new Caesar();
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String input = "def";
|
assertEquals("", cipher.outputString);
|
||||||
int shift = 3;
|
assertEquals(0, cipher.shift);
|
||||||
String correctOutput = "abc";
|
assertFalse(cipher.preserveCapitals);
|
||||||
String output = cipher.decode(shift, input);
|
assertFalse(cipher.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertFalse(cipher.preserveSymbols);
|
||||||
//Test uppercase decoding
|
|
||||||
input = "DEF";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "ABC";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test out of bounds shift decoding
|
|
||||||
input = "def";
|
|
||||||
shift = 29;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test out of bounds shift negative decoding
|
|
||||||
input = "def";
|
|
||||||
shift = -23;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
input = "def ghi";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abc def";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
input = "def-ghi@";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abc-def@";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
shift = 23;
|
|
||||||
correctOutput = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test mixed case, whitespace, and symbol decoding with negative shift
|
|
||||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
shift = -3;
|
|
||||||
correctOutput = "The quick brown fox jumps over - the lazy dog";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalDecode() throws InvalidInputException{
|
public void testConstructor_preservesCapitals(){
|
||||||
Caesar cipher = new Caesar(false, true, true);
|
cipher = new Caesar(true, false, false);
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String input = "def";
|
assertEquals("", cipher.outputString);
|
||||||
int shift = 3;
|
assertEquals(0, cipher.shift);
|
||||||
String correctOutput = "abc";
|
assertTrue(cipher.preserveCapitals);
|
||||||
String output = cipher.decode(shift, input);
|
assertFalse(cipher.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertFalse(cipher.preserveSymbols);
|
||||||
//Test uppercase decoding
|
|
||||||
input = "DEF";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test out of bounds shift decoding
|
|
||||||
input = "def";
|
|
||||||
shift = 29;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test out of bounds shift negative decoding
|
|
||||||
input = "def";
|
|
||||||
shift = -23;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
input = "def ghi";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abc def";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
input = "def-ghi@";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abc-def@";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
shift = 23;
|
|
||||||
correctOutput = "the quick brown fox jumps over - the lazy dog";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test mixed case, whitespace, and symbol with negative shift
|
|
||||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
shift = -3;
|
|
||||||
correctOutput = "the quick brown fox jumps over - the lazy dog";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoWhitespaceDecode() throws InvalidInputException{
|
public void testConstructor_preservesSymbols(){
|
||||||
Caesar cipher = new Caesar(true, false, true);
|
cipher = new Caesar(false, true, false);
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String input = "def";
|
assertEquals("", cipher.outputString);
|
||||||
int shift = 3;
|
assertEquals(0, cipher.shift);
|
||||||
String correctOutput = "abc";
|
assertFalse(cipher.preserveCapitals);
|
||||||
String output = cipher.decode(shift, input);
|
assertTrue(cipher.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertFalse(cipher.preserveSymbols);
|
||||||
//Test uppercase decoding
|
|
||||||
input = "DEF";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "ABC";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test out of bounds shift decoding
|
|
||||||
input = "def";
|
|
||||||
shift = 29;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test out of bounds shift negative decoding
|
|
||||||
input = "def";
|
|
||||||
shift = -23;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test whitespace decoding
|
|
||||||
input = "def ghi";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abcdef";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
input = "def-ghi@";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abc-def@";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
shift = 23;
|
|
||||||
correctOutput = "Thequickbrownfoxjumpsover-thelazydog";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test mixed case, whitespace, and symbol decoding with negative shift
|
|
||||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
shift = -3;
|
|
||||||
correctOutput = "Thequickbrownfoxjumpsover-thelazydog";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoSymbolDecode() throws InvalidInputException{
|
public void testConstructor_preservesWhitespace(){
|
||||||
Caesar cipher = new Caesar(true, true, false);
|
cipher = new Caesar(false, false, true);
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals("", cipher.inputString);
|
||||||
String input = "def";
|
assertEquals("", cipher.outputString);
|
||||||
int shift = 3;
|
assertEquals(0, cipher.shift);
|
||||||
String correctOutput = "abc";
|
assertFalse(cipher.preserveCapitals);
|
||||||
String output = cipher.decode(shift, input);
|
assertFalse(cipher.preserveWhitespace);
|
||||||
assertEquals(correctOutput, output);
|
assertTrue(cipher.preserveSymbols);
|
||||||
//Test uppercase decoding
|
|
||||||
input = "DEF";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "ABC";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test out of bounds shift decoding
|
|
||||||
input = "def";
|
|
||||||
shift = 29;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test out of bounds shift negative decoding
|
|
||||||
input = "def";
|
|
||||||
shift = -23;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test whitepace decoding
|
|
||||||
input = "def ghi";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abc def";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test symbol decoding
|
|
||||||
input = "def-ghi!";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abcdef";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
|
||||||
//Test mixed case, whitespace, and symbol decoding
|
|
||||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
shift = 23;
|
|
||||||
correctOutput = "The quick brown fox jumps over the lazy dog";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test mixed case, whitespace, and symbol decoding with negative shift
|
|
||||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
shift = -3;
|
|
||||||
correctOutput = "The quick brown fox jumps over the lazy dog";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidInputException{
|
public void testSetShift(){
|
||||||
Caesar cipher = new Caesar(false, false, false);
|
cipher.setShift(shift);
|
||||||
|
|
||||||
//Test lowercase decoding
|
assertEquals(shift, cipher.shift);
|
||||||
String input = "def";
|
verify(logger, times(1)).debug("Setting shift {}", shift);
|
||||||
int shift = 3;
|
verify(logger, times(1)).debug("Cleaned shift {}", shift);
|
||||||
String correctOutput = "abc";
|
}
|
||||||
String output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test uppercase decoding
|
|
||||||
input = "DEF";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetShift_large(){
|
||||||
|
cipher.setShift(shift + 26);
|
||||||
|
|
||||||
//Test out of bounds shift decoding
|
assertEquals(shift, cipher.shift);
|
||||||
input = "def";
|
verify(logger, times(1)).debug("Setting shift {}", shift + 26);
|
||||||
shift = 29;
|
verify(logger, times(1)).debug("Cleaned shift {}", shift);
|
||||||
correctOutput = "abc";
|
}
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test out of bounds shift negative decoding
|
|
||||||
input = "def";
|
|
||||||
shift = -23;
|
|
||||||
correctOutput = "abc";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetShift_negative(){
|
||||||
|
cipher.setShift(shift - 26);
|
||||||
|
|
||||||
//Test whitespace decoding
|
assertEquals(shift - 26, cipher.shift);
|
||||||
input = "def ghi";
|
verify(logger, times(1)).debug("Setting shift {}", shift - 26);
|
||||||
shift = 3;
|
verify(logger, times(1)).debug("Cleaned shift {}", shift - 26);
|
||||||
correctOutput = "abcdef";
|
}
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetInputString(){
|
||||||
|
cipher.preserveCapitals = true;
|
||||||
|
cipher.preserveWhitespace = true;
|
||||||
|
cipher.preserveSymbols = true;
|
||||||
|
|
||||||
//Test symbol decoding
|
cipher.setInputString(inputString);
|
||||||
input = "def-ghi@";
|
|
||||||
shift = 3;
|
|
||||||
correctOutput = "abcdef";
|
|
||||||
output = cipher.decode(shift, input);
|
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
|
|
||||||
|
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 mixed case, whitespace, and symbol decoding
|
@Test
|
||||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
public void testSetInputString_noCapitals(){
|
||||||
shift = 23;
|
cipher.preserveCapitals = false;
|
||||||
correctOutput = "thequickbrownfoxjumpsoverthelazydog";
|
cipher.preserveWhitespace = true;
|
||||||
output = cipher.decode(shift, input);
|
cipher.preserveSymbols = true;
|
||||||
assertEquals(correctOutput, output);
|
|
||||||
//Test mixed case, whitespace, and symbol decoding with negative shift
|
cipher.setInputString(inputString);
|
||||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
|
||||||
shift = -3;
|
assertEquals(inputString.toLowerCase(), cipher.inputString);
|
||||||
correctOutput = "thequickbrownfoxjumpsoverthelazydog";
|
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||||
output = cipher.decode(shift, input);
|
verify(logger, times(1)).debug("Removing case");
|
||||||
assertEquals(correctOutput, output);
|
verify(logger, never()).debug("Removing whitespace");
|
||||||
|
verify(logger, never()).debug("Removing symbols");
|
||||||
|
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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("");
|
||||||
|
}, "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 must contain at least 1 letter");
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
verify(logger, never()).debug("Original input string '{}'", "");
|
||||||
|
verify(logger, never()).debug("Removing case");
|
||||||
|
verify(logger, never()).debug("Removing whitespace");
|
||||||
|
verify(logger, never()).debug("Removing symbols");
|
||||||
|
verify(logger, never()).debug("Cleaned input string '{}'", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncode(){
|
||||||
|
cipher.inputString = inputString;
|
||||||
|
cipher.shift = shift;
|
||||||
|
|
||||||
|
cipher.encode();
|
||||||
|
|
||||||
|
assertEquals(outputString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Encoding");
|
||||||
|
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
|
||||||
|
verify(logger, times(2)).debug("Encoding uppercase");
|
||||||
|
verify(logger, never()).debug("Wrapping arround to Z");
|
||||||
|
verify(logger, times(1)).debug("Wrapping around to A");
|
||||||
|
verify(logger, times(33)).debug("Encoding lowercase");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncode_negative(){
|
||||||
|
cipher.inputString = inputString;
|
||||||
|
cipher.shift = shift - 26;
|
||||||
|
|
||||||
|
cipher.encode();
|
||||||
|
|
||||||
|
assertEquals(outputString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Encoding");
|
||||||
|
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
|
||||||
|
verify(logger, times(2)).debug("Encoding uppercase");
|
||||||
|
verify(logger, times(1)).debug("Wrapping around to Z");
|
||||||
|
verify(logger, never()).debug("Wrapping around to A");
|
||||||
|
verify(logger, times(33)).debug("Encoding lowercase");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecode(){
|
||||||
|
cipher.inputString = outputString;
|
||||||
|
cipher.shift = shift;
|
||||||
|
|
||||||
|
cipher.decode();
|
||||||
|
|
||||||
|
assertEquals(inputString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Decoding");
|
||||||
|
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
|
||||||
|
verify(logger, times(2)).debug("Decoding uppercase");
|
||||||
|
verify(logger, times(1)).debug("Wrapping around to Z");
|
||||||
|
verify(logger, never()).debug("Wrapping around to A");
|
||||||
|
verify(logger, times(33)).debug("Decoding lowercase");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecode_negative(){
|
||||||
|
cipher.inputString = outputString;
|
||||||
|
cipher.shift = shift - 26;
|
||||||
|
|
||||||
|
cipher.decode();
|
||||||
|
|
||||||
|
assertEquals(inputString, cipher.outputString);
|
||||||
|
verify(logger, times(1)).debug("Decoding");
|
||||||
|
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
|
||||||
|
verify(logger, times(2)).debug("Decoding uppercase");
|
||||||
|
verify(logger, never()).debug("Wrapping around to Z");
|
||||||
|
verify(logger, times(1)).debug("Wrapping around to A");
|
||||||
|
verify(logger, times(33)).debug("Decoding lowercase");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetters(){
|
||||||
|
cipher.inputString = inputString;
|
||||||
|
cipher.outputString = outputString;
|
||||||
|
cipher.shift = shift;
|
||||||
|
|
||||||
|
assertEquals(inputString, cipher.getInputString());
|
||||||
|
assertEquals(outputString, cipher.getOutputString());
|
||||||
|
assertEquals(shift, cipher.getShift());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReset(){
|
||||||
|
cipher.inputString = inputString;
|
||||||
|
cipher.outputString = outputString;
|
||||||
|
cipher.shift = shift;
|
||||||
|
|
||||||
|
cipher.reset();
|
||||||
|
|
||||||
|
assertEquals("", cipher.inputString);
|
||||||
|
assertEquals("", cipher.outputString);
|
||||||
|
assertEquals(0, cipher.shift);
|
||||||
|
verify(logger, times(1)).debug("Resetting fields");
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
assertEquals(shift, cipher.shift);
|
||||||
|
assertEquals(outputString, cipher.outputString);
|
||||||
|
assertEquals(outputString, 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);
|
||||||
|
assertEquals(shift, cipher.shift);
|
||||||
|
assertEquals(outputStringClean, cipher.outputString);
|
||||||
|
assertEquals(outputStringClean, 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);
|
||||||
|
assertEquals(shift, cipher.shift);
|
||||||
|
assertEquals(outputString, cipher.outputString);
|
||||||
|
assertEquals(outputString, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPracticalDecoding(){
|
||||||
|
cipher = new Caesar(true, true, true);
|
||||||
|
logger = mock(Logger.class);
|
||||||
|
Caesar.logger = logger;
|
||||||
|
|
||||||
|
String output = cipher.decode(shift, outputString);
|
||||||
|
|
||||||
|
assertEquals(outputString, cipher.inputString);
|
||||||
|
assertEquals(shift, cipher.shift);
|
||||||
|
assertEquals(inputString, cipher.outputString);
|
||||||
|
assertEquals(inputString, 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);
|
||||||
|
|
||||||
|
assertEquals(outputStringClean, cipher.inputString);
|
||||||
|
assertEquals(shift, cipher.shift);
|
||||||
|
assertEquals(inputStringClean, cipher.outputString);
|
||||||
|
assertEquals(inputStringClean, 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);
|
||||||
|
|
||||||
|
assertEquals(outputString, cipher.inputString);
|
||||||
|
assertEquals(shift - 26, cipher.shift);
|
||||||
|
assertEquals(inputString, cipher.outputString);
|
||||||
|
assertEquals(inputString, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user