1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.internal.asymmetric.keyfactory; |
21 |
|
|
22 |
|
import java.security.PrivateKey; |
23 |
|
import java.security.PublicKey; |
24 |
|
|
25 |
|
import org.bouncycastle.crypto.params.DSAKeyParameters; |
26 |
|
import org.bouncycastle.crypto.params.DSAPrivateKeyParameters; |
27 |
|
import org.bouncycastle.crypto.params.DSAPublicKeyParameters; |
28 |
|
import org.junit.Before; |
29 |
|
import org.junit.Rule; |
30 |
|
import org.junit.Test; |
31 |
|
import org.xwiki.crypto.AsymmetricKeyFactory; |
32 |
|
import org.xwiki.crypto.BinaryStringEncoder; |
33 |
|
import org.xwiki.crypto.internal.DefaultSecureRandomProvider; |
34 |
|
import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters; |
35 |
|
import org.xwiki.crypto.internal.encoder.Base64BinaryStringEncoder; |
36 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters; |
37 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters; |
38 |
|
import org.xwiki.test.annotation.ComponentList; |
39 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
40 |
|
|
41 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
42 |
|
import static org.hamcrest.CoreMatchers.instanceOf; |
43 |
|
import static org.hamcrest.CoreMatchers.not; |
44 |
|
import static org.hamcrest.CoreMatchers.sameInstance; |
45 |
|
import static org.junit.Assert.assertThat; |
46 |
|
|
47 |
|
@ComponentList({Base64BinaryStringEncoder.class, BcDSAKeyFactory.class, DefaultSecureRandomProvider.class}) |
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 6 |
Complexity Density: 0.25 |
|
48 |
|
public class BcDSAKeyFactoryTest |
49 |
|
{ |
50 |
|
private static final String PRIVATE_KEY = "MIIBTAIBADCCASwGByqGSM44BAEwggEfAoGBANQ9Oa1j9sWAhdXNyqz8HL/bA/e" |
51 |
|
+ "d2VrBw6TPkgMyV1Upix58RSjOHMQNrgemSGkb80dRcLqVDYbI3ObnIJh83Zx6ze" |
52 |
|
+ "aTpvUohGLyTa0F7UY15LkbJpyz8WFJaVykH85nz3Zo6Md9Z4X95yvF1+h9qYuak" |
53 |
|
+ "jWcHW31+pN4u3cJNg5FAhUAj986cVG9NgWgWzFVSLbB9pEPbFUCgYEAmQrZFH3M" |
54 |
|
+ "X5CLX/5vDvxyTeeRPZHLWc0ik3GwrIJExuVrOkuFInpx0aVbuJTxrEnY2fuc/+B" |
55 |
|
+ "yj/F56DDO31+qPu7ZxbSvvD33OOk8eFEfn+Hia3QmA+dGrhUqoMpfDf4/GBgJhn" |
56 |
|
+ "yQtFzMddHmYB0QnS9yX1n6DOWj/CSX0PvrlMYEFwIVAIO1GUQjAddL4btiFQnhe" |
57 |
|
+ "N4fxBTa"; |
58 |
|
|
59 |
|
private static final String PUBLIC_KEY = "MIIBtzCCASwGByqGSM44BAEwggEfAoGBANQ9Oa1j9sWAhdXNyqz8HL/bA/ed2VrB" |
60 |
|
+ "w6TPkgMyV1Upix58RSjOHMQNrgemSGkb80dRcLqVDYbI3ObnIJh83Zx6zeaTpvUo" |
61 |
|
+ "hGLyTa0F7UY15LkbJpyz8WFJaVykH85nz3Zo6Md9Z4X95yvF1+h9qYuakjWcHW31" |
62 |
|
+ "+pN4u3cJNg5FAhUAj986cVG9NgWgWzFVSLbB9pEPbFUCgYEAmQrZFH3MX5CLX/5v" |
63 |
|
+ "DvxyTeeRPZHLWc0ik3GwrIJExuVrOkuFInpx0aVbuJTxrEnY2fuc/+Byj/F56DDO" |
64 |
|
+ "31+qPu7ZxbSvvD33OOk8eFEfn+Hia3QmA+dGrhUqoMpfDf4/GBgJhnyQtFzMddHm" |
65 |
|
+ "YB0QnS9yX1n6DOWj/CSX0PvrlMYDgYQAAoGAJvnuTm8oI/RRI2tiZHtPkvSQaA3F" |
66 |
|
+ "P4PRsVx6z1oIGg9OAxrtSS/aiQa+HWFg7fjHlMJ30Vh0yqt7igj70jaLGyDvr3MP" |
67 |
|
+ "DyiO++72IiGUluc6yHg6m9cQ53eeJt9i44LJfTOw1S3YMU1ST7alokSnJRTICp5W" |
68 |
|
+ "By0m1scwheuTo0E="; |
69 |
|
|
70 |
|
@Rule |
71 |
|
public final MockitoComponentMockingRule<AsymmetricKeyFactory> mocker = |
72 |
|
new MockitoComponentMockingRule<AsymmetricKeyFactory>(BcDSAKeyFactory.class); |
73 |
|
|
74 |
|
private AsymmetricKeyFactory factory; |
75 |
|
|
76 |
|
private static byte[] privateKey; |
77 |
|
private static byte[] publicKey; |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
79 |
4 |
@Before... |
80 |
|
public void configure() throws Exception |
81 |
|
{ |
82 |
4 |
factory = mocker.getComponentUnderTest(); |
83 |
|
|
84 |
|
|
85 |
4 |
if (privateKey == null) { |
86 |
1 |
BinaryStringEncoder base64encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64"); |
87 |
1 |
privateKey = base64encoder.decode(PRIVATE_KEY); |
88 |
1 |
publicKey = base64encoder.decode(PUBLIC_KEY); |
89 |
|
} |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
92 |
1 |
@Test... |
93 |
|
public void testPrivateKeyFromPKCS8() throws Exception |
94 |
|
{ |
95 |
1 |
PrivateKeyParameters key = factory.fromPKCS8(privateKey); |
96 |
|
|
97 |
1 |
assertThat(key, instanceOf(BcAsymmetricKeyParameters.class)); |
98 |
1 |
assertThat(((BcAsymmetricKeyParameters) key).getParameters(), instanceOf(DSAPrivateKeyParameters.class)); |
99 |
1 |
assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) key).getParameters()).getParameters().getP().bitLength(), equalTo(1024)); |
100 |
|
|
101 |
1 |
assertThat(key.getEncoded(), equalTo(privateKey)); |
102 |
|
} |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
104 |
1 |
@Test... |
105 |
|
public void testPublicKeyFromX509() throws Exception |
106 |
|
{ |
107 |
1 |
PublicKeyParameters key = factory.fromX509(publicKey); |
108 |
|
|
109 |
1 |
assertThat(key, instanceOf(BcAsymmetricKeyParameters.class)); |
110 |
1 |
assertThat(((BcAsymmetricKeyParameters) key).getParameters(), instanceOf(DSAPublicKeyParameters.class)); |
111 |
|
|
112 |
1 |
assertThat(key.getEncoded(), equalTo(publicKey)); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
115 |
1 |
@Test... |
116 |
|
public void testPrivateKeyFromToKey() throws Exception |
117 |
|
{ |
118 |
1 |
PrivateKeyParameters key1 = factory.fromPKCS8(privateKey); |
119 |
1 |
PrivateKey pk = factory.toKey(key1); |
120 |
1 |
PrivateKeyParameters key2 = factory.fromKey(pk); |
121 |
|
|
122 |
1 |
assertThat(key1, not(sameInstance(key2))); |
123 |
1 |
assertThat(key1, equalTo(key2)); |
124 |
|
} |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
126 |
1 |
@Test... |
127 |
|
public void testPublicKeyFromToKey() throws Exception |
128 |
|
{ |
129 |
1 |
PublicKeyParameters key1 = factory.fromX509(publicKey); |
130 |
1 |
PublicKey pk = factory.toKey(key1); |
131 |
1 |
PublicKeyParameters key2 = factory.fromKey(pk); |
132 |
|
|
133 |
1 |
assertThat(key1, not(sameInstance(key2))); |
134 |
1 |
assertThat(key1, equalTo(key2)); |
135 |
|
} |
136 |
|
} |
137 |
|
|