Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/InheritHandle.java
41149 views
1
/*
2
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
* @bug 6598160
26
* @library /test/lib
27
* @summary Windows IPv6 Socket implementation doesn't set the handle to not inherit
28
* @author Chris Hegarty
29
* @run main InheritHandle
30
* @run main/othervm -Djava.net.preferIPv4Stack=true InheritHandle
31
*/
32
33
import java.net.BindException;
34
import java.net.ServerSocket;
35
import java.io.File;
36
import java.io.IOException;
37
import jdk.test.lib.net.IPSupport;
38
39
/**
40
* This test is only really applicable to Windows machines that are running IPv6, but
41
* it should still pass on other platforms so we can just run it.
42
*/
43
44
public class InheritHandle
45
{
46
static String java = System.getProperty("java.home") + File.separator +
47
"bin" + File.separator + "java";
48
49
public static void main(String[] args) {
50
IPSupport.throwSkippedExceptionIfNonOperational();
51
52
if (args.length == 1) {
53
doWait();
54
} else {
55
startTest();
56
}
57
58
}
59
60
static void startTest() {
61
ServerSocket ss;
62
int port;
63
Process process;
64
65
// create a ServerSocket listening on any port
66
try {
67
ss = new ServerSocket(0);
68
port = ss.getLocalPort();
69
System.out.println("First ServerSocket listening on port " + port);
70
} catch (IOException e) {
71
System.out.println("Cannot create ServerSocket");
72
e.printStackTrace();
73
return;
74
}
75
76
// create another java process that simply waits. If the bug is present this
77
// process will inherit the native IPv6 handle for ss and cause the second
78
// ServerSocket constructor to throw a BindException
79
try {
80
process = Runtime.getRuntime().exec(java + " InheritHandle -doWait");
81
} catch (IOException ioe) {
82
System.out.println("Cannot create process");
83
ioe.printStackTrace();
84
try {
85
ss.close();
86
} catch (IOException e) {
87
e.printStackTrace();
88
}
89
return;
90
}
91
92
// Allow some time for the process to get started
93
try {
94
System.out.println("waiting...");
95
Thread.sleep(2 * 1000);
96
97
System.out.println("Now close the socket and try to create another" +
98
" one listening on the same port");
99
ss.close();
100
int retries = 0;
101
while (retries < 5) {
102
try (ServerSocket s = new ServerSocket(port);) {
103
System.out.println("Second ServerSocket created successfully");
104
break;
105
} catch (BindException e) {
106
System.out.println("BindException \"" + e.getMessage() + "\", retrying...");
107
Thread.sleep(100L);
108
retries ++;
109
continue;
110
}
111
}
112
113
} catch (InterruptedException ie) {
114
} catch (IOException ioe) {
115
throw new RuntimeException("Failed: " + ioe);
116
} finally {
117
process.destroy();
118
}
119
120
System.out.println("OK");
121
}
122
123
static void doWait() {
124
try {
125
Thread.sleep(200 * 1000);
126
} catch (InterruptedException ie) {
127
}
128
}
129
}
130
131