jdk/test/javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java
author jbachorik
Thu, 21 Aug 2014 15:22:07 +0200
changeset 26208 a581ee8890b1
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22 Reviewed-by: dfuchs, sjiang
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 5106721
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check the NotificationAccessController methods are properly called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Luis-Miguel Alventosa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean NotificationAccessControllerTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build NotificationAccessControllerTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main NotificationAccessControllerTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.jmx.remote.security.NotificationAccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Map;
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
    40
import java.util.concurrent.CopyOnWriteArrayList;
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
    41
import java.util.concurrent.Semaphore;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.remote.JMXAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.remote.JMXPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class NotificationAccessControllerTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public class NAC implements NotificationAccessController {
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
    61
        private final boolean throwException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public NAC(boolean throwException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            this.throwException = throwException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
    65
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
    66
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public void addNotificationListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            Subject subject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            echo("addNotificationListener:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            echo("\tconnectionId: " +  connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            echo("\tname: " +  name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            echo("\tsubject: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                 (subject == null ? null : subject.getPrincipals()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (throwException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                if (name.getCanonicalName().equals("domain:name=1,type=NB")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    &&
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
    80
                    subject != null
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
    81
                    &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    subject.getPrincipals().contains(new JMXPrincipal("role")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    throw new SecurityException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
    85
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
    86
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public void removeNotificationListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Subject subject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            echo("removeNotificationListener:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            echo("\tconnectionId: " +  connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            echo("\tname: " +  name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            echo("\tsubject: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                 (subject == null ? null : subject.getPrincipals()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (throwException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (name.getCanonicalName().equals("domain:name=2,type=NB")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    &&
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   100
                    subject != null
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   101
                    &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    subject.getPrincipals().contains(new JMXPrincipal("role")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    throw new SecurityException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   105
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   106
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        public void fetchNotification(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Notification notification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            Subject subject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            echo("fetchNotification:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            echo("\tconnectionId: " +  connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            echo("\tname: " +  name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            echo("\tnotification: " +  notification);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            echo("\tsubject: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                 (subject == null ? null : subject.getPrincipals()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (!throwException)
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   120
                if (name.getCanonicalName().equals("domain:name=2,type=NB")
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   121
                    &&
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   122
                    subject != null
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   123
                    &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    subject.getPrincipals().contains(new JMXPrincipal("role")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    throw new SecurityException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public class CustomJMXAuthenticator implements JMXAuthenticator {
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   130
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        public Subject authenticate(Object credentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            String role = ((String[]) credentials)[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            echo("\nCreate principal with name = " + role);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return new Subject(true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                               Collections.singleton(new JMXPrincipal(role)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                               Collections.EMPTY_SET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                               Collections.EMPTY_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public interface NBMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        public void emitNotification(int seqnum, ObjectName name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static class NB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        implements NBMBean {
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   148
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        public void emitNotification(int seqnum, ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                sendNotification(new Notification("nb", this, seqnum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                sendNotification(new Notification("nb", name, seqnum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public class Listener implements NotificationListener {
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   159
        public final List<Notification> notifs = new CopyOnWriteArrayList<>();
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   160
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   161
        private final Semaphore s;
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   162
        public Listener(Semaphore s) {
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   163
            this.s = s;
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   164
        }
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   165
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        public void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            echo("handleNotification:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            echo("\tNotification = " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            echo("\tNotification.SeqNum = " + n.getSequenceNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            echo("\tHandback = " + h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            notifs.add(n);
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   172
            s.release();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Check received notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public int checkNotifs(int size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                           List<Notification> received,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                           List<ObjectName> expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (received.size() != size) {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   183
            echo("Error: expecting " + size + " notifications, got " +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   184
                    received.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            for (Notification n : received) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                echo("Received notification: " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (!n.getType().equals("nb")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    echo("Notification type must be \"nb\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                ObjectName o = (ObjectName) n.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                int index = (int) n.getSequenceNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                ObjectName nb = expected.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (!o.equals(nb)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    echo("Notification source must be " + nb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Run test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public int runTest(boolean enableChecks, boolean throwException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        echo("\n=-=-= " + (enableChecks ? "Enable" : "Disable") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
             " notification access control checks " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
             (!enableChecks ? "" : (throwException ? ": add/remove " :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
             ": fetch ")) + "=-=-=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        JMXConnectorServer server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        JMXConnector client = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   219
        /*
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   220
        * (!enableChecks)
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   221
        * - List must contain three notifs from sources nb1, nb2 and nb3
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   222
        * (enableChecks && !throwException)
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   223
        * - List must contain one notif from source nb1
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   224
        * (enableChecks && throwException)
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   225
        * - List must contain two notifs from sources nb2 and nb3
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   226
        */
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   227
        final int expected_notifs =
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   228
            (!enableChecks ? 3 : (throwException ? 2 : 1));
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   229
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // Create a new MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            // Create server environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            //
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   237
            final Map<String,Object> env = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            env.put("jmx.remote.authenticator", new CustomJMXAuthenticator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (enableChecks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                env.put("com.sun.jmx.remote.notification.access.controller",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        new NAC(throwException));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            // Create the JMXServiceURL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // Create a JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            server = JMXConnectorServerFactory.newJMXConnectorServer(url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                                                     env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                                                     mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // Start the JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            // Create server environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            //
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   260
            final Map<String,Object> cenv = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            String[] credentials = new String[] { "role" , "password" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            cenv.put("jmx.remote.credentials", credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // Create JMXConnector and connect to JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            client = JMXConnectorFactory.connect(server.getAddress(), cenv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // Get non-secure MBeanServerConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            final MBeanServerConnection mbsc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // Create NB MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            ObjectName nb1 = ObjectName.getInstance("domain:type=NB,name=1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            ObjectName nb2 = ObjectName.getInstance("domain:type=NB,name=2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            ObjectName nb3 = ObjectName.getInstance("domain:type=NB,name=3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            mbsc.createMBean(NB.class.getName(), nb1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            mbsc.createMBean(NB.class.getName(), nb2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            mbsc.createMBean(NB.class.getName(), nb3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // Add notification listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            //
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   284
            Semaphore s = new Semaphore(0);
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   285
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   286
            Listener li = new Listener(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                mbsc.addNotificationListener(nb1, li, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (enableChecks && throwException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    echo("Didn't get expected exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                if (enableChecks && throwException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    echo("Got expected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    echo("Got unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            mbsc.addNotificationListener(nb2, li, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   303
            System.out.println("\n+++ Expecting to receive " + expected_notifs +
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   304
                               " notification" + (expected_notifs > 1 ? "s" : "") +
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   305
                               " +++");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // Invoke the "sendNotification" method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            mbsc.invoke(nb1, "emitNotification",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                new Object[] {0, null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                new String[] {"int", "javax.management.ObjectName"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            mbsc.invoke(nb2, "emitNotification",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                new Object[] {1, null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                new String[] {"int", "javax.management.ObjectName"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            mbsc.invoke(nb2, "emitNotification",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                new Object[] {2, nb3},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                new String[] {"int", "javax.management.ObjectName"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            // Wait for notifications to be emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            //
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   320
            s.acquire(expected_notifs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // Remove notification listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (!throwException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                mbsc.removeNotificationListener(nb1, li);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                mbsc.removeNotificationListener(nb2, li);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if (enableChecks && throwException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    echo("Didn't get expected exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (enableChecks && throwException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    echo("Got expected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    echo("Got unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            List<ObjectName> sources = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            sources.add(nb1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            sources.add(nb2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            sources.add(nb3);
26208
a581ee8890b1 7132590: javax/management/remote/mandatory/notif/NotificationAccessControllerTest.java fails in JDK8-B22
jbachorik
parents: 5506
diff changeset
   346
            result = checkNotifs(expected_notifs, li.notifs, sources);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (result > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            echo("Failed to perform operation: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            // Close the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (client != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // Stop the connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (server != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            // Release the MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (mbs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                MBeanServerFactory.releaseMBeanServer(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Print message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private static void echo(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        System.out.println("\nTest notification access control.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        NotificationAccessControllerTest nact =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            new NotificationAccessControllerTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        error += nact.runTest(false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        error += nact.runTest(true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        error += nact.runTest(true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            final String msg = "\nTest FAILED! Got " + error + " error(s)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            System.out.println("\nTest PASSED!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}