Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/JdkProcServer.java
41154 views
1
/*
2
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.IOException;
25
import java.nio.file.Files;
26
import java.nio.file.Path;
27
import java.nio.file.Paths;
28
import java.util.HashMap;
29
import java.util.Map;
30
31
/*
32
* A JDK server process.
33
*/
34
public class JdkProcServer extends AbstractServer {
35
36
public static final Path PORT_LOG = Paths.get("port.log");
37
38
private final Jdk jdk;
39
private final Map<String, String> props = new HashMap<>();
40
41
private Process process;
42
43
public JdkProcServer(Builder builder) throws Exception {
44
jdk = builder.getJdk();
45
46
if (builder.getSecPropsFile() != null) {
47
props.put(JdkProcUtils.PROP_SEC_PROPS_FILE,
48
builder.getSecPropsFile().toString());
49
}
50
51
if (builder.getCertTuple() != null) {
52
props.put(JdkProcUtils.PROP_TRUSTED_CERTS,
53
JdkProcUtils.certsToStr(builder.getCertTuple().trustedCerts));
54
props.put(JdkProcUtils.PROP_EE_CERTS,
55
JdkProcUtils.certsToStr(builder.getCertTuple().endEntityCerts));
56
}
57
58
if (builder.getProtocols() != null) {
59
props.put(JdkProcUtils.PROP_PROTOCOLS,
60
Utilities.join(Utilities.enumsToStrs(builder.getProtocols())));
61
}
62
63
if (builder.getCipherSuites() != null) {
64
props.put(JdkProcUtils.PROP_CIPHER_SUITES,
65
Utilities.join(Utilities.enumsToStrs(builder.getCipherSuites())));
66
}
67
68
props.put(JdkProcUtils.PROP_CLIENT_AUTH,
69
String.valueOf(builder.getClientAuth()));
70
71
if (builder.getServerNames() != null) {
72
props.put(JdkProcUtils.PROP_SERVER_NAMES,
73
Utilities.join(builder.getServerNames()));
74
}
75
76
if (builder.getAppProtocols() != null) {
77
props.put(JdkProcUtils.PROP_APP_PROTOCOLS,
78
Utilities.join(builder.getAppProtocols()));
79
}
80
81
if (builder.getNamedGroups() != null) {
82
props.put(JdkProcUtils.PROP_NAMED_GROUPS,
83
Utilities.join(Utilities.namedGroupsToStrs(
84
builder.getNamedGroups())));
85
}
86
87
props.put("test.src", Utilities.TEST_SRC);
88
if (Utilities.DEBUG) {
89
props.put("javax.net.debug", "all");
90
}
91
}
92
93
public static class Builder extends AbstractServer.Builder {
94
95
private Jdk jdk;
96
97
private Path secPropsFile;
98
99
public Jdk getJdk() {
100
return jdk;
101
}
102
103
public Builder setJdk(Jdk jdk) {
104
this.jdk = jdk;
105
return this;
106
}
107
108
public Path getSecPropsFile() {
109
return secPropsFile;
110
}
111
112
public Builder setSecPropsFile(Path secPropsFile) {
113
this.secPropsFile = secPropsFile;
114
return this;
115
}
116
117
@Override
118
public JdkProcServer build() throws Exception {
119
return new JdkProcServer(this);
120
}
121
}
122
123
@Override
124
public Product getProduct() {
125
return jdk;
126
}
127
128
@Override
129
public int getPort() throws IOException {
130
System.out.println("Waiting for port log...");
131
if (!Utilities.waitFor(server -> server.isAlive() && readPort() > 0, this)) {
132
throw new RuntimeException("Server doesn't start in time.");
133
}
134
135
return readPort();
136
}
137
138
@Override
139
public boolean isAlive() {
140
return Utilities.isAliveProcess(process);
141
}
142
143
@Override
144
public void accept() throws IOException {
145
process = JdkProcUtils.java(getProduct().getPath(), getClass(), props,
146
getLogPath());
147
try {
148
process.waitFor();
149
} catch (InterruptedException e) {
150
throw new RuntimeException("Server was interrupted!", e);
151
}
152
153
if (process.exitValue() != 0) {
154
throw new SSLTestException("Server exited abnormally!");
155
}
156
}
157
158
@Override
159
public void signalStop() {
160
if (isAlive()) {
161
Utilities.destroyProcess(process);
162
}
163
}
164
165
@Override
166
public void close() throws IOException {
167
printLog();
168
deletePort();
169
deleteLog();
170
}
171
172
private static int readPort() {
173
try {
174
return Integer.valueOf(new String(Files.readAllBytes(PORT_LOG)));
175
} catch (Exception e) {
176
return 0;
177
}
178
}
179
180
private static void deletePort() throws IOException {
181
Utilities.deleteFile(PORT_LOG);
182
}
183
184
private static void savePort(int port) throws IOException {
185
Files.write(PORT_LOG, String.valueOf(port).getBytes(Utilities.CHARSET));
186
}
187
188
public static void main(String[] args) throws Exception {
189
String trustedCertsStr = System.getProperty(JdkProcUtils.PROP_TRUSTED_CERTS);
190
String eeCertsStr = System.getProperty(JdkProcUtils.PROP_EE_CERTS);
191
192
String protocolsStr = System.getProperty(JdkProcUtils.PROP_PROTOCOLS);
193
String cipherSuitesStr = System.getProperty(JdkProcUtils.PROP_CIPHER_SUITES);
194
195
boolean clientAuth = Boolean.getBoolean(JdkProcUtils.PROP_CLIENT_AUTH);
196
String serverNamesStr = System.getProperty(JdkProcUtils.PROP_SERVER_NAMES);
197
String appProtocolsStr = System.getProperty(JdkProcUtils.PROP_APP_PROTOCOLS);
198
199
JdkServer.Builder builder = new JdkServer.Builder();
200
builder.setCertTuple(JdkProcUtils.createCertTuple(
201
trustedCertsStr, eeCertsStr));
202
if (!Utilities.isEmpty(protocolsStr)) {
203
builder.setProtocols(Utilities.strToEnums(
204
Protocol.class, protocolsStr));
205
}
206
if (!Utilities.isEmpty(cipherSuitesStr)) {
207
builder.setCipherSuites(Utilities.strToEnums(
208
CipherSuite.class, cipherSuitesStr));
209
}
210
builder.setClientAuth(clientAuth);
211
if (!Utilities.isEmpty(serverNamesStr)) {
212
builder.setServerNames(Utilities.split(serverNamesStr));
213
}
214
if (!Utilities.isEmpty(appProtocolsStr)) {
215
builder.setAppProtocols(Utilities.split(appProtocolsStr));
216
}
217
218
try (JdkServer server = builder.build()) {
219
int port = server.getPort();
220
System.out.println("port=" + port);
221
savePort(port);
222
server.accept();
223
}
224
}
225
}
226
227