< prev index next >

test/tools/launcher/modules/permit/PermitIllegalAccess.java

Print this page




  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  * @library /lib/testlibrary
  27  * @build PermitIllegalAccess AttemptAccess jdk.testlibrary.*
  28  * @run testng PermitIllegalAccess
  29  * @summary Basic test for java --permit-illegal-access
  30  */
  31 


  32 import java.util.List;

  33 
  34 import jdk.testlibrary.ProcessTools;
  35 import jdk.testlibrary.OutputAnalyzer;
  36 
  37 import org.testng.annotations.Test;
  38 import static org.testng.Assert.*;
  39 
  40 /**
  41  * Basic test of --permit-illegal-access to ensure that it permits access
  42  * via core reflection and setAccessible/trySetAccessible.
  43  */
  44 
  45 @Test
  46 public class PermitIllegalAccess {
  47 
  48     static final String TEST_CLASSES = System.getProperty("test.classes");
  49     static final String TEST_MAIN = "AttemptAccess";
  50 
  51     static final String WARNING = "WARNING";
  52     static final String STARTUP_WARNING =
  53         "WARNING: --permit-illegal-access will be removed in the next major release";
  54     static final String ILLEGAL_ACCESS_WARNING =
  55         "WARNING: Illegal access by " + TEST_MAIN;
  56 
  57     /**
  58      * Launches AttemptAccess to execute an action, returning the OutputAnalyzer
  59      * to analyze the output/exitCode.
  60      */
  61     private OutputAnalyzer tryAction(String action, int count) throws Exception {
  62         String arg = "" + count;
  63         return ProcessTools
  64                 .executeTestJava("-cp", TEST_CLASSES, TEST_MAIN, action, arg)



  65                 .outputTo(System.out)
  66                 .errorTo(System.out);
  67     }
  68 
  69     /**
  70      * Launches AttemptAccess with --permit-illegal-access to execute an action,
  71      * returning the OutputAnalyzer to analyze the output/exitCode.
  72      */
  73     private OutputAnalyzer tryActionPermittingIllegalAccess(String action,
  74                                                             int count)
  75         throws Exception
  76     {
  77         String arg = "" + count;
  78         return ProcessTools
  79                 .executeTestJava("-cp", TEST_CLASSES, "--permit-illegal-access",
  80                                  TEST_MAIN, action, arg)
  81                 .outputTo(System.out)
  82                 .errorTo(System.out);
  83     }
  84 
  85     /**
  86      * Sanity check to ensure that IllegalAccessException is thrown.
  87      */
  88     public void testAccessFail() throws Exception {
  89         int exitValue = tryAction("access", 1)
  90                 .stdoutShouldNotContain(WARNING)
  91                 .stdoutShouldNotContain("IllegalAccessException")
  92                 .stderrShouldNotContain(WARNING)
  93                 .stderrShouldContain("IllegalAccessException")
  94                 .getExitValue();
  95         assertTrue(exitValue != 0);
  96     }
  97 
  98     /**
  99      * Sanity check to ensure that InaccessibleObjectException is thrown.
 100      */
 101     public void testSetAccessibleFail() throws Exception {
 102         int exitValue = tryAction("setAccessible", 1)


 178     }
 179 
 180     /**
 181      * Permit repeated calls to trySetAccessible to succeed
 182      */
 183     public void testRepeatedTrySetAccessiblePermitted() throws Exception {
 184         OutputAnalyzer outputAnalyzer = tryActionPermittingIllegalAccess("trySetAccessible", 10)
 185                 .stdoutShouldNotContain(WARNING)
 186                 .stdoutShouldNotContain("InaccessibleObjectException")
 187                 .stderrShouldContain(STARTUP_WARNING)
 188                 .stderrShouldNotContain("InaccessibleObjectException")
 189                 .stderrShouldContain(ILLEGAL_ACCESS_WARNING)
 190                 .shouldHaveExitValue(0);
 191 
 192         // should only have one illegal access warning
 193         assertTrue(containsCount(outputAnalyzer.asLines(), ILLEGAL_ACCESS_WARNING) == 1);
 194 
 195     }
 196 
 197     /**























































 198      * Returns the number of lines in the given input that contain the
 199      * given char sequence.
 200      */
 201     private int containsCount(List<String> lines, CharSequence cs) {
 202         int count = 0;
 203         for (String line : lines) {
 204             if (line.contains(cs)) count++;
 205         }
 206         return count;
 207     }
 208 }


  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  * @library /lib/testlibrary
  27  * @build PermitIllegalAccess AttemptAccess jdk.testlibrary.*
  28  * @run testng PermitIllegalAccess
  29  * @summary Basic test for java --permit-illegal-access
  30  */
  31 
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.List;
  35 import java.util.stream.Stream;
  36 
  37 import jdk.testlibrary.ProcessTools;
  38 import jdk.testlibrary.OutputAnalyzer;
  39 
  40 import org.testng.annotations.Test;
  41 import static org.testng.Assert.*;
  42 
  43 /**
  44  * Basic test of --permit-illegal-access to ensure that it permits access
  45  * via core reflection and setAccessible/trySetAccessible.
  46  */
  47 
  48 @Test
  49 public class PermitIllegalAccess {
  50 
  51     static final String TEST_CLASSES = System.getProperty("test.classes");
  52     static final String TEST_MAIN = "AttemptAccess";
  53 
  54     static final String WARNING = "WARNING";
  55     static final String STARTUP_WARNING =
  56         "WARNING: --permit-illegal-access will be removed in the next major release";
  57     static final String ILLEGAL_ACCESS_WARNING =
  58         "WARNING: Illegal access by " + TEST_MAIN;
  59 
  60     /**
  61      * Launches AttemptAccess to execute an action, returning the OutputAnalyzer
  62      * to analyze the output/exitCode.
  63      */
  64     private OutputAnalyzer tryAction(String action, int count, String... args)
  65         throws Exception
  66     {
  67         Stream<String> s1 = Stream.of(args);
  68         Stream<String> s2 = Stream.of("-cp", TEST_CLASSES, TEST_MAIN, action, "" + count);
  69         String[] opts = Stream.concat(s1, s2).toArray(String[]::new);
  70         return ProcessTools.executeTestJava(opts)
  71                 .outputTo(System.out)
  72                 .errorTo(System.out);
  73     }
  74 
  75     /**
  76      * Launches AttemptAccess with --permit-illegal-access to execute an action,
  77      * returning the OutputAnalyzer to analyze the output/exitCode.
  78      */
  79     private OutputAnalyzer tryActionPermittingIllegalAccess(String action, int count)

  80         throws Exception
  81     {
  82         return tryAction(action, count, "--permit-illegal-access");





  83     }
  84 
  85     /**
  86      * Sanity check to ensure that IllegalAccessException is thrown.
  87      */
  88     public void testAccessFail() throws Exception {
  89         int exitValue = tryAction("access", 1)
  90                 .stdoutShouldNotContain(WARNING)
  91                 .stdoutShouldNotContain("IllegalAccessException")
  92                 .stderrShouldNotContain(WARNING)
  93                 .stderrShouldContain("IllegalAccessException")
  94                 .getExitValue();
  95         assertTrue(exitValue != 0);
  96     }
  97 
  98     /**
  99      * Sanity check to ensure that InaccessibleObjectException is thrown.
 100      */
 101     public void testSetAccessibleFail() throws Exception {
 102         int exitValue = tryAction("setAccessible", 1)


 178     }
 179 
 180     /**
 181      * Permit repeated calls to trySetAccessible to succeed
 182      */
 183     public void testRepeatedTrySetAccessiblePermitted() throws Exception {
 184         OutputAnalyzer outputAnalyzer = tryActionPermittingIllegalAccess("trySetAccessible", 10)
 185                 .stdoutShouldNotContain(WARNING)
 186                 .stdoutShouldNotContain("InaccessibleObjectException")
 187                 .stderrShouldContain(STARTUP_WARNING)
 188                 .stderrShouldNotContain("InaccessibleObjectException")
 189                 .stderrShouldContain(ILLEGAL_ACCESS_WARNING)
 190                 .shouldHaveExitValue(0);
 191 
 192         // should only have one illegal access warning
 193         assertTrue(containsCount(outputAnalyzer.asLines(), ILLEGAL_ACCESS_WARNING) == 1);
 194 
 195     }
 196 
 197     /**
 198      * Permit access to succeed with --add-exports. No warning should be printed.
 199      */
 200     public void testAccessWithAddExports() throws Exception {
 201         tryAction("access", 1, "--add-exports", "java.base/sun.security.x509=ALL-UNNAMED")
 202                 .stdoutShouldNotContain(WARNING)
 203                 .stdoutShouldNotContain("IllegalAccessException")
 204                 .stderrShouldNotContain(WARNING)
 205                 .stderrShouldNotContain("IllegalAccessException")
 206                 .shouldHaveExitValue(0);
 207     }
 208 
 209     /**
 210      * Permit access to succeed with --add-exports and --permit-illegal-access.
 211      * The only warning emitted should be the startup warning.
 212      */
 213     public void testAccessWithePermittedAddExports() throws Exception {
 214         tryAction("access", 1, "--permit-illegal-access",
 215                     "--add-exports", "java.base/sun.security.x509=ALL-UNNAMED")
 216                 .stdoutShouldNotContain(WARNING)
 217                 .stdoutShouldNotContain("IllegalAccessException")
 218                 .stderrShouldContain(STARTUP_WARNING)
 219                 .stderrShouldNotContain("IllegalAccessException")
 220                 .stderrShouldNotContain(ILLEGAL_ACCESS_WARNING)
 221                 .shouldHaveExitValue(0);
 222     }
 223 
 224     /**
 225      * Permit setAccessible to succeed with --add-opens. No warning should be printed.
 226      */
 227     public void testSetAccessibleWithAddOpens() throws Exception {
 228         tryAction("setAccessible", 1, "--add-opens", "java.base/java.lang=ALL-UNNAMED")
 229                 .stdoutShouldNotContain(WARNING)
 230                 .stdoutShouldNotContain("InaccessibleObjectException")
 231                 .stderrShouldNotContain(WARNING)
 232                 .stderrShouldNotContain("InaccessibleObjectException")
 233                 .shouldHaveExitValue(0);
 234     }
 235 
 236     /**
 237      * Permit setAccessible to succeed with both --add-opens and --permit-illegal-access.
 238      * The only warning emitted should be the startup warning.
 239      */
 240     public void testSetAccessiblePermittedWithAddOpens() throws Exception {
 241         tryAction("setAccessible", 1, "--permit-illegal-access",
 242                     "--add-opens", "java.base/java.lang=ALL-UNNAMED")
 243                 .stdoutShouldNotContain(WARNING)
 244                 .stdoutShouldNotContain("InaccessibleObjectException")
 245                 .stderrShouldContain(STARTUP_WARNING)
 246                 .stderrShouldNotContain("InaccessibleObjectException")
 247                 .stderrShouldNotContain(ILLEGAL_ACCESS_WARNING)
 248                 .shouldHaveExitValue(0);
 249     }
 250 
 251 
 252     /**
 253      * Returns the number of lines in the given input that contain the
 254      * given char sequence.
 255      */
 256     private int containsCount(List<String> lines, CharSequence cs) {
 257         int count = 0;
 258         for (String line : lines) {
 259             if (line.contains(cs)) count++;
 260         }
 261         return count;
 262     }
 263 }
< prev index next >