< prev index next >

test/jdk/java/net/ServerSocket/AcceptInheritHandle.java

Print this page

  1 /*
  2  * Copyright (c) 2015, 2019, 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  * @library /test/lib
 28  * @summary Socket returned by ServerSocket.accept() is inherited by child process on Windows
 29  * @author Chris Hegarty
 30  */
 31 
 32 import java.io.*;
 33 import java.net.*;
 34 import java.nio.channels.ServerSocketChannel;
 35 import java.util.ArrayList;
 36 import java.util.Arrays;

 37 import java.util.List;
 38 import java.util.concurrent.TimeUnit;
 39 import java.util.function.Supplier;
 40 import jdk.test.lib.net.IPSupport;

 41 
 42 public class AcceptInheritHandle {
 43 
 44     enum ServerSocketProducer {
 45         JAVA_NET(() -> {
 46             try {
 47                 return new ServerSocket();
 48             } catch(IOException x) {
 49                 throw new UncheckedIOException(x);
 50             }
 51         }),
 52         NIO_CHANNELS(() -> {
 53             try {
 54                 return ServerSocketChannel.open().socket();
 55             } catch (IOException x) {
 56                 throw new UncheckedIOException(x);
 57             }
 58         });
 59 
 60         final Supplier<ServerSocket> supplier;

 78 
 79     static void mainEntry() throws Exception {
 80         testJavaNetServerSocket();
 81         testNioServerSocketChannel();
 82     }
 83 
 84     static void testJavaNetServerSocket() throws Exception {
 85         test(ServerSocketProducer.JAVA_NET);
 86         if (IPSupport.hasIPv4()) {
 87             test(ServerSocketProducer.JAVA_NET, "-Djava.net.preferIPv4Stack=true");
 88         }
 89     }
 90     static void testNioServerSocketChannel() throws Exception {
 91         test(ServerSocketProducer.NIO_CHANNELS);
 92     }
 93 
 94     static void test(ServerSocketProducer ssp, String... jvmArgs) throws Exception {
 95         System.out.println("\nStarting test for " + ssp.name());
 96 
 97         List<String> commands = new ArrayList<>();
 98         commands.add(JAVA);
 99         for (String arg : jvmArgs)
100             commands.add(arg);
101         commands.add("-cp");
102         commands.add(CLASSPATH);
103         commands.add("AcceptInheritHandle");
104         commands.add(ssp.name());
105 
106         System.out.println("Executing: "+ commands);
107         ProcessBuilder pb = new ProcessBuilder(commands);
108         pb.redirectError(ProcessBuilder.Redirect.INHERIT);
109         Process serverProcess = pb.start();
110         DataInputStream dis = new DataInputStream(serverProcess.getInputStream());
111 
112         int port = dis.readInt();
113         System.out.println("Server process listening on " + port + ", connecting...");
114 
115         String address;
116         if (Arrays.stream(jvmArgs).anyMatch("-Djava.net.preferIPv4Stack=true"::equals)) {
117             address = "127.0.0.1";
118         } else {
119             InetAddress loopback = InetAddress.getLoopbackAddress();
120             address = loopback.getHostAddress();
121         }
122         Socket socket = new Socket(address, port);
123         String s = dis.readUTF();
124         System.out.println("Server process said " + s);
125 
126         serverProcess.destroy();
127         serverProcess.waitFor(30, TimeUnit.SECONDS);

  1 /*
  2  * Copyright (c) 2015, 2024, 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  * @library /test/lib
 28  * @summary Socket returned by ServerSocket.accept() is inherited by child process on Windows
 29  * @author Chris Hegarty
 30  */
 31 
 32 import java.io.*;
 33 import java.net.*;
 34 import java.nio.channels.ServerSocketChannel;
 35 import java.util.ArrayList;
 36 import java.util.Arrays;
 37 import java.util.Collections;
 38 import java.util.List;
 39 import java.util.concurrent.TimeUnit;
 40 import java.util.function.Supplier;
 41 import jdk.test.lib.net.IPSupport;
 42 import jdk.test.lib.process.ProcessTools;
 43 
 44 public class AcceptInheritHandle {
 45 
 46     enum ServerSocketProducer {
 47         JAVA_NET(() -> {
 48             try {
 49                 return new ServerSocket();
 50             } catch(IOException x) {
 51                 throw new UncheckedIOException(x);
 52             }
 53         }),
 54         NIO_CHANNELS(() -> {
 55             try {
 56                 return ServerSocketChannel.open().socket();
 57             } catch (IOException x) {
 58                 throw new UncheckedIOException(x);
 59             }
 60         });
 61 
 62         final Supplier<ServerSocket> supplier;

 80 
 81     static void mainEntry() throws Exception {
 82         testJavaNetServerSocket();
 83         testNioServerSocketChannel();
 84     }
 85 
 86     static void testJavaNetServerSocket() throws Exception {
 87         test(ServerSocketProducer.JAVA_NET);
 88         if (IPSupport.hasIPv4()) {
 89             test(ServerSocketProducer.JAVA_NET, "-Djava.net.preferIPv4Stack=true");
 90         }
 91     }
 92     static void testNioServerSocketChannel() throws Exception {
 93         test(ServerSocketProducer.NIO_CHANNELS);
 94     }
 95 
 96     static void test(ServerSocketProducer ssp, String... jvmArgs) throws Exception {
 97         System.out.println("\nStarting test for " + ssp.name());
 98 
 99         List<String> commands = new ArrayList<>();
100         Collections.addAll(commands, jvmArgs);




101         commands.add("AcceptInheritHandle");
102         commands.add(ssp.name());
103 
104         System.out.println("Executing: "+ commands);
105         ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(commands);
106         pb.redirectError(ProcessBuilder.Redirect.INHERIT);
107         Process serverProcess = pb.start();
108         DataInputStream dis = new DataInputStream(serverProcess.getInputStream());
109 
110         int port = dis.readInt();
111         System.out.println("Server process listening on " + port + ", connecting...");
112 
113         String address;
114         if (Arrays.stream(jvmArgs).anyMatch("-Djava.net.preferIPv4Stack=true"::equals)) {
115             address = "127.0.0.1";
116         } else {
117             InetAddress loopback = InetAddress.getLoopbackAddress();
118             address = loopback.getHostAddress();
119         }
120         Socket socket = new Socket(address, port);
121         String s = dis.readUTF();
122         System.out.println("Server process said " + s);
123 
124         serverProcess.destroy();
125         serverProcess.waitFor(30, TimeUnit.SECONDS);
< prev index next >