< prev index next >

test/jdk/java/lang/reflect/Proxy/nonPublicProxy/NonPublicProxyClass.java

Print this page
rev 47439 : 8189291: Test policy should extend the default system policy
Reviewed-by:
   1 /*
   2  * Copyright (c) 2013, 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 import java.lang.reflect.Constructor;
  25 import java.lang.reflect.InvocationHandler;
  26 import java.lang.reflect.Method;
  27 import java.lang.reflect.Modifier;
  28 import java.lang.reflect.Proxy;
  29 import java.lang.reflect.ReflectPermission;
  30 import java.security.AccessControlException;
  31 import java.security.CodeSource;
  32 import java.security.Permission;
  33 import java.security.PermissionCollection;
  34 import java.security.Permissions;
  35 import java.security.Policy;
  36 import java.security.ProtectionDomain;
  37 import java.security.SecurityPermission;
  38 import java.util.*;
  39 
  40 /*
  41  * @test
  42  * @bug 8004260
  43  * @summary Test proxy classes that implement non-public interface
  44  *
  45  * @build p.Foo
  46  * @run main/othervm NonPublicProxyClass grant
  47  * @run main/othervm NonPublicProxyClass deny
  48  * @run main/othervm NonPublicProxyClass
  49  */
  50 public class NonPublicProxyClass {


  51     public interface PublicInterface {
  52         void foo();
  53     }
  54     interface NonPublicInterface {
  55         void bar();
  56     }
  57 
  58     public static void main(String[] args) throws Exception {
  59         ClassLoader loader = ClassLoader.getSystemClassLoader();
  60         Class<?> zipConstantsClass = Class.forName("java.util.zip.ZipConstants", false, null);
  61         Class<?> fooClass = Class.forName("p.Foo");
  62 
  63         NonPublicProxyClass test1 =
  64             new NonPublicProxyClass(loader, PublicInterface.class, NonPublicInterface.class);
  65         NonPublicProxyClass test2 =
  66             new NonPublicProxyClass(loader, fooClass, PublicInterface.class);
  67         NonPublicProxyClass test3 =
  68             new NonPublicProxyClass(null, zipConstantsClass);
  69 
  70         if (args.length == 1) {


 183         final PermissionCollection permissions = new Permissions();
 184         final boolean grant;
 185         NewInstancePolicy(boolean grant) {
 186             this.grant = grant;
 187             permissions.add(new SecurityPermission("getPolicy"));
 188             if (grant) {
 189                 permissions.add(new RuntimePermission("getClassLoader"));
 190                 permissions.add(new ReflectPermission(NEW_PROXY_IN_PKG + "p"));
 191                 permissions.add(new ReflectPermission(NEW_PROXY_IN_PKG + "java.util.zip"));
 192             }
 193         }
 194         public PermissionCollection getPermissions(ProtectionDomain domain) {
 195             return permissions;
 196         }
 197 
 198         public PermissionCollection getPermissions(CodeSource codesource) {
 199             return permissions;
 200         }
 201 
 202         public boolean implies(ProtectionDomain domain, Permission perm) {
 203             return permissions.implies(perm);

 204         }
 205 
 206         public String toString() {
 207             StringBuilder sb = new StringBuilder("policy: ");
 208             Enumeration<Permission> perms = permissions.elements();
 209             while (perms.hasMoreElements()) {
 210                 sb.append("\n").append(perms.nextElement().toString());
 211             }
 212             return sb.toString();
 213         }
 214     }
 215 }
   1 /*
   2  * Copyright (c) 2013, 2017, 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 import java.lang.reflect.Constructor;
  25 import java.lang.reflect.InvocationHandler;
  26 import java.lang.reflect.Method;
  27 import java.lang.reflect.Modifier;
  28 import java.lang.reflect.Proxy;
  29 import java.lang.reflect.ReflectPermission;
  30 import java.security.AccessControlException;
  31 import java.security.CodeSource;
  32 import java.security.Permission;
  33 import java.security.PermissionCollection;
  34 import java.security.Permissions;
  35 import java.security.Policy;
  36 import java.security.ProtectionDomain;
  37 import java.security.SecurityPermission;
  38 import java.util.*;
  39 
  40 /*
  41  * @test
  42  * @bug 8004260 8189291
  43  * @summary Test proxy classes that implement non-public interface
  44  *
  45  * @build p.Foo
  46  * @run main/othervm NonPublicProxyClass grant
  47  * @run main/othervm NonPublicProxyClass deny
  48  * @run main/othervm NonPublicProxyClass
  49  */
  50 public class NonPublicProxyClass {
  51     static final Policy DEFAULT_POLICY = Policy.getPolicy();
  52 
  53     public interface PublicInterface {
  54         void foo();
  55     }
  56     interface NonPublicInterface {
  57         void bar();
  58     }
  59 
  60     public static void main(String[] args) throws Exception {
  61         ClassLoader loader = ClassLoader.getSystemClassLoader();
  62         Class<?> zipConstantsClass = Class.forName("java.util.zip.ZipConstants", false, null);
  63         Class<?> fooClass = Class.forName("p.Foo");
  64 
  65         NonPublicProxyClass test1 =
  66             new NonPublicProxyClass(loader, PublicInterface.class, NonPublicInterface.class);
  67         NonPublicProxyClass test2 =
  68             new NonPublicProxyClass(loader, fooClass, PublicInterface.class);
  69         NonPublicProxyClass test3 =
  70             new NonPublicProxyClass(null, zipConstantsClass);
  71 
  72         if (args.length == 1) {


 185         final PermissionCollection permissions = new Permissions();
 186         final boolean grant;
 187         NewInstancePolicy(boolean grant) {
 188             this.grant = grant;
 189             permissions.add(new SecurityPermission("getPolicy"));
 190             if (grant) {
 191                 permissions.add(new RuntimePermission("getClassLoader"));
 192                 permissions.add(new ReflectPermission(NEW_PROXY_IN_PKG + "p"));
 193                 permissions.add(new ReflectPermission(NEW_PROXY_IN_PKG + "java.util.zip"));
 194             }
 195         }
 196         public PermissionCollection getPermissions(ProtectionDomain domain) {
 197             return permissions;
 198         }
 199 
 200         public PermissionCollection getPermissions(CodeSource codesource) {
 201             return permissions;
 202         }
 203 
 204         public boolean implies(ProtectionDomain domain, Permission perm) {
 205             return permissions.implies(perm) ||
 206                     DEFAULT_POLICY.implies(domain, perm);
 207         }
 208 
 209         public String toString() {
 210             StringBuilder sb = new StringBuilder("policy: ");
 211             Enumeration<Permission> perms = permissions.elements();
 212             while (perms.hasMoreElements()) {
 213                 sb.append("\n").append(perms.nextElement().toString());
 214             }
 215             return sb.toString();
 216         }
 217     }
 218 }
< prev index next >