Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/notif/NotificationEmissionTest.java
38867 views
/*1* Copyright (c) 2005, 2008, 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 510672126* @summary Check the emission of notifications when a Security Manager is27* installed. Test the property "jmx.remote.x.check.notification.emission".28* @author Luis-Miguel Alventosa29* @run clean NotificationEmissionTest30* @run build NotificationEmissionTest31* @run main NotificationEmissionTest 132* @run main NotificationEmissionTest 233* @run main NotificationEmissionTest 334* @run main NotificationEmissionTest 435* @run main NotificationEmissionTest 536*/3738import java.io.File;39import java.util.ArrayList;40import java.util.Collections;41import java.util.HashMap;42import java.util.List;43import java.util.Map;44import javax.management.MBeanServer;45import javax.management.MBeanServerConnection;46import javax.management.MBeanServerFactory;47import javax.management.Notification;48import javax.management.NotificationBroadcasterSupport;49import javax.management.NotificationListener;50import javax.management.ObjectName;51import javax.management.remote.JMXAuthenticator;52import javax.management.remote.JMXConnector;53import javax.management.remote.JMXConnectorFactory;54import javax.management.remote.JMXConnectorServer;55import javax.management.remote.JMXConnectorServerFactory;56import javax.management.remote.JMXPrincipal;57import javax.management.remote.JMXServiceURL;58import javax.security.auth.Subject;5960public class NotificationEmissionTest {6162public class CustomJMXAuthenticator implements JMXAuthenticator {63public Subject authenticate(Object credentials) {64String role = ((String[]) credentials)[0];65echo("Create principal with name = " + role);66return new Subject(true,67Collections.singleton(new JMXPrincipal(role)),68Collections.EMPTY_SET,69Collections.EMPTY_SET);70}71}7273public interface NBMBean {74public void emitNotification(int seqnum, ObjectName name);75}7677public static class NB78extends NotificationBroadcasterSupport79implements NBMBean {80public void emitNotification(int seqnum, ObjectName name) {81if (name == null) {82sendNotification(new Notification("nb", this, seqnum));83} else {84sendNotification(new Notification("nb", name, seqnum));85}86}87}8889public class Listener implements NotificationListener {90public List<Notification> notifs = new ArrayList<Notification>();91public void handleNotification(Notification n, Object h) {92echo("handleNotification:");93echo("\tNotification = " + n);94echo("\tNotification.SeqNum = " + n.getSequenceNumber());95echo("\tHandback = " + h);96notifs.add(n);97}98}99100public int checkNotifs(int size,101List<Notification> received,102List<ObjectName> expected) {103if (received.size() != size) {104echo("Error: expecting " + size + " notifications, got " +105received.size());106return 1;107} else {108for (Notification n : received) {109echo("Received notification: " + n);110if (!n.getType().equals("nb")) {111echo("Notification type must be \"nb\"");112return 1;113}114ObjectName o = (ObjectName) n.getSource();115int index = (int) n.getSequenceNumber();116ObjectName nb = expected.get(index);117if (!o.equals(nb)) {118echo("Notification source must be " + nb);119return 1;120}121}122}123return 0;124}125126public int runTest(int testcase) throws Exception {127echo("\n=-=-= Running testcase " + testcase + " =-=-=");128switch (testcase) {129case 1:130return testNotificationEmissionProperty();131case 2:132return testNotificationEmissionPositive(false);133case 3:134return testNotificationEmissionNegative(false);135case 4:136return testNotificationEmissionPositive(true);137case 5:138return testNotificationEmissionNegative(true);139default:140echo("Invalid testcase");141return 1;142}143}144145public int testNotificationEmissionProperty(boolean exception,146Object propValue)147throws Exception {148try {149testNotificationEmission(propValue);150if (exception) {151echo("Did not get expected exception for value: " + propValue);152return 1;153} else {154echo("Property has been correctly set to value: " + propValue);155}156} catch (Exception e) {157if (exception) {158echo("Got expected exception for value: " + propValue);159echo("Exception: " + e);160} else {161echo("Got unexpected exception for value: " + propValue);162echo("Exception: " + e);163return 1;164}165}166return 0;167}168169public int testNotificationEmissionProperty() throws Exception {170int error = 0;171error += testNotificationEmissionProperty(true, new Boolean(false));172error += testNotificationEmissionProperty(true, new Boolean(true));173error += testNotificationEmissionProperty(true, "dummy");174error += testNotificationEmissionProperty(false, "false");175error += testNotificationEmissionProperty(false, "true");176error += testNotificationEmissionProperty(false, "FALSE");177error += testNotificationEmissionProperty(false, "TRUE");178return error;179}180181public int testNotificationEmissionPositive(boolean prop) throws Exception {182return testNotificationEmission(prop, "true", true, true);183}184185public int testNotificationEmissionNegative(boolean prop) throws Exception {186return testNotificationEmission(prop, "true", true, false);187}188189public int testNotificationEmission(Object propValue) throws Exception {190return testNotificationEmission(true, propValue, false, true);191}192193public int testNotificationEmission(boolean prop,194Object propValue,195boolean sm,196boolean policyPositive)197throws Exception {198199JMXConnectorServer server = null;200JMXConnector client = null;201202// Set policy file203//204String policyFile =205System.getProperty("test.src") + File.separator +206(policyPositive ? "policy.positive" : "policy.negative");207echo("\nSetting policy file " + policyFile);208System.setProperty("java.security.policy", policyFile);209210// Create a new MBeanServer211//212final MBeanServer mbs = MBeanServerFactory.createMBeanServer();213214try {215// Create server environment map216//217final Map<String,Object> env = new HashMap<String,Object>();218env.put("jmx.remote.authenticator", new CustomJMXAuthenticator());219if (prop)220env.put("jmx.remote.x.check.notification.emission", propValue);221222// Create the JMXServiceURL223//224final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");225226// Create a JMXConnectorServer227//228server = JMXConnectorServerFactory.newJMXConnectorServer(url,229env,230mbs);231232// Start the JMXConnectorServer233//234server.start();235236// Create server environment map237//238final Map<String,Object> cenv = new HashMap<String,Object>();239String[] credentials = new String[] { "role" , "password" };240cenv.put("jmx.remote.credentials", credentials);241242// Create JMXConnector and connect to JMXConnectorServer243//244client = JMXConnectorFactory.connect(server.getAddress(), cenv);245246// Get non-secure MBeanServerConnection247//248final MBeanServerConnection mbsc =249client.getMBeanServerConnection();250251// Create NB MBean252//253ObjectName nb1 = ObjectName.getInstance("domain:type=NB,name=1");254ObjectName nb2 = ObjectName.getInstance("domain:type=NB,name=2");255ObjectName nb3 = ObjectName.getInstance("domain:type=NB,name=3");256mbsc.createMBean(NB.class.getName(), nb1);257mbsc.createMBean(NB.class.getName(), nb2);258mbsc.createMBean(NB.class.getName(), nb3);259260// Add notification listener261//262Listener li = new Listener();263mbsc.addNotificationListener(nb1, li, null, null);264mbsc.addNotificationListener(nb2, li, null, null);265266// Set security manager267//268if (sm) {269echo("Setting SM");270System.setSecurityManager(new SecurityManager());271}272273// Invoke the "sendNotification" method274//275mbsc.invoke(nb1, "emitNotification",276new Object[] {0, null},277new String[] {"int", "javax.management.ObjectName"});278mbsc.invoke(nb2, "emitNotification",279new Object[] {1, null},280new String[] {"int", "javax.management.ObjectName"});281mbsc.invoke(nb2, "emitNotification",282new Object[] {2, nb3},283new String[] {"int", "javax.management.ObjectName"});284285// If the check is effective and we're using policy.negative,286// then we should see the two notifs sent by nb2 (of which one287// has a getSource() that is nb3), but not the notif sent by nb1.288// Otherwise we should see all three notifs. The check is only289// effective if the property jmx.remote.x.check.notification.emission290// is explicitly true and there is a security manager.291int expectedNotifs =292(prop && sm && !policyPositive) ? 2 : 3;293294// Wait for notifications to be emitted295//296long deadline = System.currentTimeMillis() + 2000;297while (li.notifs.size() < expectedNotifs &&298System.currentTimeMillis() < deadline)299Thread.sleep(1);300301// Remove notification listener302//303mbsc.removeNotificationListener(nb1, li);304mbsc.removeNotificationListener(nb2, li);305306int result = 0;307List<ObjectName> sources = new ArrayList<ObjectName>();308sources.add(nb1);309sources.add(nb2);310sources.add(nb3);311312result = checkNotifs(expectedNotifs, li.notifs, sources);313if (result > 0) {314echo("...SecurityManager=" + sm + "; policy=" + policyPositive);315return result;316}317} finally {318// Close the connection319//320if (client != null)321client.close();322323// Stop the connector server324//325if (server != null)326server.stop();327328// Release the MBeanServer329//330if (mbs != null)331MBeanServerFactory.releaseMBeanServer(mbs);332}333334return 0;335}336337private static void echo(String message) {338System.out.println(message);339}340341public static void main(String[] args) throws Exception {342343echo("\n--- Check the emission of notifications " +344"when a Security Manager is installed");345346NotificationEmissionTest net = new NotificationEmissionTest();347348int error = 0;349350error += net.runTest(Integer.parseInt(args[0]));351352if (error > 0) {353final String msg = "\nTest FAILED! Got " + error + " error(s)";354echo(msg);355throw new IllegalArgumentException(msg);356} else {357echo("\nTest PASSED!");358}359}360}361362363