1 /*
   2  * Copyright (c) 2015, 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 import java.io.File;
  25 import java.net.URL;
  26 import java.security.Policy;
  27 import java.security.Security;
  28 
  29 /*
  30  * @test
  31  * @bug 8075706
  32  * @summary Check that a custom policy provider can be loaded from the classpath
  33  * @run main/othervm UseSystemClassLoader CUSTOM
  34  * @run main/othervm UseSystemClassLoader DEFAULT
  35  * @run main/othervm UseSystemClassLoader NOT_AVAIL
  36  * @run main/othervm UseSystemClassLoader NOT_SET
  37  */
  38 
  39 public class UseSystemClassLoader {
  40 
  41     enum Type {
  42         CUSTOM, DEFAULT, NOT_AVAIL, NOT_SET
  43     };
  44 
  45     public static void main(String[] args) throws Exception {
  46 
  47         Type t = Type.valueOf(args[0]);
  48 
  49         // We can't use the jtreg java.security.policy option to specify
  50         // the policy file because that causes the default JDK policy provider
  51         // to be set and once set, we cannot change it. So, instead we use the
  52         // policy.url security property.
  53         File file = new File(System.getProperty("test.src"), "test.policy");
  54         URL policyURL = file.toURI().toURL();
  55         Security.setProperty("policy.url.1", policyURL.toString());
  56 
  57         switch (t) {
  58             case CUSTOM:
  59                 // Set policy.provider to our custom policy provider
  60                 Security.setProperty("policy.provider", "CustomPolicy");
  61                 break;
  62             case NOT_AVAIL:
  63                 // Set policy.provider to a non-existent policy provider
  64                 Security.setProperty("policy.provider", "NonExistentPolicy");
  65                 break;
  66             case DEFAULT:
  67                 // Don't set policy.provider (leave default)
  68                 break;
  69             case NOT_SET:
  70                 // Set policy.provider to empty string
  71                 Security.setProperty("policy.provider", "");
  72                 break;
  73         }
  74 
  75         System.setSecurityManager(new SecurityManager());
  76         Policy p = Policy.getPolicy();
  77         switch (t) {
  78             case CUSTOM:
  79                 // check that the custom policy provider has been set
  80                 if (!(p instanceof CustomPolicy)) {
  81                     throw new Exception("CustomPolicy was not set");
  82                 }
  83                 break;
  84             case NOT_AVAIL:
  85             case DEFAULT:
  86             case NOT_SET:
  87                 // check that the default policy provider has been set
  88                 if (!(p instanceof sun.security.provider.PolicyFile)) {
  89                     throw new Exception("default provider was not set");
  90                 }
  91                 break;
  92         }
  93     }
  94 }