jdk/test/java/net/Socket/InheritHandle.java
author xiaofeya
Mon, 26 Sep 2016 08:19:07 -0700
changeset 41133 83066d79df39
parent 5506 202f599c92aa
permissions -rw-r--r--
8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result 8085575: java/net/Socket/InheritHandle.java fails intermittently with "Address already in use" Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
41133
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
     2
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
   @bug  6598160
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @summary Windows IPv6 Socket implementation doesn't set the handle to not inherit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
   @author Chris Hegarty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
41133
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    30
import java.net.BindException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This test is only really applicable to Windows machines that are running IPv6, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * it should still pass on other platforms so we can just run it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class InheritHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static String java = System.getProperty("java.home") + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                         "bin" + File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (args.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            doWait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            startTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void startTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        Process process;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // create a ServerSocket listening on any port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            port = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.out.println("First ServerSocket listening on port " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.println("Cannot create ServerSocket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // create another java process that simply waits. If the bug is present this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // process will inherit the native IPv6 handle for ss and cause the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // ServerSocket constructor to throw a BindException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            process = Runtime.getRuntime().exec(java + " InheritHandle -doWait");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            System.out.println("Cannot create process");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            ioe.printStackTrace();
41133
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    78
            try {
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    79
                ss.close();
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    80
            } catch (IOException e) {
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    81
                e.printStackTrace();
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    82
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // Allow some time for the process to get started
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            System.out.println("waiting...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            Thread.sleep(2 * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            System.out.println("Now close the socket and try to create another" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                               " one listening on the same port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            ss.close();
41133
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    94
            int retries = 0;
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    95
            while (retries < 5) {
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    96
                try (ServerSocket s = new ServerSocket(port);) {
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    97
                    System.out.println("Second ServerSocket created successfully");
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    98
                    break;
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    99
                } catch (BindException e) {
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   100
                    System.out.println("BindException \"" + e.getMessage() + "\", retrying...");
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   101
                    Thread.sleep(100L);
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   102
                    retries ++;
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   103
                    continue;
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   104
                }
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   105
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new RuntimeException("Failed: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            process.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static void doWait() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            Thread.sleep(200 * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}