blob: e886ee843582a59e14bca17a27e255b992b1a384 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 4894125
27 * @summary test that failover for KeyFactory works
28 * @author Andreas Sterbenz
29 */
30
31import java.util.*;
32
33import java.security.*;
34import java.security.interfaces.*;
35import java.security.spec.*;
36
37public class Failover {
38
39 public static void main(String[] args) throws Exception {
40 Security.insertProviderAt(new ProviderFail(), 1);
41 Security.addProvider(new ProviderPass());
42 System.out.println(Arrays.asList(Security.getProviders()));
43
44 KeyFactory kf;
45 kf = KeyFactory.getInstance("FOO");
46 kf.generatePublic(null);
47 kf.generatePublic(null);
48 kf.generatePrivate(null);
49
50 kf = KeyFactory.getInstance("FOO");
51 kf.generatePrivate(null);
52 kf.getKeySpec(null, null);
53 kf.translateKey(null);
54
55 kf = KeyFactory.getInstance("FOO");
56 kf.getKeySpec(null, null);
57 kf.translateKey(null);
58
59 kf = KeyFactory.getInstance("FOO");
60 kf.translateKey(null);
61
62 // somewhat more real tests using DSA
63 System.out.println("DSA tests...");
64
65 KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
66 kpg.initialize(512);
67 KeyPair kp = kpg.generateKeyPair();
68
69 kf = KeyFactory.getInstance("DSA");
70 System.out.println(kf.translateKey(kp.getPrivate()));
71
72 kf = KeyFactory.getInstance("DSA");
73 KeySpec spec = kf.getKeySpec(kp.getPublic(), DSAPublicKeySpec.class);
74
75 kf = KeyFactory.getInstance("DSA");
76 System.out.println(kf.generatePublic(spec));
77
78 kf = KeyFactory.getInstance("DSA");
79 try {
80 kf.generatePrivate(spec);
81 throw new Exception("no exception");
82 } catch (InvalidKeySpecException e) {
83 System.out.println(e);
84 }
85 }
86
87 private static class ProviderPass extends Provider {
88 ProviderPass() {
89 super("Pass", 1.0d, "Pass");
90 put("KeyFactory.FOO" , "Failover$KeyFactoryPass");
91 }
92 }
93
94 private static class ProviderFail extends Provider {
95 ProviderFail() {
96 super("Fail", 1.0d, "Fail");
97 put("KeyFactory.FOO" , "Failover$KeyFactoryFail");
98 put("KeyFactory.DSA" , "Failover$KeyFactoryFail");
99 }
100 }
101
102 public static class KeyFactoryPass extends KeyFactorySpi {
103
104 protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
105 System.out.println("MyKeyFactoryPass.engineGeneratePublic()");
106 return null;
107 }
108
109 protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
110 System.out.println("MyKeyFactoryPass.engineGeneratePrivate()");
111 return null;
112 }
113
114 protected <T extends KeySpec> T
115 engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException
116 {
117 System.out.println("MyKeyFactoryPass.engineGetKeySpec()");
118 return null;
119 }
120
121 protected Key engineTranslateKey(Key key) throws InvalidKeyException {
122 System.out.println("MyKeyFactoryPass.engineTranslateKey()");
123 return null;
124 }
125
126 }
127
128 public static class KeyFactoryFail extends KeyFactorySpi {
129
130 protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
131 System.out.println("MyKeyFactoryFail.engineGeneratePublic()");
132 throw new InvalidKeySpecException();
133 }
134
135 protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
136 System.out.println("MyKeyFactoryFail.engineGeneratePrivate()");
137 throw new InvalidKeySpecException();
138 }
139
140 protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
141 System.out.println("MyKeyFactoryFail.engineGetKeySpec()");
142 throw new InvalidKeySpecException();
143 }
144
145 protected Key engineTranslateKey(Key key) throws InvalidKeyException {
146 System.out.println("MyKeyFactoryFail.engineTranslateKey()");
147 throw new InvalidKeyException();
148 }
149
150 }
151
152}