Repository navigation
Expand file tree
/
Copy pathControlApplicationTest.java
More file actions
134 lines (117 loc) · 6.59 KB
/
Copy pathControlApplicationTest.java
File metadata and controls
134 lines (117 loc) · 6.59 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
124
125
126
127
128
129
130
131
132
133
134
package com.bencodez.votingplugin.control;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.junit.jupiter.api.Assertions.*;
class ControlApplicationTest {
@TempDir Path directory;
@Test void identityIsStableAndConcurrentFirstStartupPublishesOneWinner() throws Exception {
int count = 24;
CountDownLatch ready = new CountDownLatch(count);
CountDownLatch start = new CountDownLatch(1);
CountDownLatch done = new CountDownLatch(count);
List<java.util.UUID> results = java.util.Collections.synchronizedList(new ArrayList<>());
var executor = Executors.newFixedThreadPool(count);
try {
for (int i = 0; i < count; i++) {
executor.submit(() -> {
ready.countDown();
try {
start.await();
results.add(ControlApplication.loadIdentity(directory));
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
done.countDown();
}
});
}
ready.await();
start.countDown();
done.await();
} finally {
executor.shutdownNow();
}
Set<java.util.UUID> unique = new HashSet<>(results);
assertEquals(1, unique.size());
assertEquals(unique.iterator().next(), ControlApplication.loadIdentity(directory));
}
@Test void corruptIdentityFailsClosed() throws Exception {
Files.writeString(directory.resolve("instance-id"), "not-a-uuid");
assertThrows(java.io.IOException.class, () -> ControlApplication.loadIdentity(directory));
}
@Test void oversizedAndNonRegularIdentitiesFailClosedBeforeReading() throws Exception {
Path oversized = directory.resolve("oversized");
Files.createDirectories(oversized);
Files.writeString(oversized.resolve("instance-id"), "x".repeat(65));
assertThrows(java.io.IOException.class, () -> ControlApplication.loadIdentity(oversized));
Path nonRegular = directory.resolve("non-regular");
Files.createDirectories(nonRegular.resolve("instance-id"));
assertThrows(java.io.IOException.class, () -> ControlApplication.loadIdentity(nonRegular));
}
@Test void directoryDurabilityRecognizesWindowsAsUnsupported() {
assertFalse(DurableFiles.supportsDirectoryChannels("Windows Server 2025"));
assertTrue(DurableFiles.supportsDirectoryChannels("Linux"));
}
@Test void explicitAdminDirectoryDoesNotTouchConfiguredDefault() throws Exception {
Path configuredDefault = directory.resolve("unwritable-default");
Path explicit = directory.resolve("explicit");
ByteArrayOutputStream output = new ByteArrayOutputStream();
ControlApplication.runOwnerCommand(new String[]{"admin-token", explicit.toString()},
java.util.Map.of("CONTROL_DATA_DIR", configuredDefault.toString()), new PrintStream(output));
assertFalse(Files.exists(configuredDefault));
assertTrue(Files.exists(explicit.resolve("credentials.json")));
assertTrue(output.toString().trim().startsWith("vpctl_admin_"));
}
@Test void verifierEnrollmentCommandInstallsOnlyTheCallerGeneratedHash() throws Exception {
String credential = "vpctl_node_backend_generated_credential";
String verifier = java.util.HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256")
.digest(credential.getBytes(java.nio.charset.StandardCharsets.UTF_8)));
ByteArrayOutputStream output = new ByteArrayOutputStream();
ControlApplication.runOwnerCommand(
new String[]{"enroll-verifier", "backend-lobby", verifier, directory.toString()},
java.util.Map.of(), new PrintStream(output));
assertTrue(new com.bencodez.votingplugin.control.auth.CredentialStore(directory)
.verifyNode("backend-lobby", credential));
assertEquals("Enrollment verifier installed.", output.toString().trim());
assertFalse(Files.readString(directory.resolve("credentials.json")).contains(credential));
}
@Test void webPasswordCommandReadsSecretOutOfBand() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
char[] supplied = "command-password-value".toCharArray();
ControlApplication.runOwnerCommand(new String[]{"web-password", directory.toString()},
java.util.Map.of(), new PrintStream(output), () -> supplied);
assertTrue(new com.bencodez.votingplugin.control.auth.CredentialStore(directory)
.verifyWebPassword("command-password-value"));
assertTrue(output.toString().contains("WebUI password updated"));
assertTrue(java.util.Arrays.equals(new char[supplied.length], supplied));
}
@Test void startupConfigurationValidatesAllBounds() throws Exception {
assertThrows(IllegalArgumentException.class,
() -> ControlApplication.Configuration.from(java.util.Map.of("CONTROL_PORT", "70000")));
assertThrows(IllegalArgumentException.class,
() -> ControlApplication.Configuration.from(java.util.Map.of("CONTROL_OFFLINE_TIMEOUT_SECONDS", "0")));
assertThrows(IllegalArgumentException.class,
() -> ControlApplication.Configuration.from(java.util.Map.of("CONTROL_REQUEST_TIMEOUT_SECONDS", "61")));
assertThrows(IllegalArgumentException.class,
() -> ControlApplication.Configuration.from(java.util.Map.of(
"CONTROL_TRUSTED_PROXY_ADDRESSES", "proxy.local")));
assertThrows(IllegalArgumentException.class,
() -> ControlApplication.Configuration.from(java.util.Map.of("CONTROL_LAUNCH_ID", "not-a-uuid")));
assertThrows(IllegalArgumentException.class,
() -> ControlApplication.Configuration.from(java.util.Map.of("CONTROL_PARENT_PID", "0")));
assertEquals(123L, ControlApplication.Configuration.from(
java.util.Map.of("CONTROL_PARENT_PID", "123")).parentPid());
assertEquals(Set.of("127.0.0.1"), ControlApplication.Configuration.from(java.util.Map.of(
"CONTROL_TRUSTED_PROXY_ADDRESSES", "127.0.0.1")).trustedProxyAddresses());
}
}