jdk/test/java/lang/SecurityManager/CheckSecurityProvider.java
author valeriep
Fri, 26 Jun 2015 21:34:34 +0000
changeset 31270 e6470b24700d
parent 30506 1998a5644f50
child 39501 f971def61cb8
permissions -rw-r--r--
7191662: JCE providers should be located via ServiceLoader Summary: Enhanced to use ServiceLoader and switched provider to Provider.Service model. Reviewed-by: mullan, alanb, mchung

/*
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 6997010 7191662
 * @summary Consolidate java.security files into one file with modifications
 * @run main/othervm CheckSecurityProvider
 */

import java.security.Provider;
import java.security.Security;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
 * The main benefit of this test is to catch merge errors or other types
 * of issues where one or more of the security providers are accidentally
 * removed. With the security manager enabled, this test can also catch
 * scenarios where the default permission policy needs to be updated.
 */
public class CheckSecurityProvider {
    public static void main(String[] args) throws Exception {
        System.setSecurityManager(new SecurityManager());

        String os = System.getProperty("os.name");
        /*
         * This array should be updated whenever new security providers
         * are added to the the java.security file.
         * NOTE: it should be in the same order as the java.security file
         */

        List<String> expected = new ArrayList<>();

        // NOTE: the ordering must match what's defined inside java.security
        if (os.equals("SunOS")) {
            expected.add("com.oracle.security.ucrypto.UcryptoProvider");
            expected.add("sun.security.pkcs11.SunPKCS11");
        }
        expected.add("sun.security.provider.Sun");
        expected.add("sun.security.rsa.SunRsaSign");
        expected.add("sun.security.ec.SunEC");
        expected.add("com.sun.net.ssl.internal.ssl.Provider");
        expected.add("com.sun.crypto.provider.SunJCE");
        expected.add("sun.security.jgss.SunProvider");
        expected.add("com.sun.security.sasl.Provider");
        expected.add("org.jcp.xml.dsig.internal.dom.XMLDSigRI");
        expected.add("sun.security.smartcardio.SunPCSC");
        expected.add("sun.security.provider.certpath.ldap.JdkLDAP");
        expected.add("com.sun.security.sasl.gsskerb.JdkSASL");
        if (os.startsWith("Windows")) {
            expected.add("sun.security.mscapi.SunMSCAPI");
        }
        if (os.contains("OS X")) {
            expected.add("apple.security.AppleProvider");
        }
        if (!os.equals("SunOS")) {
            expected.add("sun.security.pkcs11.SunPKCS11");
        }

        Iterator<String> iter = expected.iterator();
        for (Provider p: Security.getProviders()) {
            if (!iter.hasNext()) {
                throw new Exception("Less expected");
            }
            String n1 = iter.next();
            String n2 = p.getClass().getName();
            if (!n1.equals(n2)) {
                throw new Exception("Expected " + n1 + ", actual " + n2);
            }
        }
        if (iter.hasNext()) {
            throw new Exception("More expected");
        }
    }
}