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