Update unit test coverage

This commit is contained in:
2023-04-22 10:52:16 -04:00
parent 494293c311
commit 59885b8df6
21 changed files with 2784 additions and 2005 deletions

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/combination/TestADFGVX.java
//Mattrixwv
// Created: 01-26-22
//Modified: 04-14-23
//Modified: 04-21-23
package com.mattrixwv.cipherstream.combination;
@@ -23,20 +23,20 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
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;
import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare;
public class TestADFGVX{
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 decodedString = "Message to^encode";
private String decodedStringClean = "MESSAGETOENCODE";
private String encodedString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
private String encodedStringClean = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
private String keyword = "keyword";
private String squareKeyword = "SquareKeyword";
@@ -141,25 +141,10 @@ public class TestADFGVX{
@Test
public void testFormateOutputStringEncode(){
cipher.inputString = encodedString;
cipher.outputString = decodedStringClean;
cipher.inputString = decodedString;
cipher.outputString = encodedStringClean;
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 testFormatOutputStringDecode(){
cipher.outputString = encodedStringClean;
cipher.inputString = decodedString;
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());
@@ -170,14 +155,29 @@ public class TestADFGVX{
}
@Test
public void testEncode(){
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 testEncode(){
cipher.inputString = decodedString;
cipher.keyword = keyword;
cipher.squareKeyword = squareKeyword;
cipher.encode();
assertEquals(decodedString, cipher.outputString);
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");
@@ -185,20 +185,20 @@ public class TestADFGVX{
@Test
public void testDecode(){
cipher.inputString = decodedString;
cipher.inputString = encodedString;
cipher.keyword = keyword;
cipher.squareKeyword = squareKeyword;
cipher.decode();
assertEquals(encodedString, cipher.outputString);
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(){
public void testConstructor_default(){
cipher = new ADFGVX();
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
@@ -209,7 +209,10 @@ public class TestADFGVX{
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);
@@ -220,27 +223,45 @@ public class TestADFGVX{
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 testGetters(){
cipher.inputString = encodedString;
cipher.outputString = decodedString;
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.squareKeyword = squareKeyword;
cipher.keyword = keyword;
assertEquals(encodedString, cipher.getInputString());
assertEquals(decodedString, cipher.getOutputString());
assertEquals(decodedString, cipher.getInputString());
assertEquals(encodedString, cipher.getOutputString());
assertEquals(squareKeyword, cipher.getSquareKeyword());
assertEquals(keyword, cipher.getKeyword());
}
@@ -249,8 +270,8 @@ public class TestADFGVX{
public void testReset(){
LargePolybiusSquare ps = cipher.largePolybiusSquare;
Columnar columnar = cipher.columnar;
cipher.inputString = encodedString;
cipher.outputString = decodedString;
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.squareKeyword = squareKeyword;
cipher.keyword = keyword;
@@ -267,27 +288,49 @@ public class TestADFGVX{
@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
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(keyword, cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new ADFGVX(false, false, false);
output = cipher.encode(squareKeyword, keyword, encodedString);
assertEquals(decodedStringClean, output);
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword, cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, 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
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(keyword, cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new ADFGVX(false, false, false);
output = cipher.decode(squareKeyword, keyword, decodedString);
assertEquals(encodedStringClean, output);
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword, cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamjava/combination/TestADFGX.java
//Mattrixwv
// Created: 01-25-22
//Modified: 04-14-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.combination;
@@ -198,7 +198,7 @@ public class TestADFGX{
}
@Test
public void testConstructors(){
public void testConstructor_default(){
cipher = new ADFGX();
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
@@ -209,7 +209,10 @@ public class TestADFGX{
assertEquals("", cipher.squareKeyword);
assertNotNull(cipher.polybiusSquare);
assertNotNull(cipher.columnar);
}
@Test
public void testConstructor_preservesCapitals(){
cipher = new ADFGX(true, false, false);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols);
@@ -220,16 +223,34 @@ public class TestADFGX{
assertEquals("", cipher.squareKeyword);
assertNotNull(cipher.polybiusSquare);
assertNotNull(cipher.columnar);
}
@Test
public void testConstructor_preservesWhitespace(){
cipher = new ADFGX(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.polybiusSquare);
assertNotNull(cipher.columnar);
}
@Test
public void testConstructor_preservesSymbols(){
cipher = new ADFGX(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.polybiusSquare);
assertNotNull(cipher.columnar);
}
@Test
@@ -267,27 +288,49 @@ public class TestADFGX{
@Test
public void testPracticalEncoding(){
//Test as original
cipher = new ADFGX(true, true, true);
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new ADFGX(false, false, false);
output = cipher.encode(squareKeyword, keyword, decodedString);
String output = cipher.encode(squareKeyword, keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
//Test as original
cipher = new ADFGX(true, true, true);
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(decodedString, output);
//Test fully cleaned
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new ADFGX(false, false, false);
output = cipher.decode(squareKeyword, keyword, encodedString);
String output = cipher.decode(squareKeyword, keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestAffine.java
//Mattrixwv
// Created: 01-26-22
//Modified: 04-15-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -30,9 +30,9 @@ public class TestAffine{
private Affine cipher;
private Logger logger;
//Variables
private String decodedString = "Message to^encode";
private String decodedString = "MEssage to^encode";
private String decodedStringClean = "messagetoencode";
private String encodedString = "Pbtthlb yz^burzwb";
private String encodedString = "PBtthlb yz^burzwb";
private String encodedStringClean = "pbtthlbyzburzwb";
private int key1 = 5;
private int key2 = 7;
@@ -59,7 +59,7 @@ public class TestAffine{
}
@Test
public void testConstructor_preserves(){
public void testConstructor_preservesCapitals(){
cipher = new Affine(true, false, false);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols);
@@ -68,16 +68,30 @@ public class TestAffine{
assertEquals("", cipher.outputString);
assertEquals(0, cipher.key1);
assertEquals(0, cipher.key2);
}
@Test
public void testConstructor_preservesWhitespace(){
cipher = new Affine(false, true, false);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveSymbols);
assertTrue(cipher.preserveWhitespace);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(0, cipher.key1);
assertEquals(0, cipher.key2);
}
@Test
public void testConstructor_preservesSymbols(){
cipher = new Affine(false, false, true);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveSymbols);
assertFalse(cipher.preserveWhitespace);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals(0, cipher.key1);
assertEquals(0, cipher.key2);
}
@Test
@@ -316,27 +330,53 @@ public class TestAffine{
@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
String output = cipher.encode(key1, key2, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(key1, cipher.key1);
assertEquals(key2, cipher.key2);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Affine(false, false, false);
output = cipher.encode(key1, key2, decodedString);
String output = cipher.encode(key1, key2, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(key1, cipher.key1);
assertEquals(key2, cipher.key2);
assertEquals(encodedStringClean, cipher.outputString);
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
String output = cipher.decode(key1, key2, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(key1, cipher.key1);
assertEquals(key2, cipher.key2);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testpracticalDecoding_clean(){
cipher = new Affine(false, false, false);
output = cipher.decode(key1, key2, encodedString);
String output = cipher.decode(key1, key2, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(key1, cipher.key1);
assertEquals(key2, cipher.key2);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAtbash.java
//Mattrixwv
// Created: 07-25-21
//Modified: 04-15-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -51,23 +51,33 @@ public class TestAtbash{
}
@Test
public void testConstructor_preserves(){
public void testConstructor_preservesCapitals(){
cipher = new Atbash(true, false, false);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
}
@Test
public void testConstructor_preservesWhitespace(){
cipher = new Atbash(false, true, false);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
}
@Test
public void testConstructor_preservesSymbols(){
cipher = new Atbash(false, false, true);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
}
@Test
@@ -198,27 +208,45 @@ public class TestAtbash{
@Test
public void testPracticalEncoding(){
//Test as original
cipher = new Atbash(true, true, true);
String output = cipher.encode(decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
String output = cipher.encode(decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Atbash(false, false, false);
output = cipher.encode(decodedString);
String output = cipher.encode(decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(encodedStringClean, cipher.outputString);
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
String output = cipher.decode(encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new Atbash(false, false, false);
output = cipher.decode(encodedString);
String output = cipher.decode(encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestAutokey.java
//Mattrixwv
// Created: 07-26-21
//Modified: 04-15-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -58,7 +58,7 @@ public class TestAutokey{
}
@Test
public void testConstructor_preserves(){
public void testConstructor_preservesCapitals(){
cipher = new Autokey(true, false, false);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
@@ -67,16 +67,32 @@ public class TestAutokey{
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(0, cipher.offset.size());
}
@Test
public void testConstructor_preservesWhitespace(){
cipher = new Autokey(false, true, false);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(0, cipher.offset.size());
}
@Test
public void testConstructor_preservesSymbols(){
cipher = new Autokey(false, false, true);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(0, cipher.offset.size());
}
@Test
@@ -166,27 +182,49 @@ public class TestAutokey{
@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
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals((keyword + decodedStringClean.substring(0, 8)).toUpperCase(), cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Autokey(false, false, false);
output = cipher.encode(keyword, decodedString);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals((keyword + decodedStringClean.substring(0, 8)).toUpperCase(), cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
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
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals((keyword + decodedStringClean.substring(0, 14)).toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testpracticalDecoding_clean(){
cipher = new Autokey(false, false, false);
output = cipher.decode(keyword, encodedString);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals((keyword + decodedStringClean.substring(0, 14)).toUpperCase(), cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/TestBaconian.java
//Mattrixwv
// Created: 01-12-22
//Modified: 04-16-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -31,7 +31,7 @@ public class TestBaconian{
private Logger logger;
//Variables
private String decodedString = "Message to-encode";
private String decodedStringCleanUpper = "Messagetoencode";
private String decodedStringClean = "Messagetoencode";
private String decodedStringCleanLower = "messagetoencode";
private String encodedString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
@@ -54,7 +54,7 @@ public class TestBaconian{
}
@Test
public void testConstructor_preserves(){
public void testConstructor_preservesCapitals(){
cipher = new Baconian(true);
assertTrue(cipher.preserveCapitals);
@@ -68,10 +68,10 @@ public class TestBaconian{
cipher.setInputStringEncode(decodedString);
assertEquals(decodedStringCleanUpper, cipher.inputString);
assertEquals(decodedStringClean, 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, times(1)).debug("Cleaned input string '{}'", decodedStringClean);
verify(logger, never()).debug(anyString());
verify(logger, times(2)).debug(anyString(), anyString());
}
@@ -226,7 +226,7 @@ public class TestBaconian{
@Test
public void testEncode(){
cipher.preserveCapitals = true;
cipher.inputString = decodedStringCleanUpper;
cipher.inputString = decodedStringClean;
cipher.encode();
@@ -248,14 +248,14 @@ public class TestBaconian{
cipher.decode();
assertEquals(decodedStringCleanUpper, cipher.outputString);
assertEquals(decodedStringClean, 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(1)).debug("Saving output string '{}'", decodedStringClean);
verify(logger, times(16)).debug(anyString());
verify(logger, times(16)).debug(anyString(), anyString());
}
@@ -283,37 +283,67 @@ public class TestBaconian{
@Test
public void testPracticalEncoding(){
//Test as original
cipher.preserveCapitals = true;
String output = cipher.encode(decodedString);
assertEquals(encodedString, output);
//Test fully cleaned
String output = cipher.encode(decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher.preserveCapitals = false;
output = cipher.encode(decodedString);
String output = cipher.encode(decodedString);
assertEquals(decodedStringCleanLower, cipher.inputString);
assertEquals(encodedString.toLowerCase(), cipher.outputString);
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
assertEquals(encodedString, cipher.inputString);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher.preserveCapitals = false;
output = cipher.decode(encodedString);
String output = cipher.decode(encodedString);
assertEquals(encodedString.toLowerCase(), cipher.inputString);
assertEquals(decodedStringCleanLower, cipher.outputString);
assertEquals(decodedStringCleanLower, output);
}
//Test uppercase input
@Test
public void testPracticalDecoding_upper(){
cipher.preserveCapitals = false;
output = cipher.decode(encodedString.toUpperCase());
String output = cipher.decode(encodedString.toUpperCase());
assertEquals(encodedString.toLowerCase(), cipher.inputString);
assertEquals(decodedStringCleanLower, cipher.outputString);
assertEquals(decodedStringCleanLower, output);
}
//Test lowercase input
@Test
public void testPracticalDecoding_lower(){
cipher.preserveCapitals = false;
output = cipher.decode(encodedString.toLowerCase());
String output = cipher.decode(encodedString.toLowerCase());
assertEquals(encodedString.toLowerCase(), cipher.inputString);
assertEquals(decodedStringCleanLower, cipher.outputString);
assertEquals(decodedStringCleanLower, output);
}
}

View File

@@ -287,6 +287,13 @@ public class TestBaseX{
assertEquals(encodedString_16, output);
}
@Test
public void testPracticalEncoding_inputOnly(){
String output = cipher.encode(decodedString);
assertEquals(encodedString_2, output);
}
@Test
public void testPracticalDecoding_2(){
String output = cipher.decode(2, encodedString_2);
@@ -314,4 +321,11 @@ public class TestBaseX{
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_inputOnly(){
String output = cipher.decode(encodedString_2);
assertEquals(decodedString, output);
}
}

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBeaufort.java
//Mattrixwv
// Created: 02-23-22
//Modified: 04-16-23
//Modified: 04-17-23
package com.mattrixwv.cipherstream.monosubstitution;
@@ -353,9 +353,9 @@ public class TestBeaufort{
cipher.outputString = encodedString;
cipher.keyword = keyword;
assertEquals(decodedString, cipher.inputString);
assertEquals(encodedString, cipher.outputString);
assertEquals(keyword, cipher.keyword);
assertEquals(decodedString, cipher.getInputString());
assertEquals(encodedString, cipher.getOutputString());
assertEquals(keyword, cipher.getKeyword());
}
@Test
@@ -373,27 +373,45 @@ public class TestBeaufort{
@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
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Beaufort(false, false, false);
output = cipher.encode(keyword, decodedString);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(encodedStringClean, cipher.outputString);
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
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new Beaufort(false, false, false);
output = cipher.decode(keyword, encodedString);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -27,10 +27,10 @@ public class TestCaesar{
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 String decodedString = "The quick brown fox jumps over - the lAzy dog";
private String decodedStringClean = "thequickbrownfoxjumpsoverthelazydog";
private String encodedString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb iXwv ald";
private String encodedStringClean = "qebnrfzhyoltkclugrjmplsboqebixwvald";
private int shift = 23;
@@ -124,14 +124,14 @@ public class TestCaesar{
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.setInputString(inputString);
cipher.setInputString(decodedString);
assertEquals(inputString, cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
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 '{}'", inputString);
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
}
@Test
@@ -140,14 +140,14 @@ public class TestCaesar{
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.setInputString(inputString);
cipher.setInputString(decodedString);
assertEquals(inputString.toLowerCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
assertEquals(decodedString.toLowerCase(), 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 '{}'", inputString.toLowerCase());
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toLowerCase());
}
@Test
@@ -156,14 +156,14 @@ public class TestCaesar{
cipher.preserveWhitespace = false;
cipher.preserveSymbols = true;
cipher.setInputString(inputString);
cipher.setInputString(decodedString);
assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
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 '{}'", inputString.replaceAll("\\s", ""));
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
}
@Test
@@ -172,14 +172,14 @@ public class TestCaesar{
cipher.preserveWhitespace = true;
cipher.preserveSymbols = false;
cipher.setInputString(inputString);
cipher.setInputString(decodedString);
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
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 '{}'", inputString.replaceAll("[^a-zA-Z\\s]", ""));
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
}
@Test
@@ -220,12 +220,12 @@ public class TestCaesar{
@Test
public void testEncode(){
cipher.inputString = inputString;
cipher.inputString = decodedString;
cipher.shift = shift;
cipher.encode();
assertEquals(outputString, cipher.outputString);
assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Encoding");
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Encoding uppercase");
@@ -235,17 +235,17 @@ public class TestCaesar{
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);
verify(logger, times(1)).debug("Saving encoded string '{}'", encodedString);
}
@Test
public void testEncode_negative(){
cipher.inputString = inputString;
cipher.inputString = decodedString;
cipher.shift = shift - 26;
cipher.encode();
assertEquals(outputString, cipher.outputString);
assertEquals(encodedString, cipher.outputString);
verify(logger, times(1)).debug("Encoding");
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Encoding uppercase");
@@ -255,17 +255,17 @@ public class TestCaesar{
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);
verify(logger, times(1)).debug("Saving encoded string '{}'", encodedString);
}
@Test
public void testDecode(){
cipher.inputString = outputString;
cipher.inputString = encodedString;
cipher.shift = shift;
cipher.decode();
assertEquals(inputString, cipher.outputString);
assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Decoding");
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Decoding uppercase");
@@ -275,17 +275,17 @@ public class TestCaesar{
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);
verify(logger, times(1)).debug("Saving decoded string '{}'", decodedString);
}
@Test
public void testDecode_negative(){
cipher.inputString = outputString;
cipher.inputString = encodedString;
cipher.shift = shift - 26;
cipher.decode();
assertEquals(inputString, cipher.outputString);
assertEquals(decodedString, cipher.outputString);
verify(logger, times(1)).debug("Decoding");
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Decoding uppercase");
@@ -295,24 +295,24 @@ public class TestCaesar{
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);
verify(logger, times(1)).debug("Saving decoded string '{}'", decodedString);
}
@Test
public void testGetters(){
cipher.inputString = inputString;
cipher.outputString = outputString;
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.shift = shift;
assertEquals(inputString, cipher.getInputString());
assertEquals(outputString, cipher.getOutputString());
assertEquals(decodedString, cipher.getInputString());
assertEquals(encodedString, cipher.getOutputString());
assertEquals(shift, cipher.getShift());
}
@Test
public void testReset(){
cipher.inputString = inputString;
cipher.outputString = outputString;
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.shift = shift;
cipher.reset();
@@ -326,80 +326,71 @@ public class TestCaesar{
@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);
String output = cipher.encode(shift, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(shift, cipher.shift);
assertEquals(outputString, cipher.outputString);
assertEquals(outputString, output);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, 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);
String output = cipher.encode(shift, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(shift, cipher.shift);
assertEquals(outputStringClean, cipher.outputString);
assertEquals(outputStringClean, output);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, 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);
String output = cipher.encode(shift, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(shift, cipher.shift);
assertEquals(outputString, cipher.outputString);
assertEquals(outputString, output);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalDecoding(){
cipher = new Caesar(true, true, true);
logger = mock(Logger.class);
Caesar.logger = logger;
String output = cipher.decode(shift, outputString);
String output = cipher.decode(shift, encodedString);
assertEquals(outputString, cipher.inputString);
assertEquals(encodedString, cipher.inputString);
assertEquals(shift, cipher.shift);
assertEquals(inputString, cipher.outputString);
assertEquals(inputString, output);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, 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);
String output = cipher.decode(shift, encodedString);
assertEquals(outputStringClean, cipher.inputString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(shift, cipher.shift);
assertEquals(inputStringClean, cipher.outputString);
assertEquals(inputStringClean, output);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, 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);
String output = cipher.decode(shift - 26, encodedString);
assertEquals(outputString, cipher.inputString);
assertEquals(encodedString, cipher.inputString);
assertEquals(shift - 26, cipher.shift);
assertEquals(inputString, cipher.outputString);
assertEquals(inputString, output);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
}

View File

@@ -1,407 +1,241 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestOneTimePad.java
//Mattrixwv
// Created: 02-23-22
//Modified: 07-09-22
//Modified: 04-17-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 java.util.ArrayList;
import java.util.Arrays;
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 TestOneTimePad{
@Test
public void testEncode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, true, true);
private OneTimePad cipher;
private Logger logger;
//Variables
private String decodedString = "Message to^encode";
private String decodedStringClean = "MESSAGETOENCODE";
private String encodedString = "Wiqooxh mv^egkgws";
private String encodedStringClean = "WIQOOXHMVEGKGWS";
private String keyword = "keywordThatIsTotallyRandom";
private ArrayList<Integer> offset = new ArrayList<>(Arrays.asList(10, 4, 24, 22, 14, 17, 3, 19, 7, 0, 19, 8, 18, 19, 14, 19, 0, 11, 11, 24, 17, 0, 13, 3, 14, 12));
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "wiqooxhmvegkgws";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxh mv egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxh*mv+egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol encoding
inputString = "Message to^encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Wiqooxh mv^egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(false, true, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "WIQOOXHMVEGKGWS";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXH MV EGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXH*MV+EGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol encoding
inputString = "Message to^encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXH MV^EGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, false, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "wiqooxhmvegkgws";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxhmvegkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxh*mv+egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol encoding
inputString = "Message to^encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Wiqooxhmv^egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, true, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "wiqooxhmvegkgws";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxh mv egkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "wiqooxhmvegkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol encoding
inputString = "Message to^encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Wiqooxh mvegkgws";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(false, false, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "WIQOOXHMVEGKGWS";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol encoding
inputString = "Message to^encode";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "WIQOOXHMVEGKGWS";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
@BeforeEach
public void setup(){
cipher = new OneTimePad();
logger = mock(Logger.class);
OneTimePad.logger = logger;
}
@Test
public void testDecode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, true, true);
public void testConstructor_default(){
cipher = new OneTimePad();
//Test lowercase decoding
String inputString = "wiqooxhmvegkgws";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHMVEGKGWS";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh mv egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "message to encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*mv+egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "message*to+encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Wiqooxh mv^egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Message to^encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
}
@Test
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(false, true, true);
public void testConstructor_preservesCapitals(){
cipher = new OneTimePad(true, false, false);
//Test lowercase decoding
String inputString = "wiqooxhmvegkgws";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHMVEGKGWS";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh mv egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGE TO ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*mv+egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGE*TO+ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Wiqooxh mv^egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGE TO^ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
}
@Test
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, false, true);
public void testConstructor_preservesWhitespace(){
cipher = new OneTimePad(false, true, false);
//Test lowercase decoding
String inputString = "wiqooxhmvegkgws";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHMVEGKGWS";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh mv egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "messagetoencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*mv+egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "message*to+encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Wiqooxh mv^egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Messageto^encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
}
@Test
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(true, true, false);
public void testConstructor_preservesSymbols(){
cipher = new OneTimePad(false, false, true);
//Test lowercase decoding
String inputString = "wiqooxhmvegkgws";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHMVEGKGWS";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh mv egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "message to encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*mv+egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "messagetoencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whtiespace, symbol decoding
inputString = "Wiqooxh mv^egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "Message toencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
OneTimePad cipher = new OneTimePad(false, false, false);
public void testEncode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
//Test lowercase decoding
String inputString = "wiqooxhmvegkgws";
String keyword = "keywordThatIsTotallyRandom";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHMVEGKGWS";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
String output = cipher.encode(keyword, decodedString);
//Test whitespace decoding
inputString = "wiqooxh mv egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(decodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Encoding");
}
//Test symbol decoding
inputString = "wiqooxh*mv+egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
@Test
public void testEncode_clean(){
cipher.preserveCapitals = false;
cipher.preserveWhitespace = false;
cipher.preserveSymbols = false;
//Test mixed case, whtiespace, symbol decoding
inputString = "Wiqooxh mv^egkgws";
keyword = "keywordThatIsTotallyRandom";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Encoding");
}
@Test
public void testEncode_short(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
assertThrows(InvalidKeywordException.class, () -> {
cipher.encode("keyword", decodedString);
}, "Key must be at least as long as the input");
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
verify(logger, never()).debug(anyString());
}
@Test
public void testDecode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Decoding");
}
@Test
public void testDecode_clean(){
cipher.preserveCapitals = false;
cipher.preserveWhitespace = false;
cipher.preserveSymbols = false;
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Decoding");
}
@Test
public void testDecode_short(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.decode("keyword", encodedString);
});
assertEquals("", cipher.inputString);
assertEquals("", cipher.keyword);
assertEquals("", cipher.outputString);
assertEquals(new ArrayList<>(), cipher.offset);
verify(logger, never()).debug(anyString());
}
@Test
public void testPracticalEncoding(){
cipher = new OneTimePad(true, true, true);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new OneTimePad(false, false, false);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalDecoding(){
cipher = new OneTimePad(true, true, true);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new OneTimePad(false, false, false);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
}

View File

@@ -1,467 +1,485 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestSubstitution.java
//Mattrixwv
// Created: 02-22-22
//Modified: 07-09-22
//Modified: 04-18-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.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 TestSubstitution{
@Test
public void testEncode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, true, true);
private Substitution cipher;
private Logger logger;
//Variables
private String decodedString = "Message to^encode";
private String decodedStringClean = "MESSAGETOENCODE";
private String decodedStringAlNum = "Message to^encode 123";
private String decodedStringAlNumClean = "MESSAGETOENCODE";
private String encodedString = "Oguucig vq^gpeqfg";
private String encodedStringClean = "OGUUCIGVQGPEQFG";
private String encodedStringAlNum = "Oguucig vq^gpeqfg 876";
private String encodedStringAlNumClean = "OGUUCIGVQGPEQFG";
private String keyword = "cdefghijklmnopqrstuvwxyzab";
private String keywordAlNum = "cdefghijklmnopqrstuvwxyzab9876543210";
//Test lowercase encoding
String inputString = "messagetoencode";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "oguucigvqgpeqfg";
String output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucig vq gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucig*vq+gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Oguucig vq^gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number encoding with long key
inputString = "Message to&encode 123";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Oguucig vq&gpeqfg 876";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(false, true, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "OGUUCIGVQGPEQFG";
String output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIG VQ GPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIG*VQ+GPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIG VQ^GPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number encoding with long key
inputString = "Message to&encode 123";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "OGUUCIG VQ&GPEQFG 876";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, false, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "oguucigvqgpeqfg";
String output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucigvqgpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucig*vq+gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Oguucigvq^gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number encoding with long key
inputString = "Message to&encode 123";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Oguucigvq&gpeqfg876";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, true, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "oguucigvqgpeqfg";
String output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucig vq gpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "oguucigvqgpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Oguucig vqgpeqfg";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number encoding with long key
inputString = "Message to&encode 123";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Oguucig vqgpeqfg ";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(false, false, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "OGUUCIGVQGPEQFG";
String output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number encoding with long key
inputString = "Message to&encode 123";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "OGUUCIGVQGPEQFG";
output = cipher.encode(key, inputString);
assertEquals(correctOutput, output);
@BeforeEach
public void setup(){
cipher = new Substitution();
logger = mock(Logger.class);
Substitution.logger = logger;
}
@Test
public void testDecode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, true, true);
public void testConstructor_default(){
cipher = new Substitution();
//Test lowercase decoding
String inputString = "oguucigvqgpeqfg";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "messagetoencode";
String output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "OGUUCIGVQGPEQFG";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "oguucig vq gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "message to encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "oguucig*vq+gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "message*to+encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Oguucig vq^gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Message to^encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number decoding with long key
inputString = "Oguucig vq&gpeqfg 876";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Message to&encode 123";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
}
@Test
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(false, true, true);
public void testConstructor_preservesCapitals(){
cipher = new Substitution(true, false, false);
//Test lowercase decoding
String inputString = "oguucigvqgpeqfg";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "OGUUCIGVQGPEQFG";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "oguucig vq gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGE TO ENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "oguucig*vq+gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGE*TO+ENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Oguucig vq^gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGE TO^ENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number decoding with long key
inputString = "Oguucig vq&gpeqfg 876";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "MESSAGE TO&ENCODE 123";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
}
@Test
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, false, true);
public void testConstructor_preservesWhitespace(){
cipher = new Substitution(false, true, false);
//Test lowercase decoding
String inputString = "oguucigvqgpeqfg";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "messagetoencode";
String output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "OGUUCIGVQGPEQFG";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "oguucig vq gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "messagetoencode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "oguucig*vq+gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "message*to+encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Oguucig vq^gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Messageto^encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number decoding with long key
inputString = "Oguucig vq&gpeqfg 876";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Messageto&encode123";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
}
@Test
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(true, true, false);
public void testConstructor_preservesSymbols(){
cipher = new Substitution(false, false, true);
//Test lowercase decoding
String inputString = "oguucigvqgpeqfg";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "messagetoencode";
String output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "OGUUCIGVQGPEQFG";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "oguucig vq gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "message to encode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "oguucig*vq+gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "messagetoencode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Oguucig vq^gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "Message toencode";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number decoding with long key
inputString = "Oguucig vq&gpeqfg 876";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "Message toencode ";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
}
@Test
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Substitution cipher = new Substitution(false, false, false);
public void testSetKey(){
cipher.setKeyword(keyword);
//Test lowercase decoding
String inputString = "oguucigvqgpeqfg";
String key = "cdefghijklmnopqrstuvwxyzab";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "OGUUCIGVQGPEQFG";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertEquals(keyword.toUpperCase(), cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", keyword);
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, times(1)).debug("Ensuring there are only letters in the key");
verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, times(1)).debug("Cleaned key '{}'", keyword.toUpperCase());
}
//Test whitespace decoding
inputString = "oguucig vq gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
@Test
public void testSetKey_alNum(){
cipher.setKeyword(keywordAlNum);
//Test symbol decoding
inputString = "oguucig*vq+gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", keywordAlNum);
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, never()).debug("Ensuring there are only letters in the key");
verify(logger, times(1)).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, times(1)).debug("Cleaned key '{}'", keywordAlNum.toUpperCase());
}
//Test mixed case, whitespace, symbol decoding
inputString = "Oguucig vq^gpeqfg";
key = "cdefghijklmnopqrstuvwxyzab";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol, number decoding with long key
inputString = "Oguucig vq&gpeqfg 876";
key = "cdefghijklmnopqrstuvwxyzab9876543210";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(key, inputString);
assertEquals(correctOutput, output);
@Test
public void testSetKey_duplicate(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("ABA");
});
assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", "ABA");
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, never()).debug("Ensuring there are only letters in the key");
verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, never()).debug(eq("Cleaned key '{}'"), anyString());
}
@Test
public void testSetKey_invalidLetter(){
assertThrows(InvalidKeywordException.class, () ->{
cipher.setKeyword("abcdefghijklmnop1rstuvwxyz");
});
assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", "abcdefghijklmnop1rstuvwxyz");
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, times(1)).debug("Ensuring there are only letters in the key");
verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, never()).debug(eq("Cleaned key '{}'"), anyString());
}
@Test
public void testSetKey_invalidAlNum(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("abcdefghijklmnop^rstuvwxyz0123456789");
});
assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", "abcdefghijklmnop^rstuvwxyz0123456789");
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, never()).debug("Ensuring there are only letters in the key");
verify(logger, times(1)).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, never()).debug(eq("Cleaned key '{}'"), anyString());
}
@Test
public void testSetKey_invalidLength(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("AB");
}, "The key must contain all letters and can contain all numbers");
assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Original key '{}'", "AB");
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
verify(logger, never()).debug("Ensuring there are only letters in the key");
verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, never()).debug(eq("Cleaned key '{}'"), anyString());
}
@Test
public void testSetKey_null(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null);
}, "Key cannot be null");
assertEquals("", cipher.keyword);
verify(logger, never()).debug(eq("Original key '{}'"), anyString());
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Ensuring there are no duplicate mappings");
verify(logger, never()).debug("Ensuring there are only letters in the key");
verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key");
verify(logger, never()).debug(eq("Cleaned key '{}'"), anyString());
}
@Test
public void testSetInputString(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.setInputString(decodedString);
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);
}
@Test
public void testSetInputString_noCapitals(){
cipher.preserveCapitals = false;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
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());
}
@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", ""));
}
@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]", ""));
}
@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 cannot be null");
assertEquals("", cipher.inputString);
verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
}
@Test
public void testEncode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.inputString = decodedStringAlNum;
cipher.keyword = keywordAlNum.toUpperCase();
cipher.encode();
assertEquals(encodedStringAlNum, cipher.outputString);
verify(logger, times(1)).debug("Encoding");
verify(logger, times(21)).debug(eq("Working character {}"), anyChar());
verify(logger, times(1)).debug("Encoding uppercase");
verify(logger, times(14)).debug("Encoding lowercase");
verify(logger, times(3)).debug("Encoding digit");
verify(logger, times(3)).debug("Passing symbol through");
verify(logger, times(1)).debug("Encoded message '{}'", encodedStringAlNum);
}
@Test
public void testDecode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.inputString = encodedStringAlNum;
cipher.keyword = keywordAlNum.toUpperCase();
cipher.decode();
assertEquals(decodedStringAlNum, cipher.outputString);
verify(logger, times(1)).debug("Decoding");
verify(logger, times(21)).debug(eq("Working character {}"), anyChar());
verify(logger, times(1)).debug("Encoding uppercase");
verify(logger, times(14)).debug("Encoding lowercase");
verify(logger, times(3)).debug("Encoding digit");
verify(logger, times(3)).debug("Passing symbol through");
verify(logger, times(1)).debug("Decoded message '{}'", decodedStringAlNum);
}
@Test
public void testGetters(){
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.keyword = keyword;
assertEquals(decodedString, cipher.getInputString());
assertEquals(encodedString, cipher.getOutputString());
assertEquals(keyword, cipher.getKeyword());
}
@Test
public void testReset(){
cipher.inputString = decodedString;
cipher.outputString = encodedString;
cipher.keyword = keyword;
cipher.reset();
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
verify(logger, times(1)).debug("Resetting fields");
}
@Test
public void testPracticalEncoding(){
cipher = new Substitution(true, true, true);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedString, cipher.outputString);
assertEquals(encodedString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new Substitution(false, false, false);
String output = cipher.encode(keyword, decodedString);
assertEquals(decodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedStringClean, cipher.outputString);
assertEquals(encodedStringClean, output);
}
@Test
public void testPracticalEncoding_alNum(){
cipher = new Substitution(true, true, true);
String output = cipher.encode(keywordAlNum, decodedStringAlNum);
assertEquals(decodedStringAlNum, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword);
assertEquals(encodedStringAlNum, cipher.outputString);
assertEquals(encodedStringAlNum, output);
}
@Test
public void testPracticalEncoding_alNumClean(){
cipher = new Substitution(false, false, false);
String output = cipher.encode(keywordAlNum, decodedStringAlNum);
assertEquals(decodedStringAlNumClean, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword);
assertEquals(encodedStringAlNumClean, cipher.outputString);
assertEquals(encodedStringAlNumClean, output);
}
@Test
public void testPrecticalEncoding_noAlNumKey(){
cipher = new Substitution(true, true, true);
String output = cipher.encode(keyword, decodedStringAlNum);
assertEquals(decodedStringAlNum, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(encodedString + " 123", cipher.outputString);
assertEquals(encodedString + " 123", output);
}
@Test
public void testPracticalDecoding(){
cipher = new Substitution(true, true, true);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString, cipher.outputString);
assertEquals(decodedString, output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new Substitution(false, false, false);
String output = cipher.decode(keyword, encodedString);
assertEquals(encodedStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedStringClean, cipher.outputString);
assertEquals(decodedStringClean, output);
}
@Test
public void testPracticalDecoding_alNum(){
cipher = new Substitution(true, true, true);
String output = cipher.decode(keywordAlNum, encodedStringAlNum);
assertEquals(encodedStringAlNum, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword);
assertEquals(decodedStringAlNum, cipher.outputString);
assertEquals(decodedStringAlNum, output);
}
@Test
public void testPracticalDecoding_alNumClean(){
cipher = new Substitution(false, false, false);
String output = cipher.decode(keywordAlNum, encodedStringAlNum);
assertEquals(encodedStringAlNumClean, cipher.inputString);
assertEquals(keywordAlNum.toUpperCase(), cipher.keyword);
assertEquals(decodedStringAlNumClean, cipher.outputString);
assertEquals(decodedStringAlNumClean, output);
}
@Test
public void testPracticalDecoding_noAlNumKey(){
cipher = new Substitution(true, true, true);
String output = cipher.decode(keyword, encodedString + " 123");
assertEquals(encodedString + " 123", cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(decodedString + " 123", cipher.outputString);
assertEquals(decodedString + " 123", output);
}
}

View File

@@ -1,407 +1,391 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestVigenere.java
//Mattrixwv
// Created: 07-25-21
//Modified: 07-09-22
//Modified: 04-18-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.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 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 TestVigenere{
@Test
public void testEncode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, true, true);
private Vigenere cipher;
private Logger logger;
//Variables
private String inputString = "MeSsage to^encode";
private String inputStringClean = "MESSAGETOENCODE";
private String outputString = "WiQooxh ds^cjqfgo";
private String outputStringClean = "WIQOOXHDSCJQFGO";
private String keyword = "keyword";
private ArrayList<Integer> offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3));
@BeforeEach
public void setup(){
cipher = new Vigenere();
logger = mock(Logger.class);
Vigenere.logger = logger;
}
@Test
public void testConstructor_default(){
cipher = new Vigenere();
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
}
@Test
public void testConstructor_preservesCapitals(){
cipher = new Vigenere(true, false, false);
assertTrue(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
}
@Test
public void testConstructor_preservesWhitespace(){
cipher = new Vigenere(false, true, false);
assertFalse(cipher.preserveCapitals);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
}
@Test
public void testConstructor_preservesSymbols(){
cipher = new Vigenere(false, false, true);
assertFalse(cipher.preserveCapitals);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
}
@Test
public void testSetOffset(){
cipher.keyword = keyword.toUpperCase();
cipher.setOffset();
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Setting offset array from keyword");
verify(logger, times(1)).debug("Offset {}", offset);
}
@Test
public void testSetInputString(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
cipher.setInputString(inputString);
assertEquals(inputString, cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString);
}
@Test
public void testSetInputString_noCapitals(){
cipher.preserveCapitals = false;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
cipher.setInputString(inputString);
assertEquals(inputString.toUpperCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, times(1)).debug("Removing case");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
}
@Test
public void testSetInputString_noWhitespace(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = false;
cipher.preserveSymbols = true;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
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.keyword = keyword.toUpperCase();
cipher.offset = offset;
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;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputString("");
}, "Input cannot be null");
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;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputString(null);
}, "Input must contain 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());
}
@Test
public void testSetKeyword(){
cipher.setKeyword(keyword);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
verify(logger, times(1)).debug("Original keyword '{}'", keyword);
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing all non-letter characters");
verify(logger, times(1)).debug("Clean keyword '{}'", keyword.toUpperCase());
}
@Test
public void testSetKeyword_blank(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword("");
}, "Keyword must contain at least 2 letters");
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
verify(logger, times(1)).debug("Original keyword '{}'", "");
verify(logger, times(1)).debug("Removing case");
verify(logger, times(1)).debug("Removing all non-letter characters");
verify(logger, times(1)).debug("Clean keyword '{}'", "");
}
@Test
public void testSetKeyword_null(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null);
}, "Keyword must contain at least 2 letters");
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
verify(logger, never()).debug(eq("Original keyword '{}'"), anyString());
verify(logger, never()).debug("Removing case");
verify(logger, never()).debug("Removing all non-letter characters");
verify(logger, never()).debug(eq("Clean keyword '{}'"), anyString());
}
@Test
public void testEncode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.inputString = inputString;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
cipher.encode();
assertEquals(outputString, cipher.outputString);
verify(logger, times(1)).debug("Encoding");
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Encoding uppercase");
verify(logger, times(1)).debug("Wrapping around to A");
verify(logger, times(13)).debug("Encoding lowercase");
verify(logger, times(5)).debug("Wrapping around to a");
verify(logger, times(17)).debug(eq("Encoded character {}"), anyChar());
verify(logger, times(1)).debug("Encoded message '{}'", outputString);
}
@Test
public void testDecode(){
cipher.preserveCapitals = true;
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.inputString = outputString;
cipher.keyword = keyword.toUpperCase();
cipher.offset = offset;
cipher.decode();
assertEquals(inputString, cipher.outputString);
verify(logger, times(1)).debug("Decoding");
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
verify(logger, times(2)).debug("Decoding uppercase");
verify(logger, times(1)).debug("Wrapping around to Z");
verify(logger, times(13)).debug("Decoding lowercase");
verify(logger, times(5)).debug("Wrapping around to z");
verify(logger, times(17)).debug(eq("Decoded character {}"), anyChar());
verify(logger, times(1)).debug("Decoded message '{}'", inputString);
}
@Test
public void testGetters(){
cipher.inputString = inputString;
cipher.outputString = outputString;
cipher.keyword = keyword;
cipher.offset = offset;
assertEquals(inputString, cipher.getInputString());
assertEquals(outputString, cipher.getOutputString());
assertEquals(keyword, cipher.getKeyword());
assertEquals(offset, cipher.getOffsets());
}
@Test
public void testReset(){
cipher.inputString = inputString;
cipher.outputString = outputString;
cipher.keyword = keyword;
cipher.offset = offset;
cipher.reset();
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertEquals(new ArrayList<>(), cipher.offset);
verify(logger, times(1)).debug("Resetting fields");
}
@Test
public void testPracticalEncoding(){
cipher = new Vigenere(true, true, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keyword";
String correctOutput = "wiqooxhdscjqfgo";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keyword";
correctOutput = "wiqooxh ds cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keyword";
correctOutput = "wiqooxh*ds+cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
keyword = "keyword";
correctOutput = "Wiqooxh ds^cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(inputString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
assertEquals(outputString, cipher.outputString);
assertEquals(outputString, output);
}
@Test
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(false, true, true);
public void testPracticalEncoding_clean(){
cipher = new Vigenere(false, false, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keyword";
String correctOutput = "WIQOOXHDSCJQFGO";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keyword";
correctOutput = "WIQOOXH DS CJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keyword";
correctOutput = "WIQOOXH*DS+CJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
keyword = "keyword";
correctOutput = "WIQOOXH DS^CJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(inputStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
assertEquals(outputStringClean, cipher.outputString);
assertEquals(outputStringClean, output);
}
@Test
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, false, true);
public void testPracticalDecoding(){
cipher = new Vigenere(true, true, true);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keyword";
String correctOutput = "wiqooxhdscjqfgo";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
String output = cipher.decode(keyword, outputString);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keyword";
correctOutput = "wiqooxhdscjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keyword";
correctOutput = "wiqooxh*ds+cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
keyword = "keyword";
correctOutput = "Wiqooxhds^cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(outputString, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
assertEquals(inputString, cipher.outputString);
assertEquals(inputString, output);
}
@Test
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, true, false);
public void testPracticalDecoding_clean(){
cipher = new Vigenere(false, false, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keyword";
String correctOutput = "wiqooxhdscjqfgo";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
String output = cipher.decode(keyword, outputString);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keyword";
correctOutput = "wiqooxh ds cjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keyword";
correctOutput = "wiqooxhdscjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
keyword = "keyword";
correctOutput = "Wiqooxh dscjqfgo";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(false, false, false);
//Test lowercase encoding
String inputString = "messagetoencode";
String keyword = "keyword";
String correctOutput = "WIQOOXHDSCJQFGO";
String output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase encoding
inputString = "MESSAGETOENCODE";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace encoding
inputString = "message to encode";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol encoding
inputString = "message*to+encode";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol encoding
inputString = "Message to^encode";
keyword = "keyword";
correctOutput = "WIQOOXHDSCJQFGO";
output = cipher.encode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testDecode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, true, true);
//Test lowercase decoding
String inputString = "wiqooxhdscjqfgo";
String keyword = "keyword";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHDSCJQFGO";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh ds cjqfgo";
keyword = "keyword";
correctOutput = "message to encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*ds+cjqfgo";
keyword = "keyword";
correctOutput = "message*to+encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Wiqooxh ds^cjqfgo";
keyword = "keyword";
correctOutput = "Message to^encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(false, true, true);
//Test lowercase decoding
String inputString = "wiqooxhdscjqfgo";
String keyword = "keyword";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHDSCJQFGO";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh ds cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGE TO ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*ds+cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGE*TO+ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Wiqooxh ds^cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGE TO^ENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, false, true);
//Test lowercase decoding
String inputString = "wiqooxhdscjqfgo";
String keyword = "keyword";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHDSCJQFGO";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh ds cjqfgo";
keyword = "keyword";
correctOutput = "messagetoencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*ds+cjqfgo";
keyword = "keyword";
correctOutput = "message*to+encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Wiqooxh ds^cjqfgo";
keyword = "keyword";
correctOutput = "Messageto^encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(true, true, false);
//Test lowercase decoding
String inputString = "wiqooxhdscjqfgo";
String keyword = "keyword";
String correctOutput = "messagetoencode";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHDSCJQFGO";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh ds cjqfgo";
keyword = "keyword";
correctOutput = "message to encode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*ds+cjqfgo";
keyword = "keyword";
correctOutput = "messagetoencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Wiqooxh ds^cjqfgo";
keyword = "keyword";
correctOutput = "Message toencode";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
}
@Test
public void testNoWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{
Vigenere cipher = new Vigenere(false, false, false);
//Test lowercase decoding
String inputString = "wiqooxhdscjqfgo";
String keyword = "keyword";
String correctOutput = "MESSAGETOENCODE";
String output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test uppercase decoding
inputString = "WIQOOXHDSCJQFGO";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test whitespace decoding
inputString = "wiqooxh ds cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test symbol decoding
inputString = "wiqooxh*ds+cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
//Test mixed case, whitespace, symbol decoding
inputString = "Wiqooxh ds^cjqfgo";
keyword = "keyword";
correctOutput = "MESSAGETOENCODE";
output = cipher.decode(keyword, inputString);
assertEquals(correctOutput, output);
assertEquals(outputStringClean, cipher.inputString);
assertEquals(keyword.toUpperCase(), cipher.keyword);
assertEquals(offset, cipher.offset);
assertEquals(inputStringClean, cipher.outputString);
assertEquals(inputStringClean, output);
}
}

View File

@@ -0,0 +1,359 @@
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TestLargePolybiusSquare.java
//Mattrixwv
// Created: 04-21-23
//Modified: 04-21-23
package com.mattrixwv.cipherstream.polysubstitution;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
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.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 TestLargePolybiusSquare{
private LargePolybiusSquare cipher;
private Logger logger;
//Variables
private String inputString = "Message to^encode";
private String inputStringClean = "MESSAGETOENCODE";
private String outputString = "35124343222612 4415^123624152112";
private String outputStringClean = "31 15 41 41 11 21 15 42 33 15 32 13 33 14 15";
private String keyword = "keyword";
private String keywordClean = "KEYWORDABCFGHIJLMNPQSTUVXZ0123456789";
private String keywordBlank = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private char[][] grid = {
{'K', 'E', 'Y', 'W', 'O', 'R'},
{'D', 'A', 'B', 'C', 'F', 'G'},
{'H', 'I', 'J', 'L', 'M', 'N'},
{'P', 'Q', 'S', 'T', 'U', 'V'},
{'X', 'Z', '0', '1', '2', '3'},
{'4', '5', '6', '7', '8', '9'}
};
private char[][] gridClean = {
{'A', 'B', 'C', 'D', 'E', 'F'},
{'G', 'H', 'I', 'J', 'K', 'L'},
{'M', 'N', 'O', 'P', 'Q', 'R'},
{'S', 'T', 'U', 'V', 'W', 'X'},
{'Y', 'Z', '0', '1', '2', '3'},
{'4', '5', '6', '7', '8', '9'},
};
@BeforeEach
public void setup(){
cipher = new LargePolybiusSquare();
logger = mock(Logger.class);
LargePolybiusSquare.logger = logger;
}
@Test
public void testConstructor_default(){
cipher = new LargePolybiusSquare();
assertFalse(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertArrayEquals(new char[6][6], cipher.grid);
}
@Test
public void testConstructor_preserveWhitespace(){
cipher = new LargePolybiusSquare(true, false);
assertTrue(cipher.preserveWhitespace);
assertFalse(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertArrayEquals(new char[6][6], cipher.grid);
}
@Test
public void testConstructor_preserveSymbols(){
cipher = new LargePolybiusSquare(false, true);
assertFalse(cipher.preserveWhitespace);
assertTrue(cipher.preserveSymbols);
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertArrayEquals(new char[6][6], cipher.grid);
}
@Test
public void testCreateGrid(){
cipher.keyword = keywordClean;
cipher.createGrid();
assertArrayEquals(grid, cipher.grid);
verify(logger, times(1)).debug("Creating grid");
}
@Test
public void testSetInputStringEncoding(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.setInputStringEncoding(inputString);
assertEquals(inputString.toUpperCase(), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
}
@Test
public void testSetInputStringEncoding_noWhitespace(){
cipher.preserveWhitespace = false;
cipher.preserveSymbols = true;
cipher.setInputStringEncoding(inputString);
assertEquals(inputString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, times(1)).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("\\s", ""));
}
@Test
public void testSetInputStringEncoding_noSymbols(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = false;
cipher.setInputStringEncoding(inputString);
assertEquals(inputString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""), cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, never()).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""));
}
@Test
public void testSetInputStringEncoding_noWhitespaceNoSymbols(){
cipher.preserveWhitespace = false;
cipher.preserveSymbols = false;
cipher.setInputStringEncoding(inputString);
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", inputString);
verify(logger, times(1)).debug("Removing whitespace");
verify(logger, times(1)).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", "M E S S A G E T O E N C O D E");
}
@Test
public void testSetInputStringEncoding_preparedBlank(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncoding("*");
});
assertEquals("*", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", "*");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", "*");
}
@Test
public void testSetInputStringEncoding_blank(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncoding("");
});
assertEquals("", cipher.inputString);
verify(logger, times(1)).debug("Original input string '{}'", "");
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, times(1)).debug("Cleaned input string '{}'", "");
}
@Test
public void testSetInputStringEncoding_null(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
assertThrows(InvalidInputException.class, () -> {
cipher.setInputStringEncoding(null);
});
assertEquals("", cipher.inputString);
verify(logger, never()).debug(eq("Original input string '{}'"), anyString());
verify(logger, never()).debug("Removing whitespace");
verify(logger, never()).debug("Removing symbols");
verify(logger, never()).debug(eq("Cleaned input string '{}'"), anyString());
}
@Test
public void testGetPreparedInputStringEncoding(){
cipher.inputString = inputString.toUpperCase();
String preparedInputString = cipher.getPreparedInputStringEncoding();
assertEquals(inputString.toUpperCase(), cipher.inputString);
assertEquals(inputString.toUpperCase().replaceAll("[^A-Z0-9]", ""), preparedInputString);
verify(logger, times(1)).debug("Preparing input string for encoding");
verify(logger, times(1)).debug("Prepared input string '{}'", preparedInputString);
}
@Test
public void testSetKeyword(){
cipher.setKeyword(keyword);
assertEquals(keywordClean, cipher.keyword);
assertArrayEquals(grid, cipher.grid);
verify(logger, times(1)).debug("Original keyword '{}'", keyword);
verify(logger, times(1)).debug("Cleaned keyword '{}'", keywordClean);
}
@Test
public void testSetKeyword_blank(){
cipher.setKeyword("");
assertEquals(keywordBlank, cipher.keyword);
assertArrayEquals(gridClean, cipher.grid);
verify(logger, times(1)).debug("Original keyword '{}'", "");
verify(logger, times(1)).debug("Cleaned keyword '{}'", keywordBlank);
}
@Test
public void testSetKeyword_null(){
assertThrows(InvalidKeywordException.class, () -> {
cipher.setKeyword(null);
});
assertEquals("", cipher.keyword);
assertArrayEquals(new char[6][6], cipher.grid);
verify(logger, never()).debug(eq("Original keyword '{}'"), anyString());
verify(logger, never()).debug(eq("Cleaned keyword '{}'"), anyString());
}
@Test
public void testAddCharactersToCleanStringEncode(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.keyword = keywordClean;
cipher.grid = grid;
cipher.inputString = inputString.toUpperCase();
cipher.addCharactersToCleanStringEncode(outputString.replaceAll("\\s", "").replaceAll("[^0-9]", ""));
assertEquals(outputString, cipher.outputString);
verify(logger, times(1)).debug("Formatting output string");
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
verify(logger, times(15)).debug("Appending character");
verify(logger, times(2)).debug("Appending symbol");
verify(logger, times(1)).debug("Saving output string {}", outputString);
}
@Test
public void testAddCharactersToCleanStringDecode(){
cipher.preserveWhitespace = true;
cipher.preserveSymbols = true;
cipher.keyword = keywordClean;
cipher.grid = grid;
cipher.inputString = outputString;
cipher.addCharactersToCleanStringDecode(inputStringClean);
verify(logger, times(1)).debug("Formatting output string");
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
verify(logger, times(15)).debug("Appending character");
verify(logger, times(2)).debug("Appending symbol");
verify(logger, times(1)).debug("Saving output string {}", inputString.toUpperCase());
}
@Test
public void testReset(){
cipher.inputString = inputString;
cipher.outputString = outputString;
cipher.keyword = keyword;
cipher.grid = grid;
cipher.reset();
assertEquals("", cipher.inputString);
assertEquals("", cipher.outputString);
assertEquals("", cipher.keyword);
assertArrayEquals(new char[6][6], cipher.grid);
verify(logger, times(1)).debug("Resetting");
}
@Test
public void testPracticalEncoding(){
cipher = new LargePolybiusSquare(true, true);
String output = cipher.encode(keyword, inputString);
assertEquals(inputString.toUpperCase(), cipher.inputString);
assertEquals(keywordClean, cipher.keyword);
assertEquals(outputString, cipher.outputString);
assertEquals(outputString, output);
}
@Test
public void testPracticalEncoding_clean(){
cipher = new LargePolybiusSquare(false, false);
String output = cipher.encode(inputString);
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
assertEquals(keywordBlank, cipher.keyword);
assertEquals(outputStringClean, cipher.outputString);
assertEquals(outputStringClean, output);
}
@Test
public void testPracticalDecoding(){
cipher = new LargePolybiusSquare(true, true);
String output = cipher.decode(keyword, outputString);
assertEquals(outputString, cipher.inputString);
assertEquals(keywordClean, cipher.keyword);
assertEquals(inputString.toUpperCase(), cipher.outputString);
assertEquals(inputString.toUpperCase(), output);
}
@Test
public void testPracticalDecoding_clean(){
cipher = new LargePolybiusSquare(false, false);
String output = cipher.decode(outputStringClean);
assertEquals(outputStringClean.replaceAll("\\s", ""), cipher.inputString);
assertEquals(keywordBlank, cipher.keyword);
assertEquals(inputStringClean, cipher.outputString);
assertEquals(inputStringClean, output);
}
}