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