Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/ConnectionTest.java
41159 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* 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*/2223/*24* @test25* @bug 486539726* @summary Tests remote JMX connections27* @author Eamonn McManus28*29* @run clean ConnectionTest30* @run build ConnectionTest31* @run main ConnectionTest32*/3334import java.io.IOException;35import java.net.MalformedURLException;36import java.util.Collections;37import java.util.HashMap;38import java.util.HashSet;39import java.util.Iterator;40import java.util.LinkedList;41import java.util.List;42import java.util.Map;43import java.util.Set;44import java.util.StringTokenizer;4546import java.security.Principal;47import java.util.regex.Pattern;48import javax.security.auth.Subject;4950import javax.management.MBeanServer;51import javax.management.MBeanServerConnection;52import javax.management.MBeanServerFactory;53import javax.management.Notification;54import javax.management.NotificationListener;55import javax.management.ObjectName;5657import javax.management.remote.JMXAuthenticator;58import javax.management.remote.JMXConnectionNotification;59import javax.management.remote.JMXConnector;60import javax.management.remote.JMXConnectorFactory;61import javax.management.remote.JMXConnectorServer;62import javax.management.remote.JMXConnectorServerFactory;63import javax.management.remote.JMXPrincipal;64import javax.management.remote.JMXServiceURL;6566public class ConnectionTest {6768public static void main(String[] args) {69// System.setProperty("java.util.logging.config.file",70// "../../../../logging.properties");71// // we are in <workspace>/build/test/JTwork/scratch72// java.util.logging.LogManager.getLogManager().readConfiguration();73boolean ok = true;74String[] protocols = {"rmi", "iiop", "jmxmp"};75if (args.length > 0)76protocols = args;77for (int i = 0; i < protocols.length; i++) {78final String proto = protocols[i];79System.out.println("Testing for protocol " + proto);80try {81ok &= test(proto);82} catch (Exception e) {83System.err.println("Unexpected exception: " + e);84e.printStackTrace();85ok = false;86}87}8889if (ok)90System.out.println("Test passed");91else {92System.out.println("TEST FAILED");93System.exit(1);94}95}9697private static boolean test(String proto) throws Exception {98ObjectName serverName = ObjectName.getInstance("d:type=server");99MBeanServer mbs = MBeanServerFactory.newMBeanServer();100JMXAuthenticator authenticator = new BogusAuthenticator();101Map env = Collections.singletonMap("jmx.remote.authenticator",102authenticator);103JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + "://");104JMXConnectorServer server;105try {106server =107JMXConnectorServerFactory.newJMXConnectorServer(url, env,108null);109} catch (MalformedURLException e) {110System.out.println("Protocol " + proto +111" not supported, ignoring");112return true;113}114System.out.println("Created connector server");115mbs.registerMBean(server, serverName);116System.out.println("Registered connector server in MBean server");117mbs.addNotificationListener(serverName, logListener, null, null);118mbs.invoke(serverName, "start", null, null);119System.out.println("Started connector server");120JMXServiceURL address =121(JMXServiceURL) mbs.getAttribute(serverName, "Address");122System.out.println("Retrieved address: " + address);123124if (address.getHost().length() == 0) {125System.out.println("Generated address has empty hostname");126return false;127}128129JMXConnector client = JMXConnectorFactory.connect(address);130System.out.println("Client connected");131132String clientConnId = client.getConnectionId();133System.out.println("Got connection ID on client: " + clientConnId);134boolean ok = checkConnectionId(proto, clientConnId);135if (!ok)136return false;137System.out.println("Connection ID is OK");138139// 4901826: connection ids need some time to be updated using jmxmp140// we don't get the notif immediately either141// this was originally timeout 1ms, which was not enough142Notification notif = waitForNotification(1000);143System.out.println("Server got notification: " + notif);144145ok = mustBeConnectionNotification(notif, clientConnId,146JMXConnectionNotification.OPENED);147if (!ok)148return false;149150client.close();151System.out.println("Closed client");152153notif = waitForNotification(1000);154System.out.println("Got notification: " + notif);155156ok = mustBeConnectionNotification(notif, clientConnId,157JMXConnectionNotification.CLOSED);158if (!ok)159return false;160161client = JMXConnectorFactory.connect(address);162System.out.println("Second client connected");163164String clientConnId2 = client.getConnectionId();165if (clientConnId.equals(clientConnId2)) {166System.out.println("Same connection ID for two connections: " +167clientConnId2);168return false;169}170System.out.println("Second client connection ID is different");171172notif = waitForNotification(1);173ok = mustBeConnectionNotification(notif, clientConnId2,174JMXConnectionNotification.OPENED);175if (!ok)176return false;177178MBeanServerConnection mbsc = client.getMBeanServerConnection();179Map attrs = (Map) mbsc.getAttribute(serverName, "Attributes");180System.out.println("Server attributes received by client: " + attrs);181182server.stop();183System.out.println("Server stopped");184185notif = waitForNotification(1000);186System.out.println("Server got connection-closed notification: " +187notif);188189ok = mustBeConnectionNotification(notif, clientConnId2,190JMXConnectionNotification.CLOSED);191if (!ok)192return false;193194try {195mbsc.getDefaultDomain();196System.out.println("Connection still working but should not be");197return false;198} catch (IOException e) {199System.out.println("Connection correctly got exception: " + e);200}201202try {203client = JMXConnectorFactory.connect(address);204System.out.println("Connector server still working but should " +205"not be");206return false;207} catch (IOException e) {208System.out.println("New connection correctly got exception: " + e);209}210211return true;212}213214private static boolean215mustBeConnectionNotification(Notification notif,216String requiredConnId,217String requiredType) {218219if (!(notif instanceof JMXConnectionNotification)) {220System.out.println("Should have been a " +221"JMXConnectionNotification: " +222notif.getClass());223return false;224}225226JMXConnectionNotification cnotif = (JMXConnectionNotification) notif;227if (!cnotif.getType().equals(requiredType)) {228System.out.println("Wrong type notif: is \"" + cnotif.getType() +229"\", should be \"" + requiredType + "\"");230return false;231}232233if (!cnotif.getConnectionId().equals(requiredConnId)) {234System.out.println("Wrong connection id: is \"" +235cnotif.getConnectionId() + "\", should be \"" +236requiredConnId);237return false;238}239240return true;241}242243private static final String IPV4_PTN = "^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}(\\:[1-9][0-9]{3})?$";244245/**246* Checks the connection id for validity.247* The {@link248* javax.management.remote package description} describes the249* conventions for connection IDs.250* @param proto Connection protocol251* @param clientConnId The connection ID252* @return Returns {@code true} if the connection id conforms to the specification; {@code false} otherwise.253* @throws Exception254*/255private static boolean checkConnectionId(String proto, String clientConnId)256throws Exception {257StringTokenizer tok = new StringTokenizer(clientConnId, " ", true);258String s;259s = tok.nextToken();260if (!s.startsWith(proto + ":")) {261System.out.println("Expected \"" + proto + ":\", found \"" + s +262"\"");263return false;264}265266int hostAddrInd = s.indexOf("//");267if (hostAddrInd > -1) {268s = s.substring(hostAddrInd + 2);269if (!Pattern.matches(IPV4_PTN, s)) {270if (!s.startsWith("[") || !s.endsWith("]")) {271System.out.println("IPv6 address must be enclosed in \"[]\"");272return false;273}274}275}276s = tok.nextToken();277if (!s.equals(" ")) {278System.out.println("Expected \" \", found \"" + s + "\"");279return false;280}281s = tok.nextToken();282StringTokenizer tok2 = new StringTokenizer(s, ";", true);283Set principalNames = new HashSet();284String s2;285s2 = tok2.nextToken();286if (s2.equals(";")) {287System.out.println("In identity \"" + s +288"\", expected name, found \";\"");289return false;290}291principalNames.add(s2);292s2 = tok2.nextToken();293if (!s2.equals(";"))294throw new Exception("Can't happen");295s2 = tok2.nextToken();296if (s2.equals(";")) {297System.out.println("In identity \"" + s +298"\", expected name, found \";\"");299return false;300}301principalNames.add(s2);302if (tok2.hasMoreTokens()) {303System.out.println("In identity \"" + s + "\", too many tokens");304return false;305}306if (principalNames.size() != bogusPrincipals.size()) {307System.out.println("Wrong number of principal names: " +308principalNames.size() + " != " +309bogusPrincipals.size());310return false;311}312for (Iterator it = bogusPrincipals.iterator(); it.hasNext(); ) {313Principal p = (Principal) it.next();314if (!principalNames.contains(p.getName())) {315System.out.println("Principal names don't contain \"" +316p.getName() + "\"");317return false;318}319}320s = tok.nextToken();321if (!s.equals(" ")) {322System.out.println("Expected \" \", found \"" + s + "\"");323return false;324}325return true;326}327328private static Notification waitForNotification(long timeout)329throws InterruptedException {330synchronized (log) {331if (log.isEmpty()) {332long remainingTime = timeout;333final long startTime = System.currentTimeMillis();334335while (log.isEmpty() && remainingTime >0) {336log.wait(remainingTime);337remainingTime = timeout - (System.currentTimeMillis() - startTime);338}339340if (log.isEmpty()) {341throw new InterruptedException("Timed out waiting for " +342"notification!");343}344}345return (Notification) log.remove(0);346}347}348349private static class LogListener implements NotificationListener {350LogListener(List log) {351this.log = log;352}353354public void handleNotification(Notification n, Object h) {355synchronized (log) {356log.add(n);357log.notifyAll();358}359}360361private final List log;362}363364private static List log = new LinkedList();365private static NotificationListener logListener = new LogListener(log);366367private static class BogusAuthenticator implements JMXAuthenticator {368public Subject authenticate(Object credentials) {369Subject subject =370new Subject(true, bogusPrincipals,371Collections.EMPTY_SET, Collections.EMPTY_SET);372System.out.println("Authenticator returns: " + subject);373return subject;374}375}376377private static final Set bogusPrincipals = new HashSet();378static {379bogusPrincipals.add(new JMXPrincipal("foo"));380bogusPrincipals.add(new JMXPrincipal("bar"));381}382}383384385