OSDN Git Service

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