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

File BcDSAKeyFactoryTest.java

 

Code metrics

2
24
5
1
137
96
6
0.25
4.8
5
1.2

Classes

Class Line # Actions
BcDSAKeyFactoryTest 48 24 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 4 tests. .

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    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})
 
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   
 
79  4 toggle @Before
80    public void configure() throws Exception
81    {
82  4 factory = mocker.getComponentUnderTest();
83   
84    // Decode keys once for all tests.
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   
 
92  1 toggle @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   
 
104  1 toggle @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   
 
115  1 toggle @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   
 
126  1 toggle @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