IdentityTest.java revision fc95c99cfa4921fef424f3f411d013b821589e69
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 18/** 19* @author Aleksei Y. Semenov 20*/ 21 22package java.security; 23 24 25 26import org.apache.harmony.security.tests.support.CertificateStub; 27import org.apache.harmony.security.tests.support.IdentityStub; 28import org.apache.harmony.security.tests.support.PublicKeyStub; 29 30import junit.framework.TestCase; 31 32/** 33 * Tests for class Identity 34 * 35 */ 36 37public class IdentityTest extends TestCase { 38 39 /** 40 * Constructor for IdentityTest. 41 * @param name 42 */ 43 public IdentityTest(String name) { 44 super(name); 45 } 46 47 public void testHashCode() { 48 new IdentityStub("testHashCode").hashCode(); 49 } 50 51 public void testEquals() throws Exception { 52 Identity i1 = new IdentityStub("testEquals"); 53 Object value[] = { 54 null, Boolean.FALSE, 55 new Object(), Boolean.FALSE, 56 i1, Boolean.TRUE, 57 new IdentityStub(i1.getName()), Boolean.TRUE 58 }; 59 60 for (int k=0; k<value.length; k+=2) { 61 assertEquals(value[k+1], new Boolean(i1.equals(value[k]))); 62 if (Boolean.TRUE.equals(value[k+1])) assertEquals(i1.hashCode(), value[k].hashCode()); 63 } 64 // check other cases 65 Identity i2 = new IdentityStub("testEquals", IdentityScope.getSystemScope()); 66 assertEquals(i1.identityEquals(i2), i1.equals(i2)); 67 Identity i3 = new IdentityStub("testEquals3"); 68 assertEquals(i1.identityEquals(i3), i1.equals(i3)); 69 70 } 71 72 /** 73 * verify Identity.toString() 74 */ 75 public void testToString2() { 76 assertNotNull(new IdentityStub("testToString2").toString()); 77 } 78 79 /** 80 * verify Identity() creates instance 81 */ 82 public void testIdentity() { 83 assertNotNull(new IdentityStub()); 84 } 85 86 /* 87 * verify Identity(String) creates instance with given name 88 */ 89 public void testIdentityString() { 90 Identity i = new IdentityStub("iii"); 91 assertNotNull(i); 92 assertEquals("iii", i.getName()); 93 i=new IdentityStub(null); 94 assertNotNull(i); 95 assertNull(i.getName()); 96 } 97 98 /** 99 * verify Identity(String, IdentityScope) creates instance with given name and in give scope 100 */ 101 public void testIdentityStringIdentityScope() throws Exception { 102 IdentityScope s = IdentityScope.getSystemScope(); 103 Identity i = new IdentityStub("iii2", s); 104 assertNotNull(i); 105 assertEquals("iii2", i.getName()); 106 assertSame(s, i.getScope()); 107 assertSame(i, s.getIdentity(i.getName())); 108 } 109 110 /** 111 * verify addCertificate(Certificate certificate) adds a certificate for this identity. 112 * If the identity has a public key, the public key in the certificate must be the same 113 * 114 */ 115 public void testAddCertificate1() throws Exception { 116 Identity i = new IdentityStub("iii"); 117 PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", new byte[]{1,2,3,4,5}); 118 i.setPublicKey(pk1); 119 // try with the same key 120 CertificateStub c1 = new CertificateStub("fff", null, null, pk1); 121 i.addCertificate(c1); 122 assertSame(c1, i.certificates()[0]); 123 // try Certificate with different key 124 try { 125 i.addCertificate(new CertificateStub("ccc", null, null, new PublicKeyStub("k2", "fff", new byte[]{6,7,8,9,0}))); 126 fail("KeyManagementException should be thrown"); 127 } catch (KeyManagementException ok) {} 128 } 129 130 /** 131 * verify addCertificate(Certificate certificate) adds a certificate for this identity. 132 * if the identity does not have a public key, the identity's public key is set to be that specified in the certificate. 133 */ 134 public void testAddCertificate2() throws Exception { 135 Identity i = new IdentityStub("iii"); 136 PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null); 137 CertificateStub c1 = new CertificateStub("fff", null, null, pk1); 138 i.addCertificate(c1); 139 assertSame(c1, i.certificates()[0]); 140 assertSame(pk1, i.getPublicKey()); 141 142 } 143 144 /** 145 * verify addCertificate(Certificate certificate) throws KeyManagementException if certificate is null 146 */ 147 public void testAddCertificate4() throws Exception { 148 try { 149 new IdentityStub("aaa").addCertificate(null); 150 fail("KeyManagementException should be thrown"); 151 } catch (KeyManagementException ok) { 152 } catch (NullPointerException ok) {} 153 154 } 155// 156// Commented out since there will no be fix for the test failure 157// /** 158// * verify removeCertificate(Certificate certificate) removes certificate 159// */ 160// public void testRemoveCertificate1() throws Exception{ 161// Identity i = new IdentityStub("iii"); 162// PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null); 163// CertificateStub c1 = new CertificateStub("fff", null, null, pk1); 164// i.addCertificate(c1); 165// assertSame(c1, i.certificates()[0]); 166// i.removeCertificate(c1); 167// assertEquals(0, i.certificates().length); 168// // throw KeyManagementException if certificate not found 169// try { 170// i.removeCertificate(c1); 171// fail("KeyManagementException should be thrown"); 172// } catch (KeyManagementException ok) { 173// } 174// try { 175// i.removeCertificate(null); 176// fail("KeyManagementException should be thrown"); 177// } catch (KeyManagementException ok) { 178// 179// } 180// } 181 182 /** 183 * verify certificates() returns a copy of all certificates for this identity 184 */ 185 public void testCertificates() throws Exception { 186 Identity i = new IdentityStub("iii"); 187 PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null); 188 CertificateStub c1 = new CertificateStub("fff", null, null, pk1); 189 CertificateStub c2 = new CertificateStub("zzz", null, null, pk1); 190 i.addCertificate(c1); 191 i.addCertificate(c2); 192 Certificate[] s = i.certificates(); 193 assertEquals(2, s.length); 194 assertTrue(c1.equals(s[0]) || c2.equals(s[0])); 195 assertTrue(c1.equals(s[1]) || c2.equals(s[1])); 196 s[0] = null; 197 s[1] = null; 198 // check that the copy was modified 199 s = i.certificates(); 200 assertEquals(2, s.length); 201 assertTrue(c1.equals(s[0]) || c2.equals(s[0])); 202 assertTrue(c1.equals(s[1]) || c2.equals(s[1])); 203 } 204 205 /** 206 * verify Identity.identityEquals(Identity) return true, only if names and public keys are equal 207 */ 208 209 public void testIdentityEquals() throws Exception { 210 String name = "nnn"; 211 PublicKey pk = new PublicKeyStub("aaa", "fff", new byte[]{1,2,3,4,5}); 212 Identity i = new IdentityStub(name); 213 i.setPublicKey(pk); 214 Object[] value = { 215 //null, Boolean.FALSE, 216 //new Object(), Boolean.FALSE, 217 new IdentityStub("111"), Boolean.FALSE, 218 new IdentityStub(name), Boolean.FALSE, 219 new IdentityStub(name, IdentityScope.getSystemScope()), Boolean.FALSE, 220 i, Boolean.TRUE, 221 new IdentityStub(name, pk), Boolean.TRUE 222 }; 223 for (int k=0; k<value.length; k+=2){ 224 assertEquals(value[k+1], new Boolean(i.identityEquals((Identity)value[k]))); 225 if (Boolean.TRUE.equals(value[k+1])) assertEquals(i.hashCode(), value[k].hashCode()); 226 } 227 Identity i2 = IdentityScope.getSystemScope().getIdentity(name); 228 i2.setPublicKey(pk); 229 assertTrue(i.identityEquals(i2)); 230 } 231 232 /** 233 * verify Identity.toString(boolean) return string representation of identity 234 */ 235 public void testToStringboolean() throws Exception { 236 new IdentityStub("aaa").toString(false); 237 new IdentityStub("aaa2", IdentityScope.getSystemScope()).toString(false); 238 new IdentityStub("bbb").toString(true); 239 new IdentityStub("bbb2", IdentityScope.getSystemScope()).toString(true); 240 } 241 242 /** 243 * verify Identity.getScope() returns identity's scope 244 */ 245 public void testGetScope() throws Exception { 246 Identity i = new IdentityStub("testGetScope"); 247 assertNull(i.getScope()); 248 IdentityScope s = IdentityScope.getSystemScope(); 249 250 Identity i2 = new IdentityStub("testGetScope2", s); 251 assertSame(s, i2.getScope()); 252 253 } 254 255 /** 256 * 257 * verify Identity.setPublicKey() throws KeyManagementException if key is invalid 258 * 259 */ 260 public void testSetPublicKey2() throws Exception { 261 Identity i2 = new IdentityStub("testSetPublicKey2_2", IdentityScope.getSystemScope()); 262 new PublicKeyStub("kkk", "testSetPublicKey2", new byte[]{1,2,3,4,5}); 263 try { 264 i2.setPublicKey(null); 265 //fail("KeyManagementException should be thrown - key is null"); 266 } catch (KeyManagementException ok) {} 267 } 268 269// 270// Commented out since there will no be fix for the test failure 271// /** 272// * 273// * verify Identity.setPublicKey() throws KeyManagementException if key is already used 274// * 275// */ 276// public void testSetPublicKey3() throws Exception { 277// Identity i1 = new IdentityStub("testSetPublicKey3_1", IdentityScope.getSystemScope()); 278// Identity i2 = new IdentityStub("testSetPublicKey3_2", IdentityScope.getSystemScope()); 279// PublicKey pk = new PublicKeyStub("kkk", "fff", new byte[]{1,2,3,4,5}); 280// i1.setPublicKey(pk); 281// try { 282// i2.setPublicKey(pk); 283// fail("KeyManagementException should be thrown - key already used"); 284// } catch (KeyManagementException ok) {}; 285// } 286 /** 287 * 288 * verify Identity.setPublicKey() removes old key and all identity's certificates 289 * 290 */ 291 public void testSetPublicKey4() throws Exception { 292 Identity i = new IdentityStub("testSetPublicKey4"); 293 PublicKeyStub pk1 = new PublicKeyStub("kkk", "Identity.testSetPublicKey4", null); 294 CertificateStub c1 = new CertificateStub("fff", null, null, pk1); 295 CertificateStub c2 = new CertificateStub("zzz", null, null, pk1); 296 i.addCertificate(c1); 297 i.addCertificate(c2); 298 assertEquals(2, i.certificates().length); 299 assertSame(pk1, i.getPublicKey()); 300 301 PublicKeyStub pk2 = new PublicKeyStub("zzz", "Identity.testSetPublicKey4", null); 302 i.setPublicKey(pk2); 303 assertSame(pk2, i.getPublicKey()); 304 assertEquals(0, i.certificates().length); 305 } 306 307 /** 308 * verify Identity.getPublicKey() returns public key 309 */ 310 311 public void testGetPublicKey() throws Exception { 312 Identity i = new IdentityStub("testGetPublicKey"); 313 assertNull(i.getPublicKey()); 314 PublicKey pk = new PublicKeyStub("kkk", "Identity.testGetPublicKey", null); 315 i.setPublicKey(pk); 316 assertSame(pk, i.getPublicKey()); 317 } 318 319 public void testGetInfo() { 320 321 Identity i = new IdentityStub("testGetInfo"); 322 i.setInfo("some info"); 323 assertEquals("some info", i.getInfo()); 324 } 325 326 public void testGetName() { 327 Identity i = new IdentityStub("testGetName"); 328 assertEquals ("testGetName", i.getName()); 329 } 330 331} 332