373 lines
12 KiB
Java
373 lines
12 KiB
Java
package com.mattrixwv.cipherstream.combination;
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.mockito.ArgumentMatchers.*;
|
|
import static org.mockito.Mockito.*;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
import org.mockito.InjectMocks;
|
|
import org.mockito.Mock;
|
|
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.LargePolybiusSquare;
|
|
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
public class ADFGVXTest{
|
|
@InjectMocks
|
|
private ADFGVX cipher;
|
|
@Mock
|
|
private Logger logger;
|
|
//Variables
|
|
private static final String DECODED_STRING = "Message to^encode";
|
|
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
|
private static final String ENCODED_STRING = "AXgvdavfxgagfa afag^aaxdxfgdagda";
|
|
private static final String ENCODED_STRING_CLEAN = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
|
|
private static final String KEYWORD = "keyword";
|
|
private static final String SQUARE_KEYWORD = "SquareKeyword";
|
|
|
|
|
|
@Test
|
|
public void testConstructor_default(){
|
|
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);
|
|
}
|
|
|
|
@Test
|
|
public void testConstructor_preservesCapitals(){
|
|
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);
|
|
}
|
|
|
|
@Test
|
|
public void testConstructor_preservesWhitespace(){
|
|
cipher = new ADFGVX(false, true, false);
|
|
|
|
assertFalse(cipher.preserveCapitals);
|
|
assertFalse(cipher.preserveSymbols);
|
|
assertTrue(cipher.preserveWhitespace);
|
|
assertEquals("", cipher.inputString);
|
|
assertEquals("", cipher.outputString);
|
|
assertEquals("", cipher.squareKeyword);
|
|
assertEquals("", cipher.squareKeyword);
|
|
assertNotNull(cipher.largePolybiusSquare);
|
|
assertNotNull(cipher.columnar);
|
|
}
|
|
|
|
@Test
|
|
public void testConstructor_preservesSymbols(){
|
|
cipher = new ADFGVX(false, false, true);
|
|
|
|
assertFalse(cipher.preserveCapitals);
|
|
assertTrue(cipher.preserveSymbols);
|
|
assertFalse(cipher.preserveWhitespace);
|
|
assertEquals("", cipher.inputString);
|
|
assertEquals("", cipher.outputString);
|
|
assertEquals("", cipher.squareKeyword);
|
|
assertEquals("", cipher.squareKeyword);
|
|
assertNotNull(cipher.largePolybiusSquare);
|
|
assertNotNull(cipher.columnar);
|
|
}
|
|
|
|
@Test
|
|
public void testSetSquareKeyword(){
|
|
cipher.setSquareKeyword(SQUARE_KEYWORD);
|
|
|
|
assertEquals(SQUARE_KEYWORD, cipher.squareKeyword);
|
|
verify(logger, times(1)).debug("squareKeyword '{}'", SQUARE_KEYWORD);
|
|
}
|
|
|
|
@Test
|
|
public void testSetSquareKeyword_null(){
|
|
assertThrows(InvalidKeywordException.class, () -> {
|
|
cipher.setSquareKeyword(null);
|
|
});
|
|
|
|
assertEquals("", cipher.squareKeyword);
|
|
verify(logger, never()).debug(eq("squareKeyword '{}'"), anyString());
|
|
}
|
|
|
|
@Test
|
|
public void testSetKeyword(){
|
|
cipher.setKeyword(KEYWORD);
|
|
|
|
assertEquals(KEYWORD, cipher.keyword);
|
|
verify(logger, times(1)).debug("keyword '{}'", KEYWORD);
|
|
}
|
|
|
|
@Test
|
|
public void testSetKeyword_null(){
|
|
assertThrows(InvalidKeywordException.class, () -> {
|
|
cipher.setKeyword(null);
|
|
});
|
|
|
|
assertEquals("", cipher.keyword);
|
|
verify(logger, never()).debug(eq("keyword '{}'"), anyString());
|
|
}
|
|
|
|
@Test
|
|
public void testSetInputString(){
|
|
cipher.preserveCapitals = true;
|
|
cipher.preserveWhitespace = true;
|
|
cipher.preserveSymbols = true;
|
|
|
|
cipher.setInputString(DECODED_STRING);
|
|
|
|
assertEquals(DECODED_STRING, cipher.inputString);
|
|
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
|
verify(logger, never()).debug("Removing capitals");
|
|
verify(logger, never()).debug("Removing whitespace");
|
|
verify(logger, never()).debug("Removing symbols");
|
|
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING);
|
|
}
|
|
|
|
@Test
|
|
public void testSetInputString_noCapitals(){
|
|
cipher.preserveCapitals = false;
|
|
cipher.preserveWhitespace = true;
|
|
cipher.preserveSymbols = true;
|
|
|
|
cipher.setInputString(DECODED_STRING);
|
|
|
|
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
|
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
|
verify(logger, times(1)).debug("Removing capitals");
|
|
verify(logger, never()).debug("Removing whitespace");
|
|
verify(logger, never()).debug("Removing symbols");
|
|
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase());
|
|
}
|
|
|
|
@Test
|
|
public void testSetInputString_noWhitespace(){
|
|
cipher.preserveCapitals = true;
|
|
cipher.preserveWhitespace = false;
|
|
cipher.preserveSymbols = true;
|
|
|
|
cipher.setInputString(DECODED_STRING);
|
|
|
|
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
|
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
|
verify(logger, never()).debug("Removing capitals");
|
|
verify(logger, times(1)).debug("Removing whitespace");
|
|
verify(logger, never()).debug("Removing symbols");
|
|
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
|
}
|
|
|
|
@Test
|
|
public void testSetInputString_noSymbols(){
|
|
cipher.preserveCapitals = true;
|
|
cipher.preserveWhitespace = true;
|
|
cipher.preserveSymbols = false;
|
|
|
|
cipher.setInputString(DECODED_STRING);
|
|
|
|
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
|
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
|
verify(logger, never()).debug("Removing capitals");
|
|
verify(logger, never()).debug("Removing whitespace");
|
|
verify(logger, times(1)).debug("Removing symbols");
|
|
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
|
}
|
|
|
|
@Test
|
|
public void testSetInputString_blank(){
|
|
cipher.preserveCapitals = true;
|
|
cipher.preserveWhitespace = true;
|
|
cipher.preserveSymbols = true;
|
|
|
|
assertThrows(InvalidInputException.class, () -> {
|
|
cipher.setInputString("");
|
|
});
|
|
|
|
assertEquals("", cipher.inputString);
|
|
verify(logger, times(1)).debug("Original input string '{}'", "");
|
|
verify(logger, never()).debug("Removing capitals");
|
|
verify(logger, never()).debug("Removing whitespace");
|
|
verify(logger, never()).debug("Removing symbols");
|
|
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
|
|
}
|
|
|
|
@Test
|
|
public void testSetInputString_null(){
|
|
cipher.preserveCapitals = true;
|
|
cipher.preserveWhitespace = true;
|
|
cipher.preserveSymbols = true;
|
|
|
|
assertThrows(InvalidInputException.class, () -> {
|
|
cipher.setInputString(null);
|
|
});
|
|
|
|
assertEquals("", cipher.inputString);
|
|
verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
|
|
verify(logger, never()).debug("Removing capitals");
|
|
verify(logger, never()).debug("Removing whitespace");
|
|
verify(logger, never()).debug("Removing symbols");
|
|
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
|
|
}
|
|
|
|
@Test
|
|
public void testFormateOutputStringEncode(){
|
|
cipher.inputString = DECODED_STRING;
|
|
cipher.outputString = ENCODED_STRING_CLEAN;
|
|
|
|
cipher.formatOutputStringEncode();
|
|
|
|
assertEquals(ENCODED_STRING, 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 '{}'", ENCODED_STRING);
|
|
}
|
|
|
|
@Test
|
|
public void testFormatOutputStringDecode(){
|
|
cipher.outputString = DECODED_STRING_CLEAN;
|
|
cipher.inputString = ENCODED_STRING;
|
|
|
|
cipher.formatOutputStringDecode();
|
|
|
|
assertEquals(DECODED_STRING, 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 '{}'", DECODED_STRING);
|
|
}
|
|
|
|
@Test
|
|
public void testEncode(){
|
|
cipher.inputString = DECODED_STRING;
|
|
cipher.keyword = KEYWORD;
|
|
cipher.squareKeyword = SQUARE_KEYWORD;
|
|
|
|
cipher.encode();
|
|
|
|
assertEquals(ENCODED_STRING, 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 = ENCODED_STRING;
|
|
cipher.keyword = KEYWORD;
|
|
cipher.squareKeyword = SQUARE_KEYWORD;
|
|
|
|
cipher.decode();
|
|
|
|
assertEquals(DECODED_STRING, 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 testGetters(){
|
|
cipher.inputString = DECODED_STRING;
|
|
cipher.outputString = ENCODED_STRING;
|
|
cipher.squareKeyword = SQUARE_KEYWORD;
|
|
cipher.keyword = KEYWORD;
|
|
|
|
assertEquals(DECODED_STRING, cipher.getInputString());
|
|
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
|
assertEquals(SQUARE_KEYWORD, cipher.getSquareKeyword());
|
|
assertEquals(KEYWORD, cipher.getKeyword());
|
|
}
|
|
|
|
@Test
|
|
public void testReset(){
|
|
LargePolybiusSquare ps = cipher.largePolybiusSquare;
|
|
Columnar columnar = cipher.columnar;
|
|
cipher.inputString = DECODED_STRING;
|
|
cipher.outputString = ENCODED_STRING;
|
|
cipher.squareKeyword = SQUARE_KEYWORD;
|
|
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(){
|
|
cipher = new ADFGVX(true, true, true);
|
|
|
|
String output = cipher.encode(SQUARE_KEYWORD, KEYWORD, DECODED_STRING);
|
|
|
|
assertEquals(DECODED_STRING, cipher.inputString);
|
|
assertEquals(KEYWORD, cipher.keyword);
|
|
assertEquals(ENCODED_STRING, cipher.outputString);
|
|
assertEquals(ENCODED_STRING, output);
|
|
}
|
|
|
|
@Test
|
|
public void testPracticalEncoding_clean(){
|
|
cipher = new ADFGVX(false, false, false);
|
|
|
|
String output = cipher.encode(SQUARE_KEYWORD, KEYWORD, DECODED_STRING);
|
|
|
|
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
|
assertEquals(KEYWORD, cipher.keyword);
|
|
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
|
assertEquals(ENCODED_STRING_CLEAN, output);
|
|
}
|
|
|
|
@Test
|
|
public void testPracticalDecoding(){
|
|
cipher = new ADFGVX(true, true, true);
|
|
|
|
String output = cipher.decode(SQUARE_KEYWORD, KEYWORD, ENCODED_STRING);
|
|
|
|
assertEquals(ENCODED_STRING, cipher.inputString);
|
|
assertEquals(KEYWORD, cipher.keyword);
|
|
assertEquals(DECODED_STRING, cipher.outputString);
|
|
assertEquals(DECODED_STRING, output);
|
|
}
|
|
|
|
@Test
|
|
public void testPracticalDecoding_clean(){
|
|
cipher = new ADFGVX(false, false, false);
|
|
|
|
String output = cipher.decode(SQUARE_KEYWORD, KEYWORD, ENCODED_STRING);
|
|
|
|
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
|
assertEquals(KEYWORD, cipher.keyword);
|
|
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
|
assertEquals(DECODED_STRING_CLEAN, output);
|
|
}
|
|
}
|