1 /*
   2  * Copyright (c) 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 /**
  25  * @test
  26  * @build CanAccessTest
  27  * @modules java.base/jdk.internal.misc:+open
  28  * @run testng/othervm --illegal-access=deny CanAccessTest
  29  * @summary Test AccessibleObject::canAccess method
  30  */
  31 
  32 import java.lang.reflect.Constructor;
  33 import java.lang.reflect.Method;
  34 import java.security.SecureClassLoader;
  35 
  36 import jdk.internal.misc.Unsafe;
  37 import org.testng.annotations.Test;
  38 import static org.testng.Assert.*;
  39 
  40 @Test
  41 public class CanAccessTest {
  42     private static Unsafe INSTANCE = Unsafe.getUnsafe();
  43 
  44     /**
  45      * null object parameter for Constructor
  46      */
  47     public void testConstructor() throws Exception {
  48         Constructor<?> ctor = Unsafe.class.getDeclaredConstructor();
  49         assertFalse(ctor.canAccess(null));
  50         assertTrue(ctor.trySetAccessible());
  51 
  52         try {
  53             // non-null object parameter
  54             ctor.canAccess(INSTANCE);
  55             assertTrue(false);
  56         } catch (IllegalArgumentException expected) {}
  57     }
  58 
  59     /**
  60      * Test protected constructors
  61      */
  62     public void testProtectedConstructor() throws Exception {
  63         TestLoader.testProtectedConstructorNonOpenedPackage();
  64 
  65         Constructor<?> ctor = TestLoader.class.getDeclaredConstructor();
  66         assertTrue(ctor.canAccess(null));
  67     }
  68 
  69     /**
  70      * null object parameter  for static members
  71      */
  72     public void testStaticMember() throws Exception {
  73         Method m = Unsafe.class.getDeclaredMethod("throwIllegalAccessError");
  74         assertFalse(m.canAccess(null));
  75         assertTrue(m.trySetAccessible());
  76 
  77         try {
  78             // non-null object parameter
  79             m.canAccess(INSTANCE);
  80             assertTrue(false);
  81         } catch (IllegalArgumentException expected) { }
  82     }
  83 
  84     /**
  85      * Test protected static
  86      */
  87     public void testProtectedStatic() throws Exception {
  88         Method m = TestLoader.testProtectedStatic();
  89         assertFalse(m.canAccess(null));
  90     }
  91 
  92     /**
  93      * the specified object must be an instance of the declaring class
  94      * for instance members
  95      */
  96     public void testInstanceMethod() throws Exception {
  97         Method m = Unsafe.class.getDeclaredMethod("allocateMemory0", long.class);
  98         assertFalse(m.canAccess(INSTANCE));
  99 
 100         try {
 101             m.canAccess(null);
 102             assertTrue(false);
 103         } catch (IllegalArgumentException expected) { }
 104     }
 105 
 106     /**
 107      * the specified object must be an instance of the declaring class
 108      * for instance members
 109      */
 110     public void testInvalidInstanceObject() throws Exception {
 111         Class<?> clazz = Class.forName("sun.security.x509.X500Name");
 112         Method m = clazz.getDeclaredMethod("size");
 113 
 114         try {
 115             m.canAccess(INSTANCE);
 116             assertTrue(false);
 117         } catch (IllegalArgumentException expected) { }
 118     }
 119 
 120 
 121     static class TestLoader extends SecureClassLoader {
 122         public static Method testProtectedStatic() throws Exception {
 123             Method m = ClassLoader.class.getDeclaredMethod("registerAsParallelCapable");
 124             assertTrue(m.canAccess(null));
 125             return m;
 126         }
 127 
 128         protected TestLoader() throws Exception {
 129             Constructor<?> ctor = SecureClassLoader.class.getDeclaredConstructor();
 130             assertFalse(ctor.canAccess(null));
 131             assertFalse(ctor.trySetAccessible());
 132         }
 133 
 134         public static void testProtectedConstructorNonOpenedPackage() throws Exception {
 135             new TestLoader();
 136         }
 137     }
 138 }