Skip to content

Commit c327bc5

Browse files
committed
test: exercise OpenCLI process privacy across platforms
1 parent 5ba97e5 commit c327bc5

3 files changed

Lines changed: 64 additions & 16 deletions

File tree

‎.github/workflows/contracts.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ jobs:
173173
mvn -version > platform-evidence/maven.txt 2>&1
174174
result=0
175175
mvn -B --no-transfer-progress \
176-
-Dtest=OpenCliArgvContractTest,OpenCliStructuredArgvContractTest,OpenCliProcessContractTest,OpenCliProcessBoundaryTest,OpenCliWsReverseAgentSessionTest,OpenCliWsCollectRequestsTest \
176+
-Dtest=OpenCliArgvContractTest,OpenCliStructuredArgvContractTest,OpenCliProcessContractTest,OpenCliProcessBoundaryTest,OpenCliWsReverseAgentSessionTest,OpenCliWsCollectRequestsTest,OpenCliProcessPrivacyTest \
177177
test > platform-evidence/maven.log 2>&1 || result=$?
178178
tail -100 platform-evidence/maven.log
179179
python scripts/contract_report.py \
@@ -189,6 +189,7 @@ jobs:
189189
--suite process=io.github.easy4j.opencli.contract.OpenCliProcessBoundaryTest \
190190
--suite protocol=io.github.easy4j.opencli.center.ws.OpenCliWsReverseAgentSessionTest \
191191
--suite protocol=io.github.easy4j.opencli.center.ws.OpenCliWsCollectRequestsTest \
192+
--suite process=io.github.easy4j.opencli.core.OpenCliProcessPrivacyTest \
192193
--output platform-evidence/report.json
193194
exit "$result"
194195
- name: Verify npm-installed OpenCLI on Windows

‎src/test/java/io/github/easy4j/opencli/contract/LifecycleProbe.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ private LifecycleProbe() { }
1313

1414
public static void main(String[] args) throws Exception {
1515
String mode = args[0];
16+
if ("privacy".equals(mode)) {
17+
exercisePrivacy(args[1], args[2]);
18+
return;
19+
}
1620
if ("network-follow".equals(mode)) {
1721
networkFollow(args);
1822
return;
@@ -52,6 +56,26 @@ public static void main(String[] args) throws Exception {
5256
}
5357
}
5458

59+
private static void exercisePrivacy(String outcome, String canary) throws Exception {
60+
switch (outcome) {
61+
case "nonzero":
62+
System.err.print(canary);
63+
System.exit(7);
64+
return;
65+
case "timeout":
66+
System.err.print(canary);
67+
System.err.flush();
68+
Thread.sleep(8000L);
69+
return;
70+
case "overflow":
71+
System.out.print(canary);
72+
main(new String[] {"stdout", "4096"});
73+
return;
74+
default:
75+
throw new IllegalArgumentException("unknown privacy fixture outcome");
76+
}
77+
}
78+
5579
private static void networkFollow(String[] args) throws Exception {
5680
if (!Arrays.asList("browser", "fixture", "network", "--follow")
5781
.equals(Arrays.asList(args).subList(3, args.length))) {

‎src/test/java/io/github/easy4j/opencli/core/OpenCliProcessPrivacyTest.java‎

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package io.github.easy4j.opencli.core;
22

33
import io.github.easy4j.opencli.OpenCliProperties;
4+
import io.github.easy4j.opencli.contract.LifecycleProbe;
45
import io.github.easy4j.opencli.exception.OpenCliException;
56
import io.github.easy4j.opencli.exception.OpenCliNonZeroExitException;
67
import io.github.easy4j.opencli.exception.OpenCliTimeoutException;
78
import java.io.ByteArrayOutputStream;
9+
import java.io.File;
810
import java.io.PrintStream;
911
import java.nio.charset.StandardCharsets;
1012
import java.nio.file.Path;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
1115
import java.util.UUID;
1216
import org.junit.jupiter.api.Test;
13-
import org.junit.jupiter.api.condition.EnabledOnOs;
14-
import org.junit.jupiter.api.condition.OS;
1517
import org.junit.jupiter.api.function.Executable;
1618
import org.junit.jupiter.api.io.TempDir;
1719

1820
import static org.junit.jupiter.api.Assertions.assertEquals;
1921
import static org.junit.jupiter.api.Assertions.assertFalse;
2022
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
2124

2225
class OpenCliProcessPrivacyTest {
2326

@@ -33,34 +36,54 @@ void keepsMissingExecutablePathOutOfLogsAndStackTrace(@TempDir Path directory) t
3336
}
3437

3538
@Test
36-
@EnabledOnOs({OS.LINUX, OS.MAC})
3739
void preservesStderrOnNonzeroExitWithoutLeakingArgsOrStderr() throws Exception {
3840
String canary = "stderr-" + UUID.randomUUID();
39-
OpenCliProperties properties = new OpenCliProperties();
40-
properties.setExecutable("/bin/sh");
41-
42-
try (OpenCliExecutor executor = new OpenCliExecutor(properties)) {
41+
try (OpenCliExecutor executor = new OpenCliExecutor(childProperties())) {
4342
OpenCliNonZeroExitException failure = assertPrivateFailure(OpenCliNonZeroExitException.class, canary,
44-
() -> executor.invoke("-c", "printf '%s' \"$1\" >&2; exit 7", "fixture", canary));
43+
() -> executor.invoke("privacy", "nonzero", canary));
4544
assertEquals(canary, failure.getPartialResult().getStderr());
4645
assertEquals(Integer.valueOf(7), failure.getPartialResult().getExitCode());
4746
}
4847
}
4948

5049
@Test
51-
@EnabledOnOs({OS.LINUX, OS.MAC})
52-
void keepsArgumentsOutOfTimeoutDiagnostics() throws Exception {
50+
void keepsArgumentsAndPartialStderrOutOfTimeoutDiagnostics() throws Exception {
5351
String canary = "timeout-" + UUID.randomUUID();
54-
OpenCliProperties properties = new OpenCliProperties();
55-
properties.setExecutable("/bin/sh");
56-
properties.setCommandTimeoutMillis(200);
52+
OpenCliProperties properties = childProperties();
53+
properties.setCommandTimeoutMillis(2500);
54+
55+
try (OpenCliExecutor executor = new OpenCliExecutor(properties)) {
56+
OpenCliTimeoutException failure = assertPrivateFailure(OpenCliTimeoutException.class, canary,
57+
() -> executor.invoke("privacy", "timeout", canary));
58+
assertEquals(canary, failure.getPartialResult().getStderr());
59+
}
60+
}
61+
62+
@Test
63+
void keepsPartialStdoutOutOfOutputLimitDiagnostics() throws Exception {
64+
String canary = "overflow-" + UUID.randomUUID();
65+
OpenCliProperties properties = childProperties();
66+
properties.setMaxStdoutBytes(128);
5767

5868
try (OpenCliExecutor executor = new OpenCliExecutor(properties)) {
59-
assertPrivateFailure(OpenCliTimeoutException.class, canary,
60-
() -> executor.invoke("-c", "exec sleep 5", "fixture", canary));
69+
OpenCliException failure = assertPrivateFailure(OpenCliException.class, canary,
70+
() -> executor.invoke("privacy", "overflow", canary));
71+
assertEquals("OUTPUT_LIMIT", failure.getPartialResult().getExecutionDetails().getTerminationReason().name());
72+
assertEquals(128, failure.getPartialResult().getStdout().length());
73+
assertTrue(failure.getPartialResult().getStdout().startsWith(canary));
6174
}
6275
}
6376

77+
private static OpenCliProperties childProperties() {
78+
OpenCliProperties properties = new OpenCliProperties();
79+
String executable = System.getProperty("os.name").startsWith("Windows") ? "java.exe" : "java";
80+
properties.setExecutable(new File(new File(System.getProperty("java.home"), "bin"), executable).getAbsolutePath());
81+
properties.setLeadingArguments(new ArrayList<>(Arrays.asList("-cp",
82+
System.getProperty("surefire.test.class.path", System.getProperty("java.class.path")),
83+
LifecycleProbe.class.getName())));
84+
return properties;
85+
}
86+
6487
private static <T extends OpenCliException> T assertPrivateFailure(
6588
Class<T> type, String canary, Executable invocation) throws Exception {
6689
ByteArrayOutputStream output = new ByteArrayOutputStream();

0 commit comments

Comments
 (0)