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 private static PublicKey PUB_KEY; 34 private static PublicKey getPubKey() throws Exception { 35 if (PUB_KEY == null) { 36 PUB_KEY = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic(); 37 } 38 return PUB_KEY; 39 } 40 41 public static class IdentityScopeSubclass extends IdentityScope { 42 private static final long serialVersionUID = 1L; 43 Hashtable<Identity, Identity> 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<Identity, Identity>(); 52 } 53 54 public IdentityScopeSubclass() { 55 super(); 56 identities = new Hashtable<Identity, Identity>(); 57 } 58 59 public IdentityScopeSubclass(String name) { 60 super(name); 61 identities = new Hashtable<Identity, Identity>(); 62 } 63 64 public IdentityScopeSubclass(String name, IdentityScope scope) 65 throws KeyManagementException { 66 super(name, scope); 67 identities = new Hashtable<Identity, Identity>(); 68 } 69 70 public int size() { 71 return identities.size(); 72 } 73 74 public Identity getIdentity(String name) { 75 Enumeration<Identity> 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<Identity> 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<Identity> 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 * java.security.IdentityScope#IdentityScope() 118 */ 119 public void test_Constructor() { 120 new IdentityScopeSubclass(); 121 } 122 123 /** 124 * java.security.IdentityScope#IdentityScope(java.lang.String) 125 */ 126 public void test_ConstructorLjava_lang_String() { 127 String[] str = {"test", "", null}; 128 IdentityScopeSubclass iss; 129 130 for (int i = 0; i < str.length; i++) { 131 try { 132 iss = new IdentityScopeSubclass(str[i]); 133 assertNotNull(iss); 134 assertTrue(iss instanceof IdentityScope); 135 } catch (Exception e) { 136 fail("Unexpected exception for parameter " + str[i]); 137 } 138 } 139 } 140 141 /** 142 * java.security.IdentityScope#IdentityScope(java.lang.String, 143 * java.security.IdentityScope) 144 */ 145 public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope() { 146 String nameNull = null; 147 String[] str = {"test", "", "!@#$%^&*()", "identity name"}; 148 IdentityScope is; 149 IdentityScope iss = new IdentityScopeSubclass("test scope"); 150 151 for (int i = 0; i < str.length; i++) { 152 try { 153 is = new IdentityScopeSubclass(str[i], new IdentityScopeSubclass()); 154 assertNotNull(is); 155 assertTrue(is instanceof IdentityScope); 156 } catch (Exception e) { 157 fail("Unexpected exception for parameter " + str[i]); 158 } 159 } 160 161 try { 162 is = new IdentityScopeSubclass(nameNull, new IdentityScopeSubclass()); 163 } catch (NullPointerException npe) { 164 } catch (Exception e) { 165 fail("Incorrect exception " + e + " was thrown"); 166 } 167 168 try { 169 is = new IdentityScopeSubclass("test", iss); 170 is = new IdentityScopeSubclass("test", iss); 171 fail("KeyManagementException was not thrown"); 172 } catch (KeyManagementException npe) { 173 //expected 174 } catch (Exception e) { 175 fail("Incorrect exception " + e + " was thrown instead of KeyManagementException"); 176 } 177 } 178 179 /** 180 * java.security.IdentityScope#addIdentity(java.security.Identity) 181 */ 182 public void test_addIdentityLjava_security_Identity() throws Exception { 183 IdentityScopeSubclass sub = new IdentityScopeSubclass("test", 184 new IdentityScopeSubclass()); 185 Identity id = new IdentitySubclass("id1"); 186 id.setPublicKey(getPubKey()); 187 sub.addIdentity(id); 188 try { 189 Identity id2 = new IdentitySubclass("id2"); 190 id2.setPublicKey(getPubKey()); 191 sub.addIdentity(id2); 192 fail("KeyManagementException should have been thrown"); 193 } catch (KeyManagementException e) { 194 // Expected 195 } 196 } 197 198 /** 199 * java.security.IdentityScope#removeIdentity(java.security.Identity) 200 */ 201 public void test_removeIdentityLjava_security_Identity() throws Exception { 202 IdentityScopeSubclass sub = new IdentityScopeSubclass("test", 203 new IdentityScopeSubclass()); 204 Identity id = new IdentitySubclass(); 205 id.setPublicKey(getPubKey()); 206 sub.addIdentity(id); 207 sub.removeIdentity(id); 208 try { 209 sub.removeIdentity(id); 210 fail("KeyManagementException should have been thrown"); 211 } catch (KeyManagementException expected) { 212 } 213 } 214 215 /** 216 * java.security.IdentityScope#identities() 217 */ 218 public void test_identities() throws Exception { 219 IdentityScopeSubclass sub = new IdentityScopeSubclass("test", 220 new IdentityScopeSubclass()); 221 Identity id = new IdentitySubclass(); 222 id.setPublicKey(getPubKey()); 223 sub.addIdentity(id); 224 Enumeration<Identity> en = sub.identities(); 225 assertEquals("Wrong object contained in identities", en.nextElement(), id); 226 assertFalse("Contains too many elements", en.hasMoreElements()); 227 } 228 229 /** 230 * java.security.IdentityScope#getIdentity(java.security.Principal) 231 */ 232 public void test_getIdentityLjava_security_Principal() throws Exception { 233 Identity id = new IdentitySubclass("principal name"); 234 id.setPublicKey(getPubKey()); 235 IdentityScopeSubclass sub = new IdentityScopeSubclass("test", 236 new IdentityScopeSubclass()); 237 238 try { 239 sub.getIdentity((java.security.Principal) null); 240 fail("Test 1: NullPointerException expected."); 241 } catch (NullPointerException expected) { 242 } 243 244 sub.addIdentity(id); 245 Identity returnedId = sub.getIdentity(id); 246 assertEquals("Test 2: Returned Identity not the same as the added one;", id, returnedId); 247 248 Identity id2 = new IdentitySubclass("Another identity"); 249 id2.setPublicKey(getPubKey()); 250 251 assertNull("Test 3: Null value expected.", sub.getIdentity(id2)); 252 253 try { 254 sub.getIdentity((java.security.Principal) null); 255 fail("Test 4: NullPointerException expected."); 256 } catch (NullPointerException expected) { 257 } 258 259 } 260 261 /** 262 * java.security.IdentityScope#getIdentity(java.security.PublicKey) 263 */ 264 public void test_getIdentityLjava_security_PublicKey() throws Exception { 265 IdentityScopeSubclass sub = new IdentityScopeSubclass("test", 266 new IdentityScopeSubclass()); 267 Identity id = new IdentitySubclass(); 268 id.setPublicKey(getPubKey()); 269 sub.addIdentity(id); 270 Identity returnedId = sub.getIdentity(getPubKey()); 271 assertEquals("Test 1: Returned Identity not the same as the added one;", id, returnedId); 272 273 assertNull("Test 2: Null value expected.", sub.getIdentity((PublicKey) null)); 274 275 PublicKey anotherKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic(); 276 assertNull("Test 3: Null value expected.", sub.getIdentity(anotherKey)); 277 } 278 279 /** 280 * java.security.IdentityScope#getIdentity(java.lang.String) 281 */ 282 public void test_getIdentityLjava_lang_String() throws Exception { 283 IdentityScopeSubclass sub = new IdentityScopeSubclass("test", 284 new IdentityScopeSubclass()); 285 Identity id = new IdentitySubclass("test"); 286 id.setPublicKey(getPubKey()); 287 sub.addIdentity(id); 288 Identity returnedId = sub.getIdentity("test"); 289 assertEquals("Returned Identity not the same as the added one", id, returnedId); 290 } 291 292 /** 293 * java.security.IdentityScope#size() 294 */ 295 public void test_size() throws Exception { 296 IdentityScopeSubclass sub = new IdentityScopeSubclass("test", 297 new IdentityScopeSubclass()); 298 Identity id = new IdentitySubclass(); 299 id.setPublicKey(getPubKey()); 300 sub.addIdentity(id); 301 assertEquals("Wrong size", 1, sub.size()); 302 } 303 304 /** 305 * java.security.IdentityScope#toString() 306 */ 307 public void test_toString() throws Exception { 308 IdentityScopeSubclass sub = new IdentityScopeSubclass("test", 309 new IdentityScopeSubclass()); 310 Identity id = new IdentitySubclass(); 311 id.setPublicKey(getPubKey()); 312 sub.addIdentity(id); 313 assertNotNull("toString returned a null", sub.toString()); 314 assertTrue("Not a valid String ", sub.toString().length() > 0); 315 } 316 317 public void test_getIdentity() throws Exception { 318 //Regression for HARMONY-1173 319 IdentityScope scope = IdentityScope.getSystemScope(); 320 try { 321 scope.getIdentity((String) null); 322 fail(); 323 } catch (NullPointerException expected) { 324 } 325 } 326} 327