Path: blob/master/test/jdk/java/security/KeyFactory/Failover.java
41149 views
/*1* Copyright (c) 2003, 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 4894125 7054918 813018126* @library ../testlibrary27* @summary test that failover for KeyFactory works28* @author Andreas Sterbenz29*/3031import java.util.*;3233import java.security.*;34import java.security.interfaces.*;35import java.security.spec.*;3637public class Failover {3839public static void main(String[] args) throws Exception {40ProvidersSnapshot snapshot = ProvidersSnapshot.create();41try {42main0(args);43} finally {44snapshot.restore();45}46}4748public static void main0(String[] args) throws Exception {49Security.insertProviderAt(new ProviderFail(), 1);50Security.addProvider(new ProviderPass());51System.out.println(Arrays.asList(Security.getProviders()));5253KeyFactory kf;54kf = KeyFactory.getInstance("FOO");55kf.generatePublic(null);56kf.generatePublic(null);57kf.generatePrivate(null);5859kf = KeyFactory.getInstance("FOO");60kf.generatePrivate(null);61kf.getKeySpec(null, null);62kf.translateKey(null);6364kf = KeyFactory.getInstance("FOO");65kf.getKeySpec(null, null);66kf.translateKey(null);6768kf = KeyFactory.getInstance("FOO");69kf.translateKey(null);7071// somewhat more real tests using DSA72System.out.println("DSA tests...");7374KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");75kpg.initialize(512);76KeyPair kp = kpg.generateKeyPair();7778kf = KeyFactory.getInstance("DSA");79System.out.println(kf.translateKey(kp.getPrivate()));8081kf = KeyFactory.getInstance("DSA");82KeySpec spec = kf.getKeySpec(kp.getPublic(), DSAPublicKeySpec.class);8384kf = KeyFactory.getInstance("DSA");85System.out.println(kf.generatePublic(spec));8687kf = KeyFactory.getInstance("DSA");88try {89kf.generatePrivate(spec);90throw new Exception("no exception");91} catch (InvalidKeySpecException e) {92System.out.println(e);93}94}9596private static class ProviderPass extends Provider {97ProviderPass() {98super("Pass", "1.0", "Pass");99put("KeyFactory.FOO" , "Failover$KeyFactoryPass");100}101}102103private static class ProviderFail extends Provider {104ProviderFail() {105super("Fail", "1.0", "Fail");106put("KeyFactory.FOO" , "Failover$KeyFactoryFail");107put("KeyFactory.DSA" , "Failover$KeyFactoryFail");108}109}110111public static class KeyFactoryPass extends KeyFactorySpi {112113protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {114System.out.println("MyKeyFactoryPass.engineGeneratePublic()");115return null;116}117118protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {119System.out.println("MyKeyFactoryPass.engineGeneratePrivate()");120return null;121}122123protected <T extends KeySpec> T124engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException125{126System.out.println("MyKeyFactoryPass.engineGetKeySpec()");127return null;128}129130protected Key engineTranslateKey(Key key) throws InvalidKeyException {131System.out.println("MyKeyFactoryPass.engineTranslateKey()");132return null;133}134135}136137public static class KeyFactoryFail extends KeyFactorySpi {138139protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {140System.out.println("MyKeyFactoryFail.engineGeneratePublic()");141throw new InvalidKeySpecException();142}143144protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {145System.out.println("MyKeyFactoryFail.engineGeneratePrivate()");146throw new InvalidKeySpecException();147}148149protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {150System.out.println("MyKeyFactoryFail.engineGetKeySpec()");151throw new InvalidKeySpecException();152}153154protected Key engineTranslateKey(Key key) throws InvalidKeyException {155System.out.println("MyKeyFactoryFail.engineTranslateKey()");156throw new InvalidKeyException();157}158159}160161}162163164