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.cert;
19
20import java.io.ByteArrayInputStream;
21import java.io.InputStream;
22import java.security.cert.CertificateFactory;
23import java.security.cert.X509Certificate;
24import java.util.Arrays;
25import java.util.Iterator;
26import java.util.Vector;
27
28import org.apache.harmony.security.tests.support.cert.TestUtils;
29
30import tests.support.resource.Support_Resources;
31
32public class X509Certificate2Test extends junit.framework.TestCase {
33
34    /**
35     * @tests java.security.cert.X509Certificate#getExtensionValue(java.lang.String)
36     */
37    public void test_getExtensionValueLjava_lang_String() throws Exception {
38
39        InputStream is = Support_Resources
40                .getResourceStream("hyts_certificate_PEM.txt");
41
42        CertificateFactory certFact = CertificateFactory.getInstance("X509");
43        X509Certificate pemCert = (X509Certificate) certFact
44                .generateCertificate(is);
45
46        Vector<String> extensionOids = new Vector<String>();
47        extensionOids.addAll(pemCert.getCriticalExtensionOIDs());
48        extensionOids.addAll(pemCert.getNonCriticalExtensionOIDs());
49        Iterator i = extensionOids.iterator();
50        while (i.hasNext()) {
51            String oid = (String) i.next();
52            byte[] value = pemCert.getExtensionValue(oid);
53            if (value != null && value.length > 0) {
54                // check that it is an encoded as a OCTET STRING
55                assertEquals("The extension value for the oid " + oid
56                        + " was not encoded as an OCTET STRING", 0x04, value[0]);
57            }
58        }
59    }
60
61    /**
62     * Test for X.509 Certificate provider
63     */
64    public void test_toString() throws Exception {
65
66        // Regression for HARMONY-3384
67        CertificateFactory certFact = CertificateFactory.getInstance("X509");
68        X509Certificate pemCert = (X509Certificate) certFact
69                .generateCertificate(new ByteArrayInputStream(TestUtils
70                        .getX509Certificate_v3()));
71
72        // extension value is empty sequence
73        byte[] extnValue = pemCert.getExtensionValue("2.5.29.35");
74        assertTrue(Arrays.equals(new byte[] { 0x04, 0x02, 0x30, 0x00 },
75                extnValue));
76        assertNotNull(pemCert.toString());
77        // End regression for HARMONY-3384
78    }
79}
80