Debian Codegraph @

src/test/java/com/carrotsearch/procfork/TestAllPlatforms.java

raw symbols SCIP
1 /*
2 * procfork
3 *
4 * Copyright (C) 2019, Dawid Weiss.
5 * All rights reserved.
6 */
7 package com.carrotsearch.procfork;
8
9 import com.carrotsearch.randomizedtesting.LifecycleScope;
10 import com.carrotsearch.randomizedtesting.MixWithSuiteName;
11 import com.carrotsearch.randomizedtesting.RandomizedTest;
12 import com.carrotsearch.randomizedtesting.annotations.*;
13 import com.google.common.base.Stopwatch;
14 import java.io.BufferedReader;
15 import java.io.InputStreamReader;
16 import java.nio.charset.Charset;
17 import java.nio.charset.StandardCharsets;
18 import java.nio.file.Files;
19 import java.nio.file.Path;
20 import java.nio.file.Paths;
21 import java.nio.file.attribute.PosixFilePermissions;
22 import java.util.ArrayList;
23 import org.assertj.core.api.Assertions;
24 import org.junit.Test;
25
26 @TimeoutSuite(millis = 60 * 1000)
27 @ThreadLeakGroup(ThreadLeakGroup.Group.MAIN)
28 @ThreadLeakScope(ThreadLeakScope.Scope.TEST)
29 @ThreadLeakZombies(ThreadLeakZombies.Consequence.IGNORE_REMAINING_TESTS)
30 @ThreadLeakLingering(linger = 1000)
31 @ThreadLeakAction({ThreadLeakAction.Action.WARN, ThreadLeakAction.Action.INTERRUPT})
32 @SeedDecorators({MixWithSuiteName.class})
33 @ThreadLeakFilters(filters = CustomThreadFilters.class)
34 public class TestAllPlatforms extends RandomizedTest {
35 @Test
36 public void testSpaceInPathAndArgument() throws Exception {
37 Path cwdDir = newTempDir(LifecycleScope.TEST).resolve("space (inside)");
38 Files.createDirectories(cwdDir);
39 Path cmdDir = newTempDir(LifecycleScope.TEST).resolve("New Folder (2)");
40 Files.createDirectories(cmdDir);
41
42 String expectedOutput;
43 Path command;
44 if (LocalEnvironment.IS_OS_WINDOWS) {
45 command = cmdDir.resolve("script.cmd");
46 Files.write(command, "@echo 1=%1 2=%2".getBytes(StandardCharsets.UTF_8));
47 // Quotes added by cmd or echo - they're passed to subprocess properly.
48 expectedOutput = "1=\"one and two\" 2=three";
49 } else {
50 command = cmdDir.resolve("script.sh");
51 Files.write(command, "echo 1=$1 2=$2".getBytes(StandardCharsets.UTF_8));
52 Files.setPosixFilePermissions(command, PosixFilePermissions.fromString("rwxr-x---"));
53 expectedOutput = "1=one and two 2=three";
54 }
55
56 Path output;
57 try (ForkedProcess cmd =
58 new ProcessBuilderLauncher()
59 .cwd(cwdDir)
60 .executable(command.toAbsolutePath())
61 .viaShellLauncher()
62 .args("one and two", "three")
63 .execute()) {
64
65 int exitStatus = cmd.waitFor();
66 output = cmd.getProcessOutputFile();
67 String out =
68 new String(Files.readAllBytes(cmd.getProcessOutputFile()), Charset.defaultCharset());
69 Assertions.assertThat(out).isEqualToIgnoringWhitespace(expectedOutput);
70 Assertions.assertThat(exitStatus).isEqualTo(0);
71 }
72
73 Assertions.assertThat(output).doesNotExist();
74 }
75
76 @Test
77 public void testSpaceInPathAndNoArguments() throws Exception {
78 Path cwdDir = newTempDir(LifecycleScope.TEST).resolve("space (inside)");
79 Files.createDirectories(cwdDir);
80 Path cmdDir = newTempDir(LifecycleScope.TEST).resolve("New Folder (2)");
81 Files.createDirectories(cmdDir);
82
83 String expectedOutput;
84 Path command;
85 if (LocalEnvironment.IS_OS_WINDOWS) {
86 command = cmdDir.resolve("script.cmd");
87 Files.write(command, "@echo args: %1".getBytes(StandardCharsets.UTF_8));
88 expectedOutput = "args: ";
89 } else {
90 command = cmdDir.resolve("script.sh");
91 Files.write(command, "echo args: $1".getBytes(StandardCharsets.UTF_8));
92 Files.setPosixFilePermissions(command, PosixFilePermissions.fromString("rwxr-x---"));
93 expectedOutput = "args: ";
94 }
95
96 Path output;
97 try (ForkedProcess cmd =
98 new ProcessBuilderLauncher()
99 .cwd(cwdDir)
100 .executable(command.toAbsolutePath())
101 .viaShellLauncher()
102 .execute()) {
103
104 int exitStatus = cmd.waitFor();
105 output = cmd.getProcessOutputFile();
106 String out =
107 new String(Files.readAllBytes(cmd.getProcessOutputFile()), Charset.defaultCharset());
108 Assertions.assertThat(out).isEqualToIgnoringWhitespace(expectedOutput);
109 Assertions.assertThat(exitStatus).isEqualTo(0);
110 }
111
112 Assertions.assertThat(output).doesNotExist();
113 }
114
115 @Test
116 public void testEcho() throws Exception {
117 Path output;
118 try (ForkedProcess cmd =
119 new ProcessBuilderLauncher()
120 .cwd(Paths.get("."))
121 .executable(Paths.get("echo"))
122 .args("foo", "bar")
123 .viaShellLauncher()
124 .execute()) {
125
126 Assertions.assertThat(cmd.waitFor()).isEqualTo(0);
127 output = cmd.getProcessOutputFile();
128
129 String out =
130 new String(Files.readAllBytes(cmd.getProcessOutputFile()), Charset.defaultCharset());
131 Assertions.assertThat(out).isEqualToIgnoringNewLines("foo bar");
132 }
133
134 Assertions.assertThat(output).doesNotExist();
135 }
136
137 @Test
138 public void testTermination() throws Exception {
139 Path output;
140 try (ForkedProcess cmd =
141 new ProcessBuilderLauncher()
142 .cwd(Paths.get("."))
143 .executable(Paths.get("ping"))
144 .args(pingArgs(5))
145 .viaShellLauncher()
146 .execute()) {
147 Thread.sleep(1000);
148 cmd.destroyForcibly();
149 output = cmd.getProcessOutputFile();
150 }
151
152 Assertions.assertThat(output).doesNotExist();
153 }
154
155 private String[] pingArgs(int secs) {
156 if (LocalEnvironment.IS_OS_WINDOWS) {
157 return new String[] {"localhost", "-n", Integer.toString(secs)};
158 } else {
159 return new String[] {"localhost", "-c", Integer.toString(secs)};
160 }
161 }
162
163 @Test
164 public void testOutputStreaming() throws Exception {
165 try (ForkedProcess cmd =
166 new ProcessBuilderLauncher()
167 .cwd(Paths.get("."))
168 .executable(Paths.get("ping"))
169 .args(pingArgs(5))
170 .viaShellLauncher()
171 .execute()) {
172 Stopwatch sw = Stopwatch.createStarted();
173 ArrayList<String> lines = new ArrayList<>();
174 try (BufferedReader reader =
175 new BufferedReader(new InputStreamReader(cmd.getProcessOutputAsStream()))) {
176 String line;
177 while ((line = reader.readLine()) != null && sw.elapsed().getSeconds() < 1) {
178 lines.add(line);
179 }
180 }
181
182 Assertions.assertThat(lines).isNotEmpty();
183 cmd.destroyForcibly();
184 }
185 }
186 }