01: package org.bouncycastle.jce.provider.test;
02:
03: import org.bouncycastle.jce.MultiCertStoreParameters;
04: import org.bouncycastle.jce.provider.BouncyCastleProvider;
05: import org.bouncycastle.util.test.SimpleTest;
06:
07: import java.io.ByteArrayInputStream;
08: import java.security.Security;
09: import java.security.cert.CertStore;
10: import java.security.cert.CertificateFactory;
11: import java.security.cert.CollectionCertStoreParameters;
12: import java.security.cert.X509CRL;
13: import java.security.cert.X509CertSelector;
14: import java.security.cert.X509Certificate;
15: import java.util.ArrayList;
16: import java.util.Collection;
17: import java.util.List;
18:
19: public class MultiCertStoreTest extends SimpleTest {
20:
21: public void performTest() throws Exception {
22: basicTest();
23: }
24:
25: private void basicTest() throws Exception {
26: CertificateFactory cf = CertificateFactory.getInstance("X.509",
27: "BC");
28:
29: X509Certificate rootCert = (X509Certificate) cf
30: .generateCertificate(new ByteArrayInputStream(
31: CertPathTest.rootCertBin));
32: X509Certificate interCert = (X509Certificate) cf
33: .generateCertificate(new ByteArrayInputStream(
34: CertPathTest.interCertBin));
35: X509Certificate finalCert = (X509Certificate) cf
36: .generateCertificate(new ByteArrayInputStream(
37: CertPathTest.finalCertBin));
38: X509CRL rootCrl = (X509CRL) cf
39: .generateCRL(new ByteArrayInputStream(
40: CertPathTest.rootCrlBin));
41: X509CRL interCrl = (X509CRL) cf
42: .generateCRL(new ByteArrayInputStream(
43: CertPathTest.interCrlBin));
44:
45: // Testing CollectionCertStore generation from List
46: List list = new ArrayList();
47: list.add(rootCert);
48: list.add(interCert);
49: list.add(finalCert);
50: list.add(rootCrl);
51: list.add(interCrl);
52: CollectionCertStoreParameters ccsp = new CollectionCertStoreParameters(
53: list);
54: CertStore store1 = CertStore.getInstance("Collection", ccsp,
55: "BC");
56: CertStore store2 = CertStore.getInstance("Collection", ccsp,
57: "BC");
58:
59: List storeList = new ArrayList();
60: storeList.add(store1);
61: storeList.add(store2);
62: CertStore store = CertStore.getInstance("Multi",
63: new MultiCertStoreParameters(storeList));
64:
65: // Searching for rootCert by subjectDN
66: X509CertSelector targetConstraints = new X509CertSelector();
67: targetConstraints.setSubject(rootCert.getSubjectX500Principal()
68: .getName());
69: Collection certs = store.getCertificates(targetConstraints);
70:
71: if (certs.size() != 2 || !certs.contains(rootCert)) {
72: fail("2 rootCerts not found by subjectDN");
73: }
74:
75: store = CertStore.getInstance("Multi",
76: new MultiCertStoreParameters(storeList, false));
77: certs = store.getCertificates(targetConstraints);
78:
79: if (certs.size() != 1 || !certs.contains(rootCert)) {
80: fail("1 rootCert not found by subjectDN");
81: }
82: }
83:
84: public String getName() {
85: return "MultiCertStore";
86: }
87:
88: public static void main(String[] args) {
89: Security.addProvider(new BouncyCastleProvider());
90:
91: runTest(new MultiCertStoreTest());
92: }
93:
94: }
|