Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/jmxremote/bootstrap/JMXInterfaceBindingTest.java
38867 views
/*1* Copyright (c) 2015, Red Hat Inc2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.net.InetAddress;25import java.net.NetworkInterface;26import java.net.SocketException;27import java.util.ArrayList;28import java.util.Collections;29import java.util.List;30import java.util.concurrent.CountDownLatch;31import java.util.concurrent.TimeUnit;32import java.util.function.Predicate;3334import jdk.testlibrary.ProcessTools;3536/**37* NOTE:38* This test requires at least a setup similar to the following in39* /etc/hosts file (or the windows equivalent). I.e. it expects it to40* be multi-homed and not both being the loop-back interface.41* For example:42* ----->8-------- /etc/hosts ----------->8---43* 127.0.0.1 localhost44* 192.168.0.1 localhost45* ----->8-------- /etc/hosts ----------->8---46*47* @test48* @bug 642576949* @summary Test JMX agent host address binding. Same ports but different50* interfaces to bind to (using plain sockets and SSL sockets).51*52* @modules java.management/sun.management53* java.management/sun.management.jmxremote54* @library /lib/testlibrary55* @build jdk.testlibrary.* JMXAgentInterfaceBinding56* @run main/timeout=5 JMXInterfaceBindingTest57*/58public class JMXInterfaceBindingTest {5960public static final int COMMUNICATION_ERROR_EXIT_VAL = 1;61public static final int STOP_PROCESS_EXIT_VAL = 137;62public static final int JMX_PORT = 9111;63public static final int RMI_PORT = 9112;64public static final String READY_MSG = "MainThread: Ready for connections";65public static final String TEST_CLASS = JMXAgentInterfaceBinding.class.getSimpleName();66public static final String KEYSTORE_LOC = System.getProperty("test.src", ".") +67File.separator +68"ssl" +69File.separator +70"keystore";71public static final String TRUSTSTORE_LOC = System.getProperty("test.src", ".") +72File.separator +73"ssl" +74File.separator +75"truststore";76public static final String TEST_CLASSPATH = System.getProperty("test.classes", ".");7778public void run(List<InetAddress> addrs) {79System.out.println("DEBUG: Running tests with plain sockets.");80runTests(addrs, false);81System.out.println("DEBUG: Running tests with SSL sockets.");82runTests(addrs, true);83}8485private void runTests(List<InetAddress> addrs, boolean useSSL) {86TestProcessThread[] jvms = new TestProcessThread[addrs.size()];87for (int i = 0; i < addrs.size(); i++) {88String addr = JMXAgentInterfaceBinding.wrapAddress(addrs.get(i).getHostAddress());89System.out.println();90String msg = String.format("DEBUG: Launching java tester for triplet (HOSTNAME,JMX_PORT,RMI_PORT) == (%s,%d,%d)",91addr,92JMX_PORT,93RMI_PORT);94System.out.println(msg);95jvms[i] = runJMXBindingTest(addr, useSSL);96jvms[i].start();97System.out.println("DEBUG: Started " + (i + 1) + " Process(es).");98}99int failedProcesses = 0;100for (TestProcessThread pt: jvms) {101try {102pt.stopProcess();103pt.join();104} catch (InterruptedException e) {105System.err.println("Failed to stop process: " + pt.getName());106throw new RuntimeException("Test failed", e);107}108int exitValue = pt.getExitValue();109// If there is a communication error (the case we care about)110// we get a exit code of 1111if (exitValue == COMMUNICATION_ERROR_EXIT_VAL) {112// Failure case since the java processes should still be113// running.114System.err.println("Test FAILURE on " + pt.getName());115failedProcesses++;116} else if (exitValue == STOP_PROCESS_EXIT_VAL) {117System.out.println("DEBUG: OK. Spawned java process terminated with expected exit code of " + STOP_PROCESS_EXIT_VAL);118} else {119System.err.println("Test FAILURE on " + pt.getName() + " reason: Unexpected exit code => " + exitValue);120failedProcesses++;121}122}123if (failedProcesses > 0) {124throw new RuntimeException("Test FAILED. " + failedProcesses + " out of " + addrs.size() + " process(es) failed to start the JMX agent.");125}126}127128private TestProcessThread runJMXBindingTest(String address, boolean useSSL) {129List<String> args = new ArrayList<>();130args.add("-classpath");131args.add(TEST_CLASSPATH);132args.add("-Dcom.sun.management.jmxremote.host=" + address);133args.add("-Dcom.sun.management.jmxremote.port=" + JMX_PORT);134args.add("-Dcom.sun.management.jmxremote.rmi.port=" + RMI_PORT);135args.add("-Dcom.sun.management.jmxremote.authenticate=false");136args.add("-Dcom.sun.management.jmxremote.ssl=" + Boolean.toString(useSSL));137if (useSSL) {138args.add("-Dcom.sun.management.jmxremote.registry.ssl=true");139args.add("-Djavax.net.ssl.keyStore=" + KEYSTORE_LOC);140args.add("-Djavax.net.ssl.trustStore=" + TRUSTSTORE_LOC);141args.add("-Djavax.net.ssl.keyStorePassword=password");142args.add("-Djavax.net.ssl.trustStorePassword=trustword");143}144args.add(TEST_CLASS);145args.add(address);146args.add(Integer.toString(JMX_PORT));147args.add(Integer.toString(RMI_PORT));148args.add(Boolean.toString(useSSL));149try {150ProcessBuilder builder = ProcessTools.createJavaProcessBuilder(args.toArray(new String[] {}));151System.out.println(ProcessTools.getCommandLine(builder));152TestProcessThread jvm = new TestProcessThread("JMX-Tester-" + address, JMXInterfaceBindingTest::isJMXAgentResponseAvailable, builder);153return jvm;154} catch (Exception e) {155throw new RuntimeException("Test failed", e);156}157158}159160private static boolean isJMXAgentResponseAvailable(String line) {161if (line.equals(READY_MSG)) {162System.out.println("DEBUG: Found expected READY_MSG.");163return true;164} else if (line.startsWith("Error:")) {165// Allow for a JVM process that exits with166// "Error: JMX connector server communication error: ..."167// to continue as well since we handle that case elsewhere.168// This has the effect that the test does not timeout and169// fails with an exception in the test.170System.err.println("PROBLEM: JMX agent of target JVM did not start as it should.");171return true;172} else {173return false;174}175}176177public static void main(String[] args) {178List<InetAddress> addrs = getAddressesForLocalHost();179if (addrs.size() < 2) {180System.out.println("Ignoring manual test since no more than one IPs are configured for 'localhost'");181return;182}183JMXInterfaceBindingTest test = new JMXInterfaceBindingTest();184test.run(addrs);185System.out.println("All tests PASSED.");186}187188private static List<InetAddress> getAddressesForLocalHost() {189try {190List<InetAddress> filtered = new ArrayList<>();191for (NetworkInterface iface: Collections.list(NetworkInterface.getNetworkInterfaces())) {192for (InetAddress addr: Collections.list(iface.getInetAddresses())) {193if (isNonloopbackLocalhost(addr)) {194filtered.add(addr);195}196}197}198return filtered;199} catch (SocketException e) {200throw new RuntimeException("Test failed", e);201}202}203204// we need 'real' localhost addresses only (eg. not loopback ones)205// so we can bind the remote JMX connector to them206private static boolean isNonloopbackLocalhost(InetAddress i) {207if (!i.isLoopbackAddress()) {208return i.getHostName().toLowerCase().equals("localhost");209}210return false;211}212213private static class TestProcessThread extends Thread {214215private final Predicate<String> predicate;216private final ProcessBuilder pb;217private final CountDownLatch latch;218private Process process;219220public TestProcessThread(String threadName, Predicate<String> predicate, ProcessBuilder pb) {221super(threadName);222this.predicate = predicate;223this.pb = pb;224this.latch = new CountDownLatch(1);225}226227@Override228public void run() {229try {230process = ProcessTools.startProcess(getName(), pb, predicate, 10, TimeUnit.SECONDS);231latch.countDown();232process.waitFor();233} catch (Exception e) {234throw new RuntimeException("Test failed", e);235}236}237238public void stopProcess() {239try {240latch.await();241} catch (InterruptedException e1) {242throw new RuntimeException("Test failed", e1);243}244if (process != null) {245process.destroyForcibly();246try {247process.waitFor();248} catch (InterruptedException e) {249throw new RuntimeException("Test failed", e);250}251}252}253254public int getExitValue() {255return process.exitValue();256}257}258}259260261