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.signer.internal.factory; |
22 |
|
|
23 |
|
import org.junit.Before; |
24 |
|
import org.junit.Rule; |
25 |
|
import org.junit.Test; |
26 |
|
import org.xwiki.crypto.AsymmetricKeyFactory; |
27 |
|
import org.xwiki.crypto.BinaryStringEncoder; |
28 |
|
import org.xwiki.crypto.internal.asymmetric.keyfactory.BcDSAKeyFactory; |
29 |
|
import org.xwiki.crypto.internal.encoder.Base64BinaryStringEncoder; |
30 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters; |
31 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters; |
32 |
|
import org.xwiki.crypto.signer.Signer; |
33 |
|
import org.xwiki.crypto.signer.SignerFactory; |
34 |
|
import org.xwiki.test.annotation.ComponentList; |
35 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
36 |
|
|
37 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
38 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
39 |
|
import static org.junit.Assert.assertTrue; |
40 |
|
|
41 |
|
@ComponentList({Base64BinaryStringEncoder.class, BcDSAKeyFactory.class}) |
|
|
| 94.1% |
Uncovered Elements: 1 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
42 |
|
public class BcDSAwithSHA1SignerFactoryTest |
43 |
|
{ |
44 |
|
@Rule |
45 |
|
public final MockitoComponentMockingRule<SignerFactory> mocker = |
46 |
|
new MockitoComponentMockingRule<SignerFactory>(BcDSAwithSHA1SignerFactory.class); |
47 |
|
|
48 |
|
private static final String PRIVATE_KEY = "MIIBTAIBADCCASwGByqGSM44BAEwggEfAoGBANQ9Oa1j9sWAhdXN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9OAxrtSS/aiQa+HWFg7fjHlMJ30Vh0yqt7igj70jaLGyDvr3MP" |
65 |
|
+ "DyiO++72IiGUluc6yHg6m9cQ53eeJt9i44LJfTOw1S3YMU1ST7alokSnJRTICp5W" |
66 |
|
+ "By0m1scwheuTo0E="; |
67 |
|
|
68 |
|
private static final String TEXT = "Congress shall make no law respecting an establishment of religion, or " |
69 |
|
+ "prohibiting the free exercise thereof; or abridging the freedom of speech, " |
70 |
|
+ "or of the press; or the right of the people peaceably to assemble, and to " |
71 |
|
+ "petition the Government for a redress of grievances."; |
72 |
|
|
73 |
|
protected static PrivateKeyParameters privateKey; |
74 |
|
protected static PublicKeyParameters publicKey; |
75 |
|
protected static byte[] text; |
76 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
77 |
1 |
public void setupTest(MockitoComponentMockingRule<SignerFactory> mocker) throws Exception... |
78 |
|
{ |
79 |
|
|
80 |
1 |
if (privateKey == null) { |
81 |
1 |
BinaryStringEncoder base64encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64"); |
82 |
1 |
AsymmetricKeyFactory keyFactory = mocker.getInstance(AsymmetricKeyFactory.class, "DSA"); |
83 |
1 |
privateKey = keyFactory.fromPKCS8(base64encoder.decode(PRIVATE_KEY)); |
84 |
1 |
publicKey = keyFactory.fromX509(base64encoder.decode(PUBLIC_KEY)); |
85 |
1 |
text = TEXT.getBytes("UTF-8"); |
86 |
|
} |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
89 |
1 |
protected void runTestSignatureVerification(Signer signer, Signer verifier) throws Exception... |
90 |
|
{ |
91 |
1 |
byte[] signature = signer.generate(text); |
92 |
|
|
93 |
1 |
assertThat(signer.getEncoded(), equalTo(verifier.getEncoded())); |
94 |
1 |
assertTrue(verifier.verify(signature, text)); |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
1 |
@Before... |
98 |
|
public void configure() throws Exception |
99 |
|
{ |
100 |
1 |
setupTest(mocker); |
101 |
|
} |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
103 |
1 |
@Test... |
104 |
|
public void testDSASignatureVerification() throws Exception |
105 |
|
{ |
106 |
1 |
runTestSignatureVerification( |
107 |
|
mocker.getComponentUnderTest().getInstance(true, privateKey), |
108 |
|
mocker.getComponentUnderTest().getInstance(false, publicKey) |
109 |
|
); |
110 |
|
} |
111 |
|
} |