Updated test coverage

This commit is contained in:
2023-04-17 01:17:59 -04:00
parent 575ff04ecd
commit 494293c311
22 changed files with 2479 additions and 2970 deletions

View File

@@ -1,450 +1,293 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/combination/TestADFGVX.java
//Mattrixwv
// Created: 01-26-22
//Modified: 07-09-22
//Modified: 04-14-23
package com.mattrixwv.cipherstream.combination;
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.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.InvalidKeywordException;
import com.mattrixwv.cipherstream.polysubstitution.Columnar;
public class TestADFGVX{
@Test
public void testEncode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
ADFGVX cipher = new ADFGVX(true, true, true);
private ADFGVX cipher;
private Logger logger;
//Variables
private String encodedString = "Message to^encode";
private String encodedStringClean = "MESSAGETOENCODE";
private String decodedString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
private String decodedStringClean = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
private String 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
inputString = "message to encode";
squareKeyword = "SquareKeyword";
keyword = "keyword";
correctOutput = "axgvdavfxgagfa afag aaxdxfgdagda";
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);
@BeforeEach
public void setup(){
cipher = new ADFGVX();
logger = mock(Logger.class);
ADFGVX.logger = logger;
}
@Test
public void testDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
ADFGVX cipher = new ADFGVX(true, true, true);
public void testSquareKeyword(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setSquareKeyword(null);
});
assertEquals("", cipher.squareKeyword);
verify(logger, never()).debug(anyString(), anyString());
//Test lowercase decoding
String inputString = "axgvdavfxgagfaafagaaxdxfgdagda";
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
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);
cipher.setSquareKeyword(squareKeyword);
assertEquals(squareKeyword, cipher.squareKeyword);
verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword);
}
@Test
public void testNoCapitalDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
ADFGVX cipher = new ADFGVX(false, true, true);
public void testSetKeyword(){
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 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);
cipher.setKeyword(keyword);
assertEquals(keyword, cipher.keyword);
verify(logger, times(1)).debug("keyword = {}", keyword);
}
@Test
public void testNoWhitespaceDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
ADFGVX cipher = new ADFGVX(true, false, true);
public void testSetInputString(){
//Null input
assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(null);
});
assertEquals("", cipher.inputString);
verify(logger, never()).debug(anyString(), anyString());
//Test lowercase decoding
String inputString = "axgvdavfxgagfaafagaaxdxfgdagda";
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);
String originalInputString = "Original input string '{}'";
String cleanedInputString = "Cleaned input string '{}'";
//Test whitespace decoding
inputString = "axgvdavfxgagfa afag aaxdxfgdagda";
squareKeyword = "SquareKeyword";
keyword = "keyword";
correctOutput = "messagetoencode";
output = cipher.decode(squareKeyword, keyword, inputString);
assertEquals(correctOutput, output);
//Blank input
cipher.preserveCapitals = true;
cipher.preserveSymbols = true;
cipher.preserveWhitespace = true;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputString("");
});
assertEquals("", cipher.inputString);
verify(logger, times(1)).debug(originalInputString, "");
//Test symbol decoding
inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-";
squareKeyword = "SquareKeyword";
keyword = "keyword";
correctOutput = "message*to+encode-";
output = cipher.decode(squareKeyword, keyword, inputString);
assertEquals(correctOutput, output);
//No options
String inputString = "input String*";
cipher.setInputString(inputString);
assertEquals(inputString, cipher.inputString);
verify(logger, times(1)).debug(originalInputString, inputString);
verify(logger, times(1)).debug(cleanedInputString, inputString);
//Test mixed case, whitespace, and symbol decoding
inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
squareKeyword = "SquareKeyword";
keyword = "keyword";
correctOutput = "Messageto^encode";
output = cipher.decode(squareKeyword, keyword, inputString);
assertEquals(correctOutput, output);
//capitals
cipher.preserveCapitals = false;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
inputString = "input String*";
cipher.setInputString(inputString);
assertEquals(inputString.toUpperCase(), cipher.inputString);
verify(logger, times(2)).debug(originalInputString, inputString);
verify(logger, times(1)).debug("Removing capitals");
verify(logger, times(1)).debug(cleanedInputString, inputString.toUpperCase());
//whitespace
cipher.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
public void testNoSymbolDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
ADFGVX cipher = new ADFGVX(true, true, false);
public void testFormateOutputStringEncode(){
cipher.inputString = encodedString;
cipher.outputString = decodedStringClean;
//Test lowercase decoding
String inputString = "axgvdavfxgagfaafagaaxdxfgdagda";
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
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);
cipher.formatOutputStringEncode();
assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string to match input string");
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
verify(logger, times(1)).debug("Converting output to uppercase");
verify(logger, times(14)).debug("Converting output to lowercase");
verify(logger, times(2)).debug("Appending symbol to output");
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidCharacterException, InvalidKeywordException, InvalidInputException{
ADFGVX cipher = new ADFGVX(false, false, false);
public void testFormatOutputStringDecode(){
cipher.outputString = encodedStringClean;
cipher.inputString = decodedString;
//Test lowercase decoding
String inputString = "axgvdavfxgagfaafagaaxdxfgdagda";
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);
cipher.formatOutputStringDecode();
assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string to match input string");
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
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 whitespace decoding
inputString = "axgvdavfxgagfa afag aaxdxfgdagda";
squareKeyword = "SquareKeyword";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(squareKeyword, keyword, inputString);
assertEquals(correctOutput, output);
@Test
public void testEncode(){
cipher.inputString = encodedString;
cipher.keyword = keyword;
cipher.squareKeyword = squareKeyword;
//Test symbol decoding
inputString = "axgvdavfxgagfa*afag+aaxdxfgdagda-";
squareKeyword = "SquareKeyword";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(squareKeyword, keyword, inputString);
assertEquals(correctOutput, output);
cipher.encode();
//Test mixed case, whitespace, and symbol decoding
inputString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
squareKeyword = "SquareKeyword";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(squareKeyword, keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(decodedString, 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
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);
}
}

View File

@@ -6,8 +6,14 @@ package com.mattrixwv.cipherstream.combination;
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;
@@ -15,23 +21,29 @@ import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
import com.mattrixwv.cipherstream.polysubstitution.Columnar;
import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
@ExtendWith(MockitoExtension.class)
public class TestADFGX{
private ADFGX cipher;
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
public void setup(){
cipher = new ADFGX();
cipher = new ADFGX(true, true, true);
logger = mock(Logger.class);
ADFGX.logger = logger;
}
@@ -45,7 +57,6 @@ public class TestADFGX{
assertEquals("", cipher.squareKeyword);
verify(logger, never()).debug(anyString(), anyString());
String squareKeyword = "squareKeyword";
cipher.setSquareKeyword(squareKeyword);
assertEquals(squareKeyword, cipher.squareKeyword);
verify(logger, times(1)).debug("squareKeyword = {}", squareKeyword);
@@ -74,8 +85,8 @@ public class TestADFGX{
assertEquals("", cipher.inputString);
verify(logger, never()).debug(anyString(), anyString());
String originalInputString = "original input string '{}'";
String cleanedInputString = "cleaned input string '{}'";
String originalInputString = "Original input string '{}'";
String cleanedInputString = "Cleaned input string '{}'";
//Blank input
cipher.preserveCapitals = true;
@@ -130,70 +141,153 @@ public class TestADFGX{
@Test
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
public void formatOutputStringDecode(){
//TODO:
public void testFormatOutputStringDecode(){
cipher.outputString = decodedStringClean;
cipher.inputString = encodedString;
cipher.formatOutputStringDecode();
assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string to match input string");
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
verify(logger, times(1)).debug("Converting output to uppercase");
verify(logger, times(14)).debug("Converting output to lowercase");
verify(logger, times(2)).debug("Appending symbol to output");
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
}
@Test
public void testEncodePrivate(){
//TODO:
public void testEncode(){
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
public void testDecodePrivate(){
//TODO:
public void testDecode(){
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
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
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
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
public void testPracticalEncoding(){
//Test as original
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
String output = cipher.encode("SquareKeyword", "keyword", "Message to^encode");
assertEquals("AAgagadfagaxxd axdx^adafafxddgdf", output);
cipher = new ADFGX(true, true, true);
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
cipher.preserveCapitals = false;
cipher.preserveWhitespace = false;
cipher.preserveSymbols = false;
output = cipher.encode("SquareKeyword", "keyword", "Message to^encode");
assertEquals("AAGAGADFAGAXXDAXDXADAFAFXDDGDF", output);
cipher = new ADFGX(false, false, false);
output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
//Test as original
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
String output = cipher.decode("SquareKeyword", "keyword", "AAgagadfagaxxd axdx^adafafxddgdf");
assertEquals("Message to^encode", output);
cipher = new ADFGX(true, true, true);
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(decodedString, output);
//Test fully cleaned
cipher.preserveCapitals = false;
cipher.preserveWhitespace = false;
cipher.preserveSymbols = false;
output = cipher.decode("SquareKeyword", "keyword", "AAgagadfagaxxd axdx^adafafxddgdf");
assertEquals("MESSAGETOENCODE", output);
cipher = new ADFGX(false, false, false);
output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,449 +1,342 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestAffine.java
//Mattrixwv
// Created: 01-26-22
//Modified: 07-09-22
//Modified: 04-15-23
package com.mattrixwv.cipherstream.monosubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.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.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class TestAffine{
@Test
public void testEncode() throws InvalidKeywordException, InvalidInputException{
Affine cipher = new Affine(true, true, true);
private Affine cipher;
private Logger logger;
//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
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 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);
@BeforeEach
public void setup(){
cipher = new Affine();
logger = mock(Logger.class);
Affine.logger = logger;
}
@Test
public void testDecode() throws InvalidKeywordException, InvalidInputException{
Affine cipher = new Affine(true, true, true);
//Test lowercase decoding
String inputString = "pbtthlbyzburzwb";
int key1 = 5;
int key2 = 7;
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
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);
public void testConstructor_default(){
cipher = new Affine();
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols);
assertFalse(cipher.preserveWhitespace);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(0, cipher.key1);
assertEquals(0, cipher.key2);
}
@Test
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
Affine cipher = new Affine(false, true, true);
public void testConstructor_preserves(){
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
String inputString = "pbtthlbyzburzwb";
int key1 = 5;
int key2 = 7;
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);
cipher = new Affine(false, true, false);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols);
assertTrue(cipher.preserveWhitespace);
//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);
cipher = new Affine(false, false, true);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveSymbols);
assertFalse(cipher.preserveWhitespace);
}
@Test
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
Affine cipher = new Affine(true, false, true);
//Test lowercase decoding
String inputString = "pbtthlbyzburzwb";
int key1 = 5;
int key2 = 7;
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
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);
public void testKey1(){
cipher.setKey1(key1);
assertEquals(key1, cipher.key1);
verify(logger, times(1)).debug("Setting key1 {}", key1);
verify(logger, times(1)).debug("Cleaned key1 {}", key1);
verify(logger, times(2)).debug(anyString(), anyInt());
verify(logger, never()).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
}
@Test
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Affine cipher = new Affine(true, true, false);
//Test lowercase decoding
String inputString = "pbtthlbyzburzwb";
int key1 = 5;
int key2 = 7;
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
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);
public void testSetKey1_notPrime(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKey1(2);
});
verify(logger, times(1)).debug("Setting key1 {}", 2);
verify(logger, never()).debug("Cleaned key1 {}", 2);
verify(logger, times(1)).debug(anyString(), anyInt());
verify(logger, never()).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Affine cipher = new Affine(false, false, false);
public void testSetKey1_negative(){
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
String inputString = "pbtthlbyzburzwb";
int key1 = 5;
int key2 = 7;
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
public void testSetKey1_large(){
cipher.setKey1(key1 + 26);
assertEquals(key1, cipher.key1);
verify(logger, times(1)).debug("Setting key1 {}", key1 + 26);
verify(logger, times(1)).debug("Cleaned key1 {}", key1);
verify(logger, times(2)).debug(anyString(), anyInt());
verify(logger, never()).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
}
//Test whitespace decoding
inputString = "pbtthlb yz burzwb";
key1 = 5;
key2 = 7;
correctOutput = "messagetoencode";
output = cipher.decode(key1, key2, inputString);
assertEquals(correctOutput, output);
@Test
public void testSetKey2(){
cipher.setKey2(key2);
assertEquals(key2, cipher.key2);
verify(logger, times(1)).debug("Setting key2 {}", key2);
verify(logger, times(1)).debug("Cleaned key2 {}", key2);
verify(logger, times(2)).debug(anyString(), anyInt());
verify(logger, never()).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
}
//Test symbol decoding
inputString = "pbtthlb*yz+burzwb-";
key1 = 5;
key2 = 7;
correctOutput = "messagetoencode";
output = cipher.decode(key1, key2, inputString);
assertEquals(correctOutput, output);
@Test
public void testSetKey2_negative(){
cipher.setKey2(-27);
assertEquals(25, cipher.key2);
verify(logger, times(1)).debug("Setting key2 {}", -27);
verify(logger, times(1)).debug("Cleaned key2 {}", cipher.key2);
verify(logger, times(2)).debug(anyString(), anyInt());
verify(logger, never()).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
}
//Test mixed case, whitespace, symbol decoding
inputString = "Pbtthlb yz^burzwb";
key1 = 5;
key2 = 7;
correctOutput = "messagetoencode";
output = cipher.decode(key1, key2, inputString);
assertEquals(correctOutput, output);
@Test
public void testSetKey2_large(){
cipher.setKey2(key2 + 26);
assertEquals(key2, cipher.key2);
verify(logger, times(1)).debug("Setting key2 {}", key2 + 26);
verify(logger, times(1)).debug("Cleaned key2 {}", key2);
verify(logger, times(2)).debug(anyString(), anyInt());
verify(logger, never()).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
}
@Test
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);
}
}

View File

@@ -1,356 +1,224 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java
//Mattrixwv
// Created: 07-25-21
//Modified: 07-09-22
//Modified: 04-15-23
package com.mattrixwv.cipherstream.monosubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class TestAtbash{
@Test
public void testEncode() throws InvalidInputException{
Atbash cipher = new Atbash(true, true, true);
private Atbash cipher;
private Logger logger;
//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
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 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);
@BeforeEach
public void setup(){
cipher = new Atbash();
logger = mock(Logger.class);
Atbash.logger = logger;
}
@Test
public void testDecode() throws InvalidInputException{
Atbash cipher = new Atbash(true, true, true);
//Test lowercase decoding
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
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);
public void testConstructor_default(){
cipher = new Atbash();
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
}
@Test
public void testNoCapitalDecode() throws InvalidInputException{
Atbash cipher = new Atbash(false, true, true);
public void testConstructor_preserves(){
cipher = new Atbash(true, false, false);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
//Test lowercase decoding
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);
cipher = new Atbash(false, true, false);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
//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);
cipher = new Atbash(false, false, true);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
}
@Test
public void testNoWhitespaceDecode() throws InvalidInputException{
Atbash cipher = new Atbash(true, false, true);
public void testEncode(){
cipher.inputString = decodedString;
cipher.outputString = encodedString;
//Test lowercase decoding
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
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);
assertEquals(decodedString, cipher.inputString);
assertEquals(encodedString, cipher.outputString);
}
@Test
public void testNoSymbolDecode() throws InvalidInputException{
Atbash cipher = new Atbash(true, true, false);
public void testSetInputString(){
cipher.preserveCapitals = true;
cipher.preserveSymbols = true;
cipher.preserveWhitespace = true;
//Test lowercase decoding
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);
cipher.setInputString(decodedString);
//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 = "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);
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());
verify(logger, never()).debug(anyString());
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidInputException{
Atbash cipher = new Atbash(false, false, false);
public void testSetInputString_noCapitals(){
cipher.preserveCapitals = false;
cipher.preserveSymbols = true;
cipher.preserveWhitespace = true;
//Test lowercase decoding
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);
cipher.setInputString(decodedString);
//Test whitespace decoding
inputString = "nvhhztv gl vmxlwv";
correctOutput = "MESSAGETOENCODE";
output = cipher.encode(inputString);
assertEquals(correctOutput, output);
assertEquals(decodedString.toUpperCase(), 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.toUpperCase());
verify(logger, times(2)).debug(anyString(), anyString());
verify(logger, times(1)).debug(anyString());
}
//Test symbol decoding
inputString = "nvhhztv*gl+vmxlwv";
correctOutput = "MESSAGETOENCODE";
output = cipher.encode(inputString);
assertEquals(correctOutput, output);
@Test
public void testSetInputString_noWhitespace(){
cipher.preserveCapitals = true;
cipher.preserveSymbols = true;
cipher.preserveWhitespace = false;
//Test mixed case, whitespace, and symbol decoding
inputString = "Nvhhztv gl^vmxlwv";
correctOutput = "MESSAGETOENCODE";
output = cipher.encode(inputString);
assertEquals(correctOutput, output);
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);
});
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);
}
}

