33 import java.security.Permissions;
34 import java.security.Policy;
35 import java.security.ProtectionDomain;
36 import java.util.CSM.Result;
37 import java.util.function.Supplier;
38
39 /**
40 * This test invokes StackWalker::getCallerClass via static reference,
41 * reflection, MethodHandle, lambda. Also verify that
42 * StackWalker::getCallerClass can't be called from @CallerSensitive method.
43 */
44 public class CallerSensitiveTest {
45 private static final String NON_CSM_CALLER_METHOD = "getCallerClass";
46 private static final String CSM_CALLER_METHOD = "caller";
47
48 public static void main(String... args) throws Throwable {
49 boolean sm = false;
50 if (args.length > 0 && args[0].equals("sm")) {
51 sm = true;
52 PermissionCollection perms = new Permissions();
53 perms.add(new StackFramePermission("retainClassReference"));
54 Policy.setPolicy(new Policy() {
55 @Override
56 public boolean implies(ProtectionDomain domain, Permission p) {
57 return perms.implies(p);
58 }
59 });
60 System.setSecurityManager(new SecurityManager());
61 }
62
63 System.err.format("Test %s security manager.%n",
64 sm ? "with" : "without");
65
66 CallerSensitiveTest cstest = new CallerSensitiveTest();
67 // test static call to java.util.CSM::caller and CSM::getCallerClass
68 cstest.staticMethodCall();
69 // test java.lang.reflect.Method call
70 cstest.reflectMethodCall();
71 // test java.lang.invoke.MethodHandle
72 cstest.invokeMethodHandle(Lookup1.lookup);
73 cstest.invokeMethodHandle(Lookup2.lookup);
|
33 import java.security.Permissions;
34 import java.security.Policy;
35 import java.security.ProtectionDomain;
36 import java.util.CSM.Result;
37 import java.util.function.Supplier;
38
39 /**
40 * This test invokes StackWalker::getCallerClass via static reference,
41 * reflection, MethodHandle, lambda. Also verify that
42 * StackWalker::getCallerClass can't be called from @CallerSensitive method.
43 */
44 public class CallerSensitiveTest {
45 private static final String NON_CSM_CALLER_METHOD = "getCallerClass";
46 private static final String CSM_CALLER_METHOD = "caller";
47
48 public static void main(String... args) throws Throwable {
49 boolean sm = false;
50 if (args.length > 0 && args[0].equals("sm")) {
51 sm = true;
52 PermissionCollection perms = new Permissions();
53 perms.add(new RuntimePermission("getStackWalkerWithClassReference"));
54 Policy.setPolicy(new Policy() {
55 @Override
56 public boolean implies(ProtectionDomain domain, Permission p) {
57 return perms.implies(p);
58 }
59 });
60 System.setSecurityManager(new SecurityManager());
61 }
62
63 System.err.format("Test %s security manager.%n",
64 sm ? "with" : "without");
65
66 CallerSensitiveTest cstest = new CallerSensitiveTest();
67 // test static call to java.util.CSM::caller and CSM::getCallerClass
68 cstest.staticMethodCall();
69 // test java.lang.reflect.Method call
70 cstest.reflectMethodCall();
71 // test java.lang.invoke.MethodHandle
72 cstest.invokeMethodHandle(Lookup1.lookup);
73 cstest.invokeMethodHandle(Lookup2.lookup);
|