Path: blob/master/test/jdk/java/net/ServerSocket/AcceptInheritHandle.java
41152 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 806710526* @library /test/lib27* @summary Socket returned by ServerSocket.accept() is inherited by child process on Windows28* @author Chris Hegarty29*/3031import java.io.*;32import java.net.*;33import java.nio.channels.ServerSocketChannel;34import java.util.ArrayList;35import java.util.Arrays;36import java.util.List;37import java.util.concurrent.TimeUnit;38import java.util.function.Supplier;39import jdk.test.lib.net.IPSupport;4041public class AcceptInheritHandle {4243enum ServerSocketProducer {44JAVA_NET(() -> {45try {46return new ServerSocket();47} catch(IOException x) {48throw new UncheckedIOException(x);49}50}),51NIO_CHANNELS(() -> {52try {53return ServerSocketChannel.open().socket();54} catch (IOException x) {55throw new UncheckedIOException(x);56}57});5859final Supplier<ServerSocket> supplier;60ServerSocketProducer(Supplier<ServerSocket> supplier) {61this.supplier = supplier;62}63Supplier<ServerSocket> supplier () { return supplier; }64}6566static final String JAVA = System.getProperty("java.home")67+ File.separator + "bin" + File.separator + "java";6869static final String CLASSPATH = System.getProperty("java.class.path");7071public static void main(String[] args) throws Exception {72if (args.length == 1)73server(ServerSocketProducer.valueOf(args[0]));74else75mainEntry();76}7778static void mainEntry() throws Exception {79testJavaNetServerSocket();80testNioServerSocketChannel();81}8283static void testJavaNetServerSocket() throws Exception {84test(ServerSocketProducer.JAVA_NET);85if (IPSupport.hasIPv4()) {86test(ServerSocketProducer.JAVA_NET, "-Djava.net.preferIPv4Stack=true");87}88}89static void testNioServerSocketChannel() throws Exception {90test(ServerSocketProducer.NIO_CHANNELS);91}9293static void test(ServerSocketProducer ssp, String... jvmArgs) throws Exception {94System.out.println("\nStarting test for " + ssp.name());9596List<String> commands = new ArrayList<>();97commands.add(JAVA);98for (String arg : jvmArgs)99commands.add(arg);100commands.add("-cp");101commands.add(CLASSPATH);102commands.add("AcceptInheritHandle");103commands.add(ssp.name());104105System.out.println("Executing: "+ commands);106ProcessBuilder pb = new ProcessBuilder(commands);107pb.redirectError(ProcessBuilder.Redirect.INHERIT);108Process serverProcess = pb.start();109DataInputStream dis = new DataInputStream(serverProcess.getInputStream());110111int port = dis.readInt();112System.out.println("Server process listening on " + port + ", connecting...");113114String address;115if (Arrays.stream(jvmArgs).anyMatch("-Djava.net.preferIPv4Stack=true"::equals)) {116address = "127.0.0.1";117} else {118InetAddress loopback = InetAddress.getLoopbackAddress();119address = loopback.getHostAddress();120}121Socket socket = new Socket(address, port);122String s = dis.readUTF();123System.out.println("Server process said " + s);124125serverProcess.destroy();126serverProcess.waitFor(30, TimeUnit.SECONDS);127System.out.println("serverProcess exitCode:" + serverProcess.exitValue());128129try {130socket.setSoTimeout(10 * 1000);131socket.getInputStream().read();132} catch (SocketTimeoutException x) {133// failed134throw new RuntimeException("Failed: should get reset, not " + x);135} catch (SocketException x) {136System.out.println("Expected:" + x);137}138}139140static void server(ServerSocketProducer producer) throws Exception {141try (ServerSocket ss = producer.supplier().get()) {142InetAddress loopback = InetAddress.getLoopbackAddress();143ss.bind(new InetSocketAddress(loopback, 0));144int port = ss.getLocalPort();145DataOutputStream dos = new DataOutputStream(System.out);146dos.writeInt(port);147dos.flush();148149ss.accept(); // do not close150151Runtime.getRuntime().exec("sleep 20");152Thread.sleep(3 * 1000);153154dos.writeUTF("kill me!");155dos.flush();156Thread.sleep(30 * 1000);157}158}159}160161162