1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.pkix.internal

File BcX509CertificateChainBuilderTest.java

 

Code metrics

0
41
7
1
155
108
7
0.17
5.86
7
1

Classes

Class Line # Actions
BcX509CertificateChainBuilderTest 53 41 0% 7 0
1.0100%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20   
21    package org.xwiki.crypto.pkix.internal;
22   
23    import java.util.ArrayList;
24    import java.util.Collection;
25   
26    import org.bouncycastle.cert.X509CertificateHolder;
27    import org.bouncycastle.util.CollectionStore;
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.crypto.AbstractPKIXTest;
32    import org.xwiki.crypto.BinaryStringEncoder;
33    import org.xwiki.crypto.internal.asymmetric.keyfactory.BcDSAKeyFactory;
34    import org.xwiki.crypto.internal.asymmetric.keyfactory.BcRSAKeyFactory;
35    import org.xwiki.crypto.internal.digest.factory.BcSHA1DigestFactory;
36    import org.xwiki.crypto.internal.encoder.Base64BinaryStringEncoder;
37    import org.xwiki.crypto.pkix.CertificateChainBuilder;
38    import org.xwiki.crypto.pkix.CertificateFactory;
39    import org.xwiki.crypto.pkix.CertificateProvider;
40    import org.xwiki.crypto.pkix.params.CertifiedPublicKey;
41    import org.xwiki.crypto.signer.internal.factory.BcDSAwithSHA1SignerFactory;
42    import org.xwiki.crypto.signer.internal.factory.BcSHA1withRsaSignerFactory;
43    import org.xwiki.crypto.signer.internal.factory.DefaultSignerFactory;
44    import org.xwiki.test.annotation.ComponentList;
45    import org.xwiki.test.mockito.MockitoComponentMockingRule;
46   
47    import static org.hamcrest.Matchers.contains;
48    import static org.junit.Assert.assertThat;
49   
50    @ComponentList({Base64BinaryStringEncoder.class, BcRSAKeyFactory.class, BcDSAKeyFactory.class,
51    BcSHA1DigestFactory.class, BcSHA1withRsaSignerFactory.class, BcDSAwithSHA1SignerFactory.class,
52    DefaultSignerFactory.class, BcStoreX509CertificateProvider.class, BcX509CertificateFactory.class})
 
53    public class BcX509CertificateChainBuilderTest extends AbstractPKIXTest
54    {
55    @Rule
56    public final MockitoComponentMockingRule<CertificateChainBuilder> mocker =
57    new MockitoComponentMockingRule<CertificateChainBuilder>(BcX509CertificateChainBuilder.class);
58   
59    private CertificateChainBuilder builder;
60    private CertifiedPublicKey v1CaCert;
61    private CertifiedPublicKey v1Cert;
62    private CertifiedPublicKey v3CaCert;
63    private CertifiedPublicKey v3InterCaCert;
64    private CertifiedPublicKey v3Cert;
65   
 
66  5 toggle public void setupTest(MockitoComponentMockingRule<CertificateChainBuilder> mocker) throws Exception
67    {
68  5 BinaryStringEncoder base64encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64");
69  5 CertificateFactory certFactory = mocker.getInstance(CertificateFactory.class, "X509");
70  5 v1CaCert = certFactory.decode(base64encoder.decode(V1_CA_CERT));
71  5 v1Cert = certFactory.decode(base64encoder.decode(V1_CERT));
72  5 v3CaCert = certFactory.decode(base64encoder.decode(V3_CA_CERT));
73  5 v3InterCaCert = certFactory.decode(base64encoder.decode(V3_ITERCA_CERT));
74  5 v3Cert = certFactory.decode(base64encoder.decode(V3_CERT));
75    }
76   
 
77  5 toggle @Before
78    public void configure() throws Exception
79    {
80  5 builder = mocker.getComponentUnderTest();
81  5 setupTest(mocker);
82    }
83   
 
84  1 toggle @Test
85    public void testValidV3CertificatePath() throws Exception
86    {
87  1 Collection<X509CertificateHolder> certs = new ArrayList<X509CertificateHolder>();
88  1 certs.add(BcUtils.getX509CertificateHolder(v3CaCert));
89  1 certs.add(BcUtils.getX509CertificateHolder(v3InterCaCert));
90   
91  1 CollectionStore store = new CollectionStore(certs);
92  1 CertificateProvider provider = mocker.getInstance(CertificateProvider.class, "BCStoreX509");
93  1 ((BcStoreX509CertificateProvider) provider).setStore(store);
94   
95  1 Collection<CertifiedPublicKey> chain = builder.build(v3Cert, provider);
96   
97  1 assertThat(chain, contains(v3CaCert, v3InterCaCert, v3Cert));
98    }
99   
 
100  1 toggle @Test
101    public void testIncompleteV3CertificatePath() throws Exception
102    {
103  1 Collection<X509CertificateHolder> certs = new ArrayList<X509CertificateHolder>();
104  1 certs.add(BcUtils.getX509CertificateHolder(v3InterCaCert));
105   
106  1 CollectionStore store = new CollectionStore(certs);
107  1 CertificateProvider provider = mocker.getInstance(CertificateProvider.class, "BCStoreX509");
108  1 ((BcStoreX509CertificateProvider) provider).setStore(store);
109   
110  1 Collection<CertifiedPublicKey> chain = builder.build(v3Cert, provider);
111   
112  1 assertThat(chain, contains(v3InterCaCert, v3Cert));
113    }
114   
 
115  1 toggle @Test
116    public void testBrokenV3CertificatePath() throws Exception
117    {
118  1 Collection<X509CertificateHolder> certs = new ArrayList<X509CertificateHolder>();
119  1 certs.add(BcUtils.getX509CertificateHolder(v3CaCert));
120   
121  1 CollectionStore store = new CollectionStore(certs);
122  1 CertificateProvider provider = mocker.getInstance(CertificateProvider.class, "BCStoreX509");
123  1 ((BcStoreX509CertificateProvider) provider).setStore(store);
124   
125  1 Collection<CertifiedPublicKey> chain = builder.build(v3Cert, provider);
126   
127  1 assertThat(chain, contains(v3Cert));
128    }
129   
 
130  1 toggle @Test
131    public void testValidV1CertificatePath() throws Exception
132    {
133  1 Collection<X509CertificateHolder> certs = new ArrayList<X509CertificateHolder>();
134  1 certs.add(BcUtils.getX509CertificateHolder(v1CaCert));
135   
136  1 CollectionStore store = new CollectionStore(certs);
137  1 CertificateProvider provider = mocker.getInstance(CertificateProvider.class, "BCStoreX509");
138  1 ((BcStoreX509CertificateProvider) provider).setStore(store);
139   
140  1 Collection<CertifiedPublicKey> chain = builder.build(v1Cert, provider);
141   
142  1 assertThat(chain, contains(v1CaCert, v1Cert));
143    }
144   
 
145  1 toggle @Test
146    public void testIncompleteV1CertificatePath() throws Exception
147    {
148  1 CertificateProvider provider = mocker.getInstance(CertificateProvider.class, "BCStoreX509");
149   
150  1 Collection<CertifiedPublicKey> chain = builder.build(v1Cert, provider);
151   
152  1 assertThat(chain, contains(v1Cert));
153    }
154   
155    }