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

File BcRsaOaepCipherFactoryTest.java

 
testCipherWithAsymmetricParameters: Unexpected parameters received for a asymmetric cipher: o...
testCipherWithWrongParameters: Cipher parameters are incompatible with this cipher: org....
 

Code metrics

2
39
5
3
177
135
6
0.15
7.8
1.67
1.2

Classes

Class Line # Actions
BcRsaOaepCipherFactoryTest 46 39 0% 6 8
0.8260869482.6%
BcRsaOaepCipherFactoryTest.WrongParameters 155 0 - 0 0
-1.0 -
BcRsaOaepCipherFactoryTest.SymmetricParameters 166 0 - 0 0
-1.0 -
 

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.cipher.internal.asymmetric.factory;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.junit.rules.ExpectedException;
26    import org.xwiki.crypto.AsymmetricKeyFactory;
27    import org.xwiki.crypto.BinaryStringEncoder;
28    import org.xwiki.crypto.cipher.Cipher;
29    import org.xwiki.crypto.cipher.CipherFactory;
30    import org.xwiki.crypto.internal.DefaultSecureRandomProvider;
31    import org.xwiki.crypto.internal.asymmetric.keyfactory.BcRSAKeyFactory;
32    import org.xwiki.crypto.internal.encoder.Base64BinaryStringEncoder;
33    import org.xwiki.crypto.internal.encoder.HexBinaryStringEncoder;
34    import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricCipherParameters;
35    import org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters;
36    import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters;
37    import org.xwiki.crypto.params.cipher.symmetric.SymmetricCipherParameters;
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.junit.Assert.assertThat;
43   
44    @ComponentList({Base64BinaryStringEncoder.class, HexBinaryStringEncoder.class,
45    BcRSAKeyFactory.class, DefaultSecureRandomProvider.class})
 
