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

File BcDHKeyPairGeneratorTest.java

 

Code metrics

2
32
3
1
126
87
4
0.12
10.67
3
1.33

Classes

Class Line # Actions
BcDHKeyPairGeneratorTest 53 32 0% 4 37
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.DHPrivateKeyParameters;
27    import org.bouncycastle.crypto.params.DHPublicKeyParameters;
28    import org.junit.Before;
29    import org.junit.Ignore;
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.DefaultSecureRandomProvider;
36    import org.xwiki.crypto.internal.FixedSecureRandomProvider;
37    import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters;
38    import org.xwiki.crypto.internal.asymmetric.keyfactory.BcDHKeyFactory;
39    import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricKeyPair;
40    import org.xwiki.crypto.params.generator.asymmetric.DHKeyGenerationParameters;
41    import org.xwiki.test.annotation.ComponentList;
42    import org.xwiki.test.mockito.MockitoComponentMockingRule;
43   
44    import static org.hamcrest.CoreMatchers.equalTo;
45    import static org.hamcrest.CoreMatchers.instanceOf;
46    import static org.hamcrest.CoreMatchers.not;
47    import static org.hamcrest.CoreMatchers.nullValue;
48    import static org.junit.Assert.assertThat;
49   
50    // Skipping since this could be very long or blocking due to the amount of entropy needed, DH support is AS IS.
51    @Ignore
52    @ComponentList({BcDHKeyParameterGenerator.class, BcDHKeyFactory.class, DefaultSecureRandomProvider.class})
 
53    public class BcDHKeyPairGeneratorTest
54    {
55    @Rule
56    public final MockitoComponentMockingRule<KeyPairGenerator> mocker =
57    new MockitoComponentMockingRule<KeyPairGenerator>(BcDHKeyPairGenerator.class);
58   
59    private KeyPairGenerator generator;
60    private KeyParametersGenerator parameterGenerator;
61   
 
62  0 toggle @Before
63    public void configure() throws Exception
64    {
65  0 generator = mocker.getComponentUnderTest();
66  0 parameterGenerator = mocker.getInstance(KeyParametersGenerator.class, "DH");
67   
68    // Reinitialize the random source
69  0 Provider<SecureRandom> rndprov =
70    mocker.getInstance(new DefaultParameterizedType(null, Provider.class, SecureRandom.class));
71  0 if (rndprov instanceof FixedSecureRandomProvider) {
72  0 ((FixedSecureRandomProvider) rndprov).initialize();
73    }
74    }
75   
 
76  0 toggle @Test
77    // Skipping since this could be very long or blocking due to the amount of entropy needed, DH support is AS IS.
78    public void testGenerateWithoutArgument() throws Exception
79    {
80  0 AsymmetricKeyPair kp1 = generator.generate();
81  0 AsymmetricKeyPair kp2 = generator.generate();
82   
83  0 assertThat(kp1, not(nullValue()));
84  0 assertThat(kp2, not(nullValue()));
85  0 assertThat(kp1.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class));
86  0 assertThat(kp2.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class));
87  0 assertThat(kp1.getPublic(), instanceOf(BcAsymmetricKeyParameters.class));
88  0 assertThat(kp2.getPublic(), instanceOf(BcAsymmetricKeyParameters.class));
89  0 assertThat(((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters(), instanceOf(
90    DHPrivateKeyParameters.class));
91  0 assertThat(((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters(), instanceOf(
92    DHPrivateKeyParameters.class));
93  0 assertThat(((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters(), instanceOf(
94    DHPublicKeyParameters.class));
95  0 assertThat(((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters(), instanceOf(
96    DHPublicKeyParameters.class));
97    }
98   
 
99  0 toggle @Test
100    public void testGenerateWithDefaultParameters() throws Exception
101    {
102  0 DHKeyGenerationParameters params = (DHKeyGenerationParameters) parameterGenerator.generate();
103   
104  0 assertThat(params, not(nullValue()));
105   
106  0 AsymmetricKeyPair kp1 = generator.generate(params);
107   
108  0 assertThat(kp1, not(nullValue()));
109  0 assertThat(kp1.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class));
110  0 assertThat(kp1.getPublic(), instanceOf(BcAsymmetricKeyParameters.class));
111  0 assertThat(((BcAsymmetricKeyParameters) kp1.getPrivate()).getParameters(), instanceOf(DHPrivateKeyParameters.class));
112  0 assertThat(((BcAsymmetricKeyParameters) kp1.getPublic()).getParameters(), instanceOf(DHPublicKeyParameters.class));
113   
114  0 AsymmetricKeyPair kp2 = generator.generate(params);
115   
116  0 assertThat(kp2, not(nullValue()));
117  0 assertThat(kp2.getPrivate(), instanceOf(BcAsymmetricKeyParameters.class));
118  0 assertThat(kp2.getPublic(), instanceOf(BcAsymmetricKeyParameters.class));
119  0 assertThat(((BcAsymmetricKeyParameters) kp2.getPrivate()).getParameters(), instanceOf(
120    DHPrivateKeyParameters.class));
121  0 assertThat(((BcAsymmetricKeyParameters) kp2.getPublic()).getParameters(), instanceOf(
122    DHPublicKeyParameters.class));
123  0 assertThat(kp2, not(equalTo(kp1)));
124    }
125    }
126