~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to test/jdk1.3/org/bouncycastle/jce/provider/test/MultiCertStoreTest.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

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