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.io.ByteArrayInputStream;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import java.security.Identity;
024: import java.security.IdentityScope;
025: import java.security.KeyManagementException;
026: import java.security.KeyPairGenerator;
027: import java.security.Principal;
028: import java.security.PublicKey;
029: import java.security.cert.CertificateFactory;
030: import java.security.cert.X509Certificate;
031:
032: import org.apache.harmony.security.tests.java.security.IdentityScope2Test.IdentityScopeSubclass;
033:
034: public class Identity2Test extends junit.framework.TestCase {
035:
036: static PublicKey pubKey;
037: static {
038: try {
039: pubKey = KeyPairGenerator.getInstance("DSA").genKeyPair()
040: .getPublic();
041: } catch (Exception e) {
042: fail(e.toString());
043: }
044: }
045:
046: public static class CertificateImpl implements
047: java.security.Certificate {
048:
049: X509Certificate cert;
050:
051: public CertificateImpl(X509Certificate cert) {
052: this .cert = cert;
053: }
054:
055: public Principal getGuarantor() {
056: return cert.getIssuerDN();
057: }
058:
059: public void encode(OutputStream out) {
060: }
061:
062: public void decode(InputStream in) {
063: }
064:
065: public String toString() {
066: return "";
067: }
068:
069: public String toString(boolean b) {
070: return "";
071: }
072:
073: public String getFormat() {
074: return cert.getType();
075: }
076:
077: public Principal getPrincipal() {
078: return cert.getSubjectDN();
079: }
080:
081: public PublicKey getPublicKey() {
082: return cert.getPublicKey();
083: }
084: }
085:
086: String certificate = "-----BEGIN CERTIFICATE-----\n"
087: + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
088: + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
089: + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
090: + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
091: + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
092: + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
093: + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
094: + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
095: + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
096: + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
097: + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
098: + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
099: + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
100: + "-----END CERTIFICATE-----\n";
101:
102: ByteArrayInputStream certArray = new ByteArrayInputStream(
103: certificate.getBytes());
104:
105: String certificate2 = "-----BEGIN CERTIFICATE-----\n"
106: + "MIICZzCCAdCgAwIBAgIBGzANBgkqhkiG9w0BAQUFADBhMQswCQYDVQQGEwJVUzEY\n"
107: + "MBYGA1UEChMPVS5TLiBHb3Zlcm5tZW50MQwwCgYDVQQLEwNEb0QxDDAKBgNVBAsT\n"
108: + "A1BLSTEcMBoGA1UEAxMTRG9EIFBLSSBNZWQgUm9vdCBDQTAeFw05ODA4MDMyMjAy\n"
109: + "MjlaFw0wODA4MDQyMjAyMjlaMGExCzAJBgNVBAYTAlVTMRgwFgYDVQQKEw9VLlMu\n"
110: + "IEdvdmVybm1lbnQxDDAKBgNVBAsTA0RvRDEMMAoGA1UECxMDUEtJMRwwGgYDVQQD\n"
111: + "ExNEb0QgUEtJIE1lZCBSb290IENBMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n"
112: + "gQDbrM/J9FrJSX+zxFUbsI9Vw5QbguVBIa95rwW/0M8+sM0r5gd+DY6iubm6wnXk\n"
113: + "CSvbfQlFEDSKr4WYeeGp+d9WlDnQdtDFLdA45tCi5SHjnW+hGAmZnld0rz6wQekF\n"
114: + "5xQaa5A6wjhMlLOjbh27zyscrorMJ1O5FBOWnEHcRv6xqQIDAQABoy8wLTAdBgNV\n"
115: + "HQ4EFgQUVrmYR6m9701cHQ3r5kXyG7zsCN0wDAYDVR0TBAUwAwEB/zANBgkqhkiG\n"
116: + "9w0BAQUFAAOBgQDVX1Y0YqC7vekeZjVxtyuC8Mnxbrz6D109AX07LEIRzNYzwZ0w\n"
117: + "MTImSp9sEzWW+3FueBIU7AxGys2O7X0qmN3zgszPfSiocBuQuXIYQctJhKjF5KVc\n"
118: + "VGQRYYlt+myhl2vy6yPzEVCjiKwMEb1Spu0irCf+lFW2hsdjvmSQMtZvOw==\n"
119: + "-----END CERTIFICATE-----\n";
120:
121: ByteArrayInputStream certArray2 = new ByteArrayInputStream(
122: certificate2.getBytes());
123:
124: public static class IdentitySubclass extends Identity {
125: public IdentitySubclass() {
126: super ();
127: }
128:
129: public IdentitySubclass(String name) {
130: super (name);
131: }
132:
133: public IdentitySubclass(String name, IdentityScope scope)
134: throws KeyManagementException {
135: super (name, scope);
136: }
137: }
138:
139: /**
140: * @tests java.security.Identity#Identity()
141: */
142: public void test_Constructor() {
143: new IdentitySubclass();
144: }
145:
146: /**
147: * @tests java.security.Identity#Identity(java.lang.String)
148: */
149: public void test_ConstructorLjava_lang_String() {
150: new IdentitySubclass("test");
151: }
152:
153: /**
154: * @tests java.security.Identity#Identity(java.lang.String,
155: * java.security.IdentityScope)
156: */
157: public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope()
158: throws Exception {
159: new IdentitySubclass("test", new IdentityScopeSubclass());
160: }
161:
162: /**
163: * @tests java.security.Identity#getScope()
164: */
165: public void test_getScope() throws Exception {
166: IdentityScope scope = new IdentityScopeSubclass();
167: IdentitySubclass sub = new IdentitySubclass("test", scope);
168: IdentityScope returnedScope = sub.getScope();
169: assertEquals("Wrong Scope returned", scope, returnedScope);
170: }
171:
172: /**
173: * @tests java.security.Identity#getPublicKey()
174: */
175: public void test_getPublicKey() throws Exception {
176: IdentitySubclass sub = new IdentitySubclass("test",
177: new IdentityScopeSubclass());
178: sub.setPublicKey(pubKey);
179: PublicKey returnedPubKey = sub.getPublicKey();
180: assertEquals("Wrong PublicKey returned", pubKey, returnedPubKey);
181: }
182:
183: /**
184: * @tests java.security.Identity#getName()
185: */
186: public void test_getName() throws Exception {
187: String name = "test";
188: IdentitySubclass sub = new IdentitySubclass(name,
189: new IdentityScopeSubclass());
190: assertEquals("Wrong Name returned", name, sub.getName());
191: }
192:
193: /**
194: * @tests java.security.Identity#getInfo()
195: */
196: public void test_getInfo() throws Exception {
197: String info = "This is the general information.";
198: IdentitySubclass sub = new IdentitySubclass("test",
199: new IdentityScopeSubclass());
200: sub.setInfo(info);
201: assertEquals("Wrong Info returned", info, sub.getInfo());
202: }
203:
204: /**
205: * @tests java.security.Identity#certificates()
206: */
207: public void test_certificates() throws Exception {
208: IdentitySubclass sub = new IdentitySubclass("test",
209: new IdentityScopeSubclass());
210: CertificateFactory cf = CertificateFactory.getInstance("X.509");
211: X509Certificate cert[] = new X509Certificate[1];
212: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
213: sub.setPublicKey(cert[0].getPublicKey());
214: CertificateImpl certImpl = new CertificateImpl(cert[0]);
215: sub.addCertificate(certImpl);
216: java.security.Certificate[] certs = sub.certificates();
217: assertEquals("Certificate not contained in the identity",
218: certs[0], certImpl);
219: }
220:
221: /**
222: * @tests java.security.Identity#addCertificate(java.security.Certificate)
223: */
224: public void test_addCertificateLjava_security_Certificate()
225: throws Exception {
226: IdentitySubclass sub = new IdentitySubclass("test",
227: new IdentityScopeSubclass());
228: CertificateFactory cf = CertificateFactory.getInstance("X.509");
229: X509Certificate cert[] = new X509Certificate[1];
230: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
231: sub.setPublicKey(cert[0].getPublicKey());
232: CertificateImpl certImpl = new CertificateImpl(cert[0]);
233: sub.addCertificate(certImpl);
234: }
235:
236: /**
237: * @tests java.security.Identity#removeCertificate(java.security.Certificate)
238: */
239: public void test_removeCertificateLjava_security_Certificate()
240: throws Exception {
241: IdentitySubclass sub = new IdentitySubclass("test",
242: new IdentityScopeSubclass());
243: CertificateFactory cf = CertificateFactory.getInstance("X.509");
244: X509Certificate cert[] = new X509Certificate[1];
245: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
246: sub.setPublicKey(cert[0].getPublicKey());
247: CertificateImpl certImpl = new CertificateImpl(cert[0]);
248: sub.addCertificate(certImpl);
249: sub.removeCertificate(certImpl);
250: java.security.Certificate[] certs = sub.certificates();
251: assertEquals("Certificate not removed", 0, certs.length);
252: }
253:
254: /**
255: * @tests java.security.Identity#equals(java.lang.Object)
256: */
257: public void test_equalsLjava_lang_Object() throws Exception {
258: IdentitySubclass sub = new IdentitySubclass("test",
259: new IdentityScopeSubclass());
260: CertificateFactory cf = CertificateFactory.getInstance("X.509");
261: X509Certificate cert[] = new X509Certificate[1];
262: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
263: sub.setPublicKey(cert[0].getPublicKey());
264: CertificateImpl certImpl = new CertificateImpl(cert[0]);
265: sub.addCertificate(certImpl);
266: IdentitySubclass sub2 = new IdentitySubclass("test",
267: new IdentityScopeSubclass());
268: assertEquals("the two Identity objects are not equal", sub2,
269: sub);
270: }
271:
272: /**
273: * @tests java.security.Identity#identityEquals(java.security.Identity)
274: */
275: public void test_identityEqualsLjava_security_Identity()
276: throws Exception {
277: IdentitySubclass sub = new IdentitySubclass("test", null);
278: CertificateFactory cf = CertificateFactory.getInstance("X.509");
279: X509Certificate cert[] = new X509Certificate[1];
280: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
281: sub.setPublicKey(cert[0].getPublicKey());
282: CertificateImpl certImpl = new CertificateImpl(cert[0]);
283: sub.addCertificate(certImpl);
284: IdentitySubclass sub2 = new IdentitySubclass("test", null);
285: sub2.setPublicKey(cert[0].getPublicKey());
286: assertEquals("the two Identity objects are not identity-equal",
287: sub2, sub);
288: }
289:
290: /**
291: * @tests java.security.Identity#toString()
292: */
293: public void test_toString() throws Exception {
294: IdentitySubclass sub = new IdentitySubclass("test", null);
295: assertNotNull(sub.toString());
296: assertTrue("The String returned is not valid", sub.toString()
297: .length() > 0);
298: // Regression for HARMONY-1566
299: assertNotNull(new IdentitySubclass().toString());
300: }
301:
302: /**
303: * @tests java.security.Identity#toString(boolean)
304: */
305: public void test_toStringZ() throws Exception {
306: IdentitySubclass sub = new IdentitySubclass("test", null);
307: assertNotNull(sub.toString(true));
308: assertTrue("The String returned is not valid", sub.toString(
309: true).length() > 0);
310: }
311:
312: /**
313: * @tests java.security.Identity#hashCode()
314: */
315: public void test_hashCode() throws Exception {
316: IdentitySubclass sub = new IdentitySubclass("test", null);
317: IdentitySubclass sub2 = new IdentitySubclass("test", null);
318: assertEquals("The 2 hash codes are not equal", sub.hashCode(),
319: sub2.hashCode());
320: }
321: }
|