Path: blob/master/test/jdk/java/security/Provider/RemoveProvider.java
41152 views
/*1* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4190873 7054918 813018126* @library ../testlibrary27* @summary Make sure provider instance can be removed from list of registered28* providers, and "entrySet", "keySet", and "values" methods don't loop29* indefinitely.30*/31import java.security.*;32import java.util.*;3334public class RemoveProvider {3536public static void main(String[] args) throws Exception {37ProvidersSnapshot snapshot = ProvidersSnapshot.create();38try {39main0(args);40} finally {41snapshot.restore();42}43}4445public static void main0(String[] args) throws Exception {4647// Add provider 148Provider p1 = new MyProvider("name1","1","");49Security.addProvider(p1);5051// Add provider 252Provider p2 = new MyProvider("name2","1","");53Security.addProvider(p2);5455// List all providers56System.out.println("// List all providers");57Provider[] provs = Security.getProviders();58for (int i=0; i<provs.length; i++)59System.out.println(provs[i].toString());6061// Remove one provider and list all remaining providers62System.out.println("");63System.out.println("// Remove one provider");64Security.removeProvider("name1");65provs = Security.getProviders();66for (int i=0; i<provs.length; i++)67System.out.println(provs[i].toString());6869// Iterate over the entrySet70System.out.println("");71System.out.println("// Iterate over entrySet");72Map.Entry me = null;73Set es = p1.entrySet();74Iterator i = es.iterator();75while (i.hasNext()) {76me = (Map.Entry)i.next();77System.out.println("Key: " + (String)me.getKey());78System.out.println("Value: " + (String)me.getValue());79}80// Try to modify the backing Map, and catch the expected exception81try {82me.setValue("name1.mac");83throw new Exception("Expected exception not thrown");84} catch (UnsupportedOperationException uoe) {85System.out.println("Expected exception caught");86}8788// Iterate over the keySet, and try to remove one (should fail)89System.out.println("");90System.out.println("// Iterate over keySet");91Object o = null;92Set ks = p1.keySet();93i = ks.iterator();94while (i.hasNext()) {95o = i.next();96System.out.println((String)o);97}98try {99ks.remove(o);100throw new Exception("Expected exception not thrown");101} catch (UnsupportedOperationException uoe) {102}103104// Iterate over the map values105System.out.println("");106System.out.println("// Iterate over values");107Collection c = p1.values();108i = c.iterator();109while (i.hasNext()) {110System.out.println((String)i.next());111}112113// Modify provider and make sure changes are reflected in114// existing entrySet (i.e., make sure entrySet is "live").115// First, add entry to provider.116System.out.println("");117System.out.println("// Add 'Cipher' entry to provider");118p1.put("Cipher", "name1.des");119// entrySet120i = es.iterator();121boolean found = false;122while (i.hasNext()) {123me = (Map.Entry)i.next();124System.out.println("Key: " + (String)me.getKey());125System.out.println("Value: " + (String)me.getValue());126if (((String)me.getKey()).equals("Cipher"))127found = true;128}129if (!found)130throw new Exception("EntrySet not live");131// keySet132i = ks.iterator();133while (i.hasNext()) {134o = i.next();135System.out.println((String)o);136}137// collection138i = c.iterator();139while (i.hasNext()) {140System.out.println((String)i.next());141}142143// Remove entry from provider144System.out.println("");145System.out.println("// Remove 'Digest' entry from provider");146p1.remove("Digest");147// entrySet148i = es.iterator();149while (i.hasNext()) {150me = (Map.Entry)i.next();151System.out.println("Key: " + (String)me.getKey());152System.out.println("Value: " + (String)me.getValue());153}154// keySet155i = ks.iterator();156while (i.hasNext()) {157o = i.next();158System.out.println((String)o);159}160// collection161i = c.iterator();162while (i.hasNext()) {163System.out.println((String)i.next());164}165166// Retrieve new entrySet and make sure the backing Map cannot be167// mofified168es = p1.entrySet();169i = es.iterator();170while (i.hasNext()) {171me = (Map.Entry)i.next();172System.out.println("Key: " + (String)me.getKey());173System.out.println("Value: " + (String)me.getValue());174}175try {176me.setValue("name1.mac");177throw new Exception("Expected exception not thrown");178} catch (UnsupportedOperationException uoe) {179System.out.println("Expected exception caught");180}181}182}183184class MyProvider extends Provider {185public MyProvider(String name, String version, String info) {186super(name, version, info);187put("Signature", name+".signature");188put("Digest", name+".digest");189}190}191192193