-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOffsetCipherTest.java
More file actions
123 lines (99 loc) · 4.46 KB
/
OffsetCipherTest.java
File metadata and controls
123 lines (99 loc) · 4.46 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package org.example;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class OffsetCipherTest {
private static String validKeyboardChars;
private static OffsetCipher offsetCipher;
private static String testString;
private static String hwString;
private static int testRounds;
@BeforeAll
public static void setup() {
validKeyboardChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{};:'\",.<>?/\\|";
offsetCipher = new OffsetCipher();
hwString = "HELLO WORLD";
testRounds = 99999; //TODO: You can set the number of rounds/loops for the robustness test here.
}
@BeforeEach
public void setupString() {
offsetCipher = new OffsetCipher();
//Reset the reference table every time a test is run
////Could be cheaper to just reset the table but more dangerous if there are more tests/variables are added down the road
// offsetCipher.setReferenceTable(new Character[] {
// 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
// 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
// '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
// '(', ')', '*', '+', ',', '-', '.', '/'
// });
}
@Test
public void testEncodeDecodeWithGivenLetterB() {
testString = "B" + hwString;
String encodedString = offsetCipher.encode(testString);
assertEquals("BGDKKN VNQKC", encodedString);
String decodedString = offsetCipher.decode(encodedString);
assertEquals("BHELLO WORLD", decodedString);
}
@Test
public void testEncodeDecodeWithGivenLetterF() {
testString = "F" + hwString;
String encodedString = offsetCipher.encode(testString);
assertEquals("FC/GGJ RJMG.", encodedString);
String decodedString = offsetCipher.decode(encodedString);
assertEquals("FHELLO WORLD", decodedString);
}
@Test
public void testEncodeDecodeRobustness() {
for (int i = 0; i < testRounds; i++) {
testString = generateRandomString(15, 65);
String encodedString = offsetCipher.encode(testString);
String decodedString = offsetCipher.decode(encodedString);
assertEquals(testString, decodedString);
}
}
@Test
public void testEncodeDecodeModularity() {
for (int i = 0; i < testRounds; i++) {
offsetCipher.setReferenceTable(generateRandomCharArray(validKeyboardChars));
testString = generateRandomString(15, 65);
String encodedString = offsetCipher.encode(testString);
String decodedString = offsetCipher.decode(encodedString);
assertEquals(testString, decodedString);
}
}
private String generateRandomString(int minLength, int maxLength) {
if (minLength > maxLength || minLength < 0) {
throw new IllegalArgumentException("Invalid input length range");
}
Random random = new Random();
int stringLength = minLength + random.nextInt(maxLength - minLength + 1);
StringBuilder randomString = new StringBuilder();
randomString.append(offsetCipher.getReferenceTable()[random.nextInt(offsetCipher.getReferenceTable().length)]);
int validCharsLength = validKeyboardChars.length();
for (int i = 1; i < stringLength; i++) {
int randomIndex = random.nextInt(validCharsLength);
randomString.append(validKeyboardChars.charAt(randomIndex));
}
return randomString.toString();
}
private Character[] generateRandomCharArray(String validKeyboardChars) {
Random random = new Random();
int length = random.nextInt(44) + 1; //random number between 0 (inclusive) and 44 (exclusive)
List<Character> charList = new ArrayList<>();
for (int i = 0; i < length; i++) {
char randomChar;
do {
randomChar = validKeyboardChars.charAt(random.nextInt(validKeyboardChars.length()));
} while (charList.contains(randomChar));
charList.add(randomChar);
}
Collections.shuffle(charList);
return charList.toArray(new Character[0]);
}
}