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

File BcDSAwithSHA1SignerFactoryTest.java

 

Code metrics

2
11
4
1
111
78
5
0.45
2.75
4
1.25

Classes

Class Line # Actions
BcDSAwithSHA1SignerFactoryTest 42 11 0% 5 1
0.941176594.1%
 

Contributing tests

This file is covered by 1 test. .

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.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})
 
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 = "MIIBTAIBADCCASwGByqGSM44BAEwggEfAoGBANQ9Oa1j9sWAhdXNyqz8HL/bA/e"
49    + "d2VrBw6TPkgMyV1Upix58RSjOHMQNrgemSGkb80dRcLqVDYbI3ObnIJh83Zx6ze"
50    + "aTpvUohGLyTa0F7UY15LkbJpyz8WFJaVykH85nz3Zo6Md9Z4X95yvF1+h9qYuak"
51    + "jWcHW31+pN4u3cJNg5FAhUAj986cVG9NgWgWzFVSLbB9pEPbFUCgYEAmQrZFH3M"
52    + "X5CLX/5vDvxyTeeRPZHLWc0ik3GwrIJExuVrOkuFInpx0aVbuJTxrEnY2fuc/+B"
53    + "yj/F56DDO31+qPu7ZxbSvvD33OOk8eFEfn+Hia3QmA+dGrhUqoMpfDf4/GBgJhn"
54    + "yQtFzMddHmYB0QnS9yX1n6DOWj/CSX0PvrlMYEFwIVAIO1GUQjAddL4btiFQnhe"
55    + "N4fxBTa";
56   
57    private static final String PUBLIC_KEY = "MIIBtzCCASwGByqGSM44BAEwggEfAoGBANQ9Oa1j9sWAhdXNyqz8HL/bA/ed2VrB"
58    + "w6TPkgMyV1Upix58RSjOHMQNrgemSGkb80dRcLqVDYbI3ObnIJh83Zx6zeaTpvUo"
59    + "hGLyTa0F7UY15LkbJpyz8WFJaVykH85nz3Zo6Md9Z4X95yvF1+h9qYuakjWcHW31"
60    + "+pN4u3cJNg5FAhUAj986cVG9NgWgWzFVSLbB9pEPbFUCgYEAmQrZFH3MX5CLX/5v"
61    + "DvxyTeeRPZHLWc0ik3GwrIJExuVrOkuFInpx0aVbuJTxrEnY2fuc/+Byj/F56DDO"
62    + "31+qPu7ZxbSvvD33OOk8eFEfn+Hia3QmA+dGrhUqoMpfDf4/GBgJhnyQtFzMddHm"
63    + "YB0QnS9yX1n6DOWj/CSX0PvrlMYDgYQAAoGAJvnuTm8oI/RRI2tiZHtPkvSQaA3F"
64    + "P4PRsVx6z1oIGg9OAxrtSS/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   
 
77  1 toggle public void setupTest(MockitoComponentMockingRule<SignerFactory> mocker) throws Exception
78    {
79    // Decode keys once for all tests.
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   
 
89  1 toggle 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   
 
97  1 toggle @Before
98    public void configure() throws Exception
99    {
100  1 setupTest(mocker);
101    }
102   
 
103  1 toggle @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    }