IdentityScope2Test.java revision f33eae7e84eb6d3b0f4e86b59605bb3de73009f3
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
30import dalvik.annotation.TestLevel;
31import dalvik.annotation.TestTargetClass;
32import dalvik.annotation.TestTargetNew;
33@SuppressWarnings("deprecation")
34@TestTargetClass(IdentityScope.class)
35public class IdentityScope2Test extends junit.framework.TestCase {
36
37    static PublicKey pubKey;
38    static {
39        try {
40            pubKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic();
41        } catch (Exception e) {
42            fail(e.toString());
43        }
44    }
45
46    public static class IdentityScopeSubclass extends IdentityScope {
47        private static final long serialVersionUID = 1L;
48        Hashtable<Identity, Identity> identities;
49
50        public IdentityScopeSubclass(String name, PublicKey pk) {
51            super(name);
52            try {
53                setPublicKey(pk);
54            } catch (KeyManagementException e) {
55            }
56            identities = new Hashtable<Identity, Identity>();
57        }
58
59        public IdentityScopeSubclass() {
60            super();
61            identities = new Hashtable<Identity, Identity>();
62        }
63
64        public IdentityScopeSubclass(String name) {
65            super(name);
66            identities = new Hashtable<Identity, Identity>();
67        }
68
69        public IdentityScopeSubclass(String name, IdentityScope scope)
70                throws KeyManagementException {
71            super(name, scope);
72            identities = new Hashtable<Identity, Identity>();
73        }
74
75        public int size() {
76            return identities.size();
77        }
78
79        public Identity getIdentity(String name) {
80            Enumeration<Identity> en = identities();
81            while (en.hasMoreElements()) {
82                Identity current = (Identity) en.nextElement();
83                if (current.getName().equals(name))
84                    return current;
85            }
86            return null;
87        }
88
89        public Identity getIdentity(PublicKey pk) {
90            Enumeration<Identity> en = identities();
91            while (en.hasMoreElements()) {
92                Identity current = (Identity) en.nextElement();
93                if (current.getPublicKey() == pk)
94                    return current;
95            }
96            return null;
97        }
98
99        public Enumeration<Identity> identities() {
100            return identities.elements();
101        }
102
103        public void addIdentity(Identity id) throws KeyManagementException {
104            if (identities.containsKey(id))
105                throw new KeyManagementException(
106                        "This Identity is already contained in the scope");
107            if (getIdentity(id.getPublicKey()) != null)
108                throw new KeyManagementException(
109                        "This Identity's public key already exists in the scope");
110            identities.put(id, id);
111        }
112
113        public void removeIdentity(Identity id) throws KeyManagementException {
114            if (!identities.containsKey(id))
115                throw new KeyManagementException(
116                        "This Identity is not contained in the scope");
117            identities.remove(id);
118        }
119    }
120
121    /**
122     * @tests java.security.IdentityScope#IdentityScope()
123     */
124    @TestTargetNew(
125        level = TestLevel.COMPLETE,
126        notes = "",
127        method = "IdentityScope",
128        args = {}
129    )
130    public void test_Constructor() {
131        new IdentityScopeSubclass();
132    }
133
134    /**
135     * @tests java.security.IdentityScope#IdentityScope(java.lang.String)
136     */
137    @TestTargetNew(
138        level = TestLevel.COMPLETE,
139        notes = "",
140        method = "IdentityScope",
141        args = {java.lang.String.class}
142    )
143    public void test_ConstructorLjava_lang_String() {
144        String[] str = {"test", "", null};
145        IdentityScopeSubclass iss;
146
147        for (int i = 0; i < str.length; i++) {
148            try {
149                iss = new IdentityScopeSubclass(str[i]);
150                assertNotNull(iss);
151                assertTrue(iss instanceof IdentityScope);
152            } catch (Exception e) {
153                fail("Unexpected exception for parameter " + str[i]);
154            }
155        }
156    }
157
158    /**
159     * @tests java.security.IdentityScope#IdentityScope(java.lang.String,
160     *        java.security.IdentityScope)
161     */
162    @TestTargetNew(
163        level = TestLevel.COMPLETE,
164        notes = "",
165        method = "IdentityScope",
166        args = {java.lang.String.class, java.security.IdentityScope.class}
167    )
168    public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope() {
169        String nameNull = null;
170        String[] str = {"test", "", "!@#$%^&*()", "identity name"};
171        IdentityScope is;
172        IdentityScope iss = new IdentityScopeSubclass("test scope");
173
174        for (int i = 0; i < str.length; i++) {
175            try {
176                is = new IdentityScopeSubclass(str[i], new IdentityScopeSubclass());
177                assertNotNull(is);
178                assertTrue(is instanceof IdentityScope);
179            } catch (Exception e) {
180                fail("Unexpected exception for parameter " + str[i]);
181            }
182        }
183
184        try {
185            is = new IdentityScopeSubclass(nameNull, new IdentityScopeSubclass());
186        } catch (NullPointerException npe) {
187        } catch (Exception e) {
188            fail("Incorrect exception " + e + " was thrown");
189        }
190
191        try {
192            is = new IdentityScopeSubclass("test", iss);
193            is = new IdentityScopeSubclass("test", iss);
194            fail("KeyManagementException was not thrown");
195        } catch (KeyManagementException npe) {
196            //expected
197        } catch (Exception e) {
198            fail("Incorrect exception " + e + " was thrown instead of KeyManagementException");
199        }
200    }
201
202    /**
203     * @tests java.security.IdentityScope#addIdentity(java.security.Identity)
204     */
205    @TestTargetNew(
206        level = TestLevel.COMPLETE,
207        notes = "",
208        method = "addIdentity",
209        args = {java.security.Identity.class}
210    )
211    public void test_addIdentityLjava_security_Identity() throws Exception {
212               IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
213                       new IdentityScopeSubclass());
214               Identity id = new IdentitySubclass("id1");
215               id.setPublicKey(pubKey);
216               sub.addIdentity(id);
217               try {
218                   Identity id2 = new IdentitySubclass("id2");
219                   id2.setPublicKey(pubKey);
220                   sub.addIdentity(id2);
221                   fail("KeyManagementException should have been thrown");
222               } catch (KeyManagementException e) {
223                   // Expected
224               }
225    }
226
227    /**
228     * @tests java.security.IdentityScope#removeIdentity(java.security.Identity)
229     */
230    @TestTargetNew(
231        level = TestLevel.COMPLETE,
232        notes = "",
233        method = "removeIdentity",
234        args = {java.security.Identity.class}
235    )
236    public void test_removeIdentityLjava_security_Identity() throws Exception {
237               IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
238                       new IdentityScopeSubclass());
239               Identity id = new IdentitySubclass();
240               id.setPublicKey(pubKey);
241               sub.addIdentity(id);
242               sub.removeIdentity(id);
243               try {
244                   sub.removeIdentity(id);
245                   fail("KeyManagementException should have been thrown");
246               } catch (KeyManagementException e) {
247                   // expected
248               }
249    }
250
251    /**
252     * @tests java.security.IdentityScope#identities()
253     */
254    @TestTargetNew(
255        level = TestLevel.COMPLETE,
256        notes = "",
257        method = "identities",
258        args = {}
259    )
260    public void test_identities() throws Exception {
261               IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
262                       new IdentityScopeSubclass());
263               Identity id = new IdentitySubclass();
264               id.setPublicKey(pubKey);
265               sub.addIdentity(id);
266               Enumeration<Identity> en = sub.identities();
267               assertTrue("Wrong object contained in identities", en.nextElement()
268                       .equals(id));
269               assertTrue("Contains too many elements", !en.hasMoreElements());
270    }
271
272    /**
273     * @tests java.security.IdentityScope#getIdentity(java.security.Principal)
274     */
275    @TestTargetNew(
276        level = TestLevel.COMPLETE,
277        method = "getIdentity",
278        args = {java.security.Principal.class}
279    )
280    public void test_getIdentityLjava_security_Principal() throws Exception {
281        Identity id = new IdentitySubclass("principal name");
282        id.setPublicKey(pubKey);
283        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
284                new IdentityScopeSubclass());
285
286        try {
287            sub.getIdentity((java.security.Principal) null);
288            fail("Test 1: NullPointerException expected.");
289        } catch (NullPointerException e) {
290            // Expected.
291        }
292
293        sub.addIdentity(id);
294        Identity returnedId = sub.getIdentity(id);
295        assertEquals("Test 2: Returned Identity not the same as the added one;",
296                id, returnedId);
297
298        Identity id2 = new IdentitySubclass("Another identity");
299        id2.setPublicKey(pubKey);
300
301        assertNull("Test 3: Null value expected.",
302                sub.getIdentity(id2));
303
304        try {
305            sub.getIdentity((java.security.Principal) null);
306            fail("Test 4: NullPointerException expected.");
307        } catch (NullPointerException e) {
308            // Expected.
309        }
310
311    }
312
313    /**
314     * @tests java.security.IdentityScope#getIdentity(java.security.PublicKey)
315     */
316    @TestTargetNew(
317        level = TestLevel.COMPLETE,
318        method = "getIdentity",
319        args = {java.security.PublicKey.class}
320    )
321    public void test_getIdentityLjava_security_PublicKey() throws Exception {
322        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
323                new IdentityScopeSubclass());
324        Identity id = new IdentitySubclass();
325        id.setPublicKey(pubKey);
326        sub.addIdentity(id);
327        Identity returnedId = sub.getIdentity(pubKey);
328        assertEquals("Test 1: Returned Identity not the same as the added one;",
329                id, returnedId);
330
331        assertNull("Test 2: Null value expected.",
332                sub.getIdentity((PublicKey) null));
333
334        PublicKey anotherKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic();
335        assertNull("Test 3: Null value expected.",
336                sub.getIdentity(anotherKey));
337    }
338
339    /**
340     * @tests java.security.IdentityScope#getIdentity(java.lang.String)
341     */
342    @TestTargetNew(
343        level = TestLevel.PARTIAL_COMPLETE,
344        notes = "",
345        method = "getIdentity",
346        args = {java.lang.String.class}
347    )
348    public void test_getIdentityLjava_lang_String() throws Exception {
349               IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
350                       new IdentityScopeSubclass());
351               Identity id = new IdentitySubclass("test");
352               id.setPublicKey(pubKey);
353               sub.addIdentity(id);
354               Identity returnedId = sub.getIdentity("test");
355               assertEquals("Returned Identity not the same as the added one", id,
356                       returnedId);
357    }
358
359    /**
360     * @tests java.security.IdentityScope#size()
361     */
362    @TestTargetNew(
363        level = TestLevel.COMPLETE,
364        notes = "",
365        method = "size",
366        args = {}
367    )
368    public void test_size() throws Exception {
369               IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
370                       new IdentityScopeSubclass());
371               Identity id = new IdentitySubclass();
372               id.setPublicKey(pubKey);
373               sub.addIdentity(id);
374               assertEquals("Wrong size", 1, sub.size());
375    }
376
377    /**
378     * @tests java.security.IdentityScope#toString()
379     */
380    @TestTargetNew(
381        level = TestLevel.COMPLETE,
382        notes = "",
383        method = "toString",
384        args = {}
385    )
386    public void test_toString() throws Exception {
387            IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
388                    new IdentityScopeSubclass());
389            Identity id = new IdentitySubclass();
390            id.setPublicKey(pubKey);
391            sub.addIdentity(id);
392            assertNotNull("toString returned a null", sub.toString());
393            assertTrue("Not a valid String ", sub.toString().length() > 0);
394    }
395
396    @TestTargetNew(
397        level = TestLevel.PARTIAL_COMPLETE,
398        notes = "Regression test",
399        method = "getIdentity",
400        args = {java.lang.String.class}
401    )
402    public void test_getIdentity() throws Exception {
403        //Regression for HARMONY-1173
404        IdentityScope scope = IdentityScope.getSystemScope();
405        try {
406            scope.getIdentity((String) null);
407            fail("NPE expected");
408        } catch (NullPointerException npe) {}
409    }
410}
411