46    public class BcRsaOaepCipherFactoryTest
47    {
48    private static final String PRIVATE_KEY =
49    // Link to decoded ASN.1: https://goo.gl/kgV0IB
50    "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDCmjim/3likJ4"
51    + "VF564UyygqPjIX/z090AImLl0fDLUkIyCVTSd18wJ3axr1qjLtSgNPWet0puSxO"
52    + "FH0AzFKRCJOjUkQRU8iAkz64MLAf9xrx4nBECciqeB941s01kLtG8C/UqC3O9Sw"
53    + "HSdhtUpUU8V/91SiD09yNJsnODi3WqM3oLg1QYzKhoaD2mVo2xJLQ/QXqr2XIc5"
54    + "i2Mlpfq6S5JNbFD/I+UFhBUlBNuDOEV7ttIt2eFMEUsfkCestGo0YoQYOpTLPcP"
55    + "GRS7MnSY1CLWGUYqaMSnes0nS8ke2PPD4Q0suAZz4msnhNufanscstM8tcNtsZF"
56    + "6hj0JvbZok89szAgMBAAECggEBAKWJ1SlR5ysORDtDBXRc5HiiZEbnSGIFtYXaj"
57    + "N/nCsJBWBVCb+jZeirmU9bEGoB20OQ6WOjHYCnAqraQ51wMK5HgXvZBGtSMD/AH"
58    + "pkiF4YsOYULlXiUL2aQ4NijdvEC1sz1Cw9CAKmElb83UtZ1ZGkJnjhi35giZvU5"
59    + "BQRgbK5k57DFY66yv9VDg8tuD/enI9sRsCUZfCImuShGv4nLqhPMPg+1UxDPGet"
60    + "Vs8uEaJQ017E14wLKLA0DlED13icelU1A7ufkEdeBSv/yZ7ENjervzPwa9nITK/"
61    + "19uzqaHOcYZxmDQn6UHTnaLpIEaUvpp/pbed5S97ETSsqUBC8fqEUECgYEA/Sba"
62    + "o6efydhlXDHbXtyvaJWao19sbI9OfxGC6dR2fZiBx8Do9kVDDbMtb1PYEfLhYbi"
63    + "urmKGbUtcLSFgxNbZifUmG54M92nBsnsetMCqvMVNzYl2Je83V+NrIsLJjFIZ2C"
64    + "BvZa/FKOLDTwSe35fNqaS0ExdwcGNMIT//bDQCmyECgYEAxMq6rN+HpBRuhvvst"
65    + "V99zV+lI/1DzZuXExd+c3PSchiqkJrTLaQDvcaHQir9hK7RqF9vO7tvdluJjgX+"
66    + "f/CMPNQuC5k6vY/0fS4V2NQWtln9BBSzHtocTnZzFNq8tAZqyEhZUHIbkncroXv"
67    + "eUXqtlfOnKB2aYI/+3gPEMYJlH9MCgYA4exjA9r9B65QB0+Xb7mT8cpSD6uBoAD"
68    + "lFRITu4sZlE0exZ6sSdzWUsutqMUy+BHCguvHOWpEfhXbVYuMSR9VVYGrWMpc2B"
69    + "FSBG9MoBOyTHXpUZ10C7bJtW4IlyUvqkM7PV71C9MqKar2kvaUswdPTC7pZoBso"
70    + "GB9+M6crXxdNwQKBgDUVMlGbYi1CTaYfonQyM+8IE7WnhXiatZ+ywKtH3MZmHOw"
71    + "wtzIigdfZC3cvvX7i4S73vztvjdtxSaODvmiobEukOF9sj8m+YQa7Pa1lWFML5x"
72    + "IIu2BhGS2ZCeXgMvKkoH0x9tWaUhGqD5zZmtiDrPs75CUQBypw7SDaBzwLnld9A"
73    + "oGBAPgUh90PvUzbVVkzpVCPI82cmOIVMI1rDE6uCeNzIlN6Xu80RimCSaaDsESi"
74    + "tBtoVWLRWWmuCINyqr6e9AdyvbvT6mQCjbn9+y7t6ZAhLaya5ZMUVEBLyLLqMzr"
75    + "y oi/huj7m4nV4kPZz9LKxDRu3r6o0Pah+daDsTxEYObtsKa7e";
76   
77    private static final String PUBLIC_KEY =
78    // Link to decoded ASN.1: https://goo.gl/2YsSco
79    "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwpo4pv95YpCeFReeuFM"
80    + "soKj4yF/89PdACJi5dHwy1JCMglU0ndfMCd2sa9aoy7UoDT1nrdKbksThR9AMxS"
81    + "kQiTo1JEEVPIgJM+uDCwH/ca8eJwRAnIqngfeNbNNZC7RvAv1KgtzvUsB0nYbVK"
82    + "VFPFf/dUog9PcjSbJzg4t1qjN6C4NUGMyoaGg9plaNsSS0P0F6q9lyHOYtjJaX6"
83    + "ukuSTWxQ/yPlBYQVJQTbgzhFe7bSLdnhTBFLH5AnrLRqNGKEGDqUyz3DxkUuzJ0"
84    + "mNQi1hlGKmjEp3rNJ0vJHtjzw+ENLLgGc+JrJ4Tbn2p7HLLTPLXDbbGReoY9Cb2"
85    + "2aJPPbMwIDAQAB";
86   
87    private static final String INPUT = "4e6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e";
88   
89    @Rule
90    public final MockitoComponentMockingRule<CipherFactory> mocker =
91    new MockitoComponentMockingRule<CipherFactory>(BcRsaOaepCipherFactory.class);
92   
93    private CipherFactory factory;
94   
95    private static PrivateKeyParameters privateKey;
96    private static PublicKeyParameters publicKey;
97    private static byte[] input;
98   
 
99  4 toggle @Before
100    public void configure() throws Exception
101    {
102  4 factory = mocker.getComponentUnderTest();
103   
104    // Decode keys once for all tests.
105  4 if (privateKey == null) {
106  1 BinaryStringEncoder base64Encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64");
107  1 BinaryStringEncoder hexEencoder = mocker.getInstance(BinaryStringEncoder.class, "Base64");
108  1 AsymmetricKeyFactory keyfactory = mocker.getInstance(AsymmetricKeyFactory.class, "RSA");
109   
110  1 privateKey = keyfactory.fromPKCS8(base64Encoder.decode(PRIVATE_KEY));
111  1 publicKey = keyfactory.fromX509(base64Encoder.decode(PUBLIC_KEY));
112  1 input = hexEencoder.decode(INPUT);
113    }
114    }
115   
 
116  1 toggle @Test
117    public void testRSAEncryptionDecryptionOneShot() throws Exception
118    {
119  1 Cipher cipher = factory.getInstance(true, publicKey);
120  1 byte[] encrypted = cipher.doFinal(input);
121  1 cipher = factory.getInstance(false, privateKey);
122  1 assertThat(cipher.doFinal(encrypted), equalTo(input));
123   
124  1 cipher = factory.getInstance(true, privateKey);
125  1 encrypted = cipher.doFinal(input);
126  1 cipher = factory.getInstance(false, publicKey);
127  1 assertThat(cipher.doFinal(encrypted), equalTo(input));
128    }
129   
 
130  1 toggle @Test
131    public void testRSAEncryptionDecryptionProgressive() throws Exception
132    {
133  1 Cipher cipher = factory.getInstance(true, publicKey);
134  1 cipher.update(input, 0, 17);
135  1 cipher.update(input, 17, 1);
136  1 cipher.update(input, 18, input.length - 18);
137  1 byte[] encrypted = cipher.doFinal();
138  1 cipher = factory.getInstance(false, privateKey);
139  1 cipher.update(encrypted, 0, 65);
140  1 cipher.update(encrypted, 65, 1);
141  1 cipher.update(encrypted, 66, encrypted.length - 66);
142  1 assertThat(cipher.doFinal(), equalTo(input));
143   
144  1 cipher = factory.getInstance(true, privateKey);
145  1 cipher.update(input, 0, 15);
146  1 cipher.update(input, 15, 1);
147  1 encrypted = cipher.doFinal(input, 16, input.length - 16);
148  1 cipher = factory.getInstance(false, publicKey);
149  1 cipher.update(encrypted);
150  1 assertThat(cipher.doFinal(), equalTo(input));
151    }
152   
153    @Rule public ExpectedException thrown = ExpectedException.none();
154   
 
155    class WrongParameters implements AsymmetricCipherParameters
156    { }
157   
 
158  0 toggle @Test
159    public void testCipherWithWrongParameters() throws Exception
160    {
161  0 thrown.expect(UnsupportedOperationException.class);
162  0 thrown.expectMessage("Cipher parameters are incompatible with this cipher: " + WrongParameters.class.getName());
163  0 Test failure here factory.getInstance(true, new WrongParameters());
164    }
165   
 
166    class SymmetricParameters implements SymmetricCipherParameters
167    { }
168   
 
169  0 toggle @Test
170    public void testCipherWithAsymmetricParameters() throws Exception
171    {
172  0 thrown.expect(IllegalArgumentException.class);
173  0 thrown.expectMessage("Unexpected parameters received for a asymmetric cipher: "
174    + SymmetricParameters.class.getName());
175  0 Test failure here factory.getInstance(true, new SymmetricParameters());
176    }
177    }