jdk/test/java/security/KeyPairGenerator/Failover.java
author jjg
Mon, 25 Nov 2013 17:42:28 -0800
changeset 21894 3535c1819067
parent 10057 03ae05289550
child 40416 5d91b2fd668c
permissions -rw-r--r--
8028318: [doclint] doclint will reject existing user-written doc comments using custom tags that follow the recommended rules Reviewed-by: darcy
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 KeyPairGenerator 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
        KeyPairGenerator kpg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        kpg = KeyPairGenerator.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        kpg = KeyPairGenerator.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        kpg.initialize(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        kpg.initialize(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        kpg.initialize(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        kpg = KeyPairGenerator.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        kpg.initialize(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        kpg = KeyPairGenerator.getInstance("FOO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        kpg.initialize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // the SUN DSA KeyPairGenerator implementation extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // KeyPairGenerator (in order to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // java.security.interfaces.DSAKeyPairGenerator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // failover cannot work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        kpg = KeyPairGenerator.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            kpg.initialize(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new Exception("no exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } catch (InvalidParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        KeyPair kp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        kpg = KeyPairGenerator.getInstance("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        kpg.initialize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        System.out.println(kp.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        kpg = KeyPairGenerator.getInstance("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        kpg.initialize(768);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.out.println(kp.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        kpg = KeyPairGenerator.getInstance("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.out.println(kp.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        kpg = KeyPairGenerator.getInstance("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            kpg.initialize(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new Exception("no exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch (InvalidParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static class ProviderPass extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        ProviderPass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            super("Pass", 1.0d, "Pass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            put("KeyPairGenerator.FOO" , "Failover$KeyPairGeneratorPass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static class ProviderFail extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        ProviderFail() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            super("Fail", 1.0d, "Fail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            put("KeyPairGenerator.FOO" , "Failover$KeyPairGeneratorFail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            put("KeyPairGenerator.DSA" , "Failover$KeyPairGeneratorFail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            put("KeyPairGenerator.RSA" , "Failover$KeyPairGeneratorFail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public static class KeyPairGeneratorPass extends KeyPairGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        public void initialize(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.out.println("KeyPairGeneratorPass.initialize(" + keysize + ", " + random + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        public void initialize(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            System.out.println("KeyPairGeneratorPass.initialize()");
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 KeyPair generateKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.out.println("KeyPairGeneratorPass.generateKeyPair()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static class KeyPairGeneratorFail extends KeyPairGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        public void initialize(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (keysize != 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                System.out.println("KeyPairGeneratorFail.initialize()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new InvalidParameterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            System.out.println("KeyPairGeneratorFail.initialize() PASS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        public void initialize(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            System.out.println("KeyPairGeneratorFail.initialize()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new InvalidParameterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        public KeyPair generateKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            System.out.println("KeyPairGeneratorFail.generateKeyPair()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new InvalidParameterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}