|
5 | 5 | import static org.junit.jupiter.api.Assertions.assertNotEquals; |
6 | 6 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
7 | 7 |
|
| 8 | +import org.junit.jupiter.api.Assertions; |
8 | 9 | import org.junit.jupiter.api.BeforeEach; |
9 | 10 | import org.junit.jupiter.api.Test; |
10 | 11 |
|
@@ -44,4 +45,115 @@ public void testLongPlainText() { |
44 | 45 | assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces."); |
45 | 46 | assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(longText, keyword), "The encrypted text should be the same when encrypted again."); |
46 | 47 | } |
| 48 | + |
| 49 | + @Test |
| 50 | + void shouldNotProduceNullOrEmptyEncryptedText() { |
| 51 | + String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword); |
| 52 | + |
| 53 | + Assertions.assertNotNull(encrypted, "Encrypted text should not be null"); |
| 54 | + Assertions.assertFalse(encrypted.isEmpty(), "Encrypted text should not be empty"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void shouldChangePlaintextToEncrypted() { |
| 59 | + String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword); |
| 60 | + |
| 61 | + Assertions.assertNotEquals(plaintext, encrypted, "Encrypted text should differ from plaintext"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void shouldEncryptDetermenistically() { |
| 66 | + String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); |
| 67 | + String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); |
| 68 | + |
| 69 | + Assertions.assertEquals(encrypted1, encrypted2, "Encryptions should be equal"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void shouldProduceDifferentEncryptionsWithDifferentKeywoards() { |
| 74 | + String keyword2 = keyword + "a"; |
| 75 | + |
| 76 | + String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); |
| 77 | + String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword2); |
| 78 | + |
| 79 | + Assertions.assertNotEquals(encrypted1, encrypted2, "Should produce different encryptions"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void shouldMatchWithUnsortedKeyword() { |
| 84 | + String myPlaintext = "123456789"; |
| 85 | + String myKeyword = "unsorted"; |
| 86 | + |
| 87 | + String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); |
| 88 | + String expected = "8≈7≈2≈4≈5≈3≈6≈19"; |
| 89 | + |
| 90 | + Assertions.assertEquals(expected, encrypted, "Should match"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void shouldMatchEncryptionAndDecryptionWithNoSpacesOrPadding() { |
| 95 | + String myPlaintext = "NoSpacesOrPadding"; |
| 96 | + |
| 97 | + ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); |
| 98 | + String decrypted = ColumnarTranspositionCipher.decrypt(); |
| 99 | + |
| 100 | + Assertions.assertEquals(myPlaintext, decrypted, "Decrypted text should match original plaintext"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void shouldNotContainPaddingInDecryption() { |
| 105 | + String myPlaintext = "text"; |
| 106 | + |
| 107 | + ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); |
| 108 | + String decrypted = ColumnarTranspositionCipher.decrypt(); |
| 109 | + |
| 110 | + Assertions.assertFalse(decrypted.contains("≈"), "Should not contain padding characters"); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void shouldEncryptWithKeywordLongerThanPlaintext() { |
| 115 | + String myPlaintext = "text"; |
| 116 | + String myKeyword = "averylongkeyword"; |
| 117 | + |
| 118 | + String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); |
| 119 | + |
| 120 | + Assertions.assertNotNull(encryption, "Should encrypt where plaintext.length() < keyword.length()"); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void shouldEncryptWithKeywordWithSameLengthAsPlaintext() { |
| 125 | + String myPlaintext = "textaslongaskeyword"; |
| 126 | + String myKeyword = "keywordaslongastext"; |
| 127 | + |
| 128 | + String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); |
| 129 | + |
| 130 | + Assertions.assertNotNull(encryption, "Should encrypt where plaintext.length() == keyword.length()"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void shouldProduceDifferentEncryptionForSameKeywordButSortedDifferently() { |
| 135 | + String unsertedKeyword1 = "EFGHABCD"; |
| 136 | + String unsertedKeyword2 = "AEBFCGDH"; |
| 137 | + |
| 138 | + String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword1); |
| 139 | + String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword2); |
| 140 | + |
| 141 | + Assertions.assertNotEquals(encrypted1, encrypted2, "Should differ with different keywords"); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void shouldEncryptWithCustomAbecedarium() { |
| 146 | + String myAbecedarium = "abcdefghijklmnopqrstuvwxyz"; |
| 147 | + |
| 148 | + String encryption = ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium); |
| 149 | + |
| 150 | + Assertions.assertNotNull(encryption, "Should encrypt with custom abecedarium"); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void shouldNotEncryptWithInvalidAbecedarium() { |
| 155 | + String myAbecedarium = "abcde"; |
| 156 | + |
| 157 | + Assertions.assertThrows(NullPointerException.class, () -> ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium), "Should throw error when keyword contains characters not present in abecedarium"); |
| 158 | + } |
47 | 159 | } |
0 commit comments