jdk/test/java/security/Policy/PolicyProvider/UseSystemClassLoader.java
author mullan
Tue, 12 May 2015 13:13:06 -0400
changeset 30444 054604aed79c
child 30820 0d4717a011d3
permissions -rw-r--r--
8075706: Policy implementation does not allow policy.provider to be on the class path Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30444
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     1
/*
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     4
 *
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     8
 *
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    13
 * accompanied this code).
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    14
 *
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    18
 *
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    21
 * questions.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    22
 */
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    23
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    24
import java.io.File;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    25
import java.net.URL;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    26
import java.security.Policy;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    27
import java.security.Security;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    28
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    29
/*
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    30
 * @test
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    31
 * @bug 8075706
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    32
 * @summary Check that a custom policy provider can be loaded from the classpath
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    33
 * @run main/othervm UseSystemClassLoader CUSTOM
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    34
 * @run main/othervm UseSystemClassLoader DEFAULT
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    35
 * @run main/othervm UseSystemClassLoader NOT_AVAIL
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    36
 * @run main/othervm UseSystemClassLoader NOT_SET
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    37
 */
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    38
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    39
public class UseSystemClassLoader {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    40
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    41
    enum Type {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    42
        CUSTOM, DEFAULT, NOT_AVAIL, NOT_SET
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    43
    };
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    44
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    46
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    47
        Type t = Type.valueOf(args[0]);
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    48
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    49
        // We can't use the jtreg java.security.policy option to specify
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    50
        // the policy file because that causes the default JDK policy provider
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    51
        // to be set and once set, we cannot change it. So, instead we use the
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    52
        // policy.url security property.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    53
        File file = new File(System.getProperty("test.src"), "test.policy");
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    54
        URL policyURL = file.toURI().toURL();
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    55
        Security.setProperty("policy.url.1", policyURL.toString());
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    56
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    57
        switch (t) {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    58
            case CUSTOM:
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    59
                // Set policy.provider to our custom policy provider
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    60
                Security.setProperty("policy.provider", "CustomPolicy");
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    61
                break;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    62
            case NOT_AVAIL:
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    63
                // Set policy.provider to a non-existent policy provider
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    64
                Security.setProperty("policy.provider", "NonExistentPolicy");
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    65
                break;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    66
            case DEFAULT:
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    67
                // Don't set policy.provider (leave default)
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    68
                break;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    69
            case NOT_SET:
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    70
                // Set policy.provider to empty string
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    71
                Security.setProperty("policy.provider", "");
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    72
                break;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    73
        }
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    74
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    75
        System.setSecurityManager(new SecurityManager());
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    76
        Policy p = Policy.getPolicy();
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    77
        switch (t) {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    78
            case CUSTOM:
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    79
                // check that the custom policy provider has been set
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    80
                if (!(p instanceof CustomPolicy)) {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    81
                    throw new Exception("CustomPolicy was not set");
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    82
                }
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    83
                break;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    84
            case NOT_AVAIL:
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    85
            case DEFAULT:
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    86
            case NOT_SET:
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    87
                // check that the default policy provider has been set
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    88
                if (!(p instanceof sun.security.provider.PolicyFile)) {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    89
                    throw new Exception("default provider was not set");
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    90
                }
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    91
                break;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    92
        }
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    93
    }
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    94
}