提交 c4b18175 编写于 作者: J jbachorik

7132590:...

7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
Reviewed-by: dfuchs, sjiang
上级 9429412d
/*
* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -37,6 +37,8 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Semaphore;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
......@@ -56,10 +58,12 @@ import javax.security.auth.Subject;
public class NotificationAccessControllerTest {
public class NAC implements NotificationAccessController {
private boolean throwException;
private final boolean throwException;
public NAC(boolean throwException) {
this.throwException = throwException;
}
@Override
public void addNotificationListener(
String connectionId,
ObjectName name,
......@@ -72,10 +76,14 @@ public class NotificationAccessControllerTest {
(subject == null ? null : subject.getPrincipals()));
if (throwException)
if (name.getCanonicalName().equals("domain:name=1,type=NB")
&&
subject != null
&&
subject.getPrincipals().contains(new JMXPrincipal("role")))
throw new SecurityException();
}
@Override
public void removeNotificationListener(
String connectionId,
ObjectName name,
......@@ -88,10 +96,14 @@ public class NotificationAccessControllerTest {
(subject == null ? null : subject.getPrincipals()));
if (throwException)
if (name.getCanonicalName().equals("domain:name=2,type=NB")
&&
subject != null
&&
subject.getPrincipals().contains(new JMXPrincipal("role")))
throw new SecurityException();
}
@Override
public void fetchNotification(
String connectionId,
ObjectName name,
......@@ -105,13 +117,17 @@ public class NotificationAccessControllerTest {
echo("\tsubject: " +
(subject == null ? null : subject.getPrincipals()));
if (!throwException)
if (name.getCanonicalName().equals("domain:name=2,type=NB") &&
if (name.getCanonicalName().equals("domain:name=2,type=NB")
&&
subject != null
&&
subject.getPrincipals().contains(new JMXPrincipal("role")))
throw new SecurityException();
}
}
public class CustomJMXAuthenticator implements JMXAuthenticator {
@Override
public Subject authenticate(Object credentials) {
String role = ((String[]) credentials)[0];
echo("\nCreate principal with name = " + role);
......@@ -129,6 +145,7 @@ public class NotificationAccessControllerTest {
public static class NB
extends NotificationBroadcasterSupport
implements NBMBean {
@Override
public void emitNotification(int seqnum, ObjectName name) {
if (name == null) {
sendNotification(new Notification("nb", this, seqnum));
......@@ -139,13 +156,20 @@ public class NotificationAccessControllerTest {
}
public class Listener implements NotificationListener {
public List<Notification> notifs = new ArrayList<Notification>();
public final List<Notification> notifs = new CopyOnWriteArrayList<>();
private final Semaphore s;
public Listener(Semaphore s) {
this.s = s;
}
@Override
public void handleNotification(Notification n, Object h) {
echo("handleNotification:");
echo("\tNotification = " + n);
echo("\tNotification.SeqNum = " + n.getSequenceNumber());
echo("\tHandback = " + h);
notifs.add(n);
s.release();
}
}
......@@ -192,6 +216,17 @@ public class NotificationAccessControllerTest {
JMXConnectorServer server = null;
JMXConnector client = null;
/*
* (!enableChecks)
* - List must contain three notifs from sources nb1, nb2 and nb3
* (enableChecks && !throwException)
* - List must contain one notif from source nb1
* (enableChecks && throwException)
* - List must contain two notifs from sources nb2 and nb3
*/
final int expected_notifs =
(!enableChecks ? 3 : (throwException ? 2 : 1));
// Create a new MBeanServer
//
final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
......@@ -199,7 +234,7 @@ public class NotificationAccessControllerTest {
try {
// Create server environment map
//
final Map<String,Object> env = new HashMap<String,Object>();
final Map<String,Object> env = new HashMap<>();
env.put("jmx.remote.authenticator", new CustomJMXAuthenticator());
if (enableChecks) {
env.put("com.sun.jmx.remote.notification.access.controller",
......@@ -222,7 +257,7 @@ public class NotificationAccessControllerTest {
// Create server environment map
//
final Map<String,Object> cenv = new HashMap<String,Object>();
final Map<String,Object> cenv = new HashMap<>();
String[] credentials = new String[] { "role" , "password" };
cenv.put("jmx.remote.credentials", credentials);
......@@ -246,7 +281,9 @@ public class NotificationAccessControllerTest {
// Add notification listener
//
Listener li = new Listener();
Semaphore s = new Semaphore(0);
Listener li = new Listener(s);
try {
mbsc.addNotificationListener(nb1, li, null, null);
if (enableChecks && throwException) {
......@@ -263,6 +300,9 @@ public class NotificationAccessControllerTest {
}
mbsc.addNotificationListener(nb2, li, null, null);
System.out.println("\n+++ Expecting to receive " + expected_notifs +
" notification" + (expected_notifs > 1 ? "s" : "") +
" +++");
// Invoke the "sendNotification" method
//
mbsc.invoke(nb1, "emitNotification",
......@@ -277,7 +317,7 @@ public class NotificationAccessControllerTest {
// Wait for notifications to be emitted
//
Thread.sleep(2000);
s.acquire(expected_notifs);
// Remove notification listener
//
......@@ -303,21 +343,7 @@ public class NotificationAccessControllerTest {
sources.add(nb1);
sources.add(nb2);
sources.add(nb3);
if (!enableChecks) {
// List must contain three notifs from sources nb1, nb2 and nb3
//
result = checkNotifs(3, li.notifs, sources);
}
if (enableChecks && !throwException) {
// List must contain one notif from source nb1
//
result = checkNotifs(1, li.notifs, sources);
}
if (enableChecks && throwException) {
// List must contain two notifs from sources nb2 and nb3
//
result = checkNotifs(2, li.notifs, sources);
}
result = checkNotifs(expected_notifs, li.notifs, sources);
if (result > 0) {
return result;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册