Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/connection/ConnectionTest.java
38867 views
1
/*
2
* Copyright (c) 2003, 2013, 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
/*
25
* @test
26
* @bug 4865397
27
* @summary Tests remote JMX connections
28
* @author Eamonn McManus
29
* @run clean ConnectionTest
30
* @run build ConnectionTest
31
* @run main ConnectionTest
32
*/
33
34
import java.io.IOException;
35
import java.net.MalformedURLException;
36
import java.util.Collections;
37
import java.util.HashMap;
38
import java.util.HashSet;
39
import java.util.Iterator;
40
import java.util.LinkedList;
41
import java.util.List;
42
import java.util.Map;
43
import java.util.Set;
44
import java.util.StringTokenizer;
45
46
import java.security.Principal;
47
import java.util.regex.Pattern;
48
import javax.security.auth.Subject;
49
50
import javax.management.MBeanServer;
51
import javax.management.MBeanServerConnection;
52
import javax.management.MBeanServerFactory;
53
import javax.management.Notification;
54
import javax.management.NotificationListener;
55
import javax.management.ObjectName;
56
57
import javax.management.remote.JMXAuthenticator;
58
import javax.management.remote.JMXConnectionNotification;
59
import javax.management.remote.JMXConnector;
60
import javax.management.remote.JMXConnectorFactory;
61
import javax.management.remote.JMXConnectorServer;
62
import javax.management.remote.JMXConnectorServerFactory;
63
import javax.management.remote.JMXPrincipal;
64
import javax.management.remote.JMXServiceURL;
65
66
public class ConnectionTest {
67
68
public static void main(String[] args) {
69
// System.setProperty("java.util.logging.config.file",
70
// "../../../../logging.properties");
71
// // we are in <workspace>/build/test/JTwork/scratch
72
// java.util.logging.LogManager.getLogManager().readConfiguration();
73
boolean ok = true;
74
String[] protocols = {"rmi", "iiop", "jmxmp"};
75
if (args.length > 0)
76
protocols = args;
77
for (int i = 0; i < protocols.length; i++) {
78
final String proto = protocols[i];
79
System.out.println("Testing for protocol " + proto);
80
try {
81
ok &= test(proto);
82
} catch (Exception e) {
83
System.err.println("Unexpected exception: " + e);
84
e.printStackTrace();
85
ok = false;
86
}
87
}
88
89
if (ok)
90
System.out.println("Test passed");
91
else {
92
System.out.println("TEST FAILED");
93
System.exit(1);
94
}
95
}
96
97
private static boolean test(String proto) throws Exception {
98
ObjectName serverName = ObjectName.getInstance("d:type=server");
99
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
100
JMXAuthenticator authenticator = new BogusAuthenticator();
101
Map env = Collections.singletonMap("jmx.remote.authenticator",
102
authenticator);
103
JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + "://");
104
JMXConnectorServer server;
105
try {
106
server =
107
JMXConnectorServerFactory.newJMXConnectorServer(url, env,
108
null);
109
} catch (MalformedURLException e) {
110
System.out.println("Protocol " + proto +
111
" not supported, ignoring");
112
return true;
113
}
114
System.out.println("Created connector server");
115
mbs.registerMBean(server, serverName);
116
System.out.println("Registered connector server in MBean server");
117
mbs.addNotificationListener(serverName, logListener, null, null);
118
mbs.invoke(serverName, "start", null, null);
119
System.out.println("Started connector server");
120
JMXServiceURL address =
121
(JMXServiceURL) mbs.getAttribute(serverName, "Address");
122
System.out.println("Retrieved address: " + address);
123
124
if (address.getHost().length() == 0) {
125
System.out.println("Generated address has empty hostname");
126
return false;
127
}
128
129
JMXConnector client = JMXConnectorFactory.connect(address);
130
System.out.println("Client connected");
131
132
String clientConnId = client.getConnectionId();
133
System.out.println("Got connection ID on client: " + clientConnId);
134
boolean ok = checkConnectionId(proto, clientConnId);
135
if (!ok)
136
return false;
137
System.out.println("Connection ID is OK");
138
139
// 4901826: connection ids need some time to be updated using jmxmp
140
// we don't get the notif immediately either
141
// this was originally timeout 1ms, which was not enough
142
Notification notif = waitForNotification(1000);
143
System.out.println("Server got notification: " + notif);
144
145
ok = mustBeConnectionNotification(notif, clientConnId,
146
JMXConnectionNotification.OPENED);
147
if (!ok)
148
return false;
149
150
client.close();
151
System.out.println("Closed client");
152
153
notif = waitForNotification(1000);
154
System.out.println("Got notification: " + notif);
155
156
ok = mustBeConnectionNotification(notif, clientConnId,
157
JMXConnectionNotification.CLOSED);
158
if (!ok)
159
return false;
160
161
client = JMXConnectorFactory.connect(address);
162
System.out.println("Second client connected");
163
164
String clientConnId2 = client.getConnectionId();
165
if (clientConnId.equals(clientConnId2)) {
166
System.out.println("Same connection ID for two connections: " +
167
clientConnId2);
168
return false;
169
}
170
System.out.println("Second client connection ID is different");
171
172
notif = waitForNotification(1);
173
ok = mustBeConnectionNotification(notif, clientConnId2,
174
JMXConnectionNotification.OPENED);
175
if (!ok)
176
return false;
177
178
MBeanServerConnection mbsc = client.getMBeanServerConnection();
179
Map attrs = (Map) mbsc.getAttribute(serverName, "Attributes");
180
System.out.println("Server attributes received by client: " + attrs);
181
182
server.stop();
183
System.out.println("Server stopped");
184
185
notif = waitForNotification(1000);
186
System.out.println("Server got connection-closed notification: " +
187
notif);
188
189
ok = mustBeConnectionNotification(notif, clientConnId2,
190
JMXConnectionNotification.CLOSED);
191
if (!ok)
192
return false;
193
194
try {
195
mbsc.getDefaultDomain();
196
System.out.println("Connection still working but should not be");
197
return false;
198
} catch (IOException e) {
199
System.out.println("Connection correctly got exception: " + e);
200
}
201
202
try {
203
client = JMXConnectorFactory.connect(address);
204
System.out.println("Connector server still working but should " +
205
"not be");
206
return false;
207
} catch (IOException e) {
208
System.out.println("New connection correctly got exception: " + e);
209
}
210
211
return true;
212
}
213
214
private static boolean
215
mustBeConnectionNotification(Notification notif,
216
String requiredConnId,
217
String requiredType) {
218
219
if (!(notif instanceof JMXConnectionNotification)) {
220
System.out.println("Should have been a " +
221
"JMXConnectionNotification: " +
222
notif.getClass());
223
return false;
224
}
225
226
JMXConnectionNotification cnotif = (JMXConnectionNotification) notif;
227
if (!cnotif.getType().equals(requiredType)) {
228
System.out.println("Wrong type notif: is \"" + cnotif.getType() +
229
"\", should be \"" + requiredType + "\"");
230
return false;
231
}
232
233
if (!cnotif.getConnectionId().equals(requiredConnId)) {
234
System.out.println("Wrong connection id: is \"" +
235
cnotif.getConnectionId() + "\", should be \"" +
236
requiredConnId);
237
return false;
238
}
239
240
return true;
241
}
242
243
private static final String IPV4_PTN = "^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}(\\:[1-9][0-9]{3})?$";
244
245
/**
246
* Checks the connection id for validity.
247
* The {@link
248
* javax.management.remote package description} describes the
249
* conventions for connection IDs.
250
* @param proto Connection protocol
251
* @param clientConnId The connection ID
252
* @return Returns {@code true} if the connection id conforms to the specification; {@code false} otherwise.
253
* @throws Exception
254
*/
255
private static boolean checkConnectionId(String proto, String clientConnId)
256
throws Exception {
257
StringTokenizer tok = new StringTokenizer(clientConnId, " ", true);
258
String s;
259
s = tok.nextToken();
260
if (!s.startsWith(proto + ":")) {
261
System.out.println("Expected \"" + proto + ":\", found \"" + s +
262
"\"");
263
return false;
264
}
265
266
int hostAddrInd = s.indexOf("//");
267
if (hostAddrInd > -1) {
268
s = s.substring(hostAddrInd + 2);
269
if (!Pattern.matches(IPV4_PTN, s)) {
270
if (!s.startsWith("[") || !s.endsWith("]")) {
271
System.out.println("IPv6 address must be enclosed in \"[]\"");
272
return false;
273
}
274
}
275
}
276
s = tok.nextToken();
277
if (!s.equals(" ")) {
278
System.out.println("Expected \" \", found \"" + s + "\"");
279
return false;
280
}
281
s = tok.nextToken();
282
StringTokenizer tok2 = new StringTokenizer(s, ";", true);
283
Set principalNames = new HashSet();
284
String s2;
285
s2 = tok2.nextToken();
286
if (s2.equals(";")) {
287
System.out.println("In identity \"" + s +
288
"\", expected name, found \";\"");
289
return false;
290
}
291
principalNames.add(s2);
292
s2 = tok2.nextToken();
293
if (!s2.equals(";"))
294
throw new Exception("Can't happen");
295
s2 = tok2.nextToken();
296
if (s2.equals(";")) {
297
System.out.println("In identity \"" + s +
298
"\", expected name, found \";\"");
299
return false;
300
}
301
principalNames.add(s2);
302
if (tok2.hasMoreTokens()) {
303
System.out.println("In identity \"" + s + "\", too many tokens");
304
return false;
305
}
306
if (principalNames.size() != bogusPrincipals.size()) {
307
System.out.println("Wrong number of principal names: " +
308
principalNames.size() + " != " +
309
bogusPrincipals.size());
310
return false;
311
}
312
for (Iterator it = bogusPrincipals.iterator(); it.hasNext(); ) {
313
Principal p = (Principal) it.next();
314
if (!principalNames.contains(p.getName())) {
315
System.out.println("Principal names don't contain \"" +
316
p.getName() + "\"");
317
return false;
318
}
319
}
320
s = tok.nextToken();
321
if (!s.equals(" ")) {
322
System.out.println("Expected \" \", found \"" + s + "\"");
323
return false;
324
}
325
return true;
326
}
327
328
private static Notification waitForNotification(long timeout)
329
throws InterruptedException {
330
synchronized (log) {
331
if (log.isEmpty()) {
332
long remainingTime = timeout;
333
final long startTime = System.currentTimeMillis();
334
335
while (log.isEmpty() && remainingTime >0) {
336
log.wait(remainingTime);
337
remainingTime = timeout - (System.currentTimeMillis() - startTime);
338
}
339
340
if (log.isEmpty()) {
341
throw new InterruptedException("Timed out waiting for " +
342
"notification!");
343
}
344
}
345
return (Notification) log.remove(0);
346
}
347
}
348
349
private static class LogListener implements NotificationListener {
350
LogListener(List log) {
351
this.log = log;
352
}
353
354
public void handleNotification(Notification n, Object h) {
355
synchronized (log) {
356
log.add(n);
357
log.notifyAll();
358
}
359
}
360
361
private final List log;
362
}
363
364
private static List log = new LinkedList();
365
private static NotificationListener logListener = new LogListener(log);
366
367
private static class BogusAuthenticator implements JMXAuthenticator {
368
public Subject authenticate(Object credentials) {
369
Subject subject =
370
new Subject(true, bogusPrincipals,
371
Collections.EMPTY_SET, Collections.EMPTY_SET);
372
System.out.println("Authenticator returns: " + subject);
373
return subject;
374
}
375
}
376
377
private static final Set bogusPrincipals = new HashSet();
378
static {
379
bogusPrincipals.add(new JMXPrincipal("foo"));
380
bogusPrincipals.add(new JMXPrincipal("bar"));
381
}
382
}
383
384