5070N/A/*
5070N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
5070N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5070N/A *
5070N/A * This code is free software; you can redistribute it and/or modify it
5070N/A * under the terms of the GNU General Public License version 2 only, as
5070N/A * published by the Free Software Foundation.
5070N/A *
5070N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5070N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5070N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5070N/A * version 2 for more details (a copy is included in the LICENSE file that
5070N/A * accompanied this code).
5070N/A *
5070N/A * You should have received a copy of the GNU General Public License version
5070N/A * 2 along with this work; if not, write to the Free Software Foundation,
5070N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5070N/A *
5070N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5070N/A * or visit www.oracle.com if you need additional information or have any
5070N/A * questions.
5070N/A */
5070N/A
5070N/A/*
5070N/A * @test
5070N/A * @bug 5106721
5070N/A * @summary Check the emission of notifications when a Security Manager is
5070N/A * installed. Test the property "jmx.remote.x.check.notification.emission".
5070N/A * @author Luis-Miguel Alventosa
5070N/A * @run clean NotificationEmissionTest
5070N/A * @run build NotificationEmissionTest
5070N/A * @run main NotificationEmissionTest 1
5070N/A * @run main NotificationEmissionTest 2
5070N/A * @run main NotificationEmissionTest 3
5070N/A * @run main NotificationEmissionTest 4
5070N/A * @run main NotificationEmissionTest 5
5070N/A */
5070N/A
5070N/Aimport java.io.File;
5070N/Aimport java.util.ArrayList;
5070N/Aimport java.util.Collections;
5070N/Aimport java.util.HashMap;
5070N/Aimport java.util.List;
5070N/Aimport java.util.Map;
5070N/Aimport javax.management.MBeanServer;
5070N/Aimport javax.management.MBeanServerConnection;
5070N/Aimport javax.management.MBeanServerFactory;
5070N/Aimport javax.management.Notification;
5070N/Aimport javax.management.NotificationBroadcasterSupport;
5070N/Aimport javax.management.NotificationListener;
5070N/Aimport javax.management.ObjectName;
5070N/Aimport javax.management.remote.JMXAuthenticator;
5070N/Aimport javax.management.remote.JMXConnector;
5070N/Aimport javax.management.remote.JMXConnectorFactory;
5070N/Aimport javax.management.remote.JMXConnectorServer;
5070N/Aimport javax.management.remote.JMXConnectorServerFactory;
5070N/Aimport javax.management.remote.JMXPrincipal;
5070N/Aimport javax.management.remote.JMXServiceURL;
5070N/Aimport javax.security.auth.Subject;
5070N/A
5070N/Apublic class NotificationEmissionTest {
5070N/A
5070N/A public class CustomJMXAuthenticator implements JMXAuthenticator {
5070N/A public Subject authenticate(Object credentials) {
5070N/A String role = ((String[]) credentials)[0];
5070N/A echo("Create principal with name = " + role);
5070N/A return new Subject(true,
5070N/A Collections.singleton(new JMXPrincipal(role)),
5070N/A Collections.EMPTY_SET,
5070N/A Collections.EMPTY_SET);
5070N/A }
5070N/A }
5070N/A
5070N/A public interface NBMBean {
5070N/A public void emitNotification(int seqnum, ObjectName name);
5070N/A }
5070N/A
5070N/A public static class NB
5070N/A extends NotificationBroadcasterSupport
5070N/A implements NBMBean {
5070N/A public void emitNotification(int seqnum, ObjectName name) {
5070N/A if (name == null) {
5070N/A sendNotification(new Notification("nb", this, seqnum));
5070N/A } else {
5070N/A sendNotification(new Notification("nb", name, seqnum));
5070N/A }
5070N/A }
5070N/A }
5070N/A
5070N/A public class Listener implements NotificationListener {
5070N/A public List<Notification> notifs = new ArrayList<Notification>();
5070N/A public void handleNotification(Notification n, Object h) {
5070N/A echo("handleNotification:");
5070N/A echo("\tNotification = " + n);
5070N/A echo("\tNotification.SeqNum = " + n.getSequenceNumber());
5070N/A echo("\tHandback = " + h);
5070N/A notifs.add(n);
5070N/A }
5070N/A }
5070N/A
5070N/A public int checkNotifs(int size,
5070N/A List<Notification> received,
5070N/A List<ObjectName> expected) {
5070N/A if (received.size() != size) {
5070N/A echo("Error: expecting " + size + " notifications, got " +
5070N/A received.size());
5070N/A return 1;
5070N/A } else {
5070N/A for (Notification n : received) {
5070N/A echo("Received notification: " + n);
5070N/A if (!n.getType().equals("nb")) {
5070N/A echo("Notification type must be \"nb\"");
5070N/A return 1;
5070N/A }
5070N/A ObjectName o = (ObjectName) n.getSource();
5070N/A int index = (int) n.getSequenceNumber();
5070N/A ObjectName nb = expected.get(index);
5070N/A if (!o.equals(nb)) {
5070N/A echo("Notification source must be " + nb);
5070N/A return 1;
5070N/A }
5070N/A }
5070N/A }
5070N/A return 0;
5070N/A }
5070N/A
5070N/A public int runTest(int testcase) throws Exception {
5070N/A echo("\n=-=-= Running testcase " + testcase + " =-=-=");
5070N/A switch (testcase) {
5070N/A case 1:
5070N/A return testNotificationEmissionProperty();
5070N/A case 2:
5070N/A return testNotificationEmissionPositive(false);
5070N/A case 3:
5070N/A return testNotificationEmissionNegative(false);
5070N/A case 4:
5070N/A return testNotificationEmissionPositive(true);
5070N/A case 5:
5070N/A return testNotificationEmissionNegative(true);
5070N/A default:
5070N/A echo("Invalid testcase");
5070N/A return 1;
5070N/A }
5070N/A }
5070N/A
5070N/A public int testNotificationEmissionProperty(boolean exception,
5070N/A Object propValue)
5070N/A throws Exception {
5070N/A try {
5070N/A testNotificationEmission(propValue);
5070N/A if (exception) {
5070N/A echo("Did not get expected exception for value: " + propValue);
5070N/A return 1;
5070N/A } else {
5070N/A echo("Property has been correctly set to value: " + propValue);
5070N/A }
5070N/A } catch (Exception e) {
5070N/A if (exception) {
5070N/A echo("Got expected exception for value: " + propValue);
5070N/A echo("Exception: " + e);
5070N/A } else {
5070N/A echo("Got unexpected exception for value: " + propValue);
5070N/A echo("Exception: " + e);
5070N/A return 1;
5070N/A }
5070N/A }
5070N/A return 0;
5070N/A }
5070N/A
5070N/A public int testNotificationEmissionProperty() throws Exception {
5070N/A int error = 0;
5070N/A error += testNotificationEmissionProperty(true, new Boolean(false));
5070N/A error += testNotificationEmissionProperty(true, new Boolean(true));
5070N/A error += testNotificationEmissionProperty(true, "dummy");
5070N/A error += testNotificationEmissionProperty(false, "false");
5070N/A error += testNotificationEmissionProperty(false, "true");
5070N/A error += testNotificationEmissionProperty(false, "FALSE");
5070N/A error += testNotificationEmissionProperty(false, "TRUE");
5070N/A return error;
5070N/A }
5070N/A
5070N/A public int testNotificationEmissionPositive(boolean prop) throws Exception {
5070N/A return testNotificationEmission(prop, "true", true, true);
5070N/A }
5070N/A
5070N/A public int testNotificationEmissionNegative(boolean prop) throws Exception {
5070N/A return testNotificationEmission(prop, "true", true, false);
5070N/A }
5070N/A
5070N/A public int testNotificationEmission(Object propValue) throws Exception {
5070N/A return testNotificationEmission(true, propValue, false, true);
5070N/A }
5070N/A
5070N/A public int testNotificationEmission(boolean prop,
5070N/A Object propValue,
5070N/A boolean sm,
5070N/A boolean policyPositive)
5070N/A throws Exception {
5070N/A
5070N/A JMXConnectorServer server = null;
5070N/A JMXConnector client = null;
5070N/A
5070N/A // Set policy file
5070N/A //
5070N/A String policyFile =
5070N/A System.getProperty("test.src") + File.separator +
5070N/A (policyPositive ? "policy.positive" : "policy.negative");
5070N/A echo("\nSetting policy file " + policyFile);
5070N/A System.setProperty("java.security.policy", policyFile);
5070N/A
5070N/A // Create a new MBeanServer
5070N/A //
5070N/A final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
5070N/A
5070N/A try {
5070N/A // Create server environment map
5070N/A //
5070N/A final Map<String,Object> env = new HashMap<String,Object>();
5070N/A env.put("jmx.remote.authenticator", new CustomJMXAuthenticator());
5070N/A if (prop)
5070N/A env.put("jmx.remote.x.check.notification.emission", propValue);
5070N/A
5070N/A // Create the JMXServiceURL
5070N/A //
5070N/A final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
5070N/A
5070N/A // Create a JMXConnectorServer
5070N/A //
5070N/A server = JMXConnectorServerFactory.newJMXConnectorServer(url,
5070N/A env,
5070N/A mbs);
5070N/A
5070N/A // Start the JMXConnectorServer
5070N/A //
5070N/A server.start();
5070N/A
// Create server environment map
//
final Map<String,Object> cenv = new HashMap<String,Object>();
String[] credentials = new String[] { "role" , "password" };
cenv.put("jmx.remote.credentials", credentials);
// Create JMXConnector and connect to JMXConnectorServer
//
client = JMXConnectorFactory.connect(server.getAddress(), cenv);
// Get non-secure MBeanServerConnection
//
final MBeanServerConnection mbsc =
client.getMBeanServerConnection();
// Create NB MBean
//
ObjectName nb1 = ObjectName.getInstance("domain:type=NB,name=1");
ObjectName nb2 = ObjectName.getInstance("domain:type=NB,name=2");
ObjectName nb3 = ObjectName.getInstance("domain:type=NB,name=3");
mbsc.createMBean(NB.class.getName(), nb1);
mbsc.createMBean(NB.class.getName(), nb2);
mbsc.createMBean(NB.class.getName(), nb3);
// Add notification listener
//
Listener li = new Listener();
mbsc.addNotificationListener(nb1, li, null, null);
mbsc.addNotificationListener(nb2, li, null, null);
// Set security manager
//
if (sm) {
echo("Setting SM");
System.setSecurityManager(new SecurityManager());
}
// Invoke the "sendNotification" method
//
mbsc.invoke(nb1, "emitNotification",
new Object[] {0, null},
new String[] {"int", "javax.management.ObjectName"});
mbsc.invoke(nb2, "emitNotification",
new Object[] {1, null},
new String[] {"int", "javax.management.ObjectName"});
mbsc.invoke(nb2, "emitNotification",
new Object[] {2, nb3},
new String[] {"int", "javax.management.ObjectName"});
// If the check is effective and we're using policy.negative,
// then we should see the two notifs sent by nb2 (of which one
// has a getSource() that is nb3), but not the notif sent by nb1.
// Otherwise we should see all three notifs. The check is only
// effective if the property jmx.remote.x.check.notification.emission
// is explicitly true and there is a security manager.
int expectedNotifs =
(prop && sm && !policyPositive) ? 2 : 3;
// Wait for notifications to be emitted
//
long deadline = System.currentTimeMillis() + 2000;
while (li.notifs.size() < expectedNotifs &&
System.currentTimeMillis() < deadline)
Thread.sleep(1);
// Remove notification listener
//
mbsc.removeNotificationListener(nb1, li);
mbsc.removeNotificationListener(nb2, li);
int result = 0;
List<ObjectName> sources = new ArrayList<ObjectName>();
sources.add(nb1);
sources.add(nb2);
sources.add(nb3);
result = checkNotifs(expectedNotifs, li.notifs, sources);
if (result > 0) {
echo("...SecurityManager=" + sm + "; policy=" + policyPositive);
return result;
}
} finally {
// Close the connection
//
if (client != null)
client.close();
// Stop the connector server
//
if (server != null)
server.stop();
// Release the MBeanServer
//
if (mbs != null)
MBeanServerFactory.releaseMBeanServer(mbs);
}
return 0;
}
private static void echo(String message) {
System.out.println(message);
}
public static void main(String[] args) throws Exception {
echo("\n--- Check the emission of notifications " +
"when a Security Manager is installed");
NotificationEmissionTest net = new NotificationEmissionTest();
int error = 0;
error += net.runTest(Integer.parseInt(args[0]));
if (error > 0) {
final String msg = "\nTest FAILED! Got " + error + " error(s)";
echo(msg);
throw new IllegalArgumentException(msg);
} else {
echo("\nTest PASSED!");
}
}
}