Repository navigation
Expand file tree
/
Copy pathOneTimePadTest.java
More file actions
87 lines (74 loc) · 2.53 KB
/
Copy pathOneTimePadTest.java
File metadata and controls
87 lines (74 loc) · 2.53 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
77
78
79
80
81
82
83
84
85
86
87
package servlets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class OneTimePadTest {
private static String chars(char c, int count) {
char[] arr = new char[count];
Arrays.fill(arr, c);
return new String(arr);
}
@Test
void encryptDecrypt_roundTripWithDefaultKey() {
String plaintext = "Hello, World!";
String encrypted = OneTimePad.encrypt(plaintext);
String decrypted = OneTimePad.decrypt(encrypted);
assertEquals(plaintext, decrypted);
}
@Test
void encryptDecrypt_roundTripWithCustomKey() {
String plaintext = "Secret Message";
String key = "MyCustomKey12345";
String encrypted = OneTimePad.encrypt(plaintext, key);
String decrypted = OneTimePad.decrypt(encrypted, key);
assertEquals(plaintext, decrypted);
}
@Test
void encrypt_producesBase64Output() {
String encrypted = OneTimePad.encrypt("test");
assertNotNull(encrypted);
assertNotEquals("test", encrypted);
}
@Test
void encrypt_deterministicForSameInput() {
String first = OneTimePad.encrypt("same input");
String second = OneTimePad.encrypt("same input");
assertEquals(first, second);
}
@Test
void encrypt_differentKeysProduceDifferentOutput() {
String plaintext = "Hello";
String enc1 = OneTimePad.encrypt(plaintext, "key1key1key1key1");
String enc2 = OneTimePad.encrypt(plaintext, "key2key2key2key2");
assertNotEquals(enc1, enc2);
}
@Test
void encryptDecrypt_emptyString() {
String encrypted = OneTimePad.encrypt("");
String decrypted = OneTimePad.decrypt(encrypted);
assertEquals("", decrypted);
}
@Test
void encryptDecrypt_specialCharacters() {
String plaintext = "Spëcial Chàrs: <>&\"'!@#$%^";
String encrypted = OneTimePad.encrypt(plaintext);
String decrypted = OneTimePad.decrypt(encrypted);
assertEquals(plaintext, decrypted);
}
@Test
void encryptDecrypt_longString() {
String plaintext = chars('A', 1000);
String encrypted = OneTimePad.encrypt(plaintext);
String decrypted = OneTimePad.decrypt(encrypted);
assertEquals(plaintext, decrypted);
}
@Test
void decrypt_wrongKeyProducesGarbage() {
String plaintext = "Hello";
String encrypted = OneTimePad.encrypt(plaintext, "correctKey123456");
String decrypted = OneTimePad.decrypt(encrypted, "wrongKey12345678");
assertNotEquals(plaintext, decrypted);
}
}