0N/A/*
5359N/A * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
5359N/A * @bug 4190873 7054918
5359N/A * @library ../testlibrary
0N/A * @summary Make sure provider instance can be removed from list of registered
0N/A * providers, and "entrySet", "keySet", and "values" methods don't loop
0N/A * indefinitely.
0N/A */
0N/Aimport java.security.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic class RemoveProvider {
0N/A
0N/A public static void main(String[] args) throws Exception {
5359N/A ProvidersSnapshot snapshot = ProvidersSnapshot.create();
5359N/A try {
5359N/A main0(args);
5359N/A } finally {
5359N/A snapshot.restore();
5359N/A }
5359N/A }
5359N/A
5359N/A public static void main0(String[] args) throws Exception {
0N/A
0N/A // Add provider 1
0N/A Provider p1 = new MyProvider("name1",1,"");
0N/A Security.addProvider(p1);
0N/A
0N/A // Add provider 2
0N/A Provider p2 = new MyProvider("name2",1,"");
0N/A Security.addProvider(p2);
0N/A
0N/A // List all providers
0N/A System.out.println("// List all providers");
0N/A Provider[] provs = Security.getProviders();
0N/A for (int i=0; i<provs.length; i++)
0N/A System.out.println(provs[i].toString());
0N/A
0N/A // Remove one provider and list all remaining providers
0N/A System.out.println("");
0N/A System.out.println("// Remove one provider");
0N/A Security.removeProvider("name1");
0N/A provs = Security.getProviders();
0N/A for (int i=0; i<provs.length; i++)
0N/A System.out.println(provs[i].toString());
0N/A
0N/A // Iterate over the entrySet
0N/A System.out.println("");
0N/A System.out.println("// Iterate over entrySet");
0N/A Map.Entry me = null;
0N/A Set es = p1.entrySet();
0N/A Iterator i = es.iterator();
0N/A while (i.hasNext()) {
0N/A me = (Map.Entry)i.next();
0N/A System.out.println("Key: " + (String)me.getKey());
0N/A System.out.println("Value: " + (String)me.getValue());
0N/A }
0N/A // Try to modify the backing Map, and catch the expected exception
0N/A try {
0N/A me.setValue("name1.mac");
0N/A throw new Exception("Expected exception not thrown");
0N/A } catch (UnsupportedOperationException uoe) {
0N/A System.out.println("Expected exception caught");
0N/A }
0N/A
0N/A // Iterate over the keySet, and try to remove one (should fail)
0N/A System.out.println("");
0N/A System.out.println("// Iterate over keySet");
0N/A Object o = null;
0N/A Set ks = p1.keySet();
0N/A i = ks.iterator();
0N/A while (i.hasNext()) {
0N/A o = i.next();
0N/A System.out.println((String)o);
0N/A }
0N/A try {
0N/A ks.remove(o);
0N/A throw new Exception("Expected exception not thrown");
0N/A } catch (UnsupportedOperationException uoe) {
0N/A }
0N/A
0N/A // Iterate over the map values
0N/A System.out.println("");
0N/A System.out.println("// Iterate over values");
0N/A Collection c = p1.values();
0N/A i = c.iterator();
0N/A while (i.hasNext()) {
0N/A System.out.println((String)i.next());
0N/A }
0N/A
0N/A // Modify provider and make sure changes are reflected in
0N/A // existing entrySet (i.e., make sure entrySet is "live").
0N/A // First, add entry to provider.
0N/A System.out.println("");
0N/A System.out.println("// Add 'Cipher' entry to provider");
0N/A p1.put("Cipher", "name1.des");
0N/A // entrySet
0N/A i = es.iterator();
0N/A boolean found = false;
0N/A while (i.hasNext()) {
0N/A me = (Map.Entry)i.next();
0N/A System.out.println("Key: " + (String)me.getKey());
0N/A System.out.println("Value: " + (String)me.getValue());
0N/A if (((String)me.getKey()).equals("Cipher"))
0N/A found = true;
0N/A }
0N/A if (!found)
0N/A throw new Exception("EntrySet not live");
0N/A // keySet
0N/A i = ks.iterator();
0N/A while (i.hasNext()) {
0N/A o = i.next();
0N/A System.out.println((String)o);
0N/A }
0N/A // collection
0N/A i = c.iterator();
0N/A while (i.hasNext()) {
0N/A System.out.println((String)i.next());
0N/A }
0N/A
0N/A // Remove entry from provider
0N/A System.out.println("");
0N/A System.out.println("// Remove 'Digest' entry from provider");
0N/A p1.remove("Digest");
0N/A // entrySet
0N/A i = es.iterator();
0N/A while (i.hasNext()) {
0N/A me = (Map.Entry)i.next();
0N/A System.out.println("Key: " + (String)me.getKey());
0N/A System.out.println("Value: " + (String)me.getValue());
0N/A }
0N/A // keySet
0N/A i = ks.iterator();
0N/A while (i.hasNext()) {
0N/A o = i.next();
0N/A System.out.println((String)o);
0N/A }
0N/A // collection
0N/A i = c.iterator();
0N/A while (i.hasNext()) {
0N/A System.out.println((String)i.next());
0N/A }
0N/A
0N/A // Retrieve new entrySet and make sure the backing Map cannot be
0N/A // mofified
0N/A es = p1.entrySet();
0N/A i = es.iterator();
0N/A while (i.hasNext()) {
0N/A me = (Map.Entry)i.next();
0N/A System.out.println("Key: " + (String)me.getKey());
0N/A System.out.println("Value: " + (String)me.getValue());
0N/A }
0N/A try {
0N/A me.setValue("name1.mac");
0N/A throw new Exception("Expected exception not thrown");
0N/A } catch (UnsupportedOperationException uoe) {
0N/A System.out.println("Expected exception caught");
0N/A }
0N/A }
0N/A}
0N/A
0N/Aclass MyProvider extends Provider {
0N/A public MyProvider(String name, double version, String info) {
0N/A super(name, version, info);
0N/A put("Signature", name+".signature");
0N/A put("Digest", name+".digest");
0N/A }
0N/A}