View File

@@ -1,413 +1,198 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAutokey.java
//Mattrixwv
// Created: 07-26-21
//Modified: 07-09-22
//Modified: 04-15-23
package com.mattrixwv.cipherstream.monosubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.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.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class TestAutokey{
@Test
public void testEncode() throws InvalidKeywordException, InvalidInputException{
Autokey cipher = new Autokey(true, true, true);
private Autokey cipher;
private Logger logger;
//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
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 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);
@BeforeEach
public void setup(){
cipher = new Autokey();
logger = mock(Logger.class);
Autokey.logger = logger;
}
@Test
public void testDecode() throws InvalidKeywordException, InvalidInputException{
Autokey cipher = new Autokey(true, true, true);
//Test lowercase decoding
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
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);
public void testConstructor_default(){
cipher = new Autokey();
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(0, cipher.offset.size());
}
@Test
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
Autokey cipher = new Autokey(false, true, true);
public void testConstructor_preserves(){
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
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);
cipher = new Autokey(false, true, false);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
//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);
cipher = new Autokey(false, false, true);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
}
@Test
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
Autokey cipher = new Autokey(true, false, true);
public void testEncodeSet(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
//Test lowercase decoding
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);
cipher.encodeSet(keyword, decodedString);
//Test whitespace decoding
inputString = "wiqooxh fs wfcuhx";
keyword = "keyword";
correctOutput = "messagetoencode";
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 = "Messageto^encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(decodedString, cipher.inputString);
assertEquals((keyword + decodedString.replaceAll("\\s", "").replaceAll("[^a-zA-Z\\s]", "").substring(0, 8)).toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Setting fields for encoding");
verify(logger, times(1)).debug("Setting keyword");
verify(logger, times(1)).debug("Adding input to keyword");
verify(logger, times(1)).debug("Removing last letters in the keyword");
verify(logger, times(4)).debug(anyString());
}
@Test
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Autokey cipher = new Autokey(true, true, false);
public void testDecodeSet(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
//Test lowercase decoding
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);
cipher.decodeSet(keyword, decodedString);
//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 = "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);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.inputString);
verify(logger, times(1)).debug("Setting fields for decoding");
verify(logger, times(1)).debug("Setting keyword");
verify(logger, times(1)).debug("Setting input string");
verify(logger, times(3)).debug(anyString());
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Autokey cipher = new Autokey(false, false, false);
public void testDecode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
Autokey.logger = mock(Logger.class);
cipher.decodeSet(keyword, encodedString);
Autokey.logger = logger;
//Test lowercase decoding
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);
cipher.decode();
//Test whitespace decoding
inputString = "WIQOOXH FS WFCUHX";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Decoding");
verify(logger, times(2)).debug("Appending partial output to keyword");
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Appending uppercase");
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
inputString = "WIQOOXH*FS+WFCUHX";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
@Test
public void testGetters(){
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.keyword = keyword;
cipher.offset.add(1);
//Test mixed case, whitespace, symbol decoding
inputString = "WIQOOXH FS^WFCUHX";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(decodedString, cipher.getInputString());
assertEquals(encodedString, cipher.getOutputString());
assertEquals(keyword, cipher.getKeyword());
assertEquals(new ArrayList<>(List.of(1)), cipher.getOffsets());
}
@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
public void testKeyword() throws InvalidKeywordException{
public void testSetKeyword() throws InvalidKeywordException{
Autokey cipher = new Autokey();
//Test keyword with whitespace

View File

@@ -1,103 +1,319 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestBaconian.java
//Mattrixwv
// Created: 01-12-22
//Modified: 07-09-22
//Modified: 04-16-23
package com.mattrixwv.cipherstream.monosubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.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.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class TestBaconian{
@Test
public void testEncode() throws InvalidInputException{
Baconian cipher = new Baconian(true);
private Baconian cipher;
private Logger logger;
//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
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
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);
@BeforeEach
public void setup(){
cipher = new Baconian();
logger = mock(Logger.class);
Baconian.logger = logger;
}
@Test
public void testDecode() throws InvalidCharacterException, InvalidInputException{
Baconian cipher = new Baconian(true);
public void testConstructor_default(){
cipher = new Baconian();
//Test lowercase decoding
String inputString = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
String correctOutput = "messagetoencode";
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);
assertFalse(cipher.preserveCapitals);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
}
@Test
public void testDecodeNoCapital() throws InvalidCharacterException, InvalidInputException{
Baconian cipher = new Baconian(false);
public void testConstructor_preserves(){
cipher = new Baconian(true);
//Test lowercase decoding
String inputString = "ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
String correctOutput = "messagetoencode";
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);
assertTrue(cipher.preserveCapitals);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
}
//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
public void testSetInputStringEncode(){
cipher.preserveCapitals = true;
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);
}
}

View File

@@ -1,13 +1,24 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBaseX.java
//Mattrixwv
// Created: 01-08-22
//Modified: 07-09-22
//Modified: 04-16-23
package com.mattrixwv.cipherstream.monosubstitution;
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.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
@@ -15,270 +26,292 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class TestBaseX{
@Test
public void testBinaryEncode() throws InvalidBaseException, InvalidInputException{
BaseX cipher = new BaseX();
private BaseX cipher;
private Logger logger;
//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
inputString = "A B\tC\n";
correctOutput = "1000001 100000 1000010 1001 1000011 1010";
output = cipher.encode(inputString);
assertEquals(correctOutput, output);
//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);
@BeforeEach
public void setup(){
cipher = new BaseX();
logger = mock(Logger.class);
BaseX.logger = logger;
}
@Test
public void testBinaryDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{
BaseX cipher = new BaseX();
public void testConstructor_default(){
cipher = new BaseX();
//Test lowercase decoding
String inputString = "1100001";
String correctOutput = "a";
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);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(2, cipher.base);
}
@Test
public void testOctalDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{
BaseX cipher = new BaseX(8);
public void testConstructor_base(){
cipher = new BaseX(8);
//Test lowercase decoding
String inputString = "141";
String correctOutput = "a";
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);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(8, cipher.base);
}
@Test
public void testDecimalDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{
BaseX cipher = new BaseX(10);
public void testSetInputStringEncode(){
cipher.setInputStringEncode(decodedString);
//Test lowercase decoding
String inputString = "97";
String correctOutput = "a";
String output = cipher.decode(inputString);
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);
assertEquals(decodedString, cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
verify(logger, times(1)).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
}
@Test
public void testHexDecode() throws InvalidCharacterException, InvalidBaseException, InvalidInputException{
BaseX cipher = new BaseX(16);
public void testSetInputStringEncode_blank(){
assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncode("");
}, "Input must contain at least 1 letter");
//Test lowercase decoding
String inputString = "61";
String correctOutput = "a";
String output = cipher.decode(inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "41";
correctOutput = "A";
output = cipher.decode(inputString);
assertEquals(correctOutput, output);
assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Setting input string for encoding '{}'", "");
verify(logger, times(1)).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
}
//Test whitespace decoding
inputString = "41 20 42 9 43 A";
correctOutput = "A B\tC\n";
output = cipher.decode(inputString);
assertEquals(correctOutput, output);
@Test
public void testSetInputStringEncode_null(){
assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncode(null);
}, "Input cannot be null");
//Test symbol decoding
inputString = "41 40 42 2D 43 2B";
correctOutput = "A@B-C+";
output = cipher.decode(inputString);
assertEquals(correctOutput, output);
assertEquals("", cipher.inputString);
verify(logger, never()).debug("Setting input string for encoding '{}'", decodedString);
verify(logger, never()).debug(anyString(), anyString());
verify(logger, never()).debug(anyString());
}
//Test mixed case, whitespace, and symbol decoding
inputString = "41 2B 42 40 43 20 64 9 65 A 66";
correctOutput = "A+B@C d\te\nf";
output = cipher.decode(inputString);
assertEquals(correctOutput, output);
@Test
public void testSetInputStringDecode(){
cipher.base = 16;
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);
}
}

