Path: blob/master/test/jdk/java/security/SecureRandom/DefaultAlgo.java
41149 views
/*1* Copyright (c) 2019, 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*/2223import static java.lang.System.out;24import java.security.Provider;25import java.security.Security;26import java.security.SecureRandom;27import java.security.Provider.Service;28import java.util.Arrays;29import sun.security.provider.SunEntries;3031/**32* @test33* @bug 8228613 8246613 824850534* @summary Ensure that the default SecureRandom algo used is based35* on the registration ordering, and falls to next provider36* if none are found37* @modules java.base/sun.security.provider38*/39public class DefaultAlgo {4041private static final String SR_IMPLCLASS =42"sun.security.provider.SecureRandom";4344public static void main(String[] args) throws Exception {45String[] algos = { "A", "B", "C" };46test3rdParty(algos);47// reverse the order and re-check48String[] algosReversed = { "C", "B", "A" };49test3rdParty(algosReversed);50}5152private static void test3rdParty(String[] algos) {53Provider[] provs = {54new SampleLegacyProvider(algos),55new SampleServiceProvider(algos),56new CustomProvider(algos)57};58for (Provider p : provs) {59checkDefault(p, algos);60}61}6263// validate the specified SecureRandom obj to be from the specified64// provider and matches the specified algorithm65private static void validate(SecureRandom sr, String pName, String algo) {66if (!sr.getProvider().getName().equals(pName)) {67throw new RuntimeException("Failed provider check, exp: " +68pName + ", got " + sr.getProvider().getName());69}70if (!sr.getAlgorithm().equals(algo)) {71throw new RuntimeException("Failed algo check, exp: " +72algo + ", got " + sr.getAlgorithm());73}74}7576private static void checkDefault(Provider p, String ... algos) {77String pName = p.getName();78out.println(pName + " with " + Arrays.toString(algos));79int pos = Security.insertProviderAt(p, 1);8081boolean isLegacy = pName.equals("SampleLegacy");82try {83if (isLegacy) {84for (String s : algos) {85validate(new SecureRandom(), pName, s);86p.remove("SecureRandom." + s);87out.println("removed " + s);88}89validate(new SecureRandom(), "SUN",90SunEntries.DEF_SECURE_RANDOM_ALGO);91} else {92validate(new SecureRandom(), pName, algos[0]);93}94out.println("=> Test Passed");95} finally {96if (pos != -1) {97Security.removeProvider(pName);98}99if (isLegacy) {100// add back the removed algos101for (String s : algos) {102p.put("SecureRandom." + s, SR_IMPLCLASS);103}104}105}106}107108private static class SampleLegacyProvider extends Provider {109SampleLegacyProvider(String[] listOfSupportedRNGs) {110super("SampleLegacy", "1.0", "test provider using legacy put");111for (String s : listOfSupportedRNGs) {112put("SecureRandom." + s, SR_IMPLCLASS);113}114}115}116117private static class SampleServiceProvider extends Provider {118SampleServiceProvider(String[] listOfSupportedRNGs) {119super("SampleService", "1.0", "test provider using putService");120for (String s : listOfSupportedRNGs) {121putService(new Provider.Service(this, "SecureRandom", s,122SR_IMPLCLASS, null, null));123}124}125}126127// custom provider which overrides putService(...)/getService() and uses128// its own Service impl129private static class CustomProvider extends Provider {130private static class CustomService extends Provider.Service {131CustomService(Provider p, String type, String algo, String cName) {132super(p, type, algo, cName, null, null);133}134}135136CustomProvider(String[] listOfSupportedRNGs) {137super("Custom", "1.0", "test provider overrides putService with " +138" custom service with legacy registration");139for (String s : listOfSupportedRNGs) {140putService(new CustomService(this, "SecureRandom", s ,141SR_IMPLCLASS));142}143}144@Override145protected void putService(Provider.Service s) {146// convert to legacy puts147put(s.getType() + "." + s.getAlgorithm(), s.getClassName());148put(s.getType() + ":" + s.getAlgorithm(), s);149}150@Override151public Provider.Service getService(String type, String algo) {152return (Provider.Service) get(type + ":" + algo);153}154}155}156157158