|
31 | 31 | import static org.junit.Assert.assertTrue;
|
32 | 32 | import static org.junit.Assume.assumeTrue;
|
33 | 33 |
|
34 |
| -import java.io.BufferedReader; |
35 | 34 | import java.io.IOException;
|
36 |
| -import java.io.InputStreamReader; |
37 |
| -import java.io.OutputStream; |
38 |
| -import java.io.PrintWriter; |
39 | 35 | import java.lang.management.ClassLoadingMXBean;
|
40 | 36 | import java.lang.management.GarbageCollectorMXBean;
|
41 | 37 | import java.lang.management.ManagementFactory;
|
|
49 | 45 | import java.nio.file.Files;
|
50 | 46 | import java.nio.file.Path;
|
51 | 47 | import java.nio.file.attribute.PosixFilePermission;
|
52 |
| -import java.util.ArrayList; |
53 | 48 | import java.util.HashMap;
|
54 | 49 | import java.util.List;
|
55 | 50 | import java.util.Map;
|
56 | 51 | import java.util.Set;
|
57 | 52 | import java.util.concurrent.TimeUnit;
|
58 |
| -import java.util.stream.Collectors; |
59 | 53 |
|
60 | 54 | import javax.management.MBeanServer;
|
61 | 55 | import javax.management.MBeanServerConnection;
|
|
73 | 67 |
|
74 | 68 | import com.oracle.svm.core.VMInspectionOptions;
|
75 | 69 | import com.oracle.svm.core.jdk.management.ManagementAgentStartupHook;
|
| 70 | +import com.oracle.svm.hosted.c.util.FileUtils; |
76 | 71 | import com.oracle.svm.test.AddExports;
|
77 | 72 |
|
78 | 73 | import jdk.management.jfr.FlightRecorderMXBean;
|
@@ -148,63 +143,44 @@ public static void setup() throws IOException {
|
148 | 143 | }
|
149 | 144 |
|
150 | 145 | private static void createClientKey(Path tempDirectory) throws IOException {
|
151 |
| - final List<String> commandParameters = new ArrayList<>(List.of("keytool", "-genkey")); |
152 |
| - commandParameters.addAll(List.of("-keystore", "clientkeystore")); |
153 |
| - commandParameters.addAll(List.of("-alias", "clientkey")); |
154 |
| - commandParameters.addAll(List.of("-storepass", "clientpass")); |
155 |
| - commandParameters.addAll(List.of("-keypass", "clientpass")); |
156 |
| - commandParameters.addAll(List.of("-dname", "CN=test, OU=test, O=test, L=test, ST=test, C=test, EMAILADDRESS=test")); |
157 |
| - commandParameters.addAll(List.of("-validity", "99999")); |
158 |
| - commandParameters.addAll(List.of("-keyalg", "rsa")); |
159 |
| - |
160 |
| - ProcessBuilder pb = new ProcessBuilder().command(commandParameters); |
161 |
| - pb.directory(tempDirectory.toFile()); |
162 |
| - final Process process = pb.start(); |
163 |
| - waitForProcess(process, commandParameters); |
| 146 | + runCommand(tempDirectory, List.of("keytool", "-genkey", |
| 147 | + "-keystore", "clientkeystore", |
| 148 | + "-alias", "clientkey", |
| 149 | + "-storepass", "clientpass", |
| 150 | + "-keypass", "clientpass", |
| 151 | + "-dname", "CN=test, OU=test, O=test, L=test, ST=test, C=test, EMAILADDRESS=test", |
| 152 | + "-validity", "99999", |
| 153 | + "-keyalg", "rsa")); |
164 | 154 | }
|
165 | 155 |
|
166 | 156 | private static void createClientCert(Path tempDirectory) throws IOException {
|
167 |
| - final List<String> commandParameters = new ArrayList<>(List.of("keytool", "-exportcert")); |
168 |
| - commandParameters.addAll(List.of("-keystore", "clientkeystore")); |
169 |
| - commandParameters.addAll(List.of("-alias", "clientkey")); |
170 |
| - commandParameters.addAll(List.of("-storepass", "clientpass")); |
171 |
| - commandParameters.addAll(List.of("-file", "client.cer")); |
172 |
| - |
173 |
| - ProcessBuilder pb = new ProcessBuilder().command(commandParameters); |
174 |
| - pb.directory(tempDirectory.toFile()); |
175 |
| - final Process process = pb.start(); |
176 |
| - waitForProcess(process, commandParameters); |
| 157 | + runCommand(tempDirectory, List.of("keytool", "-exportcert", |
| 158 | + "-keystore", "clientkeystore", |
| 159 | + "-alias", "clientkey", |
| 160 | + "-storepass", "clientpass", |
| 161 | + "-file", "client.cer")); |
177 | 162 | }
|
178 | 163 |
|
179 | 164 | private static void createServerTrustStore(Path tempDirectory) throws IOException {
|
180 |
| - final List<String> commandParameters = new ArrayList<>(List.of("keytool", "-importcert")); |
181 |
| - commandParameters.addAll(List.of("-file", "client.cer")); |
182 |
| - commandParameters.addAll(List.of("-keystore", "servertruststore")); |
183 |
| - commandParameters.addAll(List.of("-storepass", "servertrustpass")); |
| 165 | + runCommand(tempDirectory, List.of("keytool", "-importcert", |
| 166 | + "-noprompt", |
| 167 | + "-file", "client.cer", |
| 168 | + "-keystore", "servertruststore", |
| 169 | + "-storepass", "servertrustpass")); |
| 170 | + } |
184 | 171 |
|
185 |
| - ProcessBuilder pb = new ProcessBuilder().command(commandParameters); |
| 172 | + private static void runCommand(Path tempDirectory, List<String> command) throws IOException { |
| 173 | + ProcessBuilder pb = new ProcessBuilder().command(command); |
186 | 174 | pb.directory(tempDirectory.toFile());
|
187 | 175 | final Process process = pb.start();
|
188 |
| - // Prompted about whether the cert should be trusted. |
189 |
| - OutputStream os = process.getOutputStream(); |
190 |
| - PrintWriter writer = new PrintWriter(os); |
191 |
| - writer.write("y\n"); |
192 |
| - writer.flush(); |
193 |
| - waitForProcess(process, commandParameters); |
194 |
| - } |
195 |
| - |
196 |
| - private static void waitForProcess(Process process, List<String> command) throws IOException { |
197 | 176 | try {
|
198 | 177 | process.waitFor(5, TimeUnit.SECONDS);
|
199 | 178 | } catch (InterruptedException e) {
|
200 | 179 | throw new IOException("Keytool execution error");
|
201 | 180 | }
|
202 |
| - |
203 | 181 | if (process.exitValue() > 0) {
|
204 |
| - final String processError = (new BufferedReader(new InputStreamReader(process.getErrorStream()))).lines() |
205 |
| - .collect(Collectors.joining(" \\ ")); |
206 |
| - final String processOutput = (new BufferedReader(new InputStreamReader(process.getInputStream()))).lines() |
207 |
| - .collect(Collectors.joining(" \\ ")); |
| 182 | + final String processError = String.join(" \\ ", FileUtils.readAllLines(process.getErrorStream())); |
| 183 | + final String processOutput = String.join(" \\ ", FileUtils.readAllLines(process.getInputStream())); |
208 | 184 | throw new IOException(
|
209 | 185 | "Keytool execution error: " + processError + ", output: " + processOutput + ", command: " + command);
|
210 | 186 | }
|
|
0 commit comments