Added more unit test coverage
This commit is contained in:
@@ -1,408 +1,348 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestBifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 07-09-22
|
||||
//Modified: 04-23-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyChar;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class TestBifid{
|
||||
@Test
|
||||
public void testEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, true);
|
||||
private Bifid cipher;
|
||||
private Logger logger;
|
||||
//Test
|
||||
private String inputString = "Message to^encode";
|
||||
private String inputStringClean = "MESSAGETOENCODE";
|
||||
private String outputString = "Mqaokne kc^vdodzd";
|
||||
private String outputStringClean = "MQAOKNEKCVDODZD";
|
||||
private String keyword = "keyword";
|
||||
private String keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ";
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup(){
|
||||
cipher = new Bifid();
|
||||
logger = mock(Logger.class);
|
||||
Bifid.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testConstructor_default(){
|
||||
cipher = new Bifid();
|
||||
|
||||
assertFalse(cipher.preserveCapitals);
|
||||
assertFalse(cipher.preserveWhitespace);
|
||||
assertFalse(cipher.preserveSymbols);
|
||||
assertNotNull(cipher.polybiusSquare);
|
||||
assertFalse(cipher.polybiusSquare.preserveWhitespace);
|
||||
assertFalse(cipher.polybiusSquare.preserveSymbols);
|
||||
assertEquals("", cipher.inputString);
|
||||
assertEquals("", cipher.keyword);
|
||||
assertEquals("", cipher.outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_preservesCapitals(){
|
||||
cipher = new Bifid(true, false, false);
|
||||
|
||||
assertTrue(cipher.preserveCapitals);
|
||||
assertFalse(cipher.preserveWhitespace);
|
||||
assertFalse(cipher.preserveSymbols);
|
||||
assertNotNull(cipher.polybiusSquare);
|
||||
assertFalse(cipher.polybiusSquare.preserveWhitespace);
|
||||
assertFalse(cipher.polybiusSquare.preserveSymbols);
|
||||
assertEquals("", cipher.inputString);
|
||||
assertEquals("", cipher.keyword);
|
||||
assertEquals("", cipher.outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_preservesWhitespace(){
|
||||
cipher = new Bifid(false, true, false);
|
||||
|
||||
assertFalse(cipher.preserveCapitals);
|
||||
assertTrue(cipher.preserveWhitespace);
|
||||
assertFalse(cipher.preserveSymbols);
|
||||
assertNotNull(cipher.polybiusSquare);
|
||||
assertFalse(cipher.polybiusSquare.preserveWhitespace);
|
||||
assertFalse(cipher.polybiusSquare.preserveSymbols);
|
||||
assertEquals("", cipher.inputString);
|
||||
assertEquals("", cipher.keyword);
|
||||
assertEquals("", cipher.outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_preservesSymbols(){
|
||||
cipher = new Bifid(false, false, true);
|
||||
|
||||
assertFalse(cipher.preserveCapitals);
|
||||
assertFalse(cipher.preserveWhitespace);
|
||||
assertTrue(cipher.preserveSymbols);
|
||||
assertNotNull(cipher.polybiusSquare);
|
||||
assertFalse(cipher.polybiusSquare.preserveWhitespace);
|
||||
assertFalse(cipher.polybiusSquare.preserveSymbols);
|
||||
assertEquals("", cipher.inputString);
|
||||
assertEquals("", cipher.keyword);
|
||||
assertEquals("", cipher.outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
|
||||
assertEquals(keyword, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Setting keyword '{}'", keyword);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword_null(){
|
||||
assertThrows(InvalidKeywordException.class, () -> {
|
||||
cipher.setKeyword(null);
|
||||
});
|
||||
|
||||
verify(logger, never()).debug(eq("Setting keyword '{}'"), anyString());
|
||||
verify(logger, never()).debug(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_noCapitals(){
|
||||
cipher.preserveCapitals = false;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_noWhitespace(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
|
||||
assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_noSymbols(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
|
||||
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_blank(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputString("");
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", "");
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_null(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputString(null);
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatOutput(){
|
||||
cipher.inputString = inputString;
|
||||
|
||||
cipher.formatOutput(outputStringClean);
|
||||
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output");
|
||||
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Altering uppercase");
|
||||
verify(logger, times(14)).debug("Altering lowercase");
|
||||
verify(logger, times(2)).debug("Adding symbol");
|
||||
verify(logger, times(1)).debug("Formatted output string '{}'", outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keyword;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(1)).debug("Encoding Polybius");
|
||||
verify(logger, times(1)).debug("Splitting Polybius Square message");
|
||||
verify(logger, times(30)).debug(eq("Current character '{}'"), anyChar());
|
||||
verify(logger, times(1)).debug("Decoding Polybius Square");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = outputString;
|
||||
cipher.keyword = keyword;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputString, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Encoding Polybius Square");
|
||||
verify(logger, times(1)).debug("Splitting Polybius Square message");
|
||||
verify(logger, times(15)).debug(eq("Current characters {} {}"), anyChar(), anyChar());
|
||||
verify(logger, times(1)).debug("Decoding Polybius Square");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.outputString = outputString;
|
||||
|
||||
assertEquals(inputString, cipher.getInputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(outputString, cipher.getOutputString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.outputString = outputString;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
assertEquals("", cipher.keyword);
|
||||
assertEquals("", cipher.outputString);
|
||||
verify(logger, times(1)).debug("Resetting fields");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Bifid(true, true, true);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "mqaoknekcvdodzd";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne kc vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne*kc+vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Mqaokne kc^vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(output, cipher.outputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, true, true);
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Bifid(false, false, false);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MQAOKNEKCVDODZD";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNE KC VDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNE*KC+VDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNE KC^VDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
assertEquals(inputStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputStringClean, cipher.outputString);
|
||||
assertEquals(outputStringClean, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, false, true);
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Bifid(true, true, true);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "mqaoknekcvdodzd";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaoknekcvdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne*kc+vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Mqaoknekc^vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputString, cipher.outputString);
|
||||
assertEquals(inputString, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, false);
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new Bifid(false, false, false);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "mqaoknekcvdodzd";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne kc vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaoknekcvdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Mqaokne kcvdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, false, false);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MQAOKNEKCVDODZD";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, true);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "messagetoencode";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message to encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message*to+encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Message to^encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, true, true);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MESSAGETOENCODE";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGE TO ENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGE*TO+ENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGE TO^ENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, false, true);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "messagetoencode";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "messagetoencode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message*to+encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Messageto^encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, false);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "messagetoencode";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message to encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "messagetoencode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Message toencode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, false, false);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MESSAGETOENCODE";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals(correctOutput, output);
|
||||
assertEquals(outputStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputStringClean, cipher.outputString);
|
||||
assertEquals(inputStringClean, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +30,10 @@ public class TestLargePolybiusSquare{
|
||||
private LargePolybiusSquare cipher;
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private String inputString = "Message to^encode";
|
||||
private String inputStringClean = "MESSAGETOENCODE";
|
||||
private String outputString = "35124343222612 4415^123624152112";
|
||||
private String outputStringClean = "31 15 41 41 11 21 15 42 33 15 32 13 33 14 15";
|
||||
private String decodedString = "Message to^encode";
|
||||
private String decodedStringClean = "MESSAGETOENCODE";
|
||||
private String encodedString = "35124343222612 4415^123624152112";
|
||||
private String encodedStringClean = "31 15 41 41 11 21 15 42 33 15 32 13 33 14 15";
|
||||
private String keyword = "keyword";
|
||||
private String keywordClean = "KEYWORDABCFGHIJLMNPQSTUVXZ0123456789";
|
||||
private String keywordBlank = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
@@ -114,13 +114,13 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputStringEncoding(inputString);
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -128,13 +128,13 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputStringEncoding(inputString);
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
|
||||
assertEquals(inputString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -142,13 +142,13 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputStringEncoding(inputString);
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
|
||||
assertEquals(inputString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -156,10 +156,10 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputStringEncoding(inputString);
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
|
||||
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", "M E S S A G E T O E N C O D E");
|
||||
@@ -171,7 +171,7 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringEncoding("*");
|
||||
cipher.setInputStringEncode("*");
|
||||
});
|
||||
|
||||
assertEquals("*", cipher.inputString);
|
||||
@@ -187,7 +187,7 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringEncoding("");
|
||||
cipher.setInputStringEncode("");
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
@@ -203,7 +203,7 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringEncoding(null);
|
||||
cipher.setInputStringEncode(null);
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
@@ -215,12 +215,12 @@ public class TestLargePolybiusSquare{
|
||||
|
||||
@Test
|
||||
public void testGetPreparedInputStringEncoding(){
|
||||
cipher.inputString = inputString.toUpperCase();
|
||||
cipher.inputString = decodedString.toUpperCase();
|
||||
|
||||
String preparedInputString = cipher.getPreparedInputStringEncoding();
|
||||
String preparedInputString = cipher.getPreparedInputStringEncode();
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(inputString.toUpperCase().replaceAll("[^A-Z0-9]", ""), preparedInputString);
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("[^A-Z0-9]", ""), preparedInputString);
|
||||
verify(logger, times(1)).debug("Preparing input string for encoding");
|
||||
verify(logger, times(1)).debug("Prepared input string '{}'", preparedInputString);
|
||||
}
|
||||
@@ -263,16 +263,16 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = inputString.toUpperCase();
|
||||
cipher.inputString = decodedString.toUpperCase();
|
||||
|
||||
cipher.addCharactersToCleanStringEncode(outputString.replaceAll("\\s", "").replaceAll("[^0-9]", ""));
|
||||
cipher.addCharactersToCleanStringEncode(encodedString.replaceAll("\\s", "").replaceAll("[^0-9]", ""));
|
||||
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string");
|
||||
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
|
||||
verify(logger, times(15)).debug("Appending character");
|
||||
verify(logger, times(2)).debug("Appending symbol");
|
||||
verify(logger, times(1)).debug("Saving output string {}", outputString);
|
||||
verify(logger, times(1)).debug("Saving output string {}", encodedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -281,21 +281,21 @@ public class TestLargePolybiusSquare{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = outputString;
|
||||
cipher.inputString = encodedString;
|
||||
|
||||
cipher.addCharactersToCleanStringDecode(inputStringClean);
|
||||
cipher.addCharactersToCleanStringDecode(decodedStringClean);
|
||||
|
||||
verify(logger, times(1)).debug("Formatting output string");
|
||||
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
|
||||
verify(logger, times(15)).debug("Appending character");
|
||||
verify(logger, times(2)).debug("Appending symbol");
|
||||
verify(logger, times(1)).debug("Saving output string {}", inputString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Saving output string {}", decodedString.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.outputString = outputString;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.grid = grid;
|
||||
|
||||
@@ -313,47 +313,47 @@ public class TestLargePolybiusSquare{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new LargePolybiusSquare(true, true);
|
||||
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(outputString, output);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new LargePolybiusSquare(false, false);
|
||||
|
||||
String output = cipher.encode(inputString);
|
||||
String output = cipher.encode(decodedString);
|
||||
|
||||
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
|
||||
assertEquals(keywordBlank, cipher.keyword);
|
||||
assertEquals(outputStringClean, cipher.outputString);
|
||||
assertEquals(outputStringClean, output);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new LargePolybiusSquare(true, true);
|
||||
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputString.toUpperCase(), cipher.outputString);
|
||||
assertEquals(inputString.toUpperCase(), output);
|
||||
assertEquals(decodedString.toUpperCase(), cipher.outputString);
|
||||
assertEquals(decodedString.toUpperCase(), output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new LargePolybiusSquare(false, false);
|
||||
|
||||
String output = cipher.decode(outputStringClean);
|
||||
String output = cipher.decode(encodedStringClean);
|
||||
|
||||
assertEquals(outputStringClean.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(encodedStringClean.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(keywordBlank, cipher.keyword);
|
||||
assertEquals(inputStringClean, cipher.outputString);
|
||||
assertEquals(inputStringClean, output);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
}
|
||||
}
|
||||
@@ -530,7 +530,7 @@ public class TestPlayfair{
|
||||
cipher.doubled = 'x';
|
||||
|
||||
assertThrows(InvalidCharacterException.class, () -> {
|
||||
cipher.setInputString(outputString + Character.toString(cipher.replaced), false);
|
||||
cipher.setInputString(outputString + cipher.replaced, false);
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user