| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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}) |
| |
|
| 100% |
Uncovered Elements: 0 (48) |
Complexity: 7 |
Complexity Density: 0.17 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
| 66 |
5 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 77 |
5 |
@Before... |
| 78 |
|
public void configure() throws Exception |
| 79 |
|
{ |
| 80 |
5 |
builder = mocker.getComponentUnderTest(); |
| 81 |
5 |
setupTest(mocker); |
| 82 |
|
} |
| 83 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 84 |
1 |
@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% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 100 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 115 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 130 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 145 |
1 |
@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 |
|
} |