|
| 1 | +import org.junit.Before; |
| 2 | +import org.junit.Ignore; |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.experimental.runners.Enclosed; |
| 5 | +import org.junit.runner.RunWith; |
| 6 | + |
| 7 | +import static org.assertj.core.api.Assertions.assertThat; |
| 8 | + |
| 9 | + |
| 10 | +@RunWith(Enclosed.class) |
| 11 | +public class SimpleCipherTest { |
| 12 | + public static class RandomKeyCipher { |
| 13 | + private Cipher cipherWithDefaultKey; |
| 14 | + |
| 15 | + @Before |
| 16 | + public void setup() { |
| 17 | + cipherWithDefaultKey = new Cipher(); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Here we take advantage of the fact that plaintext of "aaa..." doesn't output the key. This is a critical |
| 22 | + * problem with shift ciphers, some characters will always output the key verbatim. |
| 23 | + */ |
| 24 | + @Test |
| 25 | + public void cipherCanEncode() { |
| 26 | + String plainText = "aaaaaaaaaa"; |
| 27 | + String cipherText = cipherWithDefaultKey.getKey().substring(0, 10); |
| 28 | + assertThat(cipherWithDefaultKey.encode(plainText)).isEqualTo(cipherText); |
| 29 | + } |
| 30 | + |
| 31 | + @Ignore("Remove to run test") |
| 32 | + @Test |
| 33 | + public void cipherCanDecode() { |
| 34 | + String cipherText = "aaaaaaaaaa"; |
| 35 | + assertThat(cipherWithDefaultKey.decode(cipherWithDefaultKey.getKey().substring(0, 10))) |
| 36 | + .isEqualTo(cipherText); |
| 37 | + } |
| 38 | + |
| 39 | + @Ignore("Remove to run test") |
| 40 | + @Test |
| 41 | + public void cipherIsReversible() { |
| 42 | + String plainText = "abcdefghij"; |
| 43 | + assertThat(cipherWithDefaultKey.decode(cipherWithDefaultKey.encode(plainText))).isEqualTo(plainText); |
| 44 | + } |
| 45 | + |
| 46 | + @Ignore("Remove to run test") |
| 47 | + @Test |
| 48 | + public void keyIsLowercaseLetters() { |
| 49 | + assertThat(cipherWithDefaultKey.getKey()).matches("^[a-z]+$"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static class SubstitutionCipher { |
| 54 | + private Cipher cipherWithDefaultKey = new Cipher("abcdefghij"); |
| 55 | + |
| 56 | + @Ignore("Remove to run test") |
| 57 | + @Test |
| 58 | + public void cipherCanEncode() { |
| 59 | + String plainText = "aaaaaaaaaa"; |
| 60 | + String cipherText = "abcdefghij"; |
| 61 | + assertThat(cipherWithDefaultKey.encode(plainText)).isEqualTo(cipherText); |
| 62 | + } |
| 63 | + |
| 64 | + @Ignore("Remove to run test") |
| 65 | + @Test |
| 66 | + public void cipherCanDecode() { |
| 67 | + String plainText = "abcdefghij"; |
| 68 | + String cipherText = "aaaaaaaaaa"; |
| 69 | + assertThat(cipherWithDefaultKey.decode(plainText)).isEqualTo(cipherText); |
| 70 | + } |
| 71 | + |
| 72 | + @Ignore("Remove to run test") |
| 73 | + @Test |
| 74 | + public void cipherIsReversibleGivenKey() { |
| 75 | + String plainText = "abcdefghij"; |
| 76 | + assertThat(cipherWithDefaultKey.decode(cipherWithDefaultKey.encode(plainText))).isEqualTo(plainText); |
| 77 | + } |
| 78 | + |
| 79 | + @Ignore("Remove to run test") |
| 80 | + @Test |
| 81 | + public void cipherCanDoubleShiftEncode() { |
| 82 | + String plainText = "iamapandabear"; |
| 83 | + String cipherText = "qayaeaagaciai"; |
| 84 | + assertThat(new Cipher(plainText).encode(plainText)).isEqualTo(cipherText); |
| 85 | + } |
| 86 | + |
| 87 | + @Ignore("Remove to run test") |
| 88 | + @Test |
| 89 | + public void cipherCanWrapEncode() { |
| 90 | + String plainText = "zzzzzzzzzz"; |
| 91 | + String cipherText = "zabcdefghi"; |
| 92 | + assertThat(cipherWithDefaultKey.encode(plainText)).isEqualTo(cipherText); |
| 93 | + } |
| 94 | + |
| 95 | + @Ignore("Remove to run test") |
| 96 | + @Test |
| 97 | + public void cipherCanWrapDecode() { |
| 98 | + String plainText = "zabcdefghi"; |
| 99 | + String cipherText = "zzzzzzzzzz"; |
| 100 | + assertThat(cipherWithDefaultKey.decode(plainText)).isEqualTo(cipherText); |
| 101 | + } |
| 102 | + |
| 103 | + @Ignore("Remove to run test") |
| 104 | + @Test |
| 105 | + public void cipherMessageLongerThanKey() { |
| 106 | + String plainText = "iamapandabear"; |
| 107 | + String key = "abc"; |
| 108 | + String cipherText = "iboaqcnecbfcr"; |
| 109 | + assertThat(new Cipher(key).encode(plainText)).isEqualTo(cipherText); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
0 commit comments