IdentityScope2Test.java revision 229e34b182b98e1dba15d3dc6341954986ae2b7a
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.harmony.security.tests.java.security;
19
20import java.security.Identity;
21import java.security.IdentityScope;
22import java.security.KeyManagementException;
23import java.security.KeyPairGenerator;
24import java.security.PublicKey;
25import java.util.Enumeration;
26import java.util.Hashtable;
27
28import org.apache.harmony.security.tests.java.security.Identity2Test.IdentitySubclass;
29
30@SuppressWarnings("deprecation")
31public class IdentityScope2Test extends junit.framework.TestCase {
32
33    static PublicKey pubKey;
34    static {
35        try {
36            pubKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic();
37        } catch (Exception e) {
38            fail(e.toString());
39        }
40    }
41
42    public static class IdentityScopeSubclass extends IdentityScope {
43        private static final long serialVersionUID = 1L;
44        Hashtable<Identity, Identity> identities;
45
46        public IdentityScopeSubclass(String name, PublicKey pk) {
47            super(name);
48            try {
49                setPublicKey(pk);
50            } catch (KeyManagementException e) {
51            }
52            identities = new Hashtable<Identity, Identity>();
53        }
54
55        public IdentityScopeSubclass() {
56            super();
57            identities = new Hashtable<Identity, Identity>();
58        }
59
60        public IdentityScopeSubclass(String name) {
61            super(name);
62            identities = new Hashtable<Identity, Identity>();
63        }
64
65        public IdentityScopeSubclass(String name, IdentityScope scope)
66                throws KeyManagementException {
67            super(name, scope);
68            identities = new Hashtable<Identity, Identity>();
69        }
70
71        public int size() {
72            return identities.size();
73        }
74
75        public Identity getIdentity(String name) {
76            Enumeration<Identity> en = identities();
77            while (en.hasMoreElements()) {
78                Identity current = (Identity) en.nextElement();
79                if (current.getName().equals(name))
80                    return current;
81            }
82            return null;
83        }
84
85        public Identity getIdentity(PublicKey pk) {
86            Enumeration<Identity> en = identities();
87            while (en.hasMoreElements()) {
88                Identity current = (Identity) en.nextElement();
89                if (current.getPublicKey() == pk)
90                    return current;
91            }
92            return null;
93        }
94
95        public Enumeration<Identity> identities() {
96            return identities.elements();
97        }
98
99        public void addIdentity(Identity id) throws KeyManagementException {
100            if (identities.containsKey(id))
101                throw new KeyManagementException(
102                        "This Identity is already contained in the scope");
103            if (getIdentity(id.getPublicKey()) != null)
104                throw new KeyManagementException(
105                        "This Identity's public key already exists in the scope");
106            identities.put(id, id);
107        }
108
109        public void removeIdentity(Identity id) throws KeyManagementException {
110            if (!identities.containsKey(id))
111                throw new KeyManagementException(
112                        "This Identity is not contained in the scope");
113            identities.remove(id);
114        }
115    }
116
117    /**
118     * java.security.IdentityScope#IdentityScope()
119     */
120    public void test_Constructor() {
121        new IdentityScopeSubclass();
122    }
123
124    /**
125     * java.security.IdentityScope#IdentityScope(java.lang.String)
126     */
127    public void test_ConstructorLjava_lang_String() {
128        String[] str = {"test", "", null};
129        IdentityScopeSubclass iss;
130
131        for (int i = 0; i < str.length; i++) {
132            try {
133                iss = new IdentityScopeSubclass(str[i]);
134                assertNotNull(iss);
135                assertTrue(iss instanceof IdentityScope);
136            } catch (Exception e) {
137                fail("Unexpected exception for parameter " + str[i]);
138            }
139        }
140    }
141
142    /**
143     * java.security.IdentityScope#IdentityScope(java.lang.String,
144     *        java.security.IdentityScope)
145     */
146    public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope() {
147        String nameNull = null;
148        String[] str = {"test", "", "!@#$%^&*()", "identity name"};
149        IdentityScope is;
150        IdentityScope iss = new IdentityScopeSubclass("test scope");
151
152        for (int i = 0; i < str.length; i++) {
153            try {
154                is = new IdentityScopeSubclass(str[i], new IdentityScopeSubclass());
155                assertNotNull(is);
156                assertTrue(is instanceof IdentityScope);
157            } catch (Exception e) {
158                fail("Unexpected exception for parameter " + str[i]);
159            }
160        }
161
162        try {
163            is = new IdentityScopeSubclass(nameNull, new IdentityScopeSubclass());
164        } catch (NullPointerException npe) {
165        } catch (Exception e) {
166            fail("Incorrect exception " + e + " was thrown");
167        }
168
169        try {
170            is = new IdentityScopeSubclass("test", iss);
171            is = new IdentityScopeSubclass("test", iss);
172            fail("KeyManagementException was not thrown");
173        } catch (KeyManagementException npe) {
174            //expected
175        } catch (Exception e) {
176            fail("Incorrect exception " + e + " was thrown instead of KeyManagementException");
177        }
178    }
179
180    /**
181     * java.security.IdentityScope#addIdentity(java.security.Identity)
182     */
183    public void test_addIdentityLjava_security_Identity() throws Exception {
184               IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
185                       new IdentityScopeSubclass());
186               Identity id = new IdentitySubclass("id1");
187               id.setPublicKey(pubKey);
188               sub.addIdentity(id);
189               try {
190                   Identity id2 = new IdentitySubclass("id2");
191                   id2.setPublicKey(pubKey);
192                   sub.addIdentity(id2);
193                   fail("KeyManagementException should have been thrown");
194               } catch (KeyManagementException e) {
195                   // Expected
196               }
197    }
198
199    /**
200     * java.security.IdentityScope#removeIdentity(java.security.Identity)
201     */
202    public void test_removeIdentityLjava_security_Identity() throws Exception {
203               IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
204                       new IdentityScopeSubclass());
205               Identity id = new IdentitySubclass();
206               id.setPublicKey(pubKey);
207               sub.addIdentity(id);
208               sub.removeIdentity(id);
209               try {
210                   sub.removeIdentity(id);
211                   fail("KeyManagementException should have been thrown");
212               } catch (KeyManagementException e) {
213                   // expected
214               }
215    }
216
217    /**
218     * java.security.IdentityScope#identities()
219     */
220    public void test_identities() throws Exception {
221               IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
222                       new IdentityScopeSubclass());
223               Identity id = new IdentitySubclass();
224               id.setPublicKey(pubKey);
225               sub.addIdentity(id);
226               Enumeration<Identity> en = sub.identities();
227               assertTrue("Wrong object contained in identities", en.nextElement()
228                       .equals(id));
229               assertTrue("Contains too many elements", !en.hasMoreElements());
230    }
231
232    /**
233     * java.security.IdentityScope#getIdentity(java.security.Principal)
234     */
235    public void test_getIdentityLjava_security_Principal() throws Exception {
236        Identity id = new IdentitySubclass("principal name");
237        id.setPublicKey(pubKey);
238        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
239                new IdentityScopeSubclass());
240
241        try {
242            sub.getIdentity((java.security.Principal) null);
243            fail("Test 1: NullPointerException expected.");
244        } catch (NullPointerException e) {
245            // Expected.
246        }
247
248        sub.addIdentity(id);
249        Identity returnedId = sub.getIdentity(id);
250        assertEquals("Test 2: Returned Identity not the same as the added one;",
251                id, returnedId);
252
253        Identity id2 = new IdentitySubclass("Another identity");
254        id2.setPublicKey(pubKey);
255
256        assertNull("Test 3: Null value expected.",
257                sub.getIdentity(id2));
258
259        try {
260            sub.getIdentity((java.security.Principal) null);
261            fail("Test 4: NullPointerException expected.");
262        } catch (NullPointerException e) {
263            // Expected.
264        }
265
266    }
267
268    /**
269     * java.security.IdentityScope#getIdentity(java.security.PublicKey)
270     */
271    public void test_getIdentityLjava_security_PublicKey() throws Exception {
272        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
273                new IdentityScopeSubclass());
274        Identity id = new IdentitySubclass();
275        id.setPublicKey(pubKey);
276        sub.addIdentity(id);
277        Identity returnedId = sub.getIdentity(pubKey);
278        assertEquals("Test 1: Returned Identity not the same as the added one;",
279                id, returnedId);
280
281        assertNull("Test 2: Null value expected.",
282                sub.getIdentity((PublicKey) null));
283
284        PublicKey anotherKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic();
285        assertNull("Test 3: Null value expected.",
286                sub.getIdentity(anotherKey));
287    }
288
289    /**
290     * java.security.IdentityScope#getIdentity(java.lang.String)
291     */
292    public void test_getIdentityLjava_lang_String() throws Exception {
293               IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
294                       new IdentityScopeSubclass());
295               Identity id = new IdentitySubclass("test");
296               id.setPublicKey(pubKey);
297               sub.addIdentity(id);
298               Identity returnedId = sub.getIdentity("test");
299               assertEquals("Returned Identity not the same as the added one", id,
300                       returnedId);
301    }
302
303    /**
304     * java.security.IdentityScope#size()
305     */
306    public void test_size() throws Exception {
307               IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
308                       new IdentityScopeSubclass());
309               Identity id = new IdentitySubclass();
310               id.setPublicKey(pubKey);
311               sub.addIdentity(id);
312               assertEquals("Wrong size", 1, sub.size());
313    }
314
315    /**
316     * java.security.IdentityScope#toString()
317     */
318    public void test_toString() throws Exception {
319            IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
320                    new IdentityScopeSubclass());
321            Identity id = new IdentitySubclass();
322            id.setPublicKey(pubKey);
323            sub.addIdentity(id);
324            assertNotNull("toString returned a null", sub.toString());
325            assertTrue("Not a valid String ", sub.toString().length() > 0);
326    }
327
328    public void test_getIdentity() throws Exception {
329        //Regression for HARMONY-1173
330        IdentityScope scope = IdentityScope.getSystemScope();
331        try {
332            scope.getIdentity((String) null);
333            fail("NPE expected");
334        } catch (NullPointerException npe) {}
335    }
336}
337