1 /*
2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. 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 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.
22  */
23 
24 /**
25  * @test
26  * @bug 4963416 7054918
27  * @library ../../testlibrary
28  * @summary make sure removeProvider() always works correctly
29  * @author Andreas Sterbenz
30  */
31 
32 import java.util.*;
33 
34 import java.security.*;
35 
36 public class RemoveProviders {
37 
main(String[] args)38     public static void main(String[] args) throws Exception {
39         ProvidersSnapshot snapshot = ProvidersSnapshot.create();
40         try {
41             main0(args);
42         } finally {
43             snapshot.restore();
44         }
45     }
46 
main0(String[] args)47     public static void main0(String[] args) throws Exception {
48         Provider[] providers = Security.getProviders();
49         System.out.println("Providers: " + Arrays.asList(providers));
50 
51         // remove each provider and add it back in at the end of the list
52         for (int i = 0; i < providers.length; i++) {
53             Provider p = providers[i];
54             String name = p.getName();
55             Security.removeProvider(name);
56             if (Security.getProvider(name) != null) {
57                 throw new Exception("Provider not removed: " + name);
58             }
59             Security.addProvider(p);
60         }
61         if (Arrays.equals(providers, Security.getProviders()) == false) {
62             throw new Exception("Provider mismatch: " + Arrays.asList(Security.getProviders()));
63         }
64 
65         // remove non-existing provider
66         Security.removeProvider("foo");
67         if (Arrays.equals(providers, Security.getProviders()) == false) {
68             throw new Exception("Provider mismatch: " + Arrays.asList(Security.getProviders()));
69         }
70 
71         // remove from the middle of the list
72         Security.removeProvider("SunJCE");
73         if (Security.getProvider("SunJCE") != null) {
74             throw new Exception("not removed");
75         }
76 
77         System.out.println("Done.");
78     }
79 
80 }
81