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
30public class IdentityScope2Test extends junit.framework.TestCase {
31
32    static PublicKey pubKey;
33
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        Hashtable identities;
44
45        public IdentityScopeSubclass(String name, PublicKey pk) {
46            super(name);
47            try {
48                setPublicKey(pk);
49            } catch (KeyManagementException e) {
50            }
51            identities = new Hashtable();
52        }
53
54        public IdentityScopeSubclass() {
55            super();
56            identities = new Hashtable();
57        }
58
59        public IdentityScopeSubclass(String name) {
60            super(name);
61            identities = new Hashtable();
62        }
63
64        public IdentityScopeSubclass(String name, IdentityScope scope)
65                throws KeyManagementException {
66            super(name, scope);
67            identities = new Hashtable();
68        }
69
70        public int size() {
71            return identities.size();
72        }
73
74        public Identity getIdentity(String name) {
75            Enumeration en = identities();
76            while (en.hasMoreElements()) {
77                Identity current = (Identity) en.nextElement();
78                if (current.getName().equals(name))
79                    return current;
80            }
81            return null;
82        }
83
84        public Identity getIdentity(PublicKey pk) {
85            Enumeration en = identities();
86            while (en.hasMoreElements()) {
87                Identity current = (Identity) en.nextElement();
88                if (current.getPublicKey() == pk)
89                    return current;
90            }
91            return null;
92        }
93
94        public Enumeration identities() {
95            return identities.elements();
96        }
97
98        public void addIdentity(Identity id) throws KeyManagementException {
99            if (identities.containsKey(id))
100                throw new KeyManagementException(
101                        "This Identity is already contained in the scope");
102            if (getIdentity(id.getPublicKey()) != null)
103                throw new KeyManagementException(
104                        "This Identity's public key already exists in the scope");
105            identities.put(id, id);
106        }
107
108        public void removeIdentity(Identity id) throws KeyManagementException {
109            if (!identities.containsKey(id))
110                throw new KeyManagementException(
111                        "This Identity is not contained in the scope");
112            identities.remove(id);
113        }
114    }
115
116    /**
117     * @tests java.security.IdentityScope#IdentityScope()
118     */
119    public void test_Constructor() {
120        new IdentityScopeSubclass();
121    }
122
123    /**
124     * @tests java.security.IdentityScope#IdentityScope(java.lang.String)
125     */
126    public void test_ConstructorLjava_lang_String() {
127        new IdentityScopeSubclass("test");
128    }
129
130    /**
131     * @tests java.security.IdentityScope#IdentityScope(java.lang.String,
132     *java.security.IdentityScope)
133     */
134    public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope() throws Exception {
135        new IdentityScopeSubclass("test", new IdentityScopeSubclass());
136    }
137
138    /**
139     * @tests java.security.IdentityScope#addIdentity(java.security.Identity)
140     */
141    public void test_addIdentityLjava_security_Identity() throws Exception {
142        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
143                new IdentityScopeSubclass());
144        Identity id = new IdentitySubclass("id1");
145        id.setPublicKey(pubKey);
146        sub.addIdentity(id);
147        try {
148            Identity id2 = new IdentitySubclass("id2");
149            id2.setPublicKey(pubKey);
150            sub.addIdentity(id2);
151            fail("KeyManagementException should have been thrown");
152        } catch (KeyManagementException e) {
153            // Expected
154        }
155    }
156
157    /**
158     * @tests java.security.IdentityScope#removeIdentity(java.security.Identity)
159     */
160    public void test_removeIdentityLjava_security_Identity() throws Exception {
161        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
162                new IdentityScopeSubclass());
163        Identity id = new IdentitySubclass();
164        id.setPublicKey(pubKey);
165        sub.addIdentity(id);
166        sub.removeIdentity(id);
167        try {
168            sub.removeIdentity(id);
169            fail("KeyManagementException should have been thrown");
170        } catch (KeyManagementException e) {
171            // expected
172        }
173    }
174
175    /**
176     * @tests java.security.IdentityScope#identities()
177     */
178    public void test_identities() throws Exception {
179        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
180                new IdentityScopeSubclass());
181        Identity id = new IdentitySubclass();
182        id.setPublicKey(pubKey);
183        sub.addIdentity(id);
184        Enumeration en = sub.identities();
185        assertTrue("Wrong object contained in identities", en.nextElement()
186                .equals(id));
187        assertTrue("Contains too many elements", !en.hasMoreElements());
188    }
189
190    /**
191     * @tests java.security.IdentityScope#getIdentity(java.security.Principal)
192     */
193    public void test_getIdentityLjava_security_Principal() throws Exception {
194        Identity id = new IdentitySubclass("principal name");
195        id.setPublicKey(pubKey);
196        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
197                new IdentityScopeSubclass());
198        sub.addIdentity(id);
199        Identity returnedId = sub.getIdentity(id);
200        assertEquals("Returned Identity not the same as the added one", id,
201                returnedId);
202    }
203
204    /**
205     * @tests java.security.IdentityScope#getIdentity(java.security.PublicKey)
206     */
207    public void test_getIdentityLjava_security_PublicKey() throws Exception {
208        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
209                new IdentityScopeSubclass());
210        Identity id = new IdentitySubclass();
211        id.setPublicKey(pubKey);
212        sub.addIdentity(id);
213        Identity returnedId = sub.getIdentity(pubKey);
214        assertEquals("Returned Identity not the same as the added one", id,
215                returnedId);
216    }
217
218    /**
219     * @tests java.security.IdentityScope#getIdentity(java.lang.String)
220     */
221    public void test_getIdentityLjava_lang_String() throws Exception {
222        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
223                new IdentityScopeSubclass());
224        Identity id = new IdentitySubclass("test");
225        id.setPublicKey(pubKey);
226        sub.addIdentity(id);
227        Identity returnedId = sub.getIdentity("test");
228        assertEquals("Returned Identity not the same as the added one", id,
229                returnedId);
230    }
231
232    /**
233     * @tests java.security.IdentityScope#size()
234     */
235    public void test_size() throws Exception {
236        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
237                new IdentityScopeSubclass());
238        Identity id = new IdentitySubclass();
239        id.setPublicKey(pubKey);
240        sub.addIdentity(id);
241        assertEquals("Wrong size", 1, sub.size());
242    }
243
244    /**
245     * @tests java.security.IdentityScope#toString()
246     */
247    public void test_toString() throws Exception {
248        IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
249                new IdentityScopeSubclass());
250        Identity id = new IdentitySubclass();
251        id.setPublicKey(pubKey);
252        sub.addIdentity(id);
253        assertNotNull("toString returned a null", sub.toString());
254        assertTrue("Not a valid String ", sub.toString().length() > 0);
255    }
256
257    public void test_getIdentity() throws Exception {
258        //Regression for HARMONY-1173
259        IdentityScope scope = IdentityScope.getSystemScope();
260        try {
261            scope.getIdentity((String) null);
262            fail("NPE expected");
263        } catch (NullPointerException npe) {
264        }
265    }
266}
267