Path: blob/master/test/jdk/java/security/Provider/DefaultProviderList.java
41149 views
/*1* Copyright (c) 2015, 2016, 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 7191662 8157469 815748926* @summary Ensure non-java.base providers can be found by ServiceLoader27* @author Valerie Peng28*/2930import java.security.Provider;31import java.security.Security;32import java.util.Arrays;33import java.util.Iterator;34import java.util.ServiceLoader;3536public class DefaultProviderList {3738public static void main(String[] args) throws Exception {39Provider[] defaultProvs = Security.getProviders();40System.out.println("Providers: " + Arrays.asList(defaultProvs));41System.out.println();4243ServiceLoader<Provider> sl = ServiceLoader.load(Provider.class);44boolean failed = false;4546Module baseMod = Object.class.getModule();4748// Test#1: check that all non-base security providers can be found49// through ServiceLoader50for (Provider p : defaultProvs) {51String pName = p.getName();52Class pClass = p.getClass();5354if (pClass.getModule() != baseMod) {55String pClassName = pClass.getName();56Iterator<Provider> provIter = sl.iterator();57boolean found = false;58while (provIter.hasNext()) {59Provider pFromSL = provIter.next();6061// check for match by class name because PKCS11 provider62// will have a different name after being configured.63if (pFromSL.getClass().getName().equals(pClassName)) {64found = true;65System.out.println("SL found provider " + pName);66break;67}68}69if (!found) {70failed = true;71System.out.println("Error: SL cannot find provider " +72pName);73}74}75}7677// Test#2: check that all security providers found through ServiceLoader78// are not from base module79Iterator<Provider> provIter = sl.iterator();80while (provIter.hasNext()) {81Provider pFromSL = provIter.next();82if (pFromSL.getClass().getModule() == baseMod) {83failed = true;84System.out.println("Error: base provider " +85pFromSL.getName() + " loaded by SL");86}87}8889if (!failed) {90System.out.println("Test Passed");91} else {92throw new Exception("One or more tests failed");93}94}95}969798