jdk/test/java/lang/SecurityManager/CheckSecurityProvider.java
changeset 39501 f971def61cb8
parent 31270 e6470b24700d
child 42693 6645de32a866
equal deleted inserted replaced
39500:2b5f7ee28c5d 39501:f971def61cb8
     1 /*
     1 /*
     2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    26  * @bug 6997010 7191662
    26  * @bug 6997010 7191662
    27  * @summary Consolidate java.security files into one file with modifications
    27  * @summary Consolidate java.security files into one file with modifications
    28  * @run main/othervm CheckSecurityProvider
    28  * @run main/othervm CheckSecurityProvider
    29  */
    29  */
    30 
    30 
       
    31 import java.lang.reflect.Layer;
    31 import java.security.Provider;
    32 import java.security.Provider;
    32 import java.security.Security;
    33 import java.security.Security;
    33 import java.util.ArrayList;
    34 import java.util.ArrayList;
    34 import java.util.Iterator;
    35 import java.util.Iterator;
    35 import java.util.List;
    36 import java.util.List;
       
    37 import java.util.stream.Collectors;
       
    38 import java.util.stream.Stream;
    36 
    39 
    37 /*
    40 /*
    38  * The main benefit of this test is to catch merge errors or other types
    41  * The main benefit of this test is to catch merge errors or other types
    39  * of issues where one or more of the security providers are accidentally
    42  * of issues where one or more of the security providers are accidentally
    40  * removed. With the security manager enabled, this test can also catch
    43  * removed. With the security manager enabled, this test can also catch
    41  * scenarios where the default permission policy needs to be updated.
    44  * scenarios where the default permission policy needs to be updated.
    42  */
    45  */
    43 public class CheckSecurityProvider {
    46 public class CheckSecurityProvider {
    44     public static void main(String[] args) throws Exception {
    47     public static void main(String[] args) throws Exception {
       
    48         Layer layer = Layer.boot();
       
    49 
    45         System.setSecurityManager(new SecurityManager());
    50         System.setSecurityManager(new SecurityManager());
    46 
    51 
    47         String os = System.getProperty("os.name");
    52         String os = System.getProperty("os.name");
    48         /*
    53         /*
    49          * This array should be updated whenever new security providers
    54          * This array should be updated whenever new security providers
    53 
    58 
    54         List<String> expected = new ArrayList<>();
    59         List<String> expected = new ArrayList<>();
    55 
    60 
    56         // NOTE: the ordering must match what's defined inside java.security
    61         // NOTE: the ordering must match what's defined inside java.security
    57         if (os.equals("SunOS")) {
    62         if (os.equals("SunOS")) {
    58             expected.add("com.oracle.security.ucrypto.UcryptoProvider");
    63             layer.findModule("jdk.crypto.ucrypto")
    59             expected.add("sun.security.pkcs11.SunPKCS11");
    64                 .ifPresent(m -> expected.add("com.oracle.security.ucrypto.UcryptoProvider"));
       
    65             layer.findModule("jdk.crypto.pkcs11")
       
    66                 .ifPresent(m -> expected.add("sun.security.pkcs11.SunPKCS11"));
    60         }
    67         }
    61         expected.add("sun.security.provider.Sun");
    68         expected.add("sun.security.provider.Sun");
    62         expected.add("sun.security.rsa.SunRsaSign");
    69         expected.add("sun.security.rsa.SunRsaSign");
    63         expected.add("sun.security.ec.SunEC");
    70         layer.findModule("jdk.crypto.ec")
       
    71             .ifPresent(m -> expected.add("sun.security.ec.SunEC"));
    64         expected.add("com.sun.net.ssl.internal.ssl.Provider");
    72         expected.add("com.sun.net.ssl.internal.ssl.Provider");
    65         expected.add("com.sun.crypto.provider.SunJCE");
    73         expected.add("com.sun.crypto.provider.SunJCE");
    66         expected.add("sun.security.jgss.SunProvider");
    74         layer.findModule("jdk.security.jgss")
    67         expected.add("com.sun.security.sasl.Provider");
    75             .ifPresent(m -> expected.add("sun.security.jgss.SunProvider"));
    68         expected.add("org.jcp.xml.dsig.internal.dom.XMLDSigRI");
    76         layer.findModule("java.security.sasl")
    69         expected.add("sun.security.smartcardio.SunPCSC");
    77             .ifPresent(m -> expected.add("com.sun.security.sasl.Provider"));
    70         expected.add("sun.security.provider.certpath.ldap.JdkLDAP");
    78         layer.findModule("java.xml.crypto")
    71         expected.add("com.sun.security.sasl.gsskerb.JdkSASL");
    79             .ifPresent(m -> expected.add("org.jcp.xml.dsig.internal.dom.XMLDSigRI"));
       
    80         layer.findModule("java.smartcardio")
       
    81             .ifPresent(m -> expected.add("sun.security.smartcardio.SunPCSC"));
       
    82         layer.findModule("java.naming")
       
    83             .ifPresent(m -> expected.add("sun.security.provider.certpath.ldap.JdkLDAP"));
       
    84         layer.findModule("jdk.security.jgss")
       
    85             .ifPresent(m -> expected.add("com.sun.security.sasl.gsskerb.JdkSASL"));
    72         if (os.startsWith("Windows")) {
    86         if (os.startsWith("Windows")) {
    73             expected.add("sun.security.mscapi.SunMSCAPI");
    87             layer.findModule("jdk.crypto.mscapi")
       
    88                 .ifPresent(m -> expected.add("sun.security.mscapi.SunMSCAPI"));
    74         }
    89         }
    75         if (os.contains("OS X")) {
    90         if (os.contains("OS X")) {
    76             expected.add("apple.security.AppleProvider");
    91             expected.add("apple.security.AppleProvider");
    77         }
    92         }
    78         if (!os.equals("SunOS")) {
    93         if (!os.equals("SunOS")) {
    79             expected.add("sun.security.pkcs11.SunPKCS11");
    94             layer.findModule("jdk.crypto.pkcs11")
       
    95                 .ifPresent(m -> expected.add("sun.security.pkcs11.SunPKCS11"));
    80         }
    96         }
    81 
    97 
       
    98         List<String> actual = Stream.of(Security.getProviders())
       
    99             .map(p -> p.getClass().getName())
       
   100             .collect(Collectors.toList());
       
   101 
       
   102         System.out.println("Expected providers:");
       
   103         expected.stream().forEach(System.out::println);
       
   104         System.out.println("Actual providers:");
       
   105         actual.stream().forEach(System.out::println);
       
   106 
       
   107         if (expected.size() != actual.size()) {
       
   108             throw new Exception("Unexpected provider count. "
       
   109                 + "Expected: " + expected.size() + ". Actual: " + actual.size());
       
   110         }
    82         Iterator<String> iter = expected.iterator();
   111         Iterator<String> iter = expected.iterator();
    83         for (Provider p: Security.getProviders()) {
   112         for (String p: actual) {
    84             if (!iter.hasNext()) {
   113             String nextExpected = iter.next();
    85                 throw new Exception("Less expected");
   114             if (!nextExpected.equals(p)) {
       
   115                 throw new Exception("Expected " + nextExpected + ", actual " + p);
    86             }
   116             }
    87             String n1 = iter.next();
       
    88             String n2 = p.getClass().getName();
       
    89             if (!n1.equals(n2)) {
       
    90                 throw new Exception("Expected " + n1 + ", actual " + n2);
       
    91             }
       
    92         }
       
    93         if (iter.hasNext()) {
       
    94             throw new Exception("More expected");
       
    95         }
   117         }
    96     }
   118     }
    97 }
   119 }