J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | @bug 6598160 |
| 26 | @summary Windows IPv6 Socket implementation doesn't set the handle to not inherit |
| 27 | @author Chris Hegarty |
| 28 | */ |
| 29 | |
| 30 | import java.net.ServerSocket; |
| 31 | import java.io.File; |
| 32 | import java.io.IOException; |
| 33 | |
| 34 | /** |
| 35 | * This test is only really applicable to Windows machines that are running IPv6, but |
| 36 | * it should still pass on other platforms so we can just run it. |
| 37 | */ |
| 38 | |
| 39 | public class InheritHandle |
| 40 | { |
| 41 | static String java = System.getProperty("java.home") + File.separator + |
| 42 | "bin" + File.separator + "java"; |
| 43 | |
| 44 | public static void main(String[] args) { |
| 45 | if (args.length == 1) { |
| 46 | doWait(); |
| 47 | } else { |
| 48 | startTest(); |
| 49 | } |
| 50 | |
| 51 | } |
| 52 | |
| 53 | static void startTest() { |
| 54 | ServerSocket ss; |
| 55 | int port; |
| 56 | Process process; |
| 57 | |
| 58 | // create a ServerSocket listening on any port |
| 59 | try { |
| 60 | ss = new ServerSocket(0); |
| 61 | port = ss.getLocalPort(); |
| 62 | System.out.println("First ServerSocket listening on port " + port); |
| 63 | } catch (IOException e) { |
| 64 | System.out.println("Cannot create ServerSocket"); |
| 65 | e.printStackTrace(); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // create another java process that simply waits. If the bug is present this |
| 70 | // process will inherit the native IPv6 handle for ss and cause the second |
| 71 | // ServerSocket constructor to throw a BindException |
| 72 | try { |
| 73 | process = Runtime.getRuntime().exec(java + " InheritHandle -doWait"); |
| 74 | } catch (IOException ioe) { |
| 75 | System.out.println("Cannot create process"); |
| 76 | ioe.printStackTrace(); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // Allow some time for the process to get started |
| 81 | try { |
| 82 | System.out.println("waiting..."); |
| 83 | Thread.sleep(2 * 1000); |
| 84 | |
| 85 | System.out.println("Now close the socket and try to create another" + |
| 86 | " one listening on the same port"); |
| 87 | ss.close(); |
| 88 | ss = new ServerSocket(port); |
| 89 | System.out.println("Second ServerSocket created successfully"); |
| 90 | ss.close(); |
| 91 | |
| 92 | } catch (InterruptedException ie) { |
| 93 | } catch (IOException ioe) { |
| 94 | throw new RuntimeException("Failed: " + ioe); |
| 95 | } finally { |
| 96 | process.destroy(); |
| 97 | } |
| 98 | |
| 99 | System.out.println("OK"); |
| 100 | } |
| 101 | |
| 102 | static void doWait() { |
| 103 | try { |
| 104 | Thread.sleep(200 * 1000); |
| 105 | } catch (InterruptedException ie) { |
| 106 | } |
| 107 | } |
| 108 | } |