test/java/lang/SecurityManager/CheckPackageAccess.java

Print this page




   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  * @bug 7146431 8000450
  27  * @summary Test that internal packages cannot be accessed


  28  */
  29 













  30 public class CheckPackageAccess {
  31 








































  32     public static void main(String[] args) throws Exception {







  33 
  34         String[] pkgs = new String[] {
  35             "com.sun.corba.se.impl.",
  36             "com.sun.org.apache.xerces.internal.utils.",
  37             "com.sun.org.apache.xalan.internal.utils." };
  38         SecurityManager sm = new SecurityManager();
  39         System.setSecurityManager(sm);
  40         for (String pkg : pkgs) {
  41             System.out.println("Checking package access for " + pkg);















  42             try {
  43                 sm.checkPackageAccess(pkg);
  44                 throw new Exception("Expected PackageAccess SecurityException not thrown");

  45             } catch (SecurityException se) { }
  46             try {





  47                 sm.checkPackageDefinition(pkg);
  48                 throw new Exception("Expected PackageDefinition SecurityException not thrown");

  49             } catch (SecurityException se) { }





  50         }

  51     }












  52 }


   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  *  @bug 6741606 7146431 8000450
  27  *  @summary Make sure all restricted packages listed in the package.access
  28  *           property in the java.security file are blocked
  29  *  @run main/othervm CheckPackageAccess
  30  */
  31 
  32 import java.security.Security;
  33 import java.util.Collections;
  34 import java.util.Arrays;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.StringTokenizer;
  38 
  39 /*
  40  * The main benefit of this test is to catch merge errors or other types
  41  * of issues where one or more of the packages are accidentally
  42  * removed. This is why the packages that are known to be restricted have to
  43  * be explicitly listed below.
  44  */
  45 public class CheckPackageAccess {
  46 
  47     /*
  48      * This array should be updated whenever new packages are added to the
  49      * package.access property in the java.security file
  50      */
  51     private static final String[] packages = {
  52         "sun.",
  53         "com.sun.corba.se.impl.",
  54         "com.sun.xml.internal.",
  55         "com.sun.imageio.",
  56         "com.sun.istack.internal.",
  57         "com.sun.jmx.",
  58         "com.sun.proxy.",
  59         "com.sun.org.apache.bcel.internal.",
  60         "com.sun.org.apache.regexp.internal.",
  61         "com.sun.org.apache.xerces.internal.",
  62         "com.sun.org.apache.xpath.internal.",
  63         "com.sun.org.apache.xalan.internal.extensions.",
  64         "com.sun.org.apache.xalan.internal.lib.",
  65         "com.sun.org.apache.xalan.internal.res.",
  66         "com.sun.org.apache.xalan.internal.templates.",
  67         "com.sun.org.apache.xalan.internal.utils.",
  68         "com.sun.org.apache.xalan.internal.xslt.",
  69         "com.sun.org.apache.xalan.internal.xsltc.cmdline.",
  70         "com.sun.org.apache.xalan.internal.xsltc.compiler.",
  71         "com.sun.org.apache.xalan.internal.xsltc.trax.",
  72         "com.sun.org.apache.xalan.internal.xsltc.util.",
  73         "com.sun.org.apache.xml.internal.res.",
  74         "com.sun.org.apache.xml.internal.security.",
  75         "com.sun.org.apache.xml.internal.serializer.utils.",
  76         "com.sun.org.apache.xml.internal.utils.",
  77         "com.sun.org.glassfish.",
  78         "com.oracle.xmlns.internal.",
  79         "com.oracle.webservices.internal.",
  80         "oracle.jrockit.jfr.",
  81         "org.jcp.xml.dsig.internal.",
  82         "jdk.internal.",
  83         "jdk.nashorn.internal.",
  84         "jdk.nashorn.tools."
  85     };
  86 
  87     public static void main(String[] args) throws Exception {
  88         List<String> pkgs = new ArrayList<>(Arrays.asList(packages));
  89         String osName = System.getProperty("os.name");
  90         if (osName.contains("OS X")) {
  91             pkgs.add("apple.");  // add apple package for OS X
  92         } else if (osName.startsWith("Windows")) {
  93             pkgs.add("com.sun.java.accessibility.");
  94         }
  95 
  96         List<String> jspkgs =
  97             getPackages(Security.getProperty("package.access"));
  98 
  99         // Sort to ensure lists are comparable
 100         Collections.sort(pkgs);
 101         Collections.sort(jspkgs);
 102 
 103         if (!pkgs.equals(jspkgs)) {
 104             for (String p : pkgs)
 105                 if (!jspkgs.contains(p))
 106                     System.out.println("In golden set, but not in j.s file: " + p);
 107             for (String p : jspkgs)
 108                 if (!pkgs.contains(p))
 109                     System.out.println("In j.s file, but not in golden set: " + p);
 110 
 111 
 112             throw new RuntimeException("restricted packages are not " +
 113                                        "consistent with java.security file");
 114         }
 115         System.setSecurityManager(new SecurityManager());
 116         SecurityManager sm = System.getSecurityManager();
 117         for (String pkg : packages) {
 118             String subpkg = pkg + "foo";
 119             try {
 120                 sm.checkPackageAccess(pkg);
 121                 throw new RuntimeException("Able to access " + pkg +
 122                                            " package");
 123             } catch (SecurityException se) { }
 124             try {
 125                 sm.checkPackageAccess(subpkg);
 126                 throw new RuntimeException("Able to access " + subpkg +
 127                                            " package");
 128             } catch (SecurityException se) { }
 129             try {
 130                 sm.checkPackageDefinition(pkg);
 131                 throw new RuntimeException("Able to define class in " + pkg +
 132                                            " package");
 133             } catch (SecurityException se) { }
 134             try {
 135                 sm.checkPackageDefinition(subpkg);
 136                 throw new RuntimeException("Able to define class in " + subpkg +
 137                                            " package");
 138             } catch (SecurityException se) { }
 139         }
 140         System.out.println("Test passed");
 141     }
 142 
 143     private static List<String> getPackages(String p) {
 144         List<String> packages = new ArrayList<>();
 145         if (p != null && !p.equals("")) {
 146             StringTokenizer tok = new StringTokenizer(p, ",");
 147             while (tok.hasMoreElements()) {
 148                 String s = tok.nextToken().trim();
 149                 packages.add(s);
 150             }
 151         }
 152         return packages;
 153     }
 154 }