1 /* 2 * Copyright (c) 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8046171 27 * @summary Test that security checks occur for getNestHost/getNestMembers 28 * 29 * @library /test/lib 30 * @build TestSecurityManagerChecks testPkg.Host testPkg.Singleton 31 * @run driver ClassFileInstaller testPkg.Host testPkg.Host$Member testPkg.Singleton 32 * @run main/othervm -Xbootclasspath/a:. TestSecurityManagerChecks 33 */ 34 35 // ClassFileInstaller copies the testPkg files into the "current" directory 36 // so we can add it to the bootclasspath. Then when we run the test the 37 // loader for the testPkg files is the bootloader but the loader for the 38 // test class is the system loader, hence a package access check will fail 39 // because the system loader is not the same as, nor a parent of, the bootloader. 40 import java.security.Security; 41 42 public class TestSecurityManagerChecks { 43 44 public static void main(String[] args) throws Throwable { 45 46 // First get hold of the target classes before we enable security 47 Class<?> host = testPkg.Host.class; 48 Class<?> member = testPkg.Host.Member.class; 49 Class<?> memberArray = testPkg.Host.Member[].class; 50 Class<?> singleton = testPkg.Singleton.class; 51 52 // Next add testPkg to the set of packages for which package-access 53 // permission is required 54 Security.setProperty("package.access", 55 Security.getProperty("package.access") + ",testPkg."); 56 57 // Finally install a default security manager 58 SecurityManager sm = new SecurityManager(); 59 System.setSecurityManager(sm); 60 61 // These cases all succeed 62 getNestHost(int.class); // primitive 63 getNestHost(int[].class); // primitive[] 64 getNestHost(host); // host class 65 getNestHost(memberArray); // NestedT[] 66 getNestHost(singleton); // Singleton nest 67 68 getNestMembers(int.class); // primitive 69 getNestMembers(int[].class); // primitive[] 70 getNestMembers(memberArray); // NestedT[] 71 getNestMembers(singleton); // Singleton nest 72 73 // these cases all fail 74 getNestHostThrows(member); // NestedT 75 76 getNestMembersThrows(member); // NestedT 77 getNestMembersThrows(host); // host class 78 } 79 80 static void getNestHost(Class<?> c) { 81 Class<?> host = c.getNestHost(); 82 System.out.println("OK - getNestHost succeeded for " + c.getName()); 83 } 84 85 static void getNestHostThrows(Class<?> c) throws SecurityException { 86 try { 87 Class<?> host = c.getNestHost(); 88 throw new Error("getNestHost succeeded for " + c.getName()); 89 } catch (SecurityException e) { 90 System.out.println("OK - getNestHost for " + c.getName() + 91 " got expected exception: " + e); 92 } 93 } 94 95 static void getNestMembers(Class<?> c) { 96 Class<?>[] members = c.getNestMembers(); 97 System.out.println("OK - getNestMembers succeeded for " + c.getName()); 98 } 99 100 static void getNestMembersThrows(Class<?> c) throws SecurityException { 101 try { 102 Class<?>[] members = c.getNestMembers(); 103 throw new Error("getNestMembers succeeded for " + c.getName()); 104 } catch (SecurityException e) { 105 System.out.println("OK - getNestMembers for " + c.getName() + 106 " got expected exception: " + e); 107 } 108 } 109 110 }