jdk/test/java/net/Socket/InheritHandle.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 5506 202f599c92aa
child 41133 83066d79df39
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This test is only really applicable to Windows machines that are running IPv6, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * it should still pass on other platforms so we can just run it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class InheritHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static String java = System.getProperty("java.home") + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                         "bin" + File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (args.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            doWait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            startTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
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
    static void startTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Process process;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // create a ServerSocket listening on any port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            port = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.out.println("First ServerSocket listening on port " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            System.out.println("Cannot create ServerSocket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // create another java process that simply waits. If the bug is present this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // process will inherit the native IPv6 handle for ss and cause the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // ServerSocket constructor to throw a BindException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            process = Runtime.getRuntime().exec(java + " InheritHandle -doWait");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            System.out.println("Cannot create process");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Allow some time for the process to get started
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.out.println("waiting...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            Thread.sleep(2 * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.out.println("Now close the socket and try to create another" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                               " one listening on the same port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            ss = new ServerSocket(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            System.out.println("Second ServerSocket created successfully");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new RuntimeException("Failed: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            process.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.out.println("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static void doWait() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            Thread.sleep(200 * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
}