jdk/test/java/lang/SecurityManager/CheckSecurityProvider.java
author amlu
Fri, 18 Nov 2016 14:39:03 +0800
changeset 42166 5582c83b1156
parent 39501 f971def61cb8
child 42693 6645de32a866
permissions -rw-r--r--
8169836: ProblemList update for java/lang/management/MemoryMXBean/PendingAllGC.sh Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
     1
/*
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
     4
 *
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
     8
 *
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    13
 * accompanied this code).
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    14
 *
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    18
 *
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    21
 * questions.
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    22
 */
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    23
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    24
/*
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    25
 * @test
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30506
diff changeset
    26
 * @bug 6997010 7191662
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    27
 * @summary Consolidate java.security files into one file with modifications
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30506
diff changeset
    28
 * @run main/othervm CheckSecurityProvider
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    29
 */
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    30
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    31
import java.lang.reflect.Layer;
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    32
import java.security.Provider;
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    33
import java.security.Security;
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    34
import java.util.ArrayList;
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    35
import java.util.Iterator;
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    36
import java.util.List;
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    37
import java.util.stream.Collectors;
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    38
import java.util.stream.Stream;
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    39
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    40
/*
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    41
 * The main benefit of this test is to catch merge errors or other types
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    42
 * of issues where one or more of the security providers are accidentally
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30506
diff changeset
    43
 * removed. With the security manager enabled, this test can also catch
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30506
diff changeset
    44
 * scenarios where the default permission policy needs to be updated.
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    45
 */
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    46
public class CheckSecurityProvider {
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    48
        Layer layer = Layer.boot();
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    49
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30506
diff changeset
    50
        System.setSecurityManager(new SecurityManager());
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    51
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    52
        String os = System.getProperty("os.name");
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    53
        /*
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    54
         * This array should be updated whenever new security providers
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    55
         * are added to the the java.security file.
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    56
         * NOTE: it should be in the same order as the java.security file
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    57
         */
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    58
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    59
        List<String> expected = new ArrayList<>();
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    60
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30506
diff changeset
    61
        // NOTE: the ordering must match what's defined inside java.security
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    62
        if (os.equals("SunOS")) {
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    63
            layer.findModule("jdk.crypto.ucrypto")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    64
                .ifPresent(m -> expected.add("com.oracle.security.ucrypto.UcryptoProvider"));
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    65
            layer.findModule("jdk.crypto.pkcs11")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    66
                .ifPresent(m -> expected.add("sun.security.pkcs11.SunPKCS11"));
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    67
        }
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    68
        expected.add("sun.security.provider.Sun");
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    69
        expected.add("sun.security.rsa.SunRsaSign");
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    70
        layer.findModule("jdk.crypto.ec")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    71
            .ifPresent(m -> expected.add("sun.security.ec.SunEC"));
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    72
        expected.add("com.sun.net.ssl.internal.ssl.Provider");
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    73
        expected.add("com.sun.crypto.provider.SunJCE");
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    74
        layer.findModule("jdk.security.jgss")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    75
            .ifPresent(m -> expected.add("sun.security.jgss.SunProvider"));
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    76
        layer.findModule("java.security.sasl")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    77
            .ifPresent(m -> expected.add("com.sun.security.sasl.Provider"));
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    78
        layer.findModule("java.xml.crypto")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    79
            .ifPresent(m -> expected.add("org.jcp.xml.dsig.internal.dom.XMLDSigRI"));
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    80
        layer.findModule("java.smartcardio")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    81
            .ifPresent(m -> expected.add("sun.security.smartcardio.SunPCSC"));
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    82
        layer.findModule("java.naming")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    83
            .ifPresent(m -> expected.add("sun.security.provider.certpath.ldap.JdkLDAP"));
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    84
        layer.findModule("jdk.security.jgss")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    85
            .ifPresent(m -> expected.add("com.sun.security.sasl.gsskerb.JdkSASL"));
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    86
        if (os.startsWith("Windows")) {
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    87
            layer.findModule("jdk.crypto.mscapi")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    88
                .ifPresent(m -> expected.add("sun.security.mscapi.SunMSCAPI"));
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    89
        }
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    90
        if (os.contains("OS X")) {
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    91
            expected.add("apple.security.AppleProvider");
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    92
        }
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30506
diff changeset
    93
        if (!os.equals("SunOS")) {
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    94
            layer.findModule("jdk.crypto.pkcs11")
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    95
                .ifPresent(m -> expected.add("sun.security.pkcs11.SunPKCS11"));
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30506
diff changeset
    96
        }
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
    97
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    98
        List<String> actual = Stream.of(Security.getProviders())
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
    99
            .map(p -> p.getClass().getName())
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   100
            .collect(Collectors.toList());
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   101
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   102
        System.out.println("Expected providers:");
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   103
        expected.stream().forEach(System.out::println);
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   104
        System.out.println("Actual providers:");
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   105
        actual.stream().forEach(System.out::println);
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   106
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   107
        if (expected.size() != actual.size()) {
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   108
            throw new Exception("Unexpected provider count. "
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   109
                + "Expected: " + expected.size() + ". Actual: " + actual.size());
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   110
        }
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
   111
        Iterator<String> iter = expected.iterator();
39501
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   112
        for (String p: actual) {
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   113
            String nextExpected = iter.next();
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   114
            if (!nextExpected.equals(p)) {
f971def61cb8 8158670: Fix @modules in java/lang/SecurityManager/CheckSecurityProvider.java
shurailine
parents: 31270
diff changeset
   115
                throw new Exception("Expected " + nextExpected + ", actual " + p);
25974
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
   116
            }
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
   117
        }
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
   118
    }
850dc36ea410 6997010: Consolidate java.security files into one file with modifications
weijun
parents:
diff changeset
   119
}