Path: blob/master/test/jdk/java/net/Socket/InheritHandle.java
41149 views
/*1* Copyright (c) 2007, 2018, 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* @library /test/lib26* @summary Windows IPv6 Socket implementation doesn't set the handle to not inherit27* @author Chris Hegarty28* @run main InheritHandle29* @run main/othervm -Djava.net.preferIPv4Stack=true InheritHandle30*/3132import java.net.BindException;33import java.net.ServerSocket;34import java.io.File;35import java.io.IOException;36import jdk.test.lib.net.IPSupport;3738/**39* This test is only really applicable to Windows machines that are running IPv6, but40* it should still pass on other platforms so we can just run it.41*/4243public class InheritHandle44{45static String java = System.getProperty("java.home") + File.separator +46"bin" + File.separator + "java";4748public static void main(String[] args) {49IPSupport.throwSkippedExceptionIfNonOperational();5051if (args.length == 1) {52doWait();53} else {54startTest();55}5657}5859static void startTest() {60ServerSocket ss;61int port;62Process process;6364// create a ServerSocket listening on any port65try {66ss = new ServerSocket(0);67port = ss.getLocalPort();68System.out.println("First ServerSocket listening on port " + port);69} catch (IOException e) {70System.out.println("Cannot create ServerSocket");71e.printStackTrace();72return;73}7475// create another java process that simply waits. If the bug is present this76// process will inherit the native IPv6 handle for ss and cause the second77// ServerSocket constructor to throw a BindException78try {79process = Runtime.getRuntime().exec(java + " InheritHandle -doWait");80} catch (IOException ioe) {81System.out.println("Cannot create process");82ioe.printStackTrace();83try {84ss.close();85} catch (IOException e) {86e.printStackTrace();87}88return;89}9091// Allow some time for the process to get started92try {93System.out.println("waiting...");94Thread.sleep(2 * 1000);9596System.out.println("Now close the socket and try to create another" +97" one listening on the same port");98ss.close();99int retries = 0;100while (retries < 5) {101try (ServerSocket s = new ServerSocket(port);) {102System.out.println("Second ServerSocket created successfully");103break;104} catch (BindException e) {105System.out.println("BindException \"" + e.getMessage() + "\", retrying...");106Thread.sleep(100L);107retries ++;108continue;109}110}111112} catch (InterruptedException ie) {113} catch (IOException ioe) {114throw new RuntimeException("Failed: " + ioe);115} finally {116process.destroy();117}118119System.out.println("OK");120}121122static void doWait() {123try {124Thread.sleep(200 * 1000);125} catch (InterruptedException ie) {126}127}128}129130131