Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Proxy/nonPublicProxy/NonPublicProxyClass.java
38889 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.reflect.Constructor;24import java.lang.reflect.InvocationHandler;25import java.lang.reflect.Method;26import java.lang.reflect.Modifier;27import java.lang.reflect.Proxy;28import java.lang.reflect.ReflectPermission;29import java.security.AccessControlException;30import java.security.CodeSource;31import java.security.Permission;32import java.security.PermissionCollection;33import java.security.Permissions;34import java.security.Policy;35import java.security.ProtectionDomain;36import java.security.SecurityPermission;37import java.util.*;3839/*40* @test41* @bug 800426042* @summary Test proxy classes that implement non-public interface43*44* @build p.Foo45* @run main/othervm NonPublicProxyClass grant46* @run main/othervm NonPublicProxyClass deny47* @run main/othervm NonPublicProxyClass48*/49public class NonPublicProxyClass {50public interface PublicInterface {51void foo();52}53interface NonPublicInterface {54void bar();55}5657public static void main(String[] args) throws Exception {58ClassLoader loader = ClassLoader.getSystemClassLoader();59Class<?> zipConstantsClass = Class.forName("java.util.zip.ZipConstants", false, null);60Class<?> fooClass = Class.forName("p.Foo");6162NonPublicProxyClass test1 =63new NonPublicProxyClass(loader, PublicInterface.class, NonPublicInterface.class);64NonPublicProxyClass test2 =65new NonPublicProxyClass(loader, fooClass, PublicInterface.class);66NonPublicProxyClass test3 =67new NonPublicProxyClass(null, zipConstantsClass);6869if (args.length == 1) {70switch (args[0]) {71case "grant": Policy.setPolicy(new NewInstancePolicy(true));72break;73case "deny" : Policy.setPolicy(new NewInstancePolicy(false));74break;75default: throw new IllegalArgumentException(args[0]);76}77System.setSecurityManager(new SecurityManager());78}7980test1.run();81test2.run();82test3.run();83System.out.format("Test passed: security %s%n",84(args.length == 0 ? "manager not installed" : Policy.getPolicy()));85}8687private final ClassLoader loader;88private final Class<?>[] interfaces;89private final InvocationHandler handler = newInvocationHandler();90private Class<?> proxyClass;91public NonPublicProxyClass(ClassLoader loader, Class<?> ... intfs) {92this.loader = loader;93this.interfaces = intfs;94}9596public void run() throws Exception {97boolean hasAccess = loader != null || hasAccess();98try {99proxyClass = Proxy.getProxyClass(loader, interfaces);100if (!hasAccess) {101throw new RuntimeException("should have no permission to create proxy class");102}103} catch (AccessControlException e) {104if (hasAccess) {105throw e;106}107if (e.getPermission().getClass() != RuntimePermission.class ||108!e.getPermission().getName().equals("getClassLoader")) {109throw e;110}111return;112}113114if (Modifier.isPublic(proxyClass.getModifiers())) {115throw new RuntimeException(proxyClass + " must be non-public");116}117newProxyInstance();118newInstanceFromConstructor(proxyClass);119}120121private boolean hasAccess() {122if (System.getSecurityManager() == null) {123return true;124}125NewInstancePolicy policy = NewInstancePolicy.class.cast(Policy.getPolicy());126return policy.grant;127}128129private final static String NEW_PROXY_IN_PKG = "newProxyInPackage.";130private void newProxyInstance() {131// expect newProxyInstance to succeed if it's in the same runtime package132int i = proxyClass.getName().lastIndexOf('.');133String pkg = (i != -1) ? proxyClass.getName().substring(0, i) : "";134boolean hasAccess = pkg.isEmpty() || hasAccess();135try {136Proxy.newProxyInstance(loader, interfaces, handler);137if (!hasAccess) {138throw new RuntimeException("ERROR: Proxy.newProxyInstance should fail " + proxyClass);139}140} catch (AccessControlException e) {141if (hasAccess) {142throw e;143}144if (e.getPermission().getClass() != ReflectPermission.class ||145!e.getPermission().getName().equals(NEW_PROXY_IN_PKG + pkg)) {146throw e;147}148}149}150151private void newInstanceFromConstructor(Class<?> proxyClass)152throws Exception153{154// expect newInstance to succeed if it's in the same runtime package155boolean isSamePackage = proxyClass.getName().lastIndexOf('.') == -1;156try {157Constructor cons = proxyClass.getConstructor(InvocationHandler.class);158cons.newInstance(newInvocationHandler());159if (!isSamePackage) {160throw new RuntimeException("ERROR: Constructor.newInstance should not succeed");161}162} catch (IllegalAccessException e) {163if (isSamePackage) {164throw e;165}166}167}168169private static InvocationHandler newInvocationHandler() {170return new InvocationHandler() {171public Object invoke(Object proxy, Method method, Object[] args)172throws Throwable {173Class<?>[] intfs = proxy.getClass().getInterfaces();174System.out.println("Proxy for " + Arrays.toString(intfs)175+ " " + method.getName() + " is being invoked");176return null;177}178};179}180181static class NewInstancePolicy extends Policy {182final PermissionCollection permissions = new Permissions();183final boolean grant;184NewInstancePolicy(boolean grant) {185this.grant = grant;186permissions.add(new SecurityPermission("getPolicy"));187if (grant) {188permissions.add(new RuntimePermission("getClassLoader"));189permissions.add(new ReflectPermission(NEW_PROXY_IN_PKG + "p"));190permissions.add(new ReflectPermission(NEW_PROXY_IN_PKG + "java.util.zip"));191}192}193public PermissionCollection getPermissions(ProtectionDomain domain) {194return permissions;195}196197public PermissionCollection getPermissions(CodeSource codesource) {198return permissions;199}200201public boolean implies(ProtectionDomain domain, Permission perm) {202return permissions.implies(perm);203}204205public String toString() {206StringBuilder sb = new StringBuilder("policy: ");207Enumeration<Permission> perms = permissions.elements();208while (perms.hasMoreElements()) {209sb.append("\n").append(perms.nextElement().toString());210}211return sb.toString();212}213}214}215216217