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  *
  45  * @library /lib /
  46  * @run main/othervm/secure=java.lang.SecurityManager/java.security.policy=listener.policy jdk.jfr.jmx.security.TestNotificationListenerPermission
  47  */
  48 public class TestNotificationListenerPermission {
  49     private static boolean gotSecurityException;
  50 
  51     static class TestListener implements NotificationListener {
  52         private final CountDownLatch latch = new CountDownLatch(1);
  53 
  54         @Override
  55         public void handleNotification(Notification arg0, Object arg1) {
  56             try {
  57                 System.getProperty("user.name");
  58             } catch (SecurityException se) {
  59                 se.printStackTrace();
  60                 gotSecurityException = true;
  61             }
  62             latch.countDown();
  63         }
  64 
  65         public void awaitNotication() throws InterruptedException {
  66             latch.await();
  67         }
  68     }
  69 
  70     public static void main(String[] args) throws Throwable {
  71         try {
  72             System.getProperty("user.name");
  73             Asserts.fail("Didn't get security exception. Test not configured propertly?");
  74         } catch (SecurityException se) {
  75             // as expected
  76         }
  77         FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
  78         TestListener testListener = new TestListener();
  79         ManagementFactory.getPlatformMBeanServer().addNotificationListener(new ObjectName(FlightRecorderMXBean.MXBEAN_NAME), testListener, null, null);
  80         long id = bean.newRecording();
  81         bean.startRecording(id);
  82         testListener.awaitNotication();
  83         Asserts.assertTrue(gotSecurityException, "Should not get elevated privileges in notification handler!");
  84         bean.stopRecording(id);
  85         bean.closeRecording(id);
  86     }
  87 }