author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 40416 | jdk/test/java/security/Provider/RemoveProvider.java@5d91b2fd668c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
40416
5d91b2fd668c
8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents:
10057
diff
changeset
|
2 |
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
40416
5d91b2fd668c
8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents:
10057
diff
changeset
|
26 |
* @bug 4190873 7054918 8130181 |
10057 | 27 |
* @library ../testlibrary |
2 | 28 |
* @summary Make sure provider instance can be removed from list of registered |
29 |
* providers, and "entrySet", "keySet", and "values" methods don't loop |
|
30 |
* indefinitely. |
|
31 |
*/ |
|
32 |
import java.security.*; |
|
33 |
import java.util.*; |
|
34 |
||
35 |
public class RemoveProvider { |
|
36 |
||
37 |
public static void main(String[] args) throws Exception { |
|
10057 | 38 |
ProvidersSnapshot snapshot = ProvidersSnapshot.create(); |
39 |
try { |
|
40 |
main0(args); |
|
41 |
} finally { |
|
42 |
snapshot.restore(); |
|
43 |
} |
|
44 |
} |
|
45 |
||
46 |
public static void main0(String[] args) throws Exception { |
|
2 | 47 |
|
48 |
// Add provider 1 |
|
40416
5d91b2fd668c
8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents:
10057
diff
changeset
|
49 |
Provider p1 = new MyProvider("name1","1",""); |
2 | 50 |
Security.addProvider(p1); |
51 |
||
52 |
// Add provider 2 |
|
40416
5d91b2fd668c
8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents:
10057
diff
changeset
|
53 |
Provider p2 = new MyProvider("name2","1",""); |
2 | 54 |
Security.addProvider(p2); |
55 |
||
56 |
// List all providers |
|
57 |
System.out.println("// List all providers"); |
|
58 |
Provider[] provs = Security.getProviders(); |
|
59 |
for (int i=0; i<provs.length; i++) |
|
60 |
System.out.println(provs[i].toString()); |
|
61 |
||
62 |
// Remove one provider and list all remaining providers |
|
63 |
System.out.println(""); |
|
64 |
System.out.println("// Remove one provider"); |
|
65 |
Security.removeProvider("name1"); |
|
66 |
provs = Security.getProviders(); |
|
67 |
for (int i=0; i<provs.length; i++) |
|
68 |
System.out.println(provs[i].toString()); |
|
69 |
||
70 |
// Iterate over the entrySet |
|
71 |
System.out.println(""); |
|
72 |
System.out.println("// Iterate over entrySet"); |
|
73 |
Map.Entry me = null; |
|
74 |
Set es = p1.entrySet(); |
|
75 |
Iterator i = es.iterator(); |
|
76 |
while (i.hasNext()) { |
|
77 |
me = (Map.Entry)i.next(); |
|
78 |
System.out.println("Key: " + (String)me.getKey()); |
|
79 |
System.out.println("Value: " + (String)me.getValue()); |
|
80 |
} |
|
81 |
// Try to modify the backing Map, and catch the expected exception |
|
82 |
try { |
|
83 |
me.setValue("name1.mac"); |
|
84 |
throw new Exception("Expected exception not thrown"); |
|
85 |
} catch (UnsupportedOperationException uoe) { |
|
86 |
System.out.println("Expected exception caught"); |
|
87 |
} |
|
88 |
||
89 |
// Iterate over the keySet, and try to remove one (should fail) |
|
90 |
System.out.println(""); |
|
91 |
System.out.println("// Iterate over keySet"); |
|
92 |
Object o = null; |
|
93 |
Set ks = p1.keySet(); |
|
94 |
i = ks.iterator(); |
|
95 |
while (i.hasNext()) { |
|
96 |
o = i.next(); |
|
97 |
System.out.println((String)o); |
|
98 |
} |
|
99 |
try { |
|
100 |
ks.remove(o); |
|
101 |
throw new Exception("Expected exception not thrown"); |
|
102 |
} catch (UnsupportedOperationException uoe) { |
|
103 |
} |
|
104 |
||
105 |
// Iterate over the map values |
|
106 |
System.out.println(""); |
|
107 |
System.out.println("// Iterate over values"); |
|
108 |
Collection c = p1.values(); |
|
109 |
i = c.iterator(); |
|
110 |
while (i.hasNext()) { |
|
111 |
System.out.println((String)i.next()); |
|
112 |
} |
|
113 |
||
114 |
// Modify provider and make sure changes are reflected in |
|
115 |
// existing entrySet (i.e., make sure entrySet is "live"). |
|
116 |
// First, add entry to provider. |
|
117 |
System.out.println(""); |
|
118 |
System.out.println("// Add 'Cipher' entry to provider"); |
|
119 |
p1.put("Cipher", "name1.des"); |
|
120 |
// entrySet |
|
121 |
i = es.iterator(); |
|
122 |
boolean found = false; |
|
123 |
while (i.hasNext()) { |
|
124 |
me = (Map.Entry)i.next(); |
|
125 |
System.out.println("Key: " + (String)me.getKey()); |
|
126 |
System.out.println("Value: " + (String)me.getValue()); |
|
127 |
if (((String)me.getKey()).equals("Cipher")) |
|
128 |
found = true; |
|
129 |
} |
|
130 |
if (!found) |
|
131 |
throw new Exception("EntrySet not live"); |
|
132 |
// keySet |
|
133 |
i = ks.iterator(); |
|
134 |
while (i.hasNext()) { |
|
135 |
o = i.next(); |
|
136 |
System.out.println((String)o); |
|
137 |
} |
|
138 |
// collection |
|
139 |
i = c.iterator(); |
|
140 |
while (i.hasNext()) { |
|
141 |
System.out.println((String)i.next()); |
|
142 |
} |
|
143 |
||
144 |
// Remove entry from provider |
|
145 |
System.out.println(""); |
|
146 |
System.out.println("// Remove 'Digest' entry from provider"); |
|
147 |
p1.remove("Digest"); |
|
148 |
// entrySet |
|
149 |
i = es.iterator(); |
|
150 |
while (i.hasNext()) { |
|
151 |
me = (Map.Entry)i.next(); |
|
152 |
System.out.println("Key: " + (String)me.getKey()); |
|
153 |
System.out.println("Value: " + (String)me.getValue()); |
|
154 |
} |
|
155 |
// keySet |
|
156 |
i = ks.iterator(); |
|
157 |
while (i.hasNext()) { |
|
158 |
o = i.next(); |
|
159 |
System.out.println((String)o); |
|
160 |
} |
|
161 |
// collection |
|
162 |
i = c.iterator(); |
|
163 |
while (i.hasNext()) { |
|
164 |
System.out.println((String)i.next()); |
|
165 |
} |
|
166 |
||
167 |
// Retrieve new entrySet and make sure the backing Map cannot be |
|
168 |
// mofified |
|
169 |
es = p1.entrySet(); |
|
170 |
i = es.iterator(); |
|
171 |
while (i.hasNext()) { |
|
172 |
me = (Map.Entry)i.next(); |
|
173 |
System.out.println("Key: " + (String)me.getKey()); |
|
174 |
System.out.println("Value: " + (String)me.getValue()); |
|
175 |
} |
|
176 |
try { |
|
177 |
me.setValue("name1.mac"); |
|
178 |
throw new Exception("Expected exception not thrown"); |
|
179 |
} catch (UnsupportedOperationException uoe) { |
|
180 |
System.out.println("Expected exception caught"); |
|
181 |
} |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
class MyProvider extends Provider { |
|
40416
5d91b2fd668c
8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents:
10057
diff
changeset
|
186 |
public MyProvider(String name, String version, String info) { |
2 | 187 |
super(name, version, info); |
188 |
put("Signature", name+".signature"); |
|
189 |
put("Digest", name+".digest"); |
|
190 |
} |
|
191 |
} |