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.io.ByteArrayInputStream; 21import java.io.InputStream; 22import java.io.OutputStream; 23import java.io.UnsupportedEncodingException; 24import java.security.Identity; 25import java.security.IdentityScope; 26import java.security.KeyManagementException; 27import java.security.KeyPairGenerator; 28import java.security.Principal; 29import java.security.PublicKey; 30import java.security.cert.CertificateFactory; 31import java.security.cert.X509Certificate; 32 33import org.apache.harmony.security.tests.java.security.IdentityScope2Test.IdentityScopeSubclass; 34 35public class Identity2Test extends junit.framework.TestCase { 36 37 static PublicKey pubKey; 38 static { 39 try { 40 pubKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic(); 41 } catch (Exception e) { 42 fail(e.toString()); 43 } 44 } 45 46 public static class CertificateImpl implements java.security.Certificate { 47 48 X509Certificate cert; 49 50 public CertificateImpl(X509Certificate cert) { 51 this.cert = cert; 52 } 53 54 public Principal getGuarantor() { 55 return cert.getIssuerDN(); 56 } 57 58 public void encode(OutputStream out) { 59 } 60 61 public void decode(InputStream in) { 62 } 63 64 public String toString() { 65 return ""; 66 } 67 68 public String toString(boolean b) { 69 return ""; 70 } 71 72 public String getFormat() { 73 return cert.getType(); 74 } 75 76 public Principal getPrincipal() { 77 return cert.getSubjectDN(); 78 } 79 80 public PublicKey getPublicKey() { 81 return cert.getPublicKey(); 82 } 83 } 84 85 String certificate = "-----BEGIN CERTIFICATE-----\n" 86 + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n" 87 + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n" 88 + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n" 89 + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n" 90 + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n" 91 + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n" 92 + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n" 93 + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n" 94 + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n" 95 + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n" 96 + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n" 97 + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n" 98 + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n" 99 + "-----END CERTIFICATE-----\n"; 100 101 ByteArrayInputStream certArray; 102 103 String certificate2 = "-----BEGIN CERTIFICATE-----\n" 104 + "MIICZzCCAdCgAwIBAgIBGzANBgkqhkiG9w0BAQUFADBhMQswCQYDVQQGEwJVUzEY\n" 105 + "MBYGA1UEChMPVS5TLiBHb3Zlcm5tZW50MQwwCgYDVQQLEwNEb0QxDDAKBgNVBAsT\n" 106 + "A1BLSTEcMBoGA1UEAxMTRG9EIFBLSSBNZWQgUm9vdCBDQTAeFw05ODA4MDMyMjAy\n" 107 + "MjlaFw0wODA4MDQyMjAyMjlaMGExCzAJBgNVBAYTAlVTMRgwFgYDVQQKEw9VLlMu\n" 108 + "IEdvdmVybm1lbnQxDDAKBgNVBAsTA0RvRDEMMAoGA1UECxMDUEtJMRwwGgYDVQQD\n" 109 + "ExNEb0QgUEtJIE1lZCBSb290IENBMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n" 110 + "gQDbrM/J9FrJSX+zxFUbsI9Vw5QbguVBIa95rwW/0M8+sM0r5gd+DY6iubm6wnXk\n" 111 + "CSvbfQlFEDSKr4WYeeGp+d9WlDnQdtDFLdA45tCi5SHjnW+hGAmZnld0rz6wQekF\n" 112 + "5xQaa5A6wjhMlLOjbh27zyscrorMJ1O5FBOWnEHcRv6xqQIDAQABoy8wLTAdBgNV\n" 113 + "HQ4EFgQUVrmYR6m9701cHQ3r5kXyG7zsCN0wDAYDVR0TBAUwAwEB/zANBgkqhkiG\n" 114 + "9w0BAQUFAAOBgQDVX1Y0YqC7vekeZjVxtyuC8Mnxbrz6D109AX07LEIRzNYzwZ0w\n" 115 + "MTImSp9sEzWW+3FueBIU7AxGys2O7X0qmN3zgszPfSiocBuQuXIYQctJhKjF5KVc\n" 116 + "VGQRYYlt+myhl2vy6yPzEVCjiKwMEb1Spu0irCf+lFW2hsdjvmSQMtZvOw==\n" 117 + "-----END CERTIFICATE-----\n"; 118 119 ByteArrayInputStream certArray2; 120 121 { 122 try { 123 certArray = new ByteArrayInputStream(certificate 124 .getBytes("UTF-8")); 125 certArray2 = new ByteArrayInputStream(certificate2 126 .getBytes("UTF-8")); 127 } catch (UnsupportedEncodingException e) { 128 throw new RuntimeException(e.getMessage()); 129 } 130 } 131 132 public static class IdentitySubclass extends Identity { 133 public IdentitySubclass() { 134 super(); 135 } 136 137 public IdentitySubclass(String name) { 138 super(name); 139 } 140 141 public IdentitySubclass(String name, IdentityScope scope) 142 throws KeyManagementException { 143 super(name, scope); 144 } 145 } 146 147 /** 148 * @tests java.security.Identity#Identity() 149 */ 150 public void test_Constructor() { 151 new IdentitySubclass(); 152 } 153 154 /** 155 * @tests java.security.Identity#Identity(java.lang.String) 156 */ 157 public void test_ConstructorLjava_lang_String() { 158 new IdentitySubclass("test"); 159 } 160 161 /** 162 * @tests java.security.Identity#Identity(java.lang.String, 163 * java.security.IdentityScope) 164 */ 165 public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope() throws Exception { 166 new IdentitySubclass("test", new IdentityScopeSubclass()); 167 } 168 169 /** 170 * @tests java.security.Identity#getScope() 171 */ 172 public void test_getScope() throws Exception { 173 IdentityScope scope = new IdentityScopeSubclass(); 174 IdentitySubclass sub = new IdentitySubclass("test", scope); 175 IdentityScope returnedScope = sub.getScope(); 176 assertEquals("Wrong Scope returned", scope, returnedScope); 177 } 178 179 /** 180 * @tests java.security.Identity#getPublicKey() 181 */ 182 public void test_getPublicKey() throws Exception { 183 IdentitySubclass sub = new IdentitySubclass("test", 184 new IdentityScopeSubclass()); 185 sub.setPublicKey(pubKey); 186 PublicKey returnedPubKey = sub.getPublicKey(); 187 assertEquals("Wrong PublicKey returned", pubKey, returnedPubKey); 188 } 189 190 /** 191 * @tests java.security.Identity#getName() 192 */ 193 public void test_getName() throws Exception { 194 String name = "test"; 195 IdentitySubclass sub = new IdentitySubclass(name, 196 new IdentityScopeSubclass()); 197 assertEquals("Wrong Name returned", name, sub.getName()); 198 } 199 200 /** 201 * @tests java.security.Identity#getInfo() 202 */ 203 public void test_getInfo() throws Exception { 204 String info = "This is the general information."; 205 IdentitySubclass sub = new IdentitySubclass("test", 206 new IdentityScopeSubclass()); 207 sub.setInfo(info); 208 assertEquals("Wrong Info returned", info, sub.getInfo()); 209 } 210 211 /** 212 * @tests java.security.Identity#certificates() 213 */ 214 public void test_certificates() throws Exception { 215 IdentitySubclass sub = new IdentitySubclass("test", 216 new IdentityScopeSubclass()); 217 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 218 X509Certificate cert[] = new X509Certificate[1]; 219 cert[0] = (X509Certificate) cf.generateCertificate(certArray); 220 sub.setPublicKey(cert[0].getPublicKey()); 221 CertificateImpl certImpl = new CertificateImpl(cert[0]); 222 sub.addCertificate(certImpl); 223 java.security.Certificate[] certs = sub.certificates(); 224 assertEquals("Certificate not contained in the identity", 225 certs[0], certImpl); 226 } 227 228 /** 229 * @tests java.security.Identity#addCertificate(java.security.Certificate) 230 */ 231 public void test_addCertificateLjava_security_Certificate() throws Exception { 232 IdentitySubclass sub = new IdentitySubclass("test", 233 new IdentityScopeSubclass()); 234 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 235 X509Certificate cert[] = new X509Certificate[1]; 236 cert[0] = (X509Certificate) cf.generateCertificate(certArray); 237 sub.setPublicKey(cert[0].getPublicKey()); 238 CertificateImpl certImpl = new CertificateImpl(cert[0]); 239 sub.addCertificate(certImpl); 240 } 241 242 /** 243 * @tests java.security.Identity#removeCertificate(java.security.Certificate) 244 */ 245 public void test_removeCertificateLjava_security_Certificate() throws Exception { 246 IdentitySubclass sub = new IdentitySubclass("test", 247 new IdentityScopeSubclass()); 248 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 249 X509Certificate cert[] = new X509Certificate[1]; 250 cert[0] = (X509Certificate) cf.generateCertificate(certArray); 251 sub.setPublicKey(cert[0].getPublicKey()); 252 CertificateImpl certImpl = new CertificateImpl(cert[0]); 253 sub.addCertificate(certImpl); 254 sub.removeCertificate(certImpl); 255 java.security.Certificate[] certs = sub.certificates(); 256 assertEquals("Certificate not removed", 0, certs.length); 257 } 258 259 /** 260 * @tests java.security.Identity#equals(java.lang.Object) 261 */ 262 public void test_equalsLjava_lang_Object() throws Exception { 263 IdentitySubclass sub = new IdentitySubclass("test", 264 new IdentityScopeSubclass()); 265 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 266 X509Certificate cert[] = new X509Certificate[1]; 267 cert[0] = (X509Certificate) cf.generateCertificate(certArray); 268 sub.setPublicKey(cert[0].getPublicKey()); 269 CertificateImpl certImpl = new CertificateImpl(cert[0]); 270 sub.addCertificate(certImpl); 271 IdentitySubclass sub2 = new IdentitySubclass("test", 272 new IdentityScopeSubclass()); 273 assertEquals("the two Identity objects are not equal", sub2, sub); 274 } 275 276 /** 277 * @tests java.security.Identity#identityEquals(java.security.Identity) 278 */ 279 public void test_identityEqualsLjava_security_Identity() throws Exception { 280 IdentitySubclass sub = new IdentitySubclass("test", null); 281 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 282 X509Certificate cert[] = new X509Certificate[1]; 283 cert[0] = (X509Certificate) cf.generateCertificate(certArray); 284 sub.setPublicKey(cert[0].getPublicKey()); 285 CertificateImpl certImpl = new CertificateImpl(cert[0]); 286 sub.addCertificate(certImpl); 287 IdentitySubclass sub2 = new IdentitySubclass("test", null); 288 sub2.setPublicKey(cert[0].getPublicKey()); 289 assertEquals("the two Identity objects are not identity-equal", 290 sub2, sub); 291 } 292 293 /** 294 * @tests java.security.Identity#toString() 295 */ 296 public void test_toString() throws Exception { 297 IdentitySubclass sub = new IdentitySubclass("test", null); 298 assertNotNull(sub.toString()); 299 assertTrue("The String returned is not valid", sub.toString() 300 .length() > 0); 301 // Regression for HARMONY-1566 302 assertNotNull(new IdentitySubclass().toString()); 303 } 304 305 /** 306 * @tests java.security.Identity#toString(boolean) 307 */ 308 public void test_toStringZ() throws Exception { 309 IdentitySubclass sub = new IdentitySubclass("test", null); 310 assertNotNull(sub.toString(true)); 311 assertTrue("The String returned is not valid", sub.toString(true) 312 .length() > 0); 313 } 314 315 /** 316 * @tests java.security.Identity#hashCode() 317 */ 318 public void test_hashCode() throws Exception { 319 IdentitySubclass sub = new IdentitySubclass("test", null); 320 IdentitySubclass sub2 = new IdentitySubclass("test", null); 321 assertEquals("The 2 hash codes are not equal", sub.hashCode(), sub2 322 .hashCode()); 323 } 324} 325