Path: blob/master/test/jdk/java/security/Policy/PolicyProvider/UseSystemClassLoader.java
41153 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.net.URL;25import java.security.Policy;26import java.security.Security;2728/*29* @test30* @bug 807570631* @summary Check that a custom policy provider can be loaded from the classpath32* @modules java.base/sun.security.provider33* @run main/othervm -Djava.security.manager=allow UseSystemClassLoader CUSTOM34* @run main/othervm -Djava.security.manager=allow UseSystemClassLoader DEFAULT35* @run main/othervm -Djava.security.manager=allow UseSystemClassLoader NOT_AVAIL36* @run main/othervm -Djava.security.manager=allow UseSystemClassLoader NOT_SET37*/3839public class UseSystemClassLoader {4041enum Type {42CUSTOM, DEFAULT, NOT_AVAIL, NOT_SET43};4445public static void main(String[] args) throws Exception {4647Type t = Type.valueOf(args[0]);4849// We can't use the jtreg java.security.policy option to specify50// the policy file because that causes the default JDK policy provider51// to be set and once set, we cannot change it. So, instead we use the52// policy.url security property.53File file = new File(System.getProperty("test.src"), "test.policy");54URL policyURL = file.toURI().toURL();55Security.setProperty("policy.url.1", policyURL.toString());5657switch (t) {58case CUSTOM:59// Set policy.provider to our custom policy provider60Security.setProperty("policy.provider", "CustomPolicy");61break;62case NOT_AVAIL:63// Set policy.provider to a non-existent policy provider64Security.setProperty("policy.provider", "NonExistentPolicy");65break;66case DEFAULT:67// Don't set policy.provider (leave default)68break;69case NOT_SET:70// Set policy.provider to empty string71Security.setProperty("policy.provider", "");72break;73}7475System.setSecurityManager(new SecurityManager());76Policy p = Policy.getPolicy();77switch (t) {78case CUSTOM:79// check that the custom policy provider has been set80if (!(p instanceof CustomPolicy)) {81throw new Exception("CustomPolicy was not set");82}83break;84case NOT_AVAIL:85case DEFAULT:86case NOT_SET:87// check that the default policy provider has been set88if (!(p instanceof sun.security.provider.PolicyFile)) {89throw new Exception("default provider was not set");90}91break;92}93}94}959697