jdk/test/java/security/Provider/RemoveProvider.java
author alanb
Mon, 11 Apr 2011 12:52:39 +0100
changeset 9252 9484925c9c2b
parent 5506 202f599c92aa
child 10057 03ae05289550
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4190873
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Make sure provider instance can be removed from list of registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * providers, and "entrySet", "keySet", and "values" methods don't loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class RemoveProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        // Add provider 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        Provider p1 = new MyProvider("name1",1,"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        Security.addProvider(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        // Add provider 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        Provider p2 = new MyProvider("name2",1,"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        Security.addProvider(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        // List all providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        System.out.println("// List all providers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        Provider[] provs = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        for (int i=0; i<provs.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            System.out.println(provs[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        // Remove one provider and list all remaining providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        System.out.println("// Remove one provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Security.removeProvider("name1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        provs = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        for (int i=0; i<provs.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            System.out.println(provs[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // Iterate over the entrySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        System.out.println("// Iterate over entrySet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Map.Entry me = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        Set es = p1.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Iterator i = es.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            me = (Map.Entry)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.out.println("Key: " + (String)me.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            System.out.println("Value: " + (String)me.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // Try to modify the backing Map, and catch the expected exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            me.setValue("name1.mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new Exception("Expected exception not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            System.out.println("Expected exception caught");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // Iterate over the keySet, and try to remove one (should fail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println("// Iterate over keySet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Object o = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        Set ks = p1.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        i = ks.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            o = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            System.out.println((String)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            ks.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new Exception("Expected exception not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // Iterate over the map values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.out.println("// Iterate over values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Collection c = p1.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            System.out.println((String)i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // Modify provider and make sure changes are reflected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // existing entrySet (i.e., make sure entrySet is "live").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // First, add entry to provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        System.out.println("// Add 'Cipher' entry to provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        p1.put("Cipher", "name1.des");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        //  entrySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        i = es.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            me = (Map.Entry)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println("Key: " + (String)me.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println("Value: " + (String)me.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (((String)me.getKey()).equals("Cipher"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (!found)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new Exception("EntrySet not live");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // keySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        i = ks.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            o = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            System.out.println((String)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            System.out.println((String)i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // Remove entry from provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        System.out.println("// Remove 'Digest' entry from provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        p1.remove("Digest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // entrySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        i = es.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            me = (Map.Entry)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            System.out.println("Key: " + (String)me.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            System.out.println("Value: " + (String)me.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // keySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        i = ks.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            o = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            System.out.println((String)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            System.out.println((String)i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // Retrieve new entrySet and make sure the backing Map cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // mofified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        es = p1.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        i = es.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            me = (Map.Entry)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            System.out.println("Key: " + (String)me.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            System.out.println("Value: " + (String)me.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            me.setValue("name1.mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new Exception("Expected exception not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            System.out.println("Expected exception caught");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
class MyProvider extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public MyProvider(String name, double version, String info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        super(name, version, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        put("Signature", name+".signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        put("Digest", name+".digest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}