Path: blob/master/test/jdk/java/lang/SecurityManager/CheckSecurityProvider.java
41149 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6997010 719166226* @summary Consolidate java.security files into one file with modifications27* @run main/othervm -Djava.security.manager=allow CheckSecurityProvider28*/2930import java.security.Provider;31import java.security.Security;32import java.util.ArrayList;33import java.util.Iterator;34import java.util.List;35import java.util.stream.Collectors;36import java.util.stream.Stream;3738/*39* The main benefit of this test is to catch merge errors or other types40* of issues where one or more of the security providers are accidentally41* removed. With the security manager enabled, this test can also catch42* scenarios where the default permission policy needs to be updated.43*/44public class CheckSecurityProvider {45public static void main(String[] args) throws Exception {46ModuleLayer layer = ModuleLayer.boot();4748System.setSecurityManager(new SecurityManager());4950String os = System.getProperty("os.name");51/*52* This array should be updated whenever new security providers53* are added to the the java.security file.54* NOTE: it should be in the same order as the java.security file55*/5657List<String> expected = new ArrayList<>();5859// NOTE: the ordering must match what's defined inside java.security60expected.add("sun.security.provider.Sun");61expected.add("sun.security.rsa.SunRsaSign");62layer.findModule("jdk.crypto.ec")63.ifPresent(m -> expected.add("sun.security.ec.SunEC"));64expected.add("sun.security.ssl.SunJSSE");65expected.add("com.sun.crypto.provider.SunJCE");66layer.findModule("jdk.security.jgss")67.ifPresent(m -> expected.add("sun.security.jgss.SunProvider"));68layer.findModule("java.security.sasl")69.ifPresent(m -> expected.add("com.sun.security.sasl.Provider"));70layer.findModule("java.xml.crypto")71.ifPresent(m -> expected.add("org.jcp.xml.dsig.internal.dom.XMLDSigRI"));72layer.findModule("java.smartcardio")73.ifPresent(m -> expected.add("sun.security.smartcardio.SunPCSC"));74layer.findModule("java.naming")75.ifPresent(m -> expected.add("sun.security.provider.certpath.ldap.JdkLDAP"));76layer.findModule("jdk.security.jgss")77.ifPresent(m -> expected.add("com.sun.security.sasl.gsskerb.JdkSASL"));78if (os.startsWith("Windows")) {79layer.findModule("jdk.crypto.mscapi")80.ifPresent(m -> expected.add("sun.security.mscapi.SunMSCAPI"));81}82if (os.contains("OS X")) {83expected.add("apple.security.AppleProvider");84}85layer.findModule("jdk.crypto.cryptoki")86.ifPresent(m -> expected.add("sun.security.pkcs11.SunPKCS11"));8788List<String> actual = Stream.of(Security.getProviders())89.map(p -> p.getClass().getName())90.collect(Collectors.toList());9192System.out.println("Expected providers:");93expected.stream().forEach(System.out::println);94System.out.println("Actual providers:");95actual.stream().forEach(System.out::println);9697if (expected.size() != actual.size()) {98throw new Exception("Unexpected provider count. "99+ "Expected: " + expected.size() + ". Actual: " + actual.size());100}101Iterator<String> iter = expected.iterator();102for (String p: actual) {103String nextExpected = iter.next();104if (!nextExpected.equals(p)) {105throw new Exception("Expected " + nextExpected + ", actual " + p);106}107}108}109}110111112