Repository navigation
Expand file tree
/
Copy pathADFGVXCipherTest.java
More file actions
76 lines (61 loc) · 2.54 KB
/
Copy pathADFGVXCipherTest.java
File metadata and controls
76 lines (61 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.thealgorithms.ciphers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class ADFGVXCipherTest {
private final ADFGVXCipher adfgvxCipher = new ADFGVXCipher();
@Test
void testEncrypt() {
String message = "attack at 1200am";
String key = "PRIVACY";
String encrypted = adfgvxCipher.encrypt(message, key);
assertEquals("DGDDDAGDDGAFADDFDADVDVFAADVX", encrypted);
}
@Test
void testDecrypt() {
String encrypted = "DGDDDAGDDGAFADDFDADVDVFAADVX";
String key = "PRIVACY";
String decrypted = adfgvxCipher.decrypt(encrypted, key);
assertEquals("ATTACKAT1200AM", decrypted);
}
@Test
void testEmptyInput() {
String encrypted = adfgvxCipher.encrypt("", "PRIVACY");
String decrypted = adfgvxCipher.decrypt("", "PRIVACY");
assertEquals("", encrypted);
assertEquals("", decrypted);
}
@Test
void testShortKey() {
String message = "TESTING";
String key = "A";
String encrypted = adfgvxCipher.encrypt(message, key);
String decrypted = adfgvxCipher.decrypt(encrypted, key);
assertEquals("TESTING", decrypted);
}
@Test
void testRoundTripWhenLengthIsNotMultipleOfKeyLength() {
// "HELLO" fractionates to 10 ADFGVX letters; 10 % 7 != 0, so the last row of
// the transposition grid is partial. This used to decrypt to "S35LL".
String key = "PRIVACY";
String encrypted = adfgvxCipher.encrypt("HELLO", key);
assertEquals("HELLO", adfgvxCipher.decrypt(encrypted, key));
}
@Test
void testRoundTripWithRepeatedLettersInKey() {
// indexOf-based column order maps both Bs and both As to the same column,
// which used to corrupt the message and leak "null" into the output.
String key = "BADCAB";
String encrypted = adfgvxCipher.encrypt("ATTACKATDAWN", key);
assertEquals("ATTACKATDAWN", adfgvxCipher.decrypt(encrypted, key));
}
@Test
void testDecryptRejectsInvalidCiphertext() {
// Z is not an ADFGVX letter; the old code silently appended "null".
org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> adfgvxCipher.decrypt("ZZ", "KEY"));
}
@Test
void testDecryptRejectsOddLengthCiphertext() {
// A single leftover ADFGVX letter cannot form a Polybius pair.
org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> adfgvxCipher.decrypt("A", "KEY"));
}
}