Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/jmxremote/bootstrap/JMXInterfaceBindingTest.java
38867 views
1
/*
2
* Copyright (c) 2015, Red Hat Inc
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.io.File;
25
import java.net.InetAddress;
26
import java.net.NetworkInterface;
27
import java.net.SocketException;
28
import java.util.ArrayList;
29
import java.util.Collections;
30
import java.util.List;
31
import java.util.concurrent.CountDownLatch;
32
import java.util.concurrent.TimeUnit;
33
import java.util.function.Predicate;
34
35
import jdk.testlibrary.ProcessTools;
36
37
/**
38
* NOTE:
39
* This test requires at least a setup similar to the following in
40
* /etc/hosts file (or the windows equivalent). I.e. it expects it to
41
* be multi-homed and not both being the loop-back interface.
42
* For example:
43
* ----->8-------- /etc/hosts ----------->8---
44
* 127.0.0.1 localhost
45
* 192.168.0.1 localhost
46
* ----->8-------- /etc/hosts ----------->8---
47
*
48
* @test
49
* @bug 6425769
50
* @summary Test JMX agent host address binding. Same ports but different
51
* interfaces to bind to (using plain sockets and SSL sockets).
52
*
53
* @modules java.management/sun.management
54
* java.management/sun.management.jmxremote
55
* @library /lib/testlibrary
56
* @build jdk.testlibrary.* JMXAgentInterfaceBinding
57
* @run main/timeout=5 JMXInterfaceBindingTest
58
*/
59
public class JMXInterfaceBindingTest {
60
61
public static final int COMMUNICATION_ERROR_EXIT_VAL = 1;
62
public static final int STOP_PROCESS_EXIT_VAL = 137;
63
public static final int JMX_PORT = 9111;
64
public static final int RMI_PORT = 9112;
65
public static final String READY_MSG = "MainThread: Ready for connections";
66
public static final String TEST_CLASS = JMXAgentInterfaceBinding.class.getSimpleName();
67
public static final String KEYSTORE_LOC = System.getProperty("test.src", ".") +
68
File.separator +
69
"ssl" +
70
File.separator +
71
"keystore";
72
public static final String TRUSTSTORE_LOC = System.getProperty("test.src", ".") +
73
File.separator +
74
"ssl" +
75
File.separator +
76
"truststore";
77
public static final String TEST_CLASSPATH = System.getProperty("test.classes", ".");
78
79
public void run(List<InetAddress> addrs) {
80
System.out.println("DEBUG: Running tests with plain sockets.");
81
runTests(addrs, false);
82
System.out.println("DEBUG: Running tests with SSL sockets.");
83
runTests(addrs, true);
84
}
85
86
private void runTests(List<InetAddress> addrs, boolean useSSL) {
87
TestProcessThread[] jvms = new TestProcessThread[addrs.size()];
88
for (int i = 0; i < addrs.size(); i++) {
89
String addr = JMXAgentInterfaceBinding.wrapAddress(addrs.get(i).getHostAddress());
90
System.out.println();
91
String msg = String.format("DEBUG: Launching java tester for triplet (HOSTNAME,JMX_PORT,RMI_PORT) == (%s,%d,%d)",
92
addr,
93
JMX_PORT,
94
RMI_PORT);
95
System.out.println(msg);
96
jvms[i] = runJMXBindingTest(addr, useSSL);
97
jvms[i].start();
98
System.out.println("DEBUG: Started " + (i + 1) + " Process(es).");
99
}
100
int failedProcesses = 0;
101
for (TestProcessThread pt: jvms) {
102
try {
103
pt.stopProcess();
104
pt.join();
105
} catch (InterruptedException e) {
106
System.err.println("Failed to stop process: " + pt.getName());
107
throw new RuntimeException("Test failed", e);
108
}
109
int exitValue = pt.getExitValue();
110
// If there is a communication error (the case we care about)
111
// we get a exit code of 1
112
if (exitValue == COMMUNICATION_ERROR_EXIT_VAL) {
113
// Failure case since the java processes should still be
114
// running.
115
System.err.println("Test FAILURE on " + pt.getName());
116
failedProcesses++;
117
} else if (exitValue == STOP_PROCESS_EXIT_VAL) {
118
System.out.println("DEBUG: OK. Spawned java process terminated with expected exit code of " + STOP_PROCESS_EXIT_VAL);
119
} else {
120
System.err.println("Test FAILURE on " + pt.getName() + " reason: Unexpected exit code => " + exitValue);
121
failedProcesses++;
122
}
123
}
124
if (failedProcesses > 0) {
125
throw new RuntimeException("Test FAILED. " + failedProcesses + " out of " + addrs.size() + " process(es) failed to start the JMX agent.");
126
}
127
}
128
129
private TestProcessThread runJMXBindingTest(String address, boolean useSSL) {
130
List<String> args = new ArrayList<>();
131
args.add("-classpath");
132
args.add(TEST_CLASSPATH);
133
args.add("-Dcom.sun.management.jmxremote.host=" + address);
134
args.add("-Dcom.sun.management.jmxremote.port=" + JMX_PORT);
135
args.add("-Dcom.sun.management.jmxremote.rmi.port=" + RMI_PORT);
136
args.add("-Dcom.sun.management.jmxremote.authenticate=false");
137
args.add("-Dcom.sun.management.jmxremote.ssl=" + Boolean.toString(useSSL));
138
if (useSSL) {
139
args.add("-Dcom.sun.management.jmxremote.registry.ssl=true");
140
args.add("-Djavax.net.ssl.keyStore=" + KEYSTORE_LOC);
141
args.add("-Djavax.net.ssl.trustStore=" + TRUSTSTORE_LOC);
142
args.add("-Djavax.net.ssl.keyStorePassword=password");
143
args.add("-Djavax.net.ssl.trustStorePassword=trustword");
144
}
145
args.add(TEST_CLASS);
146
args.add(address);
147
args.add(Integer.toString(JMX_PORT));
148
args.add(Integer.toString(RMI_PORT));
149
args.add(Boolean.toString(useSSL));
150
try {
151
ProcessBuilder builder = ProcessTools.createJavaProcessBuilder(args.toArray(new String[] {}));
152
System.out.println(ProcessTools.getCommandLine(builder));
153
TestProcessThread jvm = new TestProcessThread("JMX-Tester-" + address, JMXInterfaceBindingTest::isJMXAgentResponseAvailable, builder);
154
return jvm;
155
} catch (Exception e) {
156
throw new RuntimeException("Test failed", e);
157
}
158
159
}
160
161
private static boolean isJMXAgentResponseAvailable(String line) {
162
if (line.equals(READY_MSG)) {
163
System.out.println("DEBUG: Found expected READY_MSG.");
164
return true;
165
} else if (line.startsWith("Error:")) {
166
// Allow for a JVM process that exits with
167
// "Error: JMX connector server communication error: ..."
168
// to continue as well since we handle that case elsewhere.
169
// This has the effect that the test does not timeout and
170
// fails with an exception in the test.
171
System.err.println("PROBLEM: JMX agent of target JVM did not start as it should.");
172
return true;
173
} else {
174
return false;
175
}
176
}
177
178
public static void main(String[] args) {
179
List<InetAddress> addrs = getAddressesForLocalHost();
180
if (addrs.size() < 2) {
181
System.out.println("Ignoring manual test since no more than one IPs are configured for 'localhost'");
182
return;
183
}
184
JMXInterfaceBindingTest test = new JMXInterfaceBindingTest();
185
test.run(addrs);
186
System.out.println("All tests PASSED.");
187
}
188
189
private static List<InetAddress> getAddressesForLocalHost() {
190
try {
191
List<InetAddress> filtered = new ArrayList<>();
192
for (NetworkInterface iface: Collections.list(NetworkInterface.getNetworkInterfaces())) {
193
for (InetAddress addr: Collections.list(iface.getInetAddresses())) {
194
if (isNonloopbackLocalhost(addr)) {
195
filtered.add(addr);
196
}
197
}
198
}
199
return filtered;
200
} catch (SocketException e) {
201
throw new RuntimeException("Test failed", e);
202
}
203
}
204
205
// we need 'real' localhost addresses only (eg. not loopback ones)
206
// so we can bind the remote JMX connector to them
207
private static boolean isNonloopbackLocalhost(InetAddress i) {
208
if (!i.isLoopbackAddress()) {
209
return i.getHostName().toLowerCase().equals("localhost");
210
}
211
return false;
212
}
213
214
private static class TestProcessThread extends Thread {
215
216
private final Predicate<String> predicate;
217
private final ProcessBuilder pb;
218
private final CountDownLatch latch;
219
private Process process;
220
221
public TestProcessThread(String threadName, Predicate<String> predicate, ProcessBuilder pb) {
222
super(threadName);
223
this.predicate = predicate;
224
this.pb = pb;
225
this.latch = new CountDownLatch(1);
226
}
227
228
@Override
229
public void run() {
230
try {
231
process = ProcessTools.startProcess(getName(), pb, predicate, 10, TimeUnit.SECONDS);
232
latch.countDown();
233
process.waitFor();
234
} catch (Exception e) {
235
throw new RuntimeException("Test failed", e);
236
}
237
}
238
239
public void stopProcess() {
240
try {
241
latch.await();
242
} catch (InterruptedException e1) {
243
throw new RuntimeException("Test failed", e1);
244
}
245
if (process != null) {
246
process.destroyForcibly();
247
try {
248
process.waitFor();
249
} catch (InterruptedException e) {
250
throw new RuntimeException("Test failed", e);
251
}
252
}
253
}
254
255
public int getExitValue() {
256
return process.exitValue();
257
}
258
}
259
}
260
261