blob: 63c419e0bd1304c9193e62e37e071de3788d0364 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998 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 4190873
27 * @summary Make sure provider instance can be removed from list of registered
28 * providers, and "entrySet", "keySet", and "values" methods don't loop
29 * indefinitely.
30 */
31import java.security.*;
32import java.util.*;
33
34public class RemoveProvider {
35
36 public static void main(String[] args) throws Exception {
37
38 // Add provider 1
39 Provider p1 = new MyProvider("name1",1,"");
40 Security.addProvider(p1);
41
42 // Add provider 2
43 Provider p2 = new MyProvider("name2",1,"");
44 Security.addProvider(p2);
45
46 // List all providers
47 System.out.println("// List all providers");
48 Provider[] provs = Security.getProviders();
49 for (int i=0; i<provs.length; i++)
50 System.out.println(provs[i].toString());
51
52 // Remove one provider and list all remaining providers
53 System.out.println("");
54 System.out.println("// Remove one provider");
55 Security.removeProvider("name1");
56 provs = Security.getProviders();
57 for (int i=0; i<provs.length; i++)
58 System.out.println(provs[i].toString());
59
60 // Iterate over the entrySet
61 System.out.println("");
62 System.out.println("// Iterate over entrySet");
63 Map.Entry me = null;
64 Set es = p1.entrySet();
65 Iterator i = es.iterator();
66 while (i.hasNext()) {
67 me = (Map.Entry)i.next();
68 System.out.println("Key: " + (String)me.getKey());
69 System.out.println("Value: " + (String)me.getValue());
70 }
71 // Try to modify the backing Map, and catch the expected exception
72 try {
73 me.setValue("name1.mac");
74 throw new Exception("Expected exception not thrown");
75 } catch (UnsupportedOperationException uoe) {
76 System.out.println("Expected exception caught");
77 }
78
79 // Iterate over the keySet, and try to remove one (should fail)
80 System.out.println("");
81 System.out.println("// Iterate over keySet");
82 Object o = null;
83 Set ks = p1.keySet();
84 i = ks.iterator();
85 while (i.hasNext()) {
86 o = i.next();
87 System.out.println((String)o);
88 }
89 try {
90 ks.remove(o);
91 throw new Exception("Expected exception not thrown");
92 } catch (UnsupportedOperationException uoe) {
93 }
94
95 // Iterate over the map values
96 System.out.println("");
97 System.out.println("// Iterate over values");
98 Collection c = p1.values();
99 i = c.iterator();
100 while (i.hasNext()) {
101 System.out.println((String)i.next());
102 }
103
104 // Modify provider and make sure changes are reflected in
105 // existing entrySet (i.e., make sure entrySet is "live").
106 // First, add entry to provider.
107 System.out.println("");
108 System.out.println("// Add 'Cipher' entry to provider");
109 p1.put("Cipher", "name1.des");
110 // entrySet
111 i = es.iterator();
112 boolean found = false;
113 while (i.hasNext()) {
114 me = (Map.Entry)i.next();
115 System.out.println("Key: " + (String)me.getKey());
116 System.out.println("Value: " + (String)me.getValue());
117 if (((String)me.getKey()).equals("Cipher"))
118 found = true;
119 }
120 if (!found)
121 throw new Exception("EntrySet not live");
122 // keySet
123 i = ks.iterator();
124 while (i.hasNext()) {
125 o = i.next();
126 System.out.println((String)o);
127 }
128 // collection
129 i = c.iterator();
130 while (i.hasNext()) {
131 System.out.println((String)i.next());
132 }
133
134 // Remove entry from provider
135 System.out.println("");
136 System.out.println("// Remove 'Digest' entry from provider");
137 p1.remove("Digest");
138 // entrySet
139 i = es.iterator();
140 while (i.hasNext()) {
141 me = (Map.Entry)i.next();
142 System.out.println("Key: " + (String)me.getKey());
143 System.out.println("Value: " + (String)me.getValue());
144 }
145 // keySet
146 i = ks.iterator();
147 while (i.hasNext()) {
148 o = i.next();
149 System.out.println((String)o);
150 }
151 // collection
152 i = c.iterator();
153 while (i.hasNext()) {
154 System.out.println((String)i.next());
155 }
156
157 // Retrieve new entrySet and make sure the backing Map cannot be
158 // mofified
159 es = p1.entrySet();
160 i = es.iterator();
161 while (i.hasNext()) {
162 me = (Map.Entry)i.next();
163 System.out.println("Key: " + (String)me.getKey());
164 System.out.println("Value: " + (String)me.getValue());
165 }
166 try {
167 me.setValue("name1.mac");
168 throw new Exception("Expected exception not thrown");
169 } catch (UnsupportedOperationException uoe) {
170 System.out.println("Expected exception caught");
171 }
172 }
173}
174
175class MyProvider extends Provider {
176 public MyProvider(String name, double version, String info) {
177 super(name, version, info);
178 put("Signature", name+".signature");
179 put("Digest", name+".digest");
180 }
181}