View File

@@ -1,407 +1,399 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBeaufort.java
//Mattrixwv
// Created: 02-23-22
//Modified: 07-09-22
//Modified: 04-16-23
package com.mattrixwv.cipherstream.monosubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class TestBeaufort{
@Test
public void testEncode() throws InvalidKeywordException, InvalidInputException{
Beaufort cipher = new Beaufort(true, true, true);
private Beaufort cipher;
private Logger logger;
//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
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 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);
@BeforeEach
public void setup(){
cipher = new Beaufort();
logger = mock(Logger.class);
Beaufort.logger = logger;
}
@Test
public void testDecode() throws InvalidKeywordException, InvalidInputException{
Beaufort cipher = new Beaufort(true, true, true);
public void testConstructor_default(){
cipher = new Beaufort();
//Test lowercase decoding
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
inputString = "yageolz rq ujmdag";
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);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertFalse(cipher.atbash.preserveCapitals);
assertFalse(cipher.atbash.preserveWhitespace);
assertFalse(cipher.atbash.preserveSymbols);
assertFalse(cipher.caesar.preserveCapitals);
assertFalse(cipher.caesar.preserveWhitespace);
assertFalse(cipher.caesar.preserveSymbols);
assertFalse(cipher.vigenere.preserveCapitals);
assertFalse(cipher.vigenere.preserveWhitespace);
assertFalse(cipher.vigenere.preserveSymbols);
}
@Test
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
Beaufort cipher = new Beaufort(false, true, true);
public void testConstructor_preservesCapitals(){
cipher = new Beaufort(true, false, false);
//Test lowercase decoding
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
inputString = "yageolz rq ujmdag";
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);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertTrue(cipher.atbash.preserveCapitals);
assertFalse(cipher.atbash.preserveWhitespace);
assertFalse(cipher.atbash.preserveSymbols);
assertTrue(cipher.caesar.preserveCapitals);
assertFalse(cipher.caesar.preserveWhitespace);
assertFalse(cipher.caesar.preserveSymbols);
assertTrue(cipher.vigenere.preserveCapitals);
assertFalse(cipher.vigenere.preserveWhitespace);
assertFalse(cipher.vigenere.preserveSymbols);
}
@Test
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
Beaufort cipher = new Beaufort(true, false, true);
public void testConstructor_preservesWhitespace(){
cipher = new Beaufort(false, true, false);
//Test lowercase decoding
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
inputString = "yageolz rq ujmdag";
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);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertFalse(cipher.atbash.preserveCapitals);
assertTrue(cipher.atbash.preserveWhitespace);
assertFalse(cipher.atbash.preserveSymbols);
assertFalse(cipher.caesar.preserveCapitals);
assertTrue(cipher.caesar.preserveWhitespace);
assertFalse(cipher.caesar.preserveSymbols);
assertFalse(cipher.vigenere.preserveCapitals);
assertTrue(cipher.vigenere.preserveWhitespace);
assertFalse(cipher.vigenere.preserveSymbols);
}
@Test
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Beaufort cipher = new Beaufort(true, true, false);
public void testConstructor_preservesSymbols(){
cipher = new Beaufort(false, false, true);
//Test lowercase decoding
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
inputString = "yageolz rq ujmdag";
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);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertFalse(cipher.atbash.preserveCapitals);
assertFalse(cipher.atbash.preserveWhitespace);
assertTrue(cipher.atbash.preserveSymbols);
assertFalse(cipher.caesar.preserveCapitals);
assertFalse(cipher.caesar.preserveWhitespace);
assertTrue(cipher.caesar.preserveSymbols);
assertFalse(cipher.vigenere.preserveCapitals);
assertFalse(cipher.vigenere.preserveWhitespace);
assertTrue(cipher.vigenere.preserveSymbols);
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Beaufort cipher = new Beaufort(false, false, false);
public void testSetInputString(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
//Test lowercase decoding
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);
cipher.setInputString(decodedString);
//Test whitespace decoding
inputString = "yageolz rq ujmdag";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(decodedString, cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
verify(logger, never()).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
}
//Test symbol decoding
inputString = "yageolz*rq+ujmdag";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
@Test
public void testSetInputString_noCapitals(){
cipher.preserveCapitals = false;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
//Test mixed case, whitespace, symbol decoding
inputString = "Yageolz rq^ujmdag";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
cipher.setInputString(decodedString);
assertEquals(decodedString.toUpperCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
verify(logger, times(1)).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
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);
}
}

