test/jdk/java/security/Provider/RemoveProvider.java
author jlaskey
Thu, 14 Nov 2019 12:33:47 -0400
branchjlaskey-prng
changeset 59084 b8fb85ee91e9
parent 47216 71c04702a3d5
permissions -rw-r--r--
restarting random branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    27
 * @library ../testlibrary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Make sure provider instance can be removed from list of registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * providers, and "entrySet", "keySet", and "values" methods don't loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class RemoveProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main(String[] args) throws Exception {
10057
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    38
        ProvidersSnapshot snapshot = ProvidersSnapshot.create();
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    39
        try {
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    40
            main0(args);
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    41
        } finally {
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    42
            snapshot.restore();
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    43
        }
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    44
    }
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    45
03ae05289550 7054918: jdk_security1 test target cleanup
weijun
parents: 5506
diff changeset
    46
    public static void main0(String[] args) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Security.addProvider(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Security.addProvider(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // List all providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        System.out.println("// List all providers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Provider[] provs = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        for (int i=0; i<provs.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.out.println(provs[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // Remove one provider and list all remaining providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        System.out.println("// Remove one provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Security.removeProvider("name1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        provs = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        for (int i=0; i<provs.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.out.println(provs[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // Iterate over the entrySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println("// Iterate over entrySet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Map.Entry me = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Set es = p1.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        Iterator i = es.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            me = (Map.Entry)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.println("Key: " + (String)me.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.out.println("Value: " + (String)me.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // Try to modify the backing Map, and catch the expected exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            me.setValue("name1.mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new Exception("Expected exception not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            System.out.println("Expected exception caught");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // Iterate over the keySet, and try to remove one (should fail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        System.out.println("// Iterate over keySet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Object o = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Set ks = p1.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        i = ks.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            o = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            System.out.println((String)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            ks.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new Exception("Expected exception not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // Iterate over the map values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        System.out.println("// Iterate over values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        Collection c = p1.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.out.println((String)i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // Modify provider and make sure changes are reflected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // existing entrySet (i.e., make sure entrySet is "live").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // First, add entry to provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        System.out.println("// Add 'Cipher' entry to provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        p1.put("Cipher", "name1.des");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        //  entrySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        i = es.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            me = (Map.Entry)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            System.out.println("Key: " + (String)me.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            System.out.println("Value: " + (String)me.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (((String)me.getKey()).equals("Cipher"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (!found)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new Exception("EntrySet not live");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // keySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        i = ks.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            o = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            System.out.println((String)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            System.out.println((String)i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Remove entry from provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        System.out.println("// Remove 'Digest' entry from provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        p1.remove("Digest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // entrySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        i = es.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            me = (Map.Entry)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            System.out.println("Key: " + (String)me.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            System.out.println("Value: " + (String)me.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // keySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        i = ks.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            o = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            System.out.println((String)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            System.out.println((String)i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // Retrieve new entrySet and make sure the backing Map cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // mofified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        es = p1.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        i = es.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            me = (Map.Entry)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            System.out.println("Key: " + (String)me.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            System.out.println("Value: " + (String)me.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            me.setValue("name1.mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new Exception("Expected exception not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            System.out.println("Expected exception caught");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        super(name, version, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        put("Signature", name+".signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        put("Digest", name+".digest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}