jdk/test/java/security/KeyFactory/Failover.java
author mcimadamore
Wed, 31 Aug 2011 16:11:28 +0100
changeset 10452 c8f32009b413
parent 10057 03ae05289550
child 40416 5d91b2fd668c
permissions -rw-r--r--
7085024: internal error; cannot instantiate Foo Summary: Types.isConvertible does not handle erroneous types correctly Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10057
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
10057
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    26
 * @bug 4894125 7054918
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    27
 * @library ../testlibrary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary test that failover for KeyFactory works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class Failover {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
10057
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    41
        ProvidersSnapshot snapshot = ProvidersSnapshot.create();
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    42
        try {
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    43
            main0(args);
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    44
        } finally {
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    45
            snapshot.restore();
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    46
        }
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    47
    }
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    48
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    49
    public static void main0(String[] args) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Security.insertProviderAt(new ProviderFail(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Security.addProvider(new ProviderPass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        System.out.println(Arrays.asList(Security.getProviders()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        KeyFactory kf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        kf = KeyFactory.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        kf.generatePublic(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        kf.generatePublic(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        kf.generatePrivate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        kf = KeyFactory.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        kf.generatePrivate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        kf.getKeySpec(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        kf.translateKey(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        kf = KeyFactory.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        kf.getKeySpec(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        kf.translateKey(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        kf = KeyFactory.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        kf.translateKey(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // somewhat more real tests using DSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        System.out.println("DSA tests...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        kpg.initialize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        kf = KeyFactory.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println(kf.translateKey(kp.getPrivate()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        kf = KeyFactory.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        KeySpec spec = kf.getKeySpec(kp.getPublic(), DSAPublicKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        kf = KeyFactory.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        System.out.println(kf.generatePublic(spec));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        kf = KeyFactory.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            kf.generatePrivate(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new Exception("no exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static class ProviderPass extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        ProviderPass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            super("Pass", 1.0d, "Pass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            put("KeyFactory.FOO" , "Failover$KeyFactoryPass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static class ProviderFail extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        ProviderFail() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            super("Fail", 1.0d, "Fail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            put("KeyFactory.FOO" , "Failover$KeyFactoryFail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            put("KeyFactory.DSA" , "Failover$KeyFactoryFail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static class KeyFactoryPass extends KeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println("MyKeyFactoryPass.engineGeneratePublic()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println("MyKeyFactoryPass.engineGeneratePrivate()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        protected <T extends KeySpec> T
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            System.out.println("MyKeyFactoryPass.engineGetKeySpec()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        protected Key engineTranslateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            System.out.println("MyKeyFactoryPass.engineTranslateKey()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static class KeyFactoryFail extends KeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            System.out.println("MyKeyFactoryFail.engineGeneratePublic()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new InvalidKeySpecException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.out.println("MyKeyFactoryFail.engineGeneratePrivate()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new InvalidKeySpecException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            System.out.println("MyKeyFactoryFail.engineGetKeySpec()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new InvalidKeySpecException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        protected Key engineTranslateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            System.out.println("MyKeyFactoryFail.engineTranslateKey()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new InvalidKeyException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}