View File

@@ -1,628 +1,405 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestCaesar.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-09-22
//Modified: 04-16-23
package com.mattrixwv.cipherstream.monosubstitution;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class TestCaesar{
@Test
public void testEncode() throws InvalidInputException{
Caesar cipher = new Caesar(true, 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);
private Caesar cipher;
private Logger logger;
//Variables
private String inputString = "The quick brown fox jumps over - the lAzy dog";
private String inputStringClean = "thequickbrownfoxjumpsoverthelazydog";
private String outputString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb iXwv ald";
private String outputStringClean = "qebnrfzhyoltkclugrjmplsboqebixwvald";
private int shift = 23;
//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 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);
@BeforeEach
public void setup(){
cipher = new Caesar();
logger = mock(Logger.class);
Caesar.logger = logger;
}
@Test
public void testDecode() throws InvalidInputException{
Caesar cipher = new Caesar(true, true, true);
public void testConstructor_default(){
cipher = new Caesar();
//Test lowercase decoding
String input = "def";
int shift = 3;
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 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);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(0, cipher.shift);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
}
@Test
public void testNoCapitalDecode() throws InvalidInputException{
Caesar cipher = new Caesar(false, true, true);
public void testConstructor_preservesCapitals(){
cipher = new Caesar(true, false, false);
//Test lowercase decoding
String input = "def";
int shift = 3;
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 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);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(0, cipher.shift);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
}
@Test
public void testNoWhitespaceDecode() throws InvalidInputException{
Caesar cipher = new Caesar(true, false, true);
public void testConstructor_preservesSymbols(){
cipher = new Caesar(false, true, false);
//Test lowercase decoding
String input = "def";
int shift = 3;
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 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);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(0, cipher.shift);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
}
@Test
public void testNoSymbolDecode() throws InvalidInputException{
Caesar cipher = new Caesar(true, true, false);
public void testConstructor_preservesWhitespace(){
cipher = new Caesar(false, false, true);
//Test lowercase decoding
String input = "def";
int shift = 3;
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 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);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(0, cipher.shift);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidInputException{
Caesar cipher = new Caesar(false, false, false);
public void testSetShift(){
cipher.setShift(shift);
//Test lowercase decoding
String input = "def";
int shift = 3;
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);
assertEquals(shift, cipher.shift);
verify(logger, times(1)).debug("Setting shift {}", shift);
verify(logger, times(1)).debug("Cleaned shift {}", shift);
}
@Test
public void testSetShift_large(){
cipher.setShift(shift + 26);
//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);
assertEquals(shift, cipher.shift);
verify(logger, times(1)).debug("Setting shift {}", shift + 26);
verify(logger, times(1)).debug("Cleaned shift {}", shift);
}
@Test
public void testSetShift_negative(){
cipher.setShift(shift - 26);
//Test whitespace decoding
input = "def ghi";
shift = 3;
correctOutput = "abcdef";
output = cipher.decode(shift, input);
assertEquals(correctOutput, output);
assertEquals(shift - 26, cipher.shift);
verify(logger, times(1)).debug("Setting shift {}", shift - 26);
verify(logger, times(1)).debug("Cleaned shift {}", shift - 26);
}
@Test
public void testSetInputString(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
//Test symbol decoding
input = "def-ghi@";
shift = 3;
correctOutput = "abcdef";
output = cipher.decode(shift, input);
assertEquals(correctOutput, output);
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 mixed case, whitespace, and symbol decoding
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
shift = 23;
correctOutput = "thequickbrownfoxjumpsoverthelazydog";
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 = "thequickbrownfoxjumpsoverthelazydog";
output = cipher.decode(shift, input);
assertEquals(correctOutput, output);
@Test
public void testSetInputString_noCapitals(){
cipher.preserveCapitals = false;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.setInputString(inputString);
assertEquals(inputString.toLowerCase(), 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.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);
}
}