1561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/*
2561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  Licensed to the Apache Software Foundation (ASF) under one or more
3561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  contributor license agreements.  See the NOTICE file distributed with
4561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  this work for additional information regarding copyright ownership.
5561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  The ASF licenses this file to You under the Apache License, Version 2.0
6561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  (the "License"); you may not use this file except in compliance with
7561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  the License.  You may obtain a copy of the License at
8561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *
9561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
10561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *
11561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  Unless required by applicable law or agreed to in writing, software
12561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  distributed under the License is distributed on an "AS IS" BASIS,
13561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  See the License for the specific language governing permissions and
15561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  limitations under the License.
16561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */
17561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
18561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/**
198d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath * @author Vera Y. Petrashkova
208d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath */
21561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
22561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespackage org.apache.harmony.security.tests.java.security.cert;
23561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
24561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.io.ByteArrayInputStream;
25561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.io.DataInputStream;
26561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.NoSuchProviderException;
27561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.Provider;
28561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.Security;
29561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.cert.CRL;
30561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.cert.CRLException;
31561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.cert.CertPath;
32561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.cert.Certificate;
33561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.cert.CertificateException;
34561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.cert.CertificateFactory;
35561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.Collection;
36561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.Iterator;
37561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.List;
38561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
39561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport org.apache.harmony.security.tests.support.SpiEngUtils;
40561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport org.apache.harmony.security.tests.support.SpiEngUtils.MyProvider;
41561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi;
42561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
43561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport junit.framework.TestCase;
44561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
45561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/**
46561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Tests for CertificateFactory class constructors and methods
47561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */
48561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
49561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespublic class CertificateFactory2Test extends TestCase {
50561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static final String defaultAlg = "CertFac";
51561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static final String CertificateFactoryProviderClass = "org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi";
528d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
53561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static final String[] invalidValues = SpiEngUtils.invalidValues;
54561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
55561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static final String[] validValues;
56561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
57561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    static {
58561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        validValues = new String[4];
59561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        validValues[0] = defaultAlg;
60561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        validValues[1] = defaultAlg.toLowerCase();
61561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        validValues[2] = "CeRtFaC";
62561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        validValues[3] = "cerTFac";
63561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
64561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
65561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    Provider mProv;
66561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
67561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    protected void setUp() throws Exception {
68561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        super.setUp();
69561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        mProv = (new SpiEngUtils()).new MyProvider("MyCFProvider",
70561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                "Provider for testing", CertificateFactory1Test.srvCertificateFactory
718d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath                .concat(".").concat(defaultAlg),
72561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                CertificateFactoryProviderClass);
73561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Security.insertProviderAt(mProv, 1);
74561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
75561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
76561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /*
77561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * @see TestCase#tearDown()
78561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
79561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    protected void tearDown() throws Exception {
80561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        super.tearDown();
81561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Security.removeProvider(mProv.getName());
82561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
83561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
84561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
85561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Constructor for CertificateFactory2Test.
868d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     *
87561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * @param arg0
88561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
89561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public CertificateFactory2Test(String arg0) {
90561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        super(arg0);
91561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
92561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
93561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private void checkResult(CertificateFactory certFactory, boolean mode)
94561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            throws CertificateException, CRLException {
95561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        MyCertificateFactorySpi.putMode(mode);
96561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
97561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
98561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        DataInputStream dis = new DataInputStream(bais);
99561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
100561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            certFactory.generateCertPath(bais);
101561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("CertificateException must be thrown");
102561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (CertificateException e) {
103561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
104561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
105561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            certFactory.generateCertPath(dis);
106561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            if (!mode) {
107561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("CertificateException must be thrown because encodings list is empty");
108561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
109561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (CertificateException e) {
110561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            if (mode) {
1118d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath                fail("Unexpected CertificateFactoryException was thrown");
112561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
113561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
114561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
115561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            certFactory.generateCertPath(bais, "aa");
116561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("CertificateException must be thrown");
117561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (CertificateException e) {
118561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
119561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
120561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            certFactory.generateCertPath(dis, "");
121561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            if (mode) {
122561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("IllegalArgumentException must be thrown");
123561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
124561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (IllegalArgumentException e) {
125561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            if (!mode) {
126561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("Unexpected IllegalArgumentException was thrown");
127561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
128561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
129561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        certFactory.generateCertPath(dis, "ss");
130561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
131561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
132561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            certFactory.generateCertificate(bais);
133561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("CertificateException must be thrown");
134561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (CertificateException e) {
135561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
136561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
137561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            certFactory.generateCertificates(null);
138561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("CertificateException must be thrown");
139561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (CertificateException e) {
140561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
141561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Certificate cert = certFactory.generateCertificate(dis);
142561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNull("Result must be null", cert);
143561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Collection col = certFactory.generateCertificates(dis);
144561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNull("Result must be null", col);
145561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
146561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
147561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            certFactory.generateCRL(bais);
148561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("CRLException must be thrown");
149561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (CRLException e) {
150561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
151561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
152561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            certFactory.generateCRLs(null);
153561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("CRLException must be thrown");
154561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (CRLException e) {
155561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
156561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        CRL crl = certFactory.generateCRL(dis);
157561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNull("Result must be null", crl);
158561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        col = certFactory.generateCRLs(dis);
159561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNull("Result must be null", col);
160561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
161561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        List list = null;
162561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        CertPath cp;
163561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
164561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            cp = certFactory.generateCertPath(list);
165561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            if (mode) {
166561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("NullPointerException must be thrown");
167561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } else {
1688d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath                assertNull("Must be null", cp);
169561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
170561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NullPointerException e) {
171561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            if (!mode) {
172561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("Unexpected NullPointerException was thrown");
173561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
174561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
175561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Iterator it = certFactory.getCertPathEncodings();
176561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (mode) {
177561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue(it.hasNext());
178561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } else {
1798d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath            assertFalse(it.hasNext());
180561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
181561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
182561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
183561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
1848d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     * Test for <code>getInstance(String type)</code> method
185561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Assertions:
1868d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     * throws NullPointerException when type is null
187561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * throws CertificateException when type is not available
188561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * returns CertificateFactory object
189561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
190561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void GetInstance01(boolean mode) throws CertificateException, CRLException {
191561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
192561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            CertificateFactory.getInstance(null);
193561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("NullPointerException or CertificateException must be thrown when type is null");
194561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (CertificateException e) {
195561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NullPointerException e) {
196561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
197561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < invalidValues.length; i++) {
198561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
199561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                CertificateFactory.getInstance(invalidValues[i]);
200561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("CertificateException must be thrown (type: ".concat(
201561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        invalidValues[i]).concat(")"));
202561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (CertificateException e) {
203561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
204561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
205561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        CertificateFactory cerF;
206561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
207561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            cerF = CertificateFactory.getInstance(validValues[i]);
208561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertEquals("Incorrect type", cerF.getType(), validValues[i]);
209561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertEquals("Incorrect provider", cerF.getProvider(), mProv);
210561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            checkResult(cerF, mode);
211561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
212561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
213561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
214561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
215561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String type, String provider)</code> method
2168d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     * Assertions:
2178d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     * throws NullPointerException when type is null
218561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * throws CertificateException when type is not available
2198d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     * throws IllegalArgumentException when provider is null or empty;
2208d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     * throws NoSuchProviderException when provider is available;
221561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * returns CertificateFactory object
222561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
223561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void GetInstance02(boolean mode) throws CertificateException,
224561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            NoSuchProviderException, IllegalArgumentException, CRLException {
225561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
226561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            CertificateFactory.getInstance(null, mProv.getName());
227561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("NullPointerException or CertificateException must be thrown when type is null");
228561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (CertificateException e) {
229561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NullPointerException e) {
230561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
231561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < invalidValues.length; i++) {
232561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
233561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                CertificateFactory.getInstance(invalidValues[i], mProv
234561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        .getName());
235561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("CertificateException must be thrown (type: ".concat(
236561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        invalidValues[i]).concat(")"));
237561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (CertificateException e) {
238561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
239561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
240561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        String prov = null;
241561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
242561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
243561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                CertificateFactory.getInstance(validValues[i], prov);
244561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("IllegalArgumentException must be thrown when provider is null (type: "
245561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        .concat(validValues[i]).concat(")"));
246561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (IllegalArgumentException e) {
247561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
248561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
249561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                CertificateFactory.getInstance(validValues[i], "");
250561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("IllegalArgumentException must be thrown when provider is empty (type: "
251561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        .concat(validValues[i]).concat(")"));
252561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (IllegalArgumentException e) {
253561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
254561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
255561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
256561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            for (int j = 1; j < invalidValues.length; j++) {
257561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                try {
258561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    CertificateFactory.getInstance(validValues[i],
259561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                            invalidValues[j]);
260561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    fail("NoSuchProviderException must be thrown (type: "
261561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                            .concat(validValues[i]).concat(" provider: ")
262561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                            .concat(invalidValues[j]).concat(")"));
263561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                } catch (NoSuchProviderException e) {
264561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                }
265561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
266561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
267561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        CertificateFactory cerF;
268561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
269561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            cerF = CertificateFactory.getInstance(validValues[i], mProv
270561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    .getName());
271561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertEquals("Incorrect type", cerF.getType(), validValues[i]);
272561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertEquals("Incorrect provider", cerF.getProvider().getName(),
273561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    mProv.getName());
274561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            checkResult(cerF, mode);
275561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
276561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
277561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
278561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
279561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String type, Provider provider)</code>
2808d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     * method
2818d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     * Assertions:
2828d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     * throws NullPointerException when type is null
283561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * throws CertificateException when type is not available
2848d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath     * throws IllegalArgumentException when provider is null;
285561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * returns CertificateFactory object
286561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
287561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void GetInstance03(boolean mode) throws CertificateException,
288561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            IllegalArgumentException, CRLException {
289561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
290561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            CertificateFactory.getInstance(null, mProv);
291561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("NullPointerException or CertificateException must be thrown when type is null");
292561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (CertificateException e) {
293561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NullPointerException e) {
294561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
295561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < invalidValues.length; i++) {
296561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
297561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                CertificateFactory.getInstance(invalidValues[i], mProv);
298561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("CertificateException must be thrown (type: ".concat(
299561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        invalidValues[i]).concat(")"));
300561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (CertificateException e) {
301561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
302561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
303561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Provider prov = null;
304561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
305561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
306561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                CertificateFactory.getInstance(validValues[i], prov);
307561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("IllegalArgumentException must be thrown when provider is null (type: "
308561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        .concat(validValues[i]).concat(")"));
309561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (IllegalArgumentException e) {
310561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
311561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
312561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        CertificateFactory cerF;
313561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
314561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            cerF = CertificateFactory.getInstance(validValues[i], mProv);
315561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertEquals("Incorrect type", cerF.getType(), validValues[i]);
3168d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath            assertEquals("Incorrect provider", cerF.getProvider(), mProv);
3178d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath            checkResult(cerF, mode);
318561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
319561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
3208d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
321561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testGetInstance01() throws CertificateException, CRLException {
3228d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath        GetInstance01(true);
323561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
3248d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
325561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testGetInstance02() throws CertificateException,
3268d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath            NoSuchProviderException, IllegalArgumentException, CRLException {
3278d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath        GetInstance02(true);
328561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
3298d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
330561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testGetInstance03() throws CertificateException,
3318d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath            IllegalArgumentException, CRLException {
3328d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath        GetInstance03(true);
333561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
3348d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
335561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testGetInstance04() throws CertificateException, CRLException {
3368d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath        GetInstance01(false);
337561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
3388d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
339561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testGetInstance05() throws CertificateException,
3408d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath            NoSuchProviderException, IllegalArgumentException, CRLException {
3418d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath        GetInstance02(false);
342561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
3438d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
344561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testGetInstance06() throws CertificateException,
3458d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath            IllegalArgumentException, CRLException {
3468d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath        GetInstance03(false);
347561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
348561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes}
349