Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/jdi/BadHandshakeTest.java
41152 views
1
/*
2
* Copyright (c) 2005, 2020, 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
import java.net.ConnectException;
25
import java.net.Socket;
26
27
import com.sun.jdi.Bootstrap;
28
import com.sun.jdi.VirtualMachine;
29
import com.sun.jdi.event.*;
30
import com.sun.jdi.connect.Connector;
31
import com.sun.jdi.connect.AttachingConnector;
32
import com.sun.jdi.connect.Connector.Argument;
33
34
import java.util.Map;
35
import java.util.List;
36
import java.util.Iterator;
37
import java.util.concurrent.TimeUnit;
38
39
import lib.jdb.Debuggee;
40
41
/* @test
42
* @bug 6306165 6432567
43
* @summary Check that a bad handshake doesn't cause a debuggee to abort
44
* @library /test/lib
45
*
46
* @modules java.management
47
* jdk.jdi
48
* @build BadHandshakeTest Exit0
49
* @run driver BadHandshakeTest
50
*/
51
public class BadHandshakeTest {
52
53
/*
54
* Find a connector by name
55
*/
56
private static Connector findConnector(String name) {
57
List<Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();
58
Iterator<Connector> iter = connectors.iterator();
59
while (iter.hasNext()) {
60
Connector connector = iter.next();
61
if (connector.name().equals(name)) {
62
return connector;
63
}
64
}
65
return null;
66
}
67
68
private static void log(Object s) {
69
System.out.println(String.valueOf(s));
70
}
71
72
public static void main(String args[]) throws Exception {
73
// Launch the server debugee
74
log("Starting debuggee...");
75
try (Debuggee debuggee = Debuggee.launcher("Exit0").launch()) {
76
log("Debuggee started.");
77
int port = Integer.parseInt(debuggee.getAddress());
78
log("Debuggee port: " + port);
79
80
log("testcase 1...");
81
// Connect to the debuggee and handshake with garbage
82
Socket s = new Socket("localhost", port);
83
s.getOutputStream().write("Here's a poke in the eye".getBytes("UTF-8"));
84
s.close();
85
86
log("testcase 2...");
87
// Re-connect and do a partial handshake - don't disconnect
88
// Re-connect just after disconnect may cause "connection refused" error (see JDK-8192057)
89
Exception error = null;
90
long retryDelay = 20;
91
for (int retry = 0; retry < 5; retry++) {
92
if (error != null) {
93
try {
94
Thread.sleep(retryDelay);
95
} catch (InterruptedException ex) {
96
// ignore
97
}
98
retryDelay *= 2;
99
error = null;
100
}
101
try {
102
log("retry: " + retry);
103
s = new Socket("localhost", port);
104
s.getOutputStream().write("JDWP-".getBytes("UTF-8"));
105
break;
106
} catch (ConnectException ex) {
107
log("got exception: " + ex.toString());
108
error = ex;
109
}
110
}
111
if (error != null) {
112
throw error;
113
}
114
115
log("final attach...");
116
// Attach to server debuggee to ensure it's still available to attach and resume it so it can exit
117
AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
118
retryDelay = 20;
119
for (int retry = 0; retry < 5; retry++) {
120
if (error != null) {
121
try {
122
Thread.sleep(retryDelay);
123
} catch (InterruptedException ex) {
124
// ignore
125
}
126
retryDelay *= 2;
127
error = null;
128
}
129
try {
130
log("retry: " + retry);
131
Map<String, Argument> conn_args = conn.defaultArguments();
132
Connector.IntegerArgument port_arg =
133
(Connector.IntegerArgument)conn_args.get("port");
134
port_arg.setValue(port);
135
VirtualMachine vm = conn.attach(conn_args);
136
137
// The first event is always a VMStartEvent, and it is always in
138
// an EventSet by itself. Wait for it.
139
EventSet evtSet = vm.eventQueue().remove();
140
for (Event event : evtSet) {
141
if (event instanceof VMStartEvent) {
142
break;
143
}
144
throw new RuntimeException("Test failed - debuggee did not start properly");
145
}
146
147
vm.eventRequestManager().deleteAllBreakpoints();
148
vm.resume();
149
break;
150
} catch (ConnectException ex) {
151
log("got exception: " + ex.toString());
152
error = ex;
153
}
154
}
155
if (error != null) {
156
throw error;
157
}
158
159
// give the debuggee some time to exit before forcibly terminating it
160
debuggee.waitFor(10, TimeUnit.SECONDS);
161
}
162
}
163
}
164
165