jdk/test/sun/security/provider/PolicyFile/TokenStore.java
author lana
Thu, 10 Jun 2010 18:58:31 -0700
changeset 5753 e0ee3917e318
parent 5506 202f599c92aa
child 30820 0d4717a011d3
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4919147
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Support for token-based KeyStores
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * TokenStore.keystore password is "TokenStore"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AllPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.provider.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class TokenStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static String DIR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                System.getProperty("test.classes", ".") + File.separatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final char[] storePassword = new char[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                { 'T', 'o', 'k', 'e', 'n', 'S', 't', 'o', 'r', 'e' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // policy files that will get written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static String NO_STORE_FILE =       DIR + "TokenStore.NoStore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static String URL_FILE =            DIR + "TokenStore.Url";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static String URL_T_FILE =          DIR + "TokenStore.UrlT";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static String URL_T_P_FILE =        DIR + "TokenStore.UrlTP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static String URL_PWD_FILE =        DIR + "TokenStore.UrlPwd";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static String URL_T_P_PWD_FILE =    DIR + "TokenStore.UrlTPPwd";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static String BADPASS_FILE =        DIR + "TokenStore.BadPass";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static String RELPASS_FILE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                System.getProperty("test.src", ".") + File.separatorChar +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                "TokenStore.RelPassPolicy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // protection domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static ProtectionDomain NO_STORE_DOMAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static ProtectionDomain URL_DOMAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static ProtectionDomain URL_T_DOMAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static ProtectionDomain URL_T_P_DOMAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // policy contents written to files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final String POLICY_NO_STORE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        "grant { permission java.security.AllPermission; };";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final String POLICY_URL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        "keystore \"file:${test.src}${/}TokenStore.keystore\";"         +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        "grant signedby \"POLICY_URL\" {"                               +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        "    permission java.security.AllPermission;"                   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        "};"                                                            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final String POLICY_URL_T =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        "keystore \"file:${test.src}${/}TokenStore.keystore\", \"JKS\";"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        "grant signedby \"POLICY_URL_T\" {"                             +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        "    permission java.security.AllPermission;"                   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        "};"                                                            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final String POLICY_URL_T_P =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        "keystore \"file:${test.src}${/}TokenStore.keystore\","         +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        "               \"JKS\", \"SUN\";"                              +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        "grant signedby \"POLICY_URL_T_P\" {"                           +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        "    permission java.security.AllPermission;"                   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        "};"                                                            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final String POLICY_URL_PWD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        "keystore \"file:${test.src}${/}TokenStore.keystore\";"         +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        "keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";"   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        "grant signedby \"POLICY_URL\" {"                               +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        "    permission java.security.AllPermission;"                   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        "};"                                                            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final String POLICY_URL_T_P_PWD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        "keystore \"file:${test.src}${/}TokenStore.keystore\","         +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        "               \"JKS\", \"SUN\";"                              +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        "keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";"   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        "grant signedby \"POLICY_URL_T_P\" {"                           +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        "    permission java.security.AllPermission;"                   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        "};"                                                            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final String POLICY_BADPASS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        "keystore \"file:${test.src}${/}TokenStore.keystore\","         +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        "               \"JKS\", \"SUN\";"                              +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        "keystorePasswordURL \"file:${test.src}${/}TokenStore.java\";"  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        "grant signedby \"POLICY_URL_T_P\" {"                           +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        "    permission java.security.AllPermission;"                   +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        "};"                                                            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static void init() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // first write policy files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        PolicyParser pp = new PolicyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        pp.read(new StringReader(POLICY_NO_STORE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        pp.write(new FileWriter(NO_STORE_FILE, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        pp = new PolicyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        pp.read(new StringReader(POLICY_URL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        pp.write(new FileWriter(URL_FILE, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        pp = new PolicyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        pp.read(new StringReader(POLICY_URL_T));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        pp.write(new FileWriter(URL_T_FILE, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        pp = new PolicyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        pp.read(new StringReader(POLICY_URL_T_P));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        pp.write(new FileWriter(URL_T_P_FILE, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        pp = new PolicyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        pp.read(new StringReader(POLICY_URL_PWD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        pp.write(new FileWriter(URL_PWD_FILE, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        pp = new PolicyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        pp.read(new StringReader(POLICY_URL_T_P_PWD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        pp.write(new FileWriter(URL_T_P_PWD_FILE, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        pp = new PolicyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        pp.read(new StringReader(POLICY_BADPASS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        pp.write(new FileWriter(BADPASS_FILE, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // next load keystore data to build PD's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        ks.load(new FileInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        (System.getProperty("test.src", ".") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        File.separatorChar +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        "TokenStore.keystore"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                storePassword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        NO_STORE_DOMAIN = new ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        (new CodeSource(new URL("file:/foo"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                            (java.security.cert.Certificate[]) null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        null,  // perms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        null,  // class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        null);  // principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        Certificate[] chain = (Certificate[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        ks.getCertificateChain("POLICY_URL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        URL_DOMAIN = new ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        (new CodeSource(new URL("file:/foo"), chain),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        null,  // perms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        null,  // class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        null);  // principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        chain = (Certificate[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        ks.getCertificateChain("POLICY_URL_T");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        URL_T_DOMAIN = new ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        (new CodeSource(new URL("file:/foo"), chain),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        null,  // perms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        null,  // class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        null);  // principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        chain = (Certificate[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        ks.getCertificateChain("POLICY_URL_T_P");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        URL_T_P_DOMAIN = new ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        (new CodeSource(new URL("file:/foo"), chain),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        null,  // perms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        null,  // class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        null);  // principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // test no key store in policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        System.setProperty("java.security.policy", "=" + NO_STORE_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        PolicyFile p = new PolicyFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        checkPerm(p, NO_STORE_DOMAIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // test policy keystore + URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        System.setProperty("java.security.policy", "=" + URL_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        p = new PolicyFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        checkPerm(p, URL_DOMAIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // test policy keystore + URL + type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        System.setProperty("java.security.policy", "=" + URL_T_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        p = new PolicyFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        checkPerm(p, URL_T_DOMAIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // test policy keystore + URL + type + provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        System.setProperty("java.security.policy", "=" + URL_T_P_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        p = new PolicyFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        checkPerm(p, URL_T_P_DOMAIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // test policy keystore + URL + password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        System.setProperty("java.security.policy", "=" + URL_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        p = new PolicyFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        checkPerm(p, URL_DOMAIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // test policy keystore + URL + type + provider + password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        System.setProperty("java.security.policy", "=" + URL_T_P_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        p = new PolicyFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        checkPerm(p, URL_T_P_DOMAIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // test policy keystore + URL + type + provider + BAD password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        System.setProperty("java.security.policy", "=" + BADPASS_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        p = new PolicyFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            checkPerm(p, URL_T_P_DOMAIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new RuntimeException("expected SecurityException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            //se.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // test policy keystore + URL + type + provider + RELATIVE password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        System.setProperty("java.security.policy", "=" + RELPASS_FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        p = new PolicyFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        checkPerm(p, URL_T_P_DOMAIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private static void checkPerm(PolicyFile p, ProtectionDomain pd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        boolean foundIt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Enumeration perms = p.getPermissions(pd).elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        while (perms.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            Permission perm = (Permission)perms.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (!(perm instanceof AllPermission)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                throw new SecurityException("expected AllPermission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                foundIt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (!foundIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throw new SecurityException("expected AllPermission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
}