diff --git a/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestSubstitution.java b/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestSubstitution.java new file mode 100644 index 0000000..04000de --- /dev/null +++ b/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestSubstitution.java @@ -0,0 +1,467 @@ +//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestSubstitution.java +//Mattrixwv +// Created: 02-22-22 +//Modified: 02-22-22 +package com.mattrixwv.CipherStreamJava.monoSubstitution; + + +import static org.junit.Assert.assertEquals; + +import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException; +import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException; + +import org.junit.Test; + + +public class TestSubstitution{ + @Test + public void testEncode() throws InvalidKeywordException, InvalidInputException{ + Substitution cipher = new Substitution(true, true, true); + + //Test lowercase encoding + String inputString = "messagetoencode"; + String key = "cdefghijklmnopqrstuvwxyzab"; + String correctOutput = "oguucigvqgpeqfg"; + String output = cipher.encode(key, inputString); + assertEquals("Substitution failed lowercase encoding.", correctOutput, output); + //Test uppercase encoding + inputString = "MESSAGETOENCODE"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIGVQGPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed uppercase encoding.", correctOutput, output); + + //Test whitespace encoding + inputString = "message to encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "oguucig vq gpeqfg"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed whitespace encoding.", correctOutput, output); + + //Test symbol encoding + inputString = "message*to+encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "oguucig*vq+gpeqfg"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed symbol encoding.", correctOutput, output); + + //Test mixed case, whitespace, symbol encoding + inputString = "Message to^encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "Oguucig vq^gpeqfg"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed mixed case, whitespace, symbol encoding.", 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("Substitution failed mixed case, whitespace, symbol, number encoding with long key.", 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("Substitution failed no capital lowercase encoding.", correctOutput, output); + //Test uppercase encoding + inputString = "MESSAGETOENCODE"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIGVQGPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no capital uppercase encoding.", correctOutput, output); + + //Test whitespace encoding + inputString = "message to encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIG VQ GPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no capital whitespace encoding.", correctOutput, output); + + //Test symbol encoding + inputString = "message*to+encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIG*VQ+GPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no capital symbol encoding.", correctOutput, output); + + //Test mixed case, whitespace, symbol encoding + inputString = "Message to^encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIG VQ^GPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no capital mixed case, whitespace, symbol encoding.", 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("Substitution failed no capital mixed case, whitespace, symbol, number encoding with long key.", 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("Substitution failed no whitespace lowercase encoding.", correctOutput, output); + //Test uppercase encoding + inputString = "MESSAGETOENCODE"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIGVQGPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no whitespace uppercase encoding.", correctOutput, output); + + //Test whitespace encoding + inputString = "message to encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "oguucigvqgpeqfg"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no whitespace whitespace encoding.", correctOutput, output); + + //Test symbol encoding + inputString = "message*to+encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "oguucig*vq+gpeqfg"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no whitespace symbol encoding.", correctOutput, output); + + //Test mixed case, whitespace, symbol encoding + inputString = "Message to^encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "Oguucigvq^gpeqfg"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no whitespace mixed case, whitespace, symbol encoding.", 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("Substitution failed no whitespace mixed case, whitespace, symbol, number encoding with long key.", 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("Substitution failed no symbol lowercase encoding.", correctOutput, output); + //Test uppercase encoding + inputString = "MESSAGETOENCODE"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIGVQGPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no symbol uppercase encoding.", correctOutput, output); + + //Test whitespace encoding + inputString = "message to encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "oguucig vq gpeqfg"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no symbol whitespace encoding.", correctOutput, output); + + //Test symbol encoding + inputString = "message*to+encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "oguucigvqgpeqfg"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no symbol symbol encoding.", correctOutput, output); + + //Test mixed case, whitespace, symbol encoding + inputString = "Message to^encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "Oguucig vqgpeqfg"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no symbol mixed case, whitespace, symbol encoding.", 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("Substitution failed no symbol mixed case, whitespace, symbol, number encoding with long key.", 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("Substitution failed no capital lowercase encoding.", correctOutput, output); + //Test uppercase encoding + inputString = "MESSAGETOENCODE"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIGVQGPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no capital uppercase encoding.", correctOutput, output); + + //Test whitespace encoding + inputString = "message to encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIGVQGPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no capital whitespace encoding.", correctOutput, output); + + //Test symbol encoding + inputString = "message*to+encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIGVQGPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no capital symbol encoding.", correctOutput, output); + + //Test mixed case, whitespace, symbol encoding + inputString = "Message to^encode"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "OGUUCIGVQGPEQFG"; + output = cipher.encode(key, inputString); + assertEquals("Substitution failed no capital mixed case, whitespace, symbol encoding.", 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("Substitution failed no capital mixed case, whitespace, symbol, number encoding with long key.", correctOutput, output); + } + + + @Test + public void testDecode() throws InvalidKeywordException, InvalidInputException{ + Substitution cipher = new Substitution(true, true, true); + + //Test lowercase decoding + String inputString = "oguucigvqgpeqfg"; + String key = "cdefghijklmnopqrstuvwxyzab"; + String correctOutput = "messagetoencode"; + String output = cipher.decode(key, inputString); + assertEquals("Substitution failed lowercase decoding.", correctOutput, output); + //Test uppercase decoding + inputString = "OGUUCIGVQGPEQFG"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed uppercase decoding.", correctOutput, output); + + //Test whitespace decoding + inputString = "oguucig vq gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "message to encode"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed whitespace decoding.", correctOutput, output); + + //Test symbol decoding + inputString = "oguucig*vq+gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "message*to+encode"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed whitespace decoding.", correctOutput, output); + + //Test mixed case, whitespace, symbol decoding + inputString = "Oguucig vq^gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "Message to^encode"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed mixed case, whitespace, symbol decoding.", 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("Substitution failed mixed case, whitespace, symbol, number decoding with long key.", correctOutput, output); + } + + @Test + public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException{ + Substitution cipher = new Substitution(false, true, true); + + //Test lowercase decoding + String inputString = "oguucigvqgpeqfg"; + String key = "cdefghijklmnopqrstuvwxyzab"; + String correctOutput = "MESSAGETOENCODE"; + String output = cipher.decode(key, inputString); + assertEquals("Substitution failed no capital lowercase decoding.", correctOutput, output); + //Test uppercase decoding + inputString = "OGUUCIGVQGPEQFG"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no capital uppercase decoding.", correctOutput, output); + + //Test whitespace decoding + inputString = "oguucig vq gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGE TO ENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no capital whitespace decoding.", correctOutput, output); + + //Test symbol decoding + inputString = "oguucig*vq+gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGE*TO+ENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no capital whitespace decoding.", correctOutput, output); + + //Test mixed case, whitespace, symbol decoding + inputString = "Oguucig vq^gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGE TO^ENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no capital mixed case, whitespace, symbol decoding.", 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("Substitution failed no capital mixed case, whitespace, symbol, number decoding with long key.", correctOutput, output); + } + + @Test + public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException{ + Substitution cipher = new Substitution(true, false, true); + + //Test lowercase decoding + String inputString = "oguucigvqgpeqfg"; + String key = "cdefghijklmnopqrstuvwxyzab"; + String correctOutput = "messagetoencode"; + String output = cipher.decode(key, inputString); + assertEquals("Substitution failed no whitespace lowercase decoding.", correctOutput, output); + //Test uppercase decoding + inputString = "OGUUCIGVQGPEQFG"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no whitespace uppercase decoding.", correctOutput, output); + + //Test whitespace decoding + inputString = "oguucig vq gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "messagetoencode"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no whitespace whitespace decoding.", correctOutput, output); + + //Test symbol decoding + inputString = "oguucig*vq+gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "message*to+encode"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no whitespace whitespace decoding.", correctOutput, output); + + //Test mixed case, whitespace, symbol decoding + inputString = "Oguucig vq^gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "Messageto^encode"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no whitespace mixed case, whitespace, symbol decoding.", 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("Substitution failed no whitespace mixed case, whitespace, symbol, number decoding with long key.", correctOutput, output); + } + + @Test + public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException{ + Substitution cipher = new Substitution(true, true, false); + + //Test lowercase decoding + String inputString = "oguucigvqgpeqfg"; + String key = "cdefghijklmnopqrstuvwxyzab"; + String correctOutput = "messagetoencode"; + String output = cipher.decode(key, inputString); + assertEquals("Substitution failed no symbol lowercase decoding.", correctOutput, output); + //Test uppercase decoding + inputString = "OGUUCIGVQGPEQFG"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no symbol uppercase decoding.", correctOutput, output); + + //Test whitespace decoding + inputString = "oguucig vq gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "message to encode"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no symbol whitespace decoding.", correctOutput, output); + + //Test symbol decoding + inputString = "oguucig*vq+gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "messagetoencode"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed no symbol whitespace decoding.", correctOutput, output); + + //Test mixed case, whitespace, symbol decoding + inputString = "Oguucig vq^gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "Message toencode"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed mixed no symbol case, whitespace, symbol decoding.", 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("Substitution failed no symbol mixed case, whitespace, symbol, number decoding with long key.", correctOutput, output); + } + + @Test + public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException{ + Substitution cipher = new Substitution(false, false, false); + + //Test lowercase decoding + String inputString = "oguucigvqgpeqfg"; + String key = "cdefghijklmnopqrstuvwxyzab"; + String correctOutput = "MESSAGETOENCODE"; + String output = cipher.decode(key, inputString); + assertEquals("Substitution failed secure lowercase decoding.", correctOutput, output); + //Test uppercase decoding + inputString = "OGUUCIGVQGPEQFG"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed secure uppercase decoding.", correctOutput, output); + + //Test whitespace decoding + inputString = "oguucig vq gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed secure whitespace decoding.", correctOutput, output); + + //Test symbol decoding + inputString = "oguucig*vq+gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed secure whitespace decoding.", correctOutput, output); + + //Test mixed case, whitespace, symbol decoding + inputString = "Oguucig vq^gpeqfg"; + key = "cdefghijklmnopqrstuvwxyzab"; + correctOutput = "MESSAGETOENCODE"; + output = cipher.decode(key, inputString); + assertEquals("Substitution failed secure mixed case, whitespace, symbol decoding.", 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("Substitution failed secure mixed case, whitespace, symbol, number decoding with long key.", correctOutput, output); + } +}