jdk/test/tools/launcher/modules/permit/PermitIllegalAccess.java
changeset 44359 c6761862ca0b
child 44364 9cc9dc782213
equal deleted inserted replaced
44210:5a8499c4b32c 44359:c6761862ca0b
       
     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  * @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)
       
   103                 .stdoutShouldNotContain(WARNING)
       
   104                 .stdoutShouldNotContain("InaccessibleObjectException")
       
   105                 .stderrShouldNotContain(WARNING)
       
   106                 .stderrShouldContain("InaccessibleObjectException")
       
   107                 .getExitValue();
       
   108         assertTrue(exitValue != 0);
       
   109     }
       
   110 
       
   111     /**
       
   112      * Permit illegal access to succeed
       
   113      */
       
   114     public void testAccessPermitted() throws Exception {
       
   115         tryActionPermittingIllegalAccess("access", 1)
       
   116                 .stdoutShouldNotContain(WARNING)
       
   117                 .stdoutShouldNotContain("IllegalAccessException")
       
   118                 .stderrShouldContain(STARTUP_WARNING)
       
   119                 .stderrShouldNotContain("IllegalAccessException")
       
   120                 .stderrShouldContain(ILLEGAL_ACCESS_WARNING)
       
   121                 .shouldHaveExitValue(0);
       
   122     }
       
   123 
       
   124     /**
       
   125      * Permit repeated illegal access to succeed
       
   126      */
       
   127     public void testRepeatedAccessPermitted() throws Exception {
       
   128         OutputAnalyzer outputAnalyzer = tryActionPermittingIllegalAccess("access", 10)
       
   129                 .stdoutShouldNotContain(WARNING)
       
   130                 .stdoutShouldNotContain("IllegalAccessException")
       
   131                 .stderrShouldContain(STARTUP_WARNING)
       
   132                 .stderrShouldNotContain("IllegalAccessException")
       
   133                 .stderrShouldContain(ILLEGAL_ACCESS_WARNING)
       
   134                 .shouldHaveExitValue(0);;
       
   135 
       
   136         // should only have one illegal access warning
       
   137         assertTrue(containsCount(outputAnalyzer.asLines(), ILLEGAL_ACCESS_WARNING) == 1);
       
   138     }
       
   139 
       
   140     /**
       
   141      * Permit setAccessible to succeed
       
   142      */
       
   143     public void testSetAccessiblePermitted() throws Exception {
       
   144         tryActionPermittingIllegalAccess("setAccessible", 1)
       
   145                 .stdoutShouldNotContain(WARNING)
       
   146                 .stdoutShouldNotContain("InaccessibleObjectException")
       
   147                 .stderrShouldContain(STARTUP_WARNING)
       
   148                 .stderrShouldNotContain("InaccessibleObjectException")
       
   149                 .stderrShouldContain(ILLEGAL_ACCESS_WARNING)
       
   150                 .shouldHaveExitValue(0);
       
   151     }
       
   152 
       
   153     /**
       
   154      * Permit repeated calls to setAccessible to succeed
       
   155      */
       
   156     public void testRepeatedSetAccessiblePermitted() throws Exception {
       
   157         OutputAnalyzer outputAnalyzer = tryActionPermittingIllegalAccess("setAccessible", 10)
       
   158                 .stdoutShouldNotContain(WARNING)
       
   159                 .stdoutShouldNotContain("InaccessibleObjectException")
       
   160                 .stderrShouldContain(STARTUP_WARNING)
       
   161                 .stderrShouldNotContain("InaccessibleObjectException")
       
   162                 .stderrShouldContain(ILLEGAL_ACCESS_WARNING)
       
   163                 .shouldHaveExitValue(0);
       
   164 
       
   165         // should only have one illegal access warning
       
   166         assertTrue(containsCount(outputAnalyzer.asLines(), ILLEGAL_ACCESS_WARNING) == 1);
       
   167     }
       
   168 
       
   169     /**
       
   170      * Permit trySetAccessible to succeed
       
   171      */
       
   172     public void testTrySetAccessiblePermitted() throws Exception {
       
   173         tryActionPermittingIllegalAccess("trySetAccessible", 1)
       
   174                 .stdoutShouldNotContain(WARNING)
       
   175                 .stderrShouldContain(STARTUP_WARNING)
       
   176                 .stderrShouldContain(ILLEGAL_ACCESS_WARNING)
       
   177                 .shouldHaveExitValue(0);
       
   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 }