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