blob: 736bac9bb2f94723840c8df690a5b0f13f74cca9 [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.*;
32import java.security.spec.*;
33
34import javax.crypto.*;
35
36public class TestGetInstance {
37
38 private static void same(Provider p1, Provider p2) throws Exception {
39 if (p1 != p2) {
40 throw new Exception("not same object");
41 }
42 }
43
44 public static void main(String[] args) throws Exception {
45 Provider p = Security.getProvider("SunJCE");
46
47 Cipher c;
48
49 c = Cipher.getInstance("des");
50 same(p, c.getProvider());
51 c = Cipher.getInstance("des/cbc/pkcs5padding");
52 same(p, c.getProvider());
53
54 c = Cipher.getInstance("des", "SunJCE");
55 same(p, c.getProvider());
56 c = Cipher.getInstance("des/cbc/pkcs5padding", "SunJCE");
57 same(p, c.getProvider());
58
59 c = Cipher.getInstance("des", p);
60 same(p, c.getProvider());
61 c = Cipher.getInstance("des/cbc/pkcs5padding", p);
62 same(p, c.getProvider());
63
64 try {
65 c = Cipher.getInstance("DES/XYZ/PKCS5Padding");
66 throw new AssertionError();
67 } catch (NoSuchAlgorithmException e) {
68 System.out.println(e);
69 }
70 try {
71 c = Cipher.getInstance("DES/XYZ/PKCS5Padding", "SunJCE");
72 throw new AssertionError();
73 } catch (NoSuchAlgorithmException e) {
74 System.out.println(e);
75 }
76 try {
77 c = Cipher.getInstance("DES/XYZ/PKCS5Padding", p);
78 throw new AssertionError();
79 } catch (NoSuchAlgorithmException e) {
80 System.out.println(e);
81 }
82
83 try {
84 c = Cipher.getInstance("DES/CBC/XYZPadding");
85 throw new AssertionError();
86 } catch (NoSuchAlgorithmException e) {
87 System.out.println(e);
88 }
89 try {
90 c = Cipher.getInstance("DES/CBC/XYZPadding", "SunJCE");
91 throw new AssertionError();
92 } catch (NoSuchPaddingException e) {
93 System.out.println(e);
94 }
95 try {
96 c = Cipher.getInstance("DES/CBC/XYZPadding", p);
97 throw new AssertionError();
98 } catch (NoSuchPaddingException e) {
99 System.out.println(e);
100 }
101
102 try {
103 c = Cipher.getInstance("foo");
104 throw new AssertionError();
105 } catch (NoSuchAlgorithmException e) {
106 System.out.println(e);
107 }
108 try {
109 c = Cipher.getInstance("foo", "SunJCE");
110 throw new AssertionError();
111 } catch (NoSuchAlgorithmException e) {
112 System.out.println(e);
113 }
114 try {
115 c = Cipher.getInstance("foo", p);
116 throw new AssertionError();
117 } catch (NoSuchAlgorithmException e) {
118 System.out.println(e);
119 }
120
121 try {
122 c = Cipher.getInstance("foo", "SUN");
123 throw new AssertionError();
124 } catch (NoSuchAlgorithmException e) {
125 System.out.println(e);
126 }
127 try {
128 c = Cipher.getInstance("foo", Security.getProvider("SUN"));
129 throw new AssertionError();
130 } catch (NoSuchAlgorithmException e) {
131 System.out.println(e);
132 }
133 try {
134 c = Cipher.getInstance("foo", "bar");
135 throw new AssertionError();
136 } catch (NoSuchProviderException e) {
137 System.out.println(e);
138 }
139
140 System.out.println("All Tests ok");
141 }
142}