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

File BcRsaSsaPssSignerFactoryTest.java

 

Code metrics

2
57
16
1
226
169
17
0.3
3.56
16
1.06

Classes

Class Line # Actions
BcRsaSsaPssSignerFactoryTest 50 57 0% 17 2
0.9733333697.3%
 

Contributing tests

This file is covered by 13 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.signer.internal.factory;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.io.OutputStream;
26   
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.crypto.internal.asymmetric.keyfactory.BcRSAKeyFactory;
31    import org.xwiki.crypto.internal.digest.factory.BcSHA1DigestFactory;
32    import org.xwiki.crypto.internal.digest.factory.BcSHA224DigestFactory;
33    import org.xwiki.crypto.internal.digest.factory.BcSHA256DigestFactory;
34    import org.xwiki.crypto.internal.digest.factory.BcSHA384DigestFactory;
35    import org.xwiki.crypto.internal.digest.factory.BcSHA512DigestFactory;
36    import org.xwiki.crypto.internal.encoder.Base64BinaryStringEncoder;
37    import org.xwiki.crypto.signer.Signer;
38    import org.xwiki.crypto.signer.SignerFactory;
39    import org.xwiki.crypto.signer.params.PssSignerParameters;
40    import org.xwiki.test.annotation.ComponentList;
41    import org.xwiki.test.mockito.MockitoComponentMockingRule;
42   
43    import static org.hamcrest.CoreMatchers.equalTo;
44    import static org.junit.Assert.assertThat;
45    import static org.junit.Assert.assertTrue;
46   
47    @ComponentList({Base64BinaryStringEncoder.class, BcRSAKeyFactory.class, BcSHA1DigestFactory.class,
48    BcSHA224DigestFactory.class, BcSHA256DigestFactory.class, BcSHA384DigestFactory.class,
49    BcSHA512DigestFactory.class})
 
50    public class BcRsaSsaPssSignerFactoryTest extends AbstractRsaSignerFactoryTest
51    {
52    @Rule
53    public final MockitoComponentMockingRule<SignerFactory> mocker =
54    new MockitoComponentMockingRule<SignerFactory>(BcRsaSsaPssSignerFactory.class);
55   
56    private SignerFactory factory;
57   
 
58  13 toggle @Before
59    public void configure() throws Exception
60    {
61  13 factory = mocker.getComponentUnderTest();
62  13 setupTest(mocker);
63    }
64   
 
65  1 toggle @Test
66    public void testDefaultSignatureVerification() throws Exception
67    {
68  1 runTestSignatureVerification(
69    factory.getInstance(true, privateKey),
70    factory.getInstance(false, publicKey)
71    );
72    }
73   
 
74  1 toggle @Test
75    public void testSha224SignatureVerification() throws Exception
76    {
77  1 runTestSignatureVerification(
78    factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-224", -1)),
79    factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-224", -1))
80    );
81    }
82   
 
83  1 toggle @Test
84    public void testSha256SignatureVerification() throws Exception
85    {
86  1 runTestSignatureVerification(
87    factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-256", -1)),
88    factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-256", -1))
89    );
90    }
91   
 
92  1 toggle @Test
93    public void testSha384SignatureVerification() throws Exception
94    {
95  1 runTestSignatureVerification(
96    factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-384", -1)),
97    factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-384", -1))
98    );
99    }
100   
 
101  1 toggle @Test
102    public void testSha512SignatureVerification() throws Exception
103    {
104  1 runTestSignatureVerification(
105    factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-512", -1)),
106    factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-512", -1))
107    );
108    }
109   
 
110  1 toggle @Test
111    public void testEncodedDefaultSignatureVerification() throws Exception
112    {
113  1 Signer signer = factory.getInstance(true, privateKey);
114  1 Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded());
115   
116  1 runTestSignatureVerification(signer, verifier);
117    }
118   
 
119  1 toggle @Test
120    public void testEncodedSha224SignatureVerification() throws Exception
121    {
122  1 Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-224", -1));
123  1 Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded());
124   
125  1 runTestSignatureVerification(signer, verifier);
126    }
127   
 
128  1 toggle @Test
129    public void testEncodedSha256SignatureVerification() throws Exception
130    {
131  1 Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-256", -1));
132  1 Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded());
133   
134  1 runTestSignatureVerification(signer, verifier);
135    }
136   
 
137  1 toggle @Test
138    public void testEncodedSha384SignatureVerification() throws Exception
139    {
140  1 Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-384", -1));
141  1 Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded());
142   
143  1 runTestSignatureVerification(signer, verifier);
144    }
145   
 
146  1 toggle @Test
147    public void testEncodedSha512SignatureVerification() throws Exception
148    {
149  1 Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-512", -1));
150  1 Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded());
151   
152  1 runTestSignatureVerification(signer, verifier);
153    }
154   
 
155  2 toggle private void progressiveUpdateSignature(Signer signer, byte[] bytes, int blockSize) throws Exception
156    {
157  2 signer.update(bytes, 0, blockSize + 1);
158  2 signer.update(bytes, blockSize + 1, blockSize - 1);
159  2 signer.update(bytes, blockSize * 2, 1);
160  2 signer.update(bytes[(blockSize * 2) + 1]);
161  2 signer.update(bytes, ((blockSize * 2) + 2), bytes.length - ((blockSize * 2) + 2));
162    }
163   
 
164  1 toggle @Test
165    public void testProgressiveSignatureVerification() throws Exception
166    {
167  1 Signer signer = factory.getInstance(true, privateKey);
168  1 progressiveUpdateSignature(signer, text, 17);
169   
170  1 byte[] signature = signer.generate();
171   
172  1 Signer verifier = factory.getInstance(true, publicKey);
173  1 progressiveUpdateSignature(verifier, text, 15);
174   
175  1 assertTrue(verifier.verify(signature));
176    }
177   
 
178  1 toggle @Test
179    public void testPartialBufferVerification() throws Exception
180    {
181  1 Signer signer = factory.getInstance(true, privateKey);
182   
183  1 byte[] source = new byte[text.length * 2];
184  1 System.arraycopy(text, 0, source, text.length - 10, text.length);
185   
186  1 byte[] signature = signer.generate(source, text.length - 10, text.length);
187   
188  1 byte[] sign = new byte[signature.length * 2];
189  1 System.arraycopy(signature, 0, sign, signature.length - 5, signature.length);
190   
191  1 Signer verifier = factory.getInstance(true, publicKey);
192  1 assertTrue(
193    verifier.verify(sign, signature.length - 5, signature.length, source, text.length - 10, text.length));
194    }
195   
 
196  1 toggle private int readAll(InputStream decis, byte[] out) throws IOException
197    {
198  1 int readLen = 0, len = 0;
199  ? while( (readLen = decis.read(out, len, Math.min(15, out.length - len))) > 0 ) {
200  19 len += readLen;
201    }
202  1 decis.close();
203  1 return len;
204    }
205   
 
206  1 toggle @Test
207    public void testStreamSignatureVerification() throws Exception
208    {
209  1 ByteArrayInputStream bais = new ByteArrayInputStream(text);
210   
211  1 Signer signer = factory.getInstance(true, privateKey);
212  1 InputStream input = signer.getInputStream(bais);
213   
214  1 byte[] buf = new byte[text.length];
215  1 assertThat(readAll(input, buf), equalTo(text.length));
216  1 assertThat(buf, equalTo(text));
217   
218  1 byte[] signature = signer.generate();
219   
220  1 Signer verifier = factory.getInstance(false, publicKey);
221  1 OutputStream output = verifier.getOutputStream();
222   
223  1 output.write(text);
224  1 assertTrue(verifier.verify(signature));
225    }
226    }