Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ServerSocket/AcceptInheritHandle.java
38812 views
1
/*
2
* Copyright (c) 2015, 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
/*
25
* @test
26
* @bug 8067105
27
* @summary Socket returned by ServerSocket.accept() is inherited by child process on Windows
28
* @author Chris Hegarty
29
*/
30
31
import java.io.*;
32
import java.net.*;
33
import java.nio.channels.ServerSocketChannel;
34
import java.util.ArrayList;
35
import java.util.List;
36
import java.util.concurrent.TimeUnit;
37
import java.util.function.Supplier;
38
39
public class AcceptInheritHandle {
40
41
enum ServerSocketProducer {
42
JAVA_NET(() -> {
43
try {
44
return new ServerSocket(); }
45
catch(IOException x) {
46
throw new UncheckedIOException(x);
47
}
48
}),
49
NIO_CHANNELS(() -> {
50
try {
51
return ServerSocketChannel.open().socket();
52
} catch (IOException x) {
53
throw new UncheckedIOException(x);
54
}
55
});
56
57
final Supplier<ServerSocket> supplier;
58
ServerSocketProducer(Supplier<ServerSocket> supplier) {
59
this.supplier = supplier;
60
}
61
Supplier<ServerSocket> supplier () { return supplier; }
62
}
63
64
static final String JAVA = System.getProperty("java.home")
65
+ File.separator + "bin" + File.separator + "java";
66
67
static final String CLASSPATH = System.getProperty("java.class.path");
68
69
public static void main(String[] args) throws Exception {
70
if (args.length == 1)
71
server(ServerSocketProducer.valueOf(args[0]));
72
else
73
mainEntry();
74
}
75
76
static void mainEntry() throws Exception {
77
testJavaNetServerSocket();
78
testNioServerSocketChannel();
79
}
80
81
static void testJavaNetServerSocket() throws Exception {
82
test(ServerSocketProducer.JAVA_NET);
83
test(ServerSocketProducer.JAVA_NET, "-Djava.net.preferIPv4Stack=true");
84
}
85
static void testNioServerSocketChannel() throws Exception {
86
test(ServerSocketProducer.NIO_CHANNELS);
87
}
88
89
static void test(ServerSocketProducer ssp, String... sysProps) throws Exception {
90
System.out.println("\nStarting test for " + ssp.name());
91
92
List<String> commands = new ArrayList<>();
93
commands.add(JAVA);
94
for (String prop : sysProps)
95
commands.add(prop);
96
commands.add("-cp");
97
commands.add(CLASSPATH);
98
commands.add("AcceptInheritHandle");
99
commands.add(ssp.name());
100
101
System.out.println("Executing: "+ commands);
102
ProcessBuilder pb = new ProcessBuilder(commands);
103
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
104
Process serverProcess = pb.start();
105
DataInputStream dis = new DataInputStream(serverProcess.getInputStream());
106
107
int port = dis.readInt();
108
System.out.println("Server process listening on " + port + ", connecting...");
109
110
Socket socket = new Socket("localhost", port);
111
String s = dis.readUTF();
112
System.out.println("Server process said " + s);
113
114
serverProcess.destroy();
115
serverProcess.waitFor(30, TimeUnit.SECONDS);
116
System.out.println("serverProcess exitCode:" + serverProcess.exitValue());
117
118
try {
119
socket.setSoTimeout(10 * 1000);
120
socket.getInputStream().read();
121
} catch (SocketTimeoutException x) {
122
// failed
123
throw new RuntimeException("Failed: should get reset, not " + x);
124
} catch (SocketException x) {
125
System.out.println("Expected:" + x);
126
}
127
}
128
129
static void server(ServerSocketProducer producer) throws Exception {
130
try (ServerSocket ss = producer.supplier().get()) {
131
ss.bind(new InetSocketAddress(0));
132
int port = ss.getLocalPort();
133
DataOutputStream dos = new DataOutputStream(System.out);
134
dos.writeInt(port);
135
dos.flush();
136
137
ss.accept(); // do not close
138
139
Runtime.getRuntime().exec("sleep 20");
140
Thread.sleep(3 * 1000);
141
142
dos.writeUTF("kill me!");
143
dos.flush();
144
Thread.sleep(30 * 1000);
145
}
146
}
147
}
148
149