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