1 package tests.security.cert;
3 import dalvik.annotation.TestLevel;
4 import dalvik.annotation.TestTargetClass;
5 import dalvik.annotation.TestTargetNew;
7 import junit.framework.TestCase;
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;
25 @TestTargetClass(CertStore.class)
26 public class CertStore2Test extends TestCase {
28 private static final String CERT_STORE_PROVIDER_NAME = "TestCertStoreProvider";
29 private static final String CERT_STORE_NAME = "TestCertStore";
33 protected void setUp() throws Exception {
35 provider = new MyCertStoreProvider();
36 Security.addProvider(provider);
40 protected void tearDown() throws Exception {
42 Security.removeProvider(CERT_STORE_PROVIDER_NAME);
46 level=TestLevel.COMPLETE,
48 args={String.class, CertStoreParameters.class}
50 public void testGetInstanceStringCertStoreParameters() {
52 CertStoreParameters parameters = new MyCertStoreParameters();
53 CertStore certStore = CertStore.getInstance(CERT_STORE_NAME,
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);
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);
75 CertStore.getInstance("UnknownCertStore", null);
76 fail("expected NoSuchAlgorithmException");
77 } catch (InvalidAlgorithmParameterException e) {
78 fail("unexpected exception: " + e);
79 } catch (NoSuchAlgorithmException e) {
84 CertStore.getInstance(CERT_STORE_NAME,
85 new MyOtherCertStoreParameters());
86 fail("expected InvalidAlgorithmParameterException");
87 } catch (InvalidAlgorithmParameterException e) {
89 } catch (NoSuchAlgorithmException e) {
90 fail("unexpected exception: " + e);
95 level=TestLevel.COMPLETE,
97 args={String.class, CertStoreParameters.class, String.class}
99 public void testGetInstanceStringCertStoreParametersString() {
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()
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);
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()
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);
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) {
140 } catch (NoSuchProviderException e) {
141 fail("unexpected exception: " + e);
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) {
157 CertStore.getInstance(CERT_STORE_NAME,
158 new MyOtherCertStoreParameters(), CERT_STORE_PROVIDER_NAME);
159 } catch (InvalidAlgorithmParameterException e) {
161 } catch (NoSuchAlgorithmException e) {
162 fail("unexpected exception: " + e);
163 } catch (NoSuchProviderException e) {
164 fail("unexpected exception: " + e);
171 level=TestLevel.COMPLETE,
172 method="getInstance",
173 args={String.class, CertStoreParameters.class, Provider.class}
175 public void testGetInstanceStringCertStoreParametersProvider() {
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);
191 CertStore certStore = CertStore.getInstance(CERT_STORE_NAME, null,
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);
203 CertStore.getInstance("UnknownCertStore", null, provider);
204 fail("expected NoSuchAlgorithmException");
205 } catch (NoSuchAlgorithmException e) {
207 } catch (InvalidAlgorithmParameterException e) {
208 fail("unexpected exception: " + e);
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) {
224 level=TestLevel.COMPLETE,
225 method="getCertificates",
226 args={CertSelector.class}
228 public void testGetCertificates() {
229 CertStore certStore = null;
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);
238 assertNotNull(certStore);
241 Collection<? extends Certificate> certificates = certStore.getCertificates(null);
242 assertNull(certificates);
243 } catch (CertStoreException e) {
244 fail("unexpected exception: " + e);
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);
256 certStore.getCertificates(new MyOtherCertSelector());
257 fail("expected CertStoreException");
258 } catch (CertStoreException e) {
264 level=TestLevel.COMPLETE,
266 args={CRLSelector.class}
268 public void testGetCRLs() {
269 CertStore certStore = null;
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);
278 assertNotNull(certStore);
281 Collection<? extends CRL> ls = certStore.getCRLs(null);
283 } catch (CertStoreException e) {
284 fail("unexpected exception: " + e);
288 Collection<? extends CRL> ls = certStore.getCRLs(new MyCRLSelector());
290 assertTrue(ls.isEmpty());
291 } catch (CertStoreException e) {
292 fail("unexpected exception: " + e);
296 certStore.getCRLs(new MyOtherCRLSelector());
297 fail("expected CertStoreException");
298 } catch (CertStoreException e) {
303 static class MyCertStoreProvider extends Provider {
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());
311 static class MyCertStoreParameters implements CertStoreParameters {
312 public Object clone() {
313 return new MyCertStoreParameters();
317 static class MyOtherCertStoreParameters implements CertStoreParameters {
318 public Object clone() {
319 return new MyCertStoreParameters();
323 static class MyCRLSelector implements CRLSelector {
325 public boolean match(CRL crl) {
329 public Object clone() {
330 return new MyCRLSelector();
334 static class MyOtherCRLSelector implements CRLSelector {
335 public boolean match(CRL crl) {
339 public Object clone() {
340 return new MyOtherCRLSelector();
345 static class MyCertSelector implements CertSelector {
347 public boolean match(Certificate cert) {
351 public Object clone() {
352 return new MyCertSelector();
357 static class MyOtherCertSelector implements CertSelector {
358 public boolean match(Certificate crl) {
362 public Object clone() {
363 return new MyOtherCRLSelector();
368 public static class MyCertStoreSpi extends CertStoreSpi {
370 public MyCertStoreSpi() throws InvalidAlgorithmParameterException {
374 public MyCertStoreSpi(CertStoreParameters params)
375 throws InvalidAlgorithmParameterException {
377 if (params != null && !(params instanceof MyCertStoreParameters)) {
378 throw new InvalidAlgorithmParameterException(
379 "invalid parameters");
384 public Collection<? extends CRL> engineGetCRLs(CRLSelector selector)
385 throws CertStoreException {
386 if (selector != null) {
387 if (!(selector instanceof MyCRLSelector)) {
388 throw new CertStoreException();
390 return new ArrayList<CRL>();
396 public Collection<? extends Certificate> engineGetCertificates(
397 CertSelector selector) throws CertStoreException {
398 if (selector != null) {
399 if (!(selector instanceof MyCertSelector)) {
400 throw new CertStoreException();
402 return new ArrayList<Certificate>();