001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.security.tests.java.security;
019:
020: import java.security.Identity;
021: import java.security.IdentityScope;
022: import java.security.KeyManagementException;
023: import java.security.KeyPairGenerator;
024: import java.security.PublicKey;
025: import java.util.Enumeration;
026: import java.util.Hashtable;
027:
028: import org.apache.harmony.security.tests.java.security.Identity2Test.IdentitySubclass;
029:
030: public class IdentityScope2Test extends junit.framework.TestCase {
031:
032: static PublicKey pubKey;
033: static {
034: try {
035: pubKey = KeyPairGenerator.getInstance("DSA").genKeyPair()
036: .getPublic();
037: } catch (Exception e) {
038: fail(e.toString());
039: }
040: }
041:
042: public static class IdentityScopeSubclass extends IdentityScope {
043: Hashtable identities;
044:
045: public IdentityScopeSubclass(String name, PublicKey pk) {
046: super (name);
047: try {
048: setPublicKey(pk);
049: } catch (KeyManagementException e) {
050: }
051: identities = new Hashtable();
052: }
053:
054: public IdentityScopeSubclass() {
055: super ();
056: identities = new Hashtable();
057: }
058:
059: public IdentityScopeSubclass(String name) {
060: super (name);
061: identities = new Hashtable();
062: }
063:
064: public IdentityScopeSubclass(String name, IdentityScope scope)
065: throws KeyManagementException {
066: super (name, scope);
067: identities = new Hashtable();
068: }
069:
070: public int size() {
071: return identities.size();
072: }
073:
074: public Identity getIdentity(String name) {
075: Enumeration en = identities();
076: while (en.hasMoreElements()) {
077: Identity current = (Identity) en.nextElement();
078: if (current.getName().equals(name))
079: return current;
080: }
081: return null;
082: }
083:
084: public Identity getIdentity(PublicKey pk) {
085: Enumeration en = identities();
086: while (en.hasMoreElements()) {
087: Identity current = (Identity) en.nextElement();
088: if (current.getPublicKey() == pk)
089: return current;
090: }
091: return null;
092: }
093:
094: public Enumeration identities() {
095: return identities.elements();
096: }
097:
098: public void addIdentity(Identity id)
099: 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)
110: throws KeyManagementException {
111: if (!identities.containsKey(id))
112: throw new KeyManagementException(
113: "This Identity is not contained in the scope");
114: identities.remove(id);
115: }
116: }
117:
118: /**
119: * @tests java.security.IdentityScope#IdentityScope()
120: */
121: public void test_Constructor() {
122: new IdentityScopeSubclass();
123: }
124:
125: /**
126: * @tests java.security.IdentityScope#IdentityScope(java.lang.String)
127: */
128: public void test_ConstructorLjava_lang_String() {
129: new IdentityScopeSubclass("test");
130: }
131:
132: /**
133: * @tests java.security.IdentityScope#IdentityScope(java.lang.String,
134: * java.security.IdentityScope)
135: */
136: public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope()
137: throws Exception {
138: new IdentityScopeSubclass("test", new IdentityScopeSubclass());
139: }
140:
141: /**
142: * @tests java.security.IdentityScope#addIdentity(java.security.Identity)
143: */
144: public void test_addIdentityLjava_security_Identity()
145: throws Exception {
146: IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
147: new IdentityScopeSubclass());
148: Identity id = new IdentitySubclass("id1");
149: id.setPublicKey(pubKey);
150: sub.addIdentity(id);
151: try {
152: Identity id2 = new IdentitySubclass("id2");
153: id2.setPublicKey(pubKey);
154: sub.addIdentity(id2);
155: fail("KeyManagementException should have been thrown");
156: } catch (KeyManagementException e) {
157: // Expected
158: }
159: }
160:
161: /**
162: * @tests java.security.IdentityScope#removeIdentity(java.security.Identity)
163: */
164: public void test_removeIdentityLjava_security_Identity()
165: throws Exception {
166: IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
167: new IdentityScopeSubclass());
168: Identity id = new IdentitySubclass();
169: id.setPublicKey(pubKey);
170: sub.addIdentity(id);
171: sub.removeIdentity(id);
172: try {
173: sub.removeIdentity(id);
174: fail("KeyManagementException should have been thrown");
175: } catch (KeyManagementException e) {
176: // expected
177: }
178: }
179:
180: /**
181: * @tests java.security.IdentityScope#identities()
182: */
183: public void test_identities() throws Exception {
184: IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
185: new IdentityScopeSubclass());
186: Identity id = new IdentitySubclass();
187: id.setPublicKey(pubKey);
188: sub.addIdentity(id);
189: Enumeration en = sub.identities();
190: assertTrue("Wrong object contained in identities", en
191: .nextElement().equals(id));
192: assertTrue("Contains too many elements", !en.hasMoreElements());
193: }
194:
195: /**
196: * @tests java.security.IdentityScope#getIdentity(java.security.Principal)
197: */
198: public void test_getIdentityLjava_security_Principal()
199: throws Exception {
200: Identity id = new IdentitySubclass("principal name");
201: id.setPublicKey(pubKey);
202: IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
203: new IdentityScopeSubclass());
204: sub.addIdentity(id);
205: Identity returnedId = sub.getIdentity(id);
206: assertEquals("Returned Identity not the same as the added one",
207: id, returnedId);
208: }
209:
210: /**
211: * @tests java.security.IdentityScope#getIdentity(java.security.PublicKey)
212: */
213: public void test_getIdentityLjava_security_PublicKey()
214: throws Exception {
215: IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
216: new IdentityScopeSubclass());
217: Identity id = new IdentitySubclass();
218: id.setPublicKey(pubKey);
219: sub.addIdentity(id);
220: Identity returnedId = sub.getIdentity(pubKey);
221: assertEquals("Returned Identity not the same as the added one",
222: id, returnedId);
223: }
224:
225: /**
226: * @tests java.security.IdentityScope#getIdentity(java.lang.String)
227: */
228: public void test_getIdentityLjava_lang_String() throws Exception {
229: IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
230: new IdentityScopeSubclass());
231: Identity id = new IdentitySubclass("test");
232: id.setPublicKey(pubKey);
233: sub.addIdentity(id);
234: Identity returnedId = sub.getIdentity("test");
235: assertEquals("Returned Identity not the same as the added one",
236: id, returnedId);
237: }
238:
239: /**
240: * @tests java.security.IdentityScope#size()
241: */
242: public void test_size() throws Exception {
243: IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
244: new IdentityScopeSubclass());
245: Identity id = new IdentitySubclass();
246: id.setPublicKey(pubKey);
247: sub.addIdentity(id);
248: assertEquals("Wrong size", 1, sub.size());
249: }
250:
251: /**
252: * @tests java.security.IdentityScope#toString()
253: */
254: public void test_toString() throws Exception {
255: IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
256: new IdentityScopeSubclass());
257: Identity id = new IdentitySubclass();
258: id.setPublicKey(pubKey);
259: sub.addIdentity(id);
260: assertNotNull("toString returned a null", sub.toString());
261: assertTrue("Not a valid String ", sub.toString().length() > 0);
262: }
263:
264: public void test_getIdentity() throws Exception {
265: //Regression for HARMONY-1173
266: IdentityScope scope = IdentityScope.getSystemScope();
267: try {
268: scope.getIdentity((String) null);
269: fail("NPE expected");
270: } catch (NullPointerException npe) {
271: }
272: }
273: }
|