|
3 | 3 | import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason; |
4 | 4 | import io.github.easy4j.opencli.contract.LifecycleProbe; |
5 | 5 | import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.concurrent.atomic.AtomicInteger; |
6 | 8 | import java.io.PrintWriter; |
7 | 9 | import java.io.StringWriter; |
8 | 10 | import java.nio.charset.StandardCharsets; |
|
12 | 14 | import static org.junit.jupiter.api.Assertions.*; |
13 | 15 | import org.junit.jupiter.api.Test; |
14 | 16 |
|
| 17 | +@org.junit.jupiter.api.parallel.Isolated |
15 | 18 | class SubprocessExecutionSupportTest { |
16 | 19 |
|
17 | 20 | @Test |
@@ -85,6 +88,42 @@ void shouldKeepNativePathOutOfDefaultHandlerException(@TempDir Path temporary) t |
85 | 88 | assertFalse(diagnostics.toString().contains(canary)); |
86 | 89 | } |
87 | 90 |
|
| 91 | + @Test |
| 92 | + void rejectsQuotedWindowsArgumentsBeforeNativeSpawnInStrictMode() throws Exception { |
| 93 | + String previousOs = System.getProperty("os.name"); |
| 94 | + String property = "jdk.lang.Process.allowAmbiguousCommands"; |
| 95 | + String previousMode = System.getProperty(property); |
| 96 | + AtomicInteger launches = new AtomicInteger(); |
| 97 | + SubprocessExecutionSupport.Runtime runtime = new SubprocessExecutionSupport.Runtime(1, builder -> { |
| 98 | + launches.incrementAndGet(); |
| 99 | + throw new IOException("fixture must not launch"); |
| 100 | + }, timeout -> new org.apache.commons.exec.ExecuteWatchdog(timeout)); |
| 101 | + CommandLine command = new CommandLine("fixture.exe") { |
| 102 | + @Override |
| 103 | + public String[] toStrings() { |
| 104 | + return new String[]{"fixture.exe", "\"private-quoted-value\""}; |
| 105 | + } |
| 106 | + }; |
| 107 | + SubprocessExecutionSupport.ExecutionRequest request = |
| 108 | + new SubprocessExecutionSupport.ExecutionRequest(command, null, null, 1_000L); |
| 109 | + try { |
| 110 | + System.setProperty("os.name", "Windows"); |
| 111 | + System.setProperty(property, "false"); |
| 112 | + UnsupportedOperationException failure = assertThrows( |
| 113 | + UnsupportedOperationException.class, () -> runtime.execute(request)); |
| 114 | + assertTrue(failure.getMessage().contains("argv[1]")); |
| 115 | + assertFalse(failure.getMessage().contains("private-quoted-value")); |
| 116 | + assertEquals(0, launches.get()); |
| 117 | + } finally { |
| 118 | + System.setProperty("os.name", previousOs); |
| 119 | + if (previousMode == null) { |
| 120 | + System.clearProperty(property); |
| 121 | + } else { |
| 122 | + System.setProperty(property, previousMode); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
88 | 127 | @Test |
89 | 128 | void shouldReturnDefaultMaxConcurrent() { |
90 | 129 | int defaultVal = SubprocessExecutionSupport.defaultMaxConcurrentExecutions(); |
|
0 commit comments