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.nio.file.Path; 29 import java.nio.file.Paths; 30 import java.util.List; 31 32 import jdk.management.jfr.ConfigurationInfo; 33 import jdk.management.jfr.FlightRecorderMXBean; 34 import jdk.testlibrary.Asserts; 35 import jdk.testlibrary.jfr.JmxHelper; 36 37 /* 38 * @test 39 * @key jfr 40 * @summary Test with minimal needed permissions. All functions should work. 41 * @library /lib/testlibrary 42 * @run main/othervm/secure=java.lang.SecurityManager/java.security.policy=enough.policy jdk.jfr.jmx.security.TestEnoughPermission 43 */ 44 public class TestEnoughPermission { 45 46 public static void main(String[] args) throws Throwable { 47 try { 48 FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean(); 49 50 System.out.println("AAAAAAAAAAAAAAAAAA"); 51 Asserts.assertFalse(bean.getEventTypes().isEmpty(), "No EventTypes"); 52 System.out.println("BBBBBBBBBBBBBBB"); 53 List<ConfigurationInfo> configs = bean.getConfigurations(); 54 System.out.println("CCCCCCCCCCCCCCCCC"); 55 for (ConfigurationInfo config : configs) { 56 System.out.println("config.name=" + config.getName() + ": " + config.getContents()); 57 } 58 59 long recId = testRecording(bean); 60 testStream(bean, recId); 61 bean.closeRecording(recId); 62 63 //*************** verifySecurityException(() -> bean.getRecordingOptions(dummyId), "getRecordingOptions()"); 64 //*************** verifySecurityException(() -> bean.getRecordingSettings(dummyId), "getRecordingSettings()"); 65 //*********** verifySecurityException(() -> bean.setConfiguration(dummyId, "<>"), "setConfiguration()"); 66 //************* verifySecurityException(() -> bean.setRecordingSettings(dummyId, dummyMap), "setRecordingSettings()"); 67 //************* verifySecurityException(() -> bean.setRecordingOptions(dummyId, dummyMap), "setRecordingOptions()"); 68 } catch (Throwable t) { 69 t.printStackTrace(); 70 throw t; 71 } 72 } 73 74 private static long testRecording(FlightRecorderMXBean bean) throws Exception { 75 System.out.println("A"); 76 long recId = bean.newRecording(); 77 System.out.println("B"); 78 bean.setPredefinedConfiguration(recId, "profile"); 79 System.out.println("C"); 80 bean.startRecording(recId); 81 System.out.println("D"); 82 Asserts.assertTrue(bean.getRecordings().stream().anyMatch(r -> { return r.getId() == recId; }), "recId not found"); 83 System.out.println("E"); 84 bean.stopRecording(recId); 85 86 final Path path = Paths.get(".", String.format("rec%d.jfr", recId)); 87 bean.copyTo(recId, path.toString()); 88 //EventSet events = EventSet.fromFile(path); 89 return recId; 90 } 91 92 private static void testStream(FlightRecorderMXBean bean, long recId) throws Exception { 93 long streamId = bean.openStream(recId, null); 94 byte[] buff = bean.readStream(streamId); 95 Asserts.assertNotNull(buff, "Stream data was empty"); 96 while (buff != null) { 97 // TODO: write to file and parse. 98 System.out.println("buff.length=" + buff.length); 99 buff = bean.readStream(streamId); 100 } 101 bean.closeStream(streamId); 102 } 103 }