1 /*
   2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.jmx.security;
  27 
  28 import java.lang.management.ManagementFactory;
  29 import java.util.concurrent.CountDownLatch;
  30 
  31 import javax.management.Notification;
  32 import javax.management.NotificationListener;
  33 import javax.management.ObjectName;
  34 
  35 import jdk.management.jfr.FlightRecorderMXBean;
  36 import jdk.test.lib.Asserts;
  37 
  38 import jdk.jfr.jmx.JmxHelper;
  39 
  40 /*
  41  * @test
  42  * @key jfr
  43  * @summary Test with minimal needed permissions. All functions should work.
  44  * @library /test/lib /test/jdk
  45  * @run main/othervm/secure=java.lang.SecurityManager/java.security.policy=listener.policy jdk.jfr.jmx.security.TestNotificationListenerPermission
  46  */
  47 public class TestNotificationListenerPermission {
  48     private static boolean gotSecurityException;
  49 
  50     static class TestListener implements NotificationListener {
  51         private final CountDownLatch latch = new CountDownLatch(1);
  52 
  53         @Override
  54         public void handleNotification(Notification arg0, Object arg1) {
  55             try {
  56                 System.getProperty("user.name");
  57             } catch (SecurityException se) {
  58                 se.printStackTrace();
  59                 gotSecurityException = true;
  60             }
  61             latch.countDown();
  62         }
  63 
  64         public void awaitNotication() throws InterruptedException {
  65             latch.await();
  66         }
  67     }
  68 
  69     public static void main(String[] args) throws Throwable {
  70         try {
  71             System.getProperty("user.name");
  72             Asserts.fail("Didn't get security exception. Test not configured propertly?");
  73         } catch (SecurityException se) {
  74             // as expected
  75         }
  76         FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
  77         TestListener testListener = new TestListener();
  78         ManagementFactory.getPlatformMBeanServer().addNotificationListener(new ObjectName(FlightRecorderMXBean.MXBEAN_NAME), testListener, null, null);
  79         long id = bean.newRecording();
  80         bean.startRecording(id);
  81         testListener.awaitNotication();
  82         Asserts.assertTrue(gotSecurityException, "Should not get elevated privileges in notification handler!");
  83         bean.stopRecording(id);
  84         bean.closeRecording(id);
  85     }
  86 }