Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/InheritHandle.java
38812 views
/*1* Copyright (c) 2007, 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/* @test24@bug 659816025@summary Windows IPv6 Socket implementation doesn't set the handle to not inherit26@author Chris Hegarty27*/2829import java.net.ServerSocket;30import java.io.File;31import java.io.IOException;3233/**34* This test is only really applicable to Windows machines that are running IPv6, but35* it should still pass on other platforms so we can just run it.36*/3738public class InheritHandle39{40static String java = System.getProperty("java.home") + File.separator +41"bin" + File.separator + "java";4243public static void main(String[] args) {44if (args.length == 1) {45doWait();46} else {47startTest();48}4950}5152static void startTest() {53ServerSocket ss;54int port;55Process process;5657// create a ServerSocket listening on any port58try {59ss = new ServerSocket(0);60port = ss.getLocalPort();61System.out.println("First ServerSocket listening on port " + port);62} catch (IOException e) {63System.out.println("Cannot create ServerSocket");64e.printStackTrace();65return;66}6768// create another java process that simply waits. If the bug is present this69// process will inherit the native IPv6 handle for ss and cause the second70// ServerSocket constructor to throw a BindException71try {72process = Runtime.getRuntime().exec(java + " InheritHandle -doWait");73} catch (IOException ioe) {74System.out.println("Cannot create process");75ioe.printStackTrace();76return;77}7879// Allow some time for the process to get started80try {81System.out.println("waiting...");82Thread.sleep(2 * 1000);8384System.out.println("Now close the socket and try to create another" +85" one listening on the same port");86ss.close();87ss = new ServerSocket(port);88System.out.println("Second ServerSocket created successfully");89ss.close();9091} catch (InterruptedException ie) {92} catch (IOException ioe) {93throw new RuntimeException("Failed: " + ioe);94} finally {95process.destroy();96}9798System.out.println("OK");99}100101static void doWait() {102try {103Thread.sleep(200 * 1000);104} catch (InterruptedException ie) {105}106}107}108109110