Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/rmi/transport/readTimeout/ReadTimeoutTest.java
41155 views
1
/*
2
* Copyright (c) 1999, 2019, 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 4208804
26
*
27
* @summary Incoming connections should be subject to timeout
28
* @author Adrian Colley
29
*
30
* @build TestIface TestImpl TestImpl_Stub
31
* @run main/othervm/policy=security.policy/timeout=60
32
* -Dsun.rmi.transport.tcp.readTimeout=5000 ReadTimeoutTest
33
*/
34
35
/* This test sets a very short read timeout, exports an object, and then
36
* connects to the port and does nothing. The server should close the
37
* connection after the timeout. If that doesn't happen, the test fails.
38
*
39
* A background thread does the read. The foreground waits for DELAY*4
40
* and then aborts. This should give sufficient margin for timing slop.
41
*/
42
43
import java.rmi.*;
44
import java.rmi.server.RMISocketFactory;
45
import java.io.*;
46
import java.net.*;
47
import java.util.concurrent.CountDownLatch;
48
import java.util.concurrent.TimeUnit;
49
50
public class ReadTimeoutTest
51
{
52
private static final int DELAY = 5000; // milliseconds
53
54
public static void main(String[] args)
55
throws Exception
56
{
57
// Make trouble for ourselves
58
if (System.getSecurityManager() == null)
59
System.setSecurityManager(new RMISecurityManager());
60
61
// Flaky code alert - hope this is executed before TCPTransport.<clinit>
62
System.setProperty("sun.rmi.transport.tcp.readTimeout", Integer.toString(DELAY));
63
64
// Set the socket factory.
65
System.err.println("(installing socket factory)");
66
SomeFactory fac = new SomeFactory();
67
RMISocketFactory.setSocketFactory(fac);
68
69
// Create remote object
70
TestImpl impl = new TestImpl();
71
72
// Export and get which port.
73
System.err.println("(exporting remote object)");
74
TestIface stub = impl.export();
75
Socket DoS = null;
76
try {
77
int port = fac.whichPort();
78
79
// Sanity
80
if (port == 0)
81
throw new Error("TEST FAILED: export didn't reserve a port(?)");
82
83
// Now, connect to that port
84
//Thread.sleep(2000);
85
System.err.println("(connecting to listening port on localhost:" +
86
port + ")");
87
DoS = new Socket(InetAddress.getLoopbackAddress(), port);
88
InputStream stream = DoS.getInputStream();
89
90
// Read on the socket in the background
91
CountDownLatch done = new CountDownLatch(1);
92
(new SomeReader(stream, done)).start();
93
94
// Wait for completion
95
if (done.await(DELAY * 4, TimeUnit.SECONDS)) {
96
System.err.println("TEST PASSED.");
97
} else {
98
throw new Error("TEST FAILED.");
99
}
100
101
} catch (InterruptedException ie) {
102
throw new Error("Unexpected interrupt", ie);
103
} finally {
104
try {
105
if (DoS != null)
106
DoS.close(); // aborts the reader if still blocked
107
impl.unexport();
108
} catch (Throwable unmatter) {
109
}
110
}
111
112
// Should exit here
113
}
114
115
private static class SomeFactory
116
extends RMISocketFactory
117
{
118
private int servport = 0;
119
120
@Override
121
public Socket createSocket(String h, int p)
122
throws IOException
123
{
124
return (new Socket(h, p));
125
}
126
127
/** Create a server socket and remember which port it's on.
128
* Aborts if createServerSocket(0) is called twice, because then
129
* it doesn't know whether to remember the first or second port.
130
*/
131
@Override
132
public ServerSocket createServerSocket(int p)
133
throws IOException
134
{
135
ServerSocket ss;
136
ss = new ServerSocket(p);
137
if (p == 0) {
138
if (servport != 0) {
139
System.err.println("TEST FAILED: " +
140
"Duplicate createServerSocket(0)");
141
throw new Error("Test aborted (createServerSocket)");
142
}
143
servport = ss.getLocalPort();
144
}
145
return (ss);
146
}
147
148
/** Return which port was reserved by createServerSocket(0).
149
* If the return value was 0, createServerSocket(0) wasn't called.
150
*/
151
public int whichPort() {
152
return (servport);
153
}
154
} // end class SomeFactory
155
156
protected static class SomeReader extends Thread {
157
private final InputStream readon;
158
private final CountDownLatch done;
159
160
public SomeReader(InputStream s, CountDownLatch done) {
161
super();
162
this.setDaemon(true);
163
this.readon = s;
164
this.done = done;
165
}
166
167
@Override
168
public void run() {
169
try {
170
int c = this.readon.read();
171
if (c != -1)
172
throw new Error ("Server returned " + c);
173
done.countDown();
174
175
} catch (IOException e) {
176
e.printStackTrace();
177
}
178
}
179
} // end class SomeReader
180
}
181
182