1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.crypto.internal.asymmetric.generator

File BcDSAKeyPairGeneratorTest.java

 

Code metrics

2
64
4
1
162
122
5
0.08
16
4
1.25

Classes

Class Line # Actions
BcDSAKeyPairGeneratorTest 54 64 0% 5 1
0.9857142698.6%
 

Contributing tests

This file is covered by 3 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.generator;
21   
22    import java.security.SecureRandom;
23   
24    import javax.inject.Provider;
25   
26    import org.bouncycastle.crypto.params.DSAKeyParameters;
27    import org.bouncycastle.crypto.params.DSAPrivateKeyParameters;
28    import org.bouncycastle.crypto.params.DSAPublicKeyParameters;
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.xwiki.component.util.DefaultParameterizedType;
33    import org.xwiki.crypto.KeyPairGenerator;
34    import org.xwiki.crypto.KeyParametersGenerator;
35    import org.xwiki.crypto.internal.FixedSecureRandomProvider;
36    import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters;
37    import org.xwiki.crypto.internal.asymmetric.keyfactory.BcDSAKeyFactory;
38    import org.xwiki.crypto.internal.digest.factory.BcSHA1DigestFactory;
39    import org.xwiki.crypto.internal.digest.factory.BcSHA224DigestFactory;
40    import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricKeyPair;
41    import org.xwiki.crypto.params.generator.asymmetric.DSAKeyGenerationParameters;
42    import org.xwiki.crypto.params.generator.asymmetric.DSAKeyParametersGenerationParameters;
43    import org.xwiki.test.annotation.ComponentList;
44    import org.xwiki.test.mockito.MockitoComponentMockingRule;
45   
46    import static org.hamcrest.CoreMatchers.equalTo;
47    import static org.hamcrest.CoreMatchers.instanceOf;
48    import static org.hamcrest.CoreMatchers.not;
49    import static org.hamcrest.CoreMatchers.nullValue;
50    import static org.junit.Assert.assertThat;
51   
52    @ComponentList({BcDSAKeyParameterGenerator.class, BcDSAKeyFactory.class, FixedSecureRandomProvider.class,
53    BcSHA1DigestFactory.class, BcSHA224DigestFactory.class})
 
54    public class BcDSAKeyPairGeneratorTest
55    {
56    @Rule
57    public final MockitoComponentMockingRule<KeyPairGenerator> mocker =
58    new MockitoComponentMockingRule<KeyPairGenerator>(BcDSAKeyPairGenerator.class);
59   
60    private KeyPairGenerator generator;
61    private KeyParametersGenerator parameterGenerator;
62   
 
63  3 toggle @Before
64    public void configure() throws Exception
65    {
66  3 generator = mocker.getComponentUnderTest();
67  3 parameterGenerator = mocker.getInstance(KeyParametersGenerator.class, "DSA");
68   
69    // Reinitialize the random source
70  3 Provider<SecureRandom> rndprov =
71    mocker.getInstance(new DefaultParameterizedType(null, Provider.class, SecureRandom.class));
72  3 if (rndprov instanceof FixedSecureRandomProvider) {
73  3 ((FixedSecureRandomProvider) rndprov).initialize();
74    }
75    }
76   
 
77  1 toggle @Test
78    public void testGenerateWithoutArgument() throws Exception
79    {
80  1 AsymmetricKeyPair kp1 = generator.generate();
81  1 AsymmetricKeyPair kp2 = generator.generate();
82   
83  1 assertThat(kp1, not(nullValue()));
84  1 assertThat(kp2, not(nullValue()));
85  1 assertThat(kp1.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class));
86  1 assertThat(kp2.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class));
87  1 assertThat(kp1.getPublic(), instanceOf(BcAsymmetricKeyParameters.class));
88  1 assertThat(kp2.getPublic(), instanceOf(BcAsymmetricKeyParameters.class));
89  1 assertThat(((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters(), instanceOf(DSAPrivateKeyParameters.class));
90  1 assertThat(((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters(), instanceOf(DSAPrivateKeyParameters.class));
91  1 assertThat(((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters(), instanceOf(DSAPublicKeyParameters.class));
92  1 assertThat(((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters(), instanceOf(DSAPublicKeyParameters.class));
93  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters()).getParameters().getP().bitLength(), equalTo(1024));
94  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters()).getParameters().getP().bitLength(), equalTo(1024));
95  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters()).getParameters().getP().bitLength(), equalTo(1024));
96  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters()).getParameters().getP().bitLength(), equalTo(1024));
97  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters()).getParameters().getQ().bitLength(), equalTo(160));
98  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters()).getParameters().getQ().bitLength(), equalTo(160));
99  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters()).getParameters().getQ().bitLength(), equalTo(160));
100  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters()).getParameters().getQ().bitLength(), equalTo(160));
101    }
102   
 
103  1 toggle @Test
104    public void testGenerateFIPS186_2() throws Exception
105    {
106  1 DSAKeyGenerationParameters params = (DSAKeyGenerationParameters) parameterGenerator.generate();
107   
108  1 assertThat(params, not(nullValue()));
109  1 assertThat(params.getP().bitLength(), equalTo(1024));
110  1 assertThat(params.getQ().bitLength(), equalTo(160));
111   
112  1 AsymmetricKeyPair kp1 = generator.generate(params);
113   
114  1 assertThat(kp1, not(nullValue()));
115  1 assertThat(kp1.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class));
116  1 assertThat(kp1.getPublic(), instanceOf(BcAsymmetricKeyParameters.class));
117  1 assertThat(((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters(), instanceOf(DSAPrivateKeyParameters.class));
118  1 assertThat(((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters(), instanceOf(DSAPublicKeyParameters.class));
119  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters()).getParameters().getP().bitLength(), equalTo(1024));
120  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters()).getParameters().getP().bitLength(), equalTo(1024));
121  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters()).getParameters().getQ().bitLength(), equalTo(160));
122  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters()).getParameters().getQ().bitLength(), equalTo(160));
123   
124  1 AsymmetricKeyPair kp2 = generator.generate(params);
125   
126  1 assertThat(kp2, not(nullValue()));
127  1 assertThat(kp2.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class));
128  1 assertThat(kp2.getPublic(), instanceOf(BcAsymmetricKeyParameters.class));
129  1 assertThat(((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters(), instanceOf(DSAPrivateKeyParameters.class));
130  1 assertThat(((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters(), instanceOf(DSAPublicKeyParameters.class));
131  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters()).getParameters().getP().bitLength(), equalTo(1024));
132  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters()).getParameters().getP().bitLength(), equalTo(1024));
133  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters()).getParameters().getQ().bitLength(), equalTo(160));
134  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters()).getParameters().getQ().bitLength(), equalTo(160));
135  1 assertThat(kp2, not(equalTo(kp1)));
136    }
137   
 
138  1 toggle @Test
139    public void testGenerateFIPS186_3() throws Exception
140    {
141  1 DSAKeyGenerationParameters params =
142    (DSAKeyGenerationParameters) parameterGenerator.generate(
143    new DSAKeyParametersGenerationParameters(256, 28, 1)
144    );
145   
146  1 assertThat(params, not(nullValue()));
147  1 assertThat(params.getP().bitLength(), equalTo(2048));
148  1 assertThat(params.getQ().bitLength(), equalTo(224));
149   
150  1 AsymmetricKeyPair kp = generator.generate(params);
151   
152  1 assertThat(kp, not(nullValue()));
153  1 assertThat(kp.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class));
154  1 assertThat(kp.getPublic(), instanceOf(BcAsymmetricKeyParameters.class));
155  1 assertThat(((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters(), instanceOf(DSAPrivateKeyParameters.class));
156  1 assertThat(((BcAsymmetricKeyParameters) kp.getPublic()).getParameters(), instanceOf(DSAPublicKeyParameters.class));
157  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters()).getParameters().getP().bitLength(), equalTo(2048));
158  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp.getPublic()).getParameters()).getParameters().getP().bitLength(), equalTo(2048));
159  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp.getPrivate()).getParameters()).getParameters().getQ().bitLength(), equalTo(224));
160  1 assertThat(((DSAKeyParameters) ((BcAsymmetricKeyParameters) kp.getPublic()).getParameters()).getParameters().getQ().bitLength(), equalTo(224));
161    }
162    }