blob: dcd7b6ad9e142d870b84174166e329f7019b4eac [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2007 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 4898428
27 * @summary test that the new getInstance() implementation works correctly
28 * @author Andreas Sterbenz
29 */
30
31import java.security.*;
32
33import javax.crypto.*;
34
35public class TestGetInstance {
36
37 private static void same(Object o1, Object o2) throws Exception {
38 if (o1 != o2) {
39 throw new Exception("not same object");
40 }
41 }
42
43 public static void main(String[] args) throws Exception {
44 long start = System.currentTimeMillis();
45
46 Provider p = Security.getProvider("SunJCE");
47
48 KeyGenerator kg;
49
50 kg = KeyGenerator.getInstance("des");
51 System.out.println("Default: " + kg.getProvider().getName());
52 kg = KeyGenerator.getInstance("des", "SunJCE");
53 same(p, kg.getProvider());
54 kg = KeyGenerator.getInstance("des", p);
55 same(p, kg.getProvider());
56
57 try {
58 kg = KeyGenerator.getInstance("foo");
59 throw new AssertionError();
60 } catch (NoSuchAlgorithmException e) {
61 System.out.println(e);
62 }
63 try {
64 kg = KeyGenerator.getInstance("foo", "SunJCE");
65 throw new AssertionError();
66 } catch (NoSuchAlgorithmException e) {
67 System.out.println(e);
68 }
69 try {
70 kg = KeyGenerator.getInstance("foo", p);
71 throw new AssertionError();
72 } catch (NoSuchAlgorithmException e) {
73 System.out.println(e);
74 }
75
76 try {
77 kg = KeyGenerator.getInstance("foo", "SUN");
78 throw new AssertionError();
79 } catch (NoSuchAlgorithmException e) {
80 System.out.println(e);
81 }
82 try {
83 kg = KeyGenerator.getInstance("foo", Security.getProvider("SUN"));
84 throw new AssertionError();
85 } catch (NoSuchAlgorithmException e) {
86 System.out.println(e);
87 }
88 try {
89 kg = KeyGenerator.getInstance("foo", "foo");
90 throw new AssertionError();
91 } catch (NoSuchProviderException e) {
92 System.out.println(e);
93 }
94
95 long stop = System.currentTimeMillis();
96 System.out.println("Done (" + (stop - start) + " ms).");
97 }
98}