1package tests.security.cert;
2
3import junit.framework.TestCase;
4
5import java.security.InvalidAlgorithmParameterException;
6import java.security.NoSuchAlgorithmException;
7import java.security.NoSuchProviderException;
8import java.security.Provider;
9import java.security.Security;
10import java.security.cert.CRL;
11import java.security.cert.CRLSelector;
12import java.security.cert.CertSelector;
13import java.security.cert.CertStore;
14import java.security.cert.CertStoreException;
15import java.security.cert.CertStoreParameters;
16import java.security.cert.CertStoreSpi;
17import java.security.cert.Certificate;
18import java.util.ArrayList;
19import java.util.Collection;
20
21public class CertStore2Test extends TestCase {
22
23    private static final String CERT_STORE_PROVIDER_NAME = "TestCertStoreProvider";
24    private static final String CERT_STORE_NAME = "TestCertStore";
25
26    Provider provider;
27
28    protected void setUp() throws Exception {
29        super.setUp();
30        provider = new MyCertStoreProvider();
31        Security.addProvider(provider);
32    }
33
34    @Override
35    protected void tearDown() throws Exception {
36        super.tearDown();
37        Security.removeProvider(CERT_STORE_PROVIDER_NAME);
38    }
39
40    public void testGetInstanceStringCertStoreParameters() {
41        try {
42            CertStoreParameters parameters = new MyCertStoreParameters();
43            CertStore certStore = CertStore.getInstance(CERT_STORE_NAME,
44                    parameters);
45            assertNotNull(certStore);
46            assertNotNull(certStore.getCertStoreParameters());
47            assertNotSame(parameters, certStore.getCertStoreParameters());
48        } catch (NoSuchAlgorithmException e) {
49            fail("unexpected exception: " + e);
50        } catch (InvalidAlgorithmParameterException e) {
51            fail("unexpected exception: " + e);
52        }
53
54        try {
55            CertStore certStore = CertStore.getInstance(CERT_STORE_NAME, null);
56            assertNotNull(certStore);
57            assertNull(certStore.getCertStoreParameters());
58        } catch (InvalidAlgorithmParameterException e) {
59            fail("unexpected exception: " + e);
60        } catch (NoSuchAlgorithmException e) {
61            fail("unexpected exception: " + e);
62        }
63
64        try {
65            CertStore.getInstance("UnknownCertStore", null);
66            fail("expected NoSuchAlgorithmException");
67        } catch (InvalidAlgorithmParameterException e) {
68            fail("unexpected exception: " + e);
69        } catch (NoSuchAlgorithmException e) {
70            // ok
71        }
72
73        try {
74            CertStore.getInstance(CERT_STORE_NAME,
75                    new MyOtherCertStoreParameters());
76            fail("expected InvalidAlgorithmParameterException");
77        } catch (InvalidAlgorithmParameterException e) {
78            // ok
79        } catch (NoSuchAlgorithmException e) {
80            fail("unexpected exception: " + e);
81        }
82    }
83
84    public void testGetInstanceStringCertStoreParametersString() {
85        try {
86            CertStoreParameters parameters = new MyCertStoreParameters();
87            CertStore certStore = CertStore.getInstance(CERT_STORE_NAME,
88                    parameters, CERT_STORE_PROVIDER_NAME);
89            assertNotNull(certStore);
90            assertNotNull(certStore.getCertStoreParameters());
91            assertNotSame(parameters, certStore.getCertStoreParameters());
92            assertEquals(CERT_STORE_PROVIDER_NAME, certStore.getProvider()
93                    .getName());
94        } catch (InvalidAlgorithmParameterException e) {
95            fail("unexpected exception: " + e);
96        } catch (NoSuchAlgorithmException e) {
97            fail("unexpected exception: " + e);
98        } catch (NoSuchProviderException e) {
99            fail("unexpected exception: " + e);
100        }
101
102        try {
103            CertStore certStore = CertStore.getInstance(CERT_STORE_NAME, null,
104                    CERT_STORE_PROVIDER_NAME);
105            assertNotNull(certStore);
106            assertNull(certStore.getCertStoreParameters());
107            assertEquals(CERT_STORE_PROVIDER_NAME, certStore.getProvider()
108                    .getName());
109        } catch (InvalidAlgorithmParameterException e) {
110            fail("unexpected exception: " + e);
111        } catch (NoSuchAlgorithmException e) {
112            fail("unexpected exception: " + e);
113        } catch (NoSuchProviderException e) {
114            fail("unexpected exception: " + e);
115        }
116
117        try {
118            CertStore.getInstance("UnknownCertStore",
119                    new MyCertStoreParameters(), CERT_STORE_PROVIDER_NAME);
120            fail("expected NoSuchAlgorithmException");
121        } catch (InvalidAlgorithmParameterException e) {
122            fail("unexpected exception: " + e);
123        } catch (NoSuchAlgorithmException e) {
124            // ok
125        } catch (NoSuchProviderException e) {
126            fail("unexpected exception: " + e);
127        }
128
129        try {
130            CertStore.getInstance(CERT_STORE_NAME, null,
131                    "UnknownCertStoreProvider");
132            fail("expected NoSuchProviderException");
133        } catch (InvalidAlgorithmParameterException e) {
134            fail("unexpected exception: " + e);
135        } catch (NoSuchAlgorithmException e) {
136            fail("unexpected exception: " + e);
137        } catch (NoSuchProviderException e) {
138            // ok
139        }
140
141        try {
142            CertStore.getInstance(CERT_STORE_NAME,
143                    new MyOtherCertStoreParameters(), CERT_STORE_PROVIDER_NAME);
144        } catch (InvalidAlgorithmParameterException e) {
145            // ok
146        } catch (NoSuchAlgorithmException e) {
147            fail("unexpected exception: " + e);
148        } catch (NoSuchProviderException e) {
149            fail("unexpected exception: " + e);
150        }
151
152
153    }
154
155    public void testGetInstanceStringCertStoreParametersProvider() {
156        try {
157            CertStoreParameters parameters = new MyCertStoreParameters();
158            CertStore certStore = CertStore.getInstance(CERT_STORE_NAME,
159                    parameters, provider);
160            assertNotNull(certStore);
161            assertNotNull(certStore.getCertStoreParameters());
162            assertNotSame(parameters, certStore.getCertStoreParameters());
163            assertSame(provider, certStore.getProvider());
164        } catch (NoSuchAlgorithmException e) {
165            fail("unexpected exception: " + e);
166        } catch (InvalidAlgorithmParameterException e) {
167            fail("unexpected exception: " + e);
168        }
169
170        try {
171            CertStore certStore = CertStore.getInstance(CERT_STORE_NAME, null,
172                    provider);
173            assertNotNull(certStore);
174            assertNull(certStore.getCertStoreParameters());
175            assertSame(provider, certStore.getProvider());
176        } catch (NoSuchAlgorithmException e) {
177            fail("unexpected exception: " + e);
178        } catch (InvalidAlgorithmParameterException e) {
179            fail("unexpected exception: " + e);
180        }
181
182        try {
183            CertStore.getInstance("UnknownCertStore", null, provider);
184            fail("expected NoSuchAlgorithmException");
185        } catch (NoSuchAlgorithmException e) {
186            // ok
187        } catch (InvalidAlgorithmParameterException e) {
188            fail("unexpected exception: " + e);
189        }
190
191        try {
192            CertStore.getInstance(CERT_STORE_NAME,
193                    new MyOtherCertStoreParameters(), provider);
194            fail("expected InvalidAlgorithmParameterException");
195        } catch (NoSuchAlgorithmException e) {
196            fail("unexpected exception: " + e);
197        } catch (InvalidAlgorithmParameterException e) {
198            // ok
199        }
200
201    }
202
203    public void testGetCertificates() {
204        CertStore certStore = null;
205        try {
206            certStore = CertStore.getInstance(CERT_STORE_NAME, null);
207        } catch (InvalidAlgorithmParameterException e) {
208            fail("unexpected exception: " + e);
209        } catch (NoSuchAlgorithmException e) {
210            fail("unexpected exception: " + e);
211        }
212
213        assertNotNull(certStore);
214
215        try {
216            Collection<? extends Certificate> certificates = certStore.getCertificates(null);
217            assertNull(certificates);
218        } catch (CertStoreException e) {
219            fail("unexpected exception: " + e);
220        }
221
222        try {
223            Collection<? extends Certificate> certificates = certStore.getCertificates(new MyCertSelector());
224            assertNotNull(certificates);
225            assertTrue(certificates.isEmpty());
226        } catch (CertStoreException e) {
227            fail("unexpected exception: " + e);
228        }
229
230        try {
231            certStore.getCertificates(new MyOtherCertSelector());
232            fail("expected CertStoreException");
233        } catch (CertStoreException e) {
234            // ok
235        }
236    }
237
238    public void testGetCRLs() {
239        CertStore certStore = null;
240        try {
241            certStore = CertStore.getInstance(CERT_STORE_NAME, new MyCertStoreParameters());
242        } catch (InvalidAlgorithmParameterException e) {
243            fail("unexpected exception: " + e);
244        } catch (NoSuchAlgorithmException e) {
245            fail("unexpected exception: " + e);
246        }
247
248        assertNotNull(certStore);
249
250        try {
251            Collection<? extends CRL> ls = certStore.getCRLs(null);
252            assertNull(ls);
253        } catch (CertStoreException e) {
254            fail("unexpected exception: " + e);
255        }
256
257        try {
258            Collection<? extends CRL> ls = certStore.getCRLs(new MyCRLSelector());
259            assertNotNull(ls);
260            assertTrue(ls.isEmpty());
261        } catch (CertStoreException e) {
262            fail("unexpected exception: " + e);
263        }
264
265        try {
266            certStore.getCRLs(new MyOtherCRLSelector());
267            fail("expected CertStoreException");
268        } catch (CertStoreException e) {
269            // ok
270        }
271    }
272
273    static class MyCertStoreProvider extends Provider {
274
275        protected MyCertStoreProvider() {
276            super(CERT_STORE_PROVIDER_NAME, 1.0, "Test CertStore Provider 1.0");
277            put("CertStore." + CERT_STORE_NAME, MyCertStoreSpi.class.getName());
278        }
279    }
280
281    static class MyCertStoreParameters implements CertStoreParameters {
282        public Object clone() {
283            return new MyCertStoreParameters();
284        }
285    }
286
287    static class MyOtherCertStoreParameters implements CertStoreParameters {
288        public Object clone() {
289            return new MyCertStoreParameters();
290        }
291    }
292
293    static class MyCRLSelector implements CRLSelector {
294
295        public boolean match(CRL crl) {
296            return false;
297        }
298
299        public Object clone() {
300            return new MyCRLSelector();
301        }
302    }
303
304    static class MyOtherCRLSelector implements CRLSelector {
305        public boolean match(CRL crl) {
306            return false;
307        }
308
309        public Object clone() {
310            return new MyOtherCRLSelector();
311        }
312
313    }
314
315    static class MyCertSelector implements CertSelector {
316
317        public boolean match(Certificate cert) {
318            return false;
319        }
320
321        public Object clone() {
322            return new MyCertSelector();
323        }
324
325    }
326
327    static class MyOtherCertSelector implements CertSelector {
328        public boolean match(Certificate crl) {
329            return false;
330        }
331
332        public Object clone() {
333            return new MyOtherCRLSelector();
334        }
335
336    }
337
338    public static class MyCertStoreSpi extends CertStoreSpi {
339
340        public MyCertStoreSpi() throws InvalidAlgorithmParameterException {
341            super(null);
342        }
343
344        public MyCertStoreSpi(CertStoreParameters params)
345                throws InvalidAlgorithmParameterException {
346            super(params);
347            if (params != null && !(params instanceof MyCertStoreParameters)) {
348                throw new InvalidAlgorithmParameterException(
349                        "invalid parameters");
350            }
351        }
352
353        @Override
354        public Collection<? extends CRL> engineGetCRLs(CRLSelector selector)
355                throws CertStoreException {
356            if (selector != null) {
357                if (!(selector instanceof MyCRLSelector)) {
358                    throw new CertStoreException();
359                }
360                return new ArrayList<CRL>();
361            }
362            return null;
363        }
364
365        @Override
366        public Collection<? extends Certificate> engineGetCertificates(
367                CertSelector selector) throws CertStoreException {
368            if (selector != null) {
369                if (!(selector instanceof MyCertSelector)) {
370                    throw new CertStoreException();
371                }
372                return new ArrayList<Certificate>();
373            }
374            return null;
375        }
376
377    }
378
379}
380