File: JMXInterfaceBindingTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (209 lines) | stat: -rw-r--r-- 9,179 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (c) 2015, Red Hat Inc
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import jdk.testlibrary.ProcessThread;
import jdk.testlibrary.ProcessTools;

/**
 * NOTE:
 *    This test requires at least a setup similar to the following in
 *    /etc/hosts file (or the windows equivalent). I.e. it expects it to
 *    be multi-homed and not both being the loop-back interface.
 *    For example:
 *    ----->8-------- /etc/hosts ----------->8---
 *    127.0.0.1   localhost
 *    192.168.0.1 localhost
 *    ----->8-------- /etc/hosts ----------->8---
 *
 * @test
 * @bug     6425769
 * @summary Test JMX agent host address binding. Same ports but different
 *          interfaces to bind to (using plain sockets and SSL sockets).
 *
 * @library /lib/testlibrary
 * @modules java.management.rmi
 *
 * @build jdk.testlibrary.* JMXAgentInterfaceBinding
 * @run main/timeout=5 JMXInterfaceBindingTest
 */
public class JMXInterfaceBindingTest {

    public static final int COMMUNICATION_ERROR_EXIT_VAL = 1;
    public static final int STOP_PROCESS_EXIT_VAL = 143;
    public static final int JMX_PORT = 9111;
    public static final int RMI_PORT = 9112;
    public static final String READY_MSG = "MainThread: Ready for connections";
    public static final String TEST_CLASS = JMXAgentInterfaceBinding.class.getSimpleName();
    public static final String KEYSTORE_LOC = System.getProperty("test.src", ".") +
                                              File.separator +
                                              "ssl" +
                                              File.separator +
                                              "keystore";
    public static final String TRUSTSTORE_LOC = System.getProperty("test.src", ".") +
                                                File.separator +
                                                "ssl" +
                                                File.separator +
                                                "truststore";
    public static final String TEST_CLASSPATH = System.getProperty("test.classes", ".");

    public void run(List<InetAddress> addrs) {
        System.out.println("DEBUG: Running tests with plain sockets.");
        runTests(addrs, false);
        System.out.println("DEBUG: Running tests with SSL sockets.");
        runTests(addrs, true);
    }

    private void runTests(List<InetAddress> addrs, boolean useSSL) {
        List<ProcessThread> jvms = new ArrayList<>(addrs.size());
        int i = 1;
        for (InetAddress addr : addrs) {
            String address = JMXAgentInterfaceBinding.wrapAddress(addr.getHostAddress());
            System.out.println();
            String msg = String.format("DEBUG: Launching java tester for triplet (HOSTNAME,JMX_PORT,RMI_PORT) == (%s,%d,%d)",
                    address,
                    JMX_PORT,
                    RMI_PORT);
            System.out.println(msg);
            ProcessThread jvm = runJMXBindingTest(address, useSSL);
            jvms.add(jvm);
            jvm.start();
            System.out.println("DEBUG: Started " + (i++) + " Process(es).");
        }
        int failedProcesses = 0;
        for (ProcessThread pt: jvms) {
            try {
                pt.stopProcess();
                pt.join();
            } catch (InterruptedException e) {
                System.err.println("Failed to stop process: " + pt.getName());
                throw new RuntimeException("Test failed", e);
            }
            int exitValue = pt.getOutput().getExitValue();
            // If there is a communication error (the case we care about)
            // we get a exit code of 1
            if (exitValue == COMMUNICATION_ERROR_EXIT_VAL) {
                // Failure case since the java processes should still be
                // running.
                System.err.println("Test FAILURE on " + pt.getName());
                failedProcesses++;
            } else if (exitValue == STOP_PROCESS_EXIT_VAL) {
                System.out.println("DEBUG: OK. Spawned java process terminated with expected exit code of " + STOP_PROCESS_EXIT_VAL);
            } else {
                System.err.println("Test FAILURE on " + pt.getName() + " reason: Unexpected exit code => " + exitValue);
                failedProcesses++;
            }
        }
        if (failedProcesses > 0) {
            throw new RuntimeException("Test FAILED. " + failedProcesses + " out of " + addrs.size() + " process(es) failed to start the JMX agent.");
        }
    }

    private ProcessThread runJMXBindingTest(String address, boolean useSSL) {
        List<String> args = new ArrayList<>();
        args.add("-classpath");
        args.add(TEST_CLASSPATH);
        args.add("-Dcom.sun.management.jmxremote.host=" + address);
        args.add("-Dcom.sun.management.jmxremote.port=" + JMX_PORT);
        args.add("-Dcom.sun.management.jmxremote.rmi.port=" + RMI_PORT);
        args.add("-Dcom.sun.management.jmxremote.authenticate=false");
        args.add("-Dcom.sun.management.jmxremote.ssl=" + Boolean.toString(useSSL));
        if (useSSL) {
            args.add("-Dcom.sun.management.jmxremote.registry.ssl=true");
            args.add("-Djavax.net.ssl.keyStore=" + KEYSTORE_LOC);
            args.add("-Djavax.net.ssl.trustStore=" + TRUSTSTORE_LOC);
            args.add("-Djavax.net.ssl.keyStorePassword=password");
            args.add("-Djavax.net.ssl.trustStorePassword=trustword");
        }
        args.add(TEST_CLASS);
        args.add(address);
        args.add(Integer.toString(JMX_PORT));
        args.add(Integer.toString(RMI_PORT));
        args.add(Boolean.toString(useSSL));
        try {
            ProcessBuilder builder = ProcessTools.createJavaProcessBuilder(args.toArray(new String[] {}));
            System.out.println(ProcessTools.getCommandLine(builder));
            ProcessThread jvm = new ProcessThread("JMX-Tester-" + address, JMXInterfaceBindingTest::isJMXAgentResponseAvailable, builder);
            return jvm;
        } catch (Exception e) {
            throw new RuntimeException("Test failed", e);
        }

    }

    private static boolean isJMXAgentResponseAvailable(String line) {
        if (line.equals(READY_MSG)) {
            System.out.println("DEBUG: Found expected READY_MSG.");
            return true;
        } else if (line.startsWith("Error:")) {
            // Allow for a JVM process that exits with
            // "Error: JMX connector server communication error: ..."
            // to continue as well since we handle that case elsewhere.
            // This has the effect that the test does not timeout and
            // fails with an exception in the test.
            System.err.println("PROBLEM: JMX agent of target JVM did not start as it should.");
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        List<InetAddress> addrs = getAddressesForLocalHost();
        if (addrs.size() < 2) {
            System.out.println("Ignoring manual test since no more than one IPs are configured for 'localhost'");
            return;
        }
        JMXInterfaceBindingTest test = new JMXInterfaceBindingTest();
        test.run(addrs);
        System.out.println("All tests PASSED.");
    }

    private static List<InetAddress> getAddressesForLocalHost() {

        try {
            return NetworkInterface.networkInterfaces()
                .flatMap(NetworkInterface::inetAddresses)
                .filter(JMXInterfaceBindingTest::isNonloopbackLocalhost)
                .collect(Collectors.toList());
        } catch (SocketException e) {
            throw new RuntimeException("Test failed", e);
        }
    }

    // we need 'real' localhost addresses only (eg. not loopback ones)
    // so we can bind the remote JMX connector to them
    private static boolean isNonloopbackLocalhost(InetAddress i) {
        if (!i.isLoopbackAddress()) {
            return i.getHostName().toLowerCase().equals("localhost");
        }
        return false;
    }
}