Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ServerSocket/AcceptInheritHandle.java
38812 views
/*1* Copyright (c) 2015, 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* @summary Socket returned by ServerSocket.accept() is inherited by child process on Windows27* @author Chris Hegarty28*/2930import java.io.*;31import java.net.*;32import java.nio.channels.ServerSocketChannel;33import java.util.ArrayList;34import java.util.List;35import java.util.concurrent.TimeUnit;36import java.util.function.Supplier;3738public class AcceptInheritHandle {3940enum ServerSocketProducer {41JAVA_NET(() -> {42try {43return new ServerSocket(); }44catch(IOException x) {45throw new UncheckedIOException(x);46}47}),48NIO_CHANNELS(() -> {49try {50return ServerSocketChannel.open().socket();51} catch (IOException x) {52throw new UncheckedIOException(x);53}54});5556final Supplier<ServerSocket> supplier;57ServerSocketProducer(Supplier<ServerSocket> supplier) {58this.supplier = supplier;59}60Supplier<ServerSocket> supplier () { return supplier; }61}6263static final String JAVA = System.getProperty("java.home")64+ File.separator + "bin" + File.separator + "java";6566static final String CLASSPATH = System.getProperty("java.class.path");6768public static void main(String[] args) throws Exception {69if (args.length == 1)70server(ServerSocketProducer.valueOf(args[0]));71else72mainEntry();73}7475static void mainEntry() throws Exception {76testJavaNetServerSocket();77testNioServerSocketChannel();78}7980static void testJavaNetServerSocket() throws Exception {81test(ServerSocketProducer.JAVA_NET);82test(ServerSocketProducer.JAVA_NET, "-Djava.net.preferIPv4Stack=true");83}84static void testNioServerSocketChannel() throws Exception {85test(ServerSocketProducer.NIO_CHANNELS);86}8788static void test(ServerSocketProducer ssp, String... sysProps) throws Exception {89System.out.println("\nStarting test for " + ssp.name());9091List<String> commands = new ArrayList<>();92commands.add(JAVA);93for (String prop : sysProps)94commands.add(prop);95commands.add("-cp");96commands.add(CLASSPATH);97commands.add("AcceptInheritHandle");98commands.add(ssp.name());99100System.out.println("Executing: "+ commands);101ProcessBuilder pb = new ProcessBuilder(commands);102pb.redirectError(ProcessBuilder.Redirect.INHERIT);103Process serverProcess = pb.start();104DataInputStream dis = new DataInputStream(serverProcess.getInputStream());105106int port = dis.readInt();107System.out.println("Server process listening on " + port + ", connecting...");108109Socket socket = new Socket("localhost", port);110String s = dis.readUTF();111System.out.println("Server process said " + s);112113serverProcess.destroy();114serverProcess.waitFor(30, TimeUnit.SECONDS);115System.out.println("serverProcess exitCode:" + serverProcess.exitValue());116117try {118socket.setSoTimeout(10 * 1000);119socket.getInputStream().read();120} catch (SocketTimeoutException x) {121// failed122throw new RuntimeException("Failed: should get reset, not " + x);123} catch (SocketException x) {124System.out.println("Expected:" + x);125}126}127128static void server(ServerSocketProducer producer) throws Exception {129try (ServerSocket ss = producer.supplier().get()) {130ss.bind(new InetSocketAddress(0));131int port = ss.getLocalPort();132DataOutputStream dos = new DataOutputStream(System.out);133dos.writeInt(port);134dos.flush();135136ss.accept(); // do not close137138Runtime.getRuntime().exec("sleep 20");139Thread.sleep(3 * 1000);140141dos.writeUTF("kill me!");142dos.flush();143Thread.sleep(30 * 1000);144}145}146}147148149