test/jdk/java/net/Socket/InheritHandle.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 49431 5812849b5027
child 54770 62b6e7587b1f
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 2007, 2018, 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
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    25
 * @bug  6598160
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    26
 * @summary Windows IPv6 Socket implementation doesn't set the handle to not inherit
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    27
 * @author Chris Hegarty
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    28
 * @run main InheritHandle
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    29
 * @run main/othervm -Djava.net.preferIPv4Stack=true InheritHandle
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
41133
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    32
import java.net.BindException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This test is only really applicable to Windows machines that are running IPv6, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * it should still pass on other platforms so we can just run it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class InheritHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static String java = System.getProperty("java.home") + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                         "bin" + File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (args.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            doWait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            startTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static void startTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Process process;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // create a ServerSocket listening on any port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            port = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.println("First ServerSocket listening on port " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            System.out.println("Cannot create ServerSocket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // create another java process that simply waits. If the bug is present this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // process will inherit the native IPv6 handle for ss and cause the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // ServerSocket constructor to throw a BindException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            process = Runtime.getRuntime().exec(java + " InheritHandle -doWait");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.println("Cannot create process");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            ioe.printStackTrace();
41133
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    80
            try {
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    81
                ss.close();
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    82
            } catch (IOException e) {
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    83
                e.printStackTrace();
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    84
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // Allow some time for the process to get started
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.out.println("waiting...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            Thread.sleep(2 * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.out.println("Now close the socket and try to create another" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                               " one listening on the same port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            ss.close();
41133
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    96
            int retries = 0;
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    97
            while (retries < 5) {
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
    98
                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
    99
                    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
   100
                    break;
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   101
                } catch (BindException e) {
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   102
                    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
   103
                    Thread.sleep(100L);
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   104
                    retries ++;
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   105
                    continue;
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   106
                }
83066d79df39 8130657: com/sun/net/httpserver/Test5.java failed with java.lang.RuntimeException: wrong string result
xiaofeya
parents: 5506
diff changeset
   107
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new RuntimeException("Failed: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            process.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static void doWait() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            Thread.sleep(200 * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}