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

File AbstractSymmetricCipherFactoryTest.java

 
testCipherWithWrongParameters: Invalid parameters for cipher: org.xwiki.crypto.cipher.in...
testCipherWithWrongParameters: Invalid parameters for cipher: org.xwiki.crypto.cipher.in...
testCipherWithAsymmetricParameters: Unexpected parameters received for a symmetric cipher: or...
testCipherWithAsymmetricParameters: Unexpected parameters received for a symmetric cipher: or...
testCipherWithWrongParameters: Invalid parameters for cipher: org.xwiki.crypto.cipher.in...
testCipherWithAsymmetricParameters: Unexpected parameters received for a symmetric cipher: or...
testCipherWithAsymmetricParameters: Unexpected parameters received for a symmetric cipher: or...
testCipherWithWrongParameters: Invalid parameters for cipher: org.xwiki.crypto.cipher.in...
testCipherWithWrongParameters: Invalid parameters for cipher: org.xwiki.crypto.cipher.in...
testCipherWithAsymmetricParameters: Unexpected parameters received for a symmetric cipher: or...
testCipherWithWrongParameters: Invalid parameters for cipher: org.xwiki.crypto.cipher.in...
testCipherWithWrongParameters: Invalid parameters for cipher: org.xwiki.crypto.cipher.in...
testCipherWithAsymmetricParameters: Unexpected parameters received for a symmetric cipher: or...
testCipherWithAsymmetricParameters: Unexpected parameters received for a symmetric cipher: or...
 

Code metrics

14
119
19
3
362
257
28
0.24
6.26
6.33
1.47

Classes

Class Line # Actions
AbstractSymmetricCipherFactoryTest 49 119 0% 28 10
0.9342105493.4%
AbstractSymmetricCipherFactoryTest.WrongParameters 340 0 - 0 0
-1.0 -
AbstractSymmetricCipherFactoryTest.AsymmetricParameters 351 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 85 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   
21    package org.xwiki.crypto.cipher.internal.symmetric.factory;
22   
23    import java.io.ByteArrayInputStream;
24    import java.io.ByteArrayOutputStream;
25    import java.io.IOException;
26    import java.io.InputStream;
27    import java.io.OutputStream;
28   
29    import org.junit.BeforeClass;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.junit.rules.ExpectedException;
33    import org.xwiki.crypto.cipher.Cipher;
34    import org.xwiki.crypto.cipher.CipherFactory;
35    import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricCipherParameters;
36    import org.xwiki.crypto.params.cipher.symmetric.SymmetricCipherParameters;
37   
38    import static org.hamcrest.CoreMatchers.equalTo;
39    import static org.hamcrest.CoreMatchers.is;
40    import static org.hamcrest.CoreMatchers.not;
41    import static org.hamcrest.CoreMatchers.nullValue;
42    import static org.junit.Assert.assertThat;
43   
44    /**
45    * Abstract base class for cipher tests.
46    *
47    * @version $Id: bdadb0665b244aba0876b1fa954a8166e2ce23cf $
48    */
 
49    public abstract class AbstractSymmetricCipherFactoryTest
50    {
51    /** Length = 272 byte = 17 * 16 */
52    private static final String TEXT = "Congress shall make no law respecting an establishment of religion, or "
53    + "prohibiting the free exercise thereof; or abridging the freedom of speech, "
54    + "or of the press; or the right of the people peaceably to assemble, and to "
55    + "petition the Government for a redress of grievances.";
56   
57    protected static final byte[] BYTES = TEXT.getBytes();
58   
59    /** Length = 113 byte = 7 * 16 + 1 */
60    private static final String ANOTHER_TEXT = "The length of this text is 113 byte. This is 1 byte more "
61    + "than a multiple of block size for 128 bit block ciphers.";
62   
63    protected static final byte[] ANOTHER_BYTES = ANOTHER_TEXT.getBytes();
64   
65    /** A poor 8bits key. */
66    protected static final byte[] KEY8 = {
67    0x58, 0x57, 0x69, 0x6b, 0x69, 0x20, 0x69, 0x73 };
68   
69    /** A poor 16bits key. */
70    protected static final byte[] KEY16 = {
71    0x58, 0x57, 0x69, 0x6b, 0x69, 0x20, 0x69, 0x73,
72    0x20, 0x74, 0x68, 0x65, 0x20, 0x6b, 0x65, 0x79 };
73   
74    /** A poor 32bits key. */
75    protected static final byte[] KEY32 = {
76    0x58, 0x57, 0x69, 0x6b, 0x69, 0x20, 0x69, 0x73,
77    0x20, 0x74, 0x68, 0x65, 0x20, 0x6b, 0x65, 0x79,
78    0x79, 0x65, 0x6b, 0x20, 0x65, 0x68, 0x74, 0x20,
79    0x73, 0x69, 0x20, 0x69, 0x6b, 0x69, 0x57, 0x58 };
80   
81    /** A poor 8bits IV. */
82    protected static final byte[] IV8 = { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 };
83   
84    /** A poor 16bits IV. */
85    protected static final byte[] IV16 = {
86    0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12,
87    0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 };
88   
89    protected CipherFactory factory;
90   
91    protected String CIPHER_ALGO;
92    protected int BLOCK_SIZE;
93    protected int KEY_SIZE;
94    protected int[] SUPPORTED_KEY_SIZE;
95   
96    protected int BYTES_ENCRYPTED_SIZE;
97    protected int ANOTHER_BYTES_ENCRYPTED_SIZE;
98   
 
99  7 toggle @Test
100    public void testGetCipherFactoryProperties() throws Exception
101    {
102  7 assertThat(factory.getCipherAlgorithmName(),equalTo(CIPHER_ALGO));
103  7 assertThat(factory.getIVSize(),equalTo(BLOCK_SIZE));
104  7 assertThat(factory.getKeySize(),equalTo(KEY_SIZE));
105  7 assertThat(factory.getSupportedKeySizes(),equalTo(SUPPORTED_KEY_SIZE));
106    }
107   
 
108  7 toggle @Test
109    public void testGetCipherProperties() throws Exception
110    {
111  7 Cipher cipher = getCipher(true);
112   
113  7 assertThat(cipher.getAlgorithmName(),equalTo(CIPHER_ALGO));
114  7 assertThat(cipher.getOutputBlockSize(),equalTo(BLOCK_SIZE));
115  7 assertThat(cipher.isForEncryption(),is(true));
116   
117  7 cipher = getCipher(false);
118  7 assertThat(cipher.getAlgorithmName(),equalTo(CIPHER_ALGO));
119  7 assertThat(cipher.getOutputBlockSize(),equalTo(BLOCK_SIZE));
120  7 assertThat(cipher.isForEncryption(),is(false));
121    }
122   
123    private static Cipher encryptCipher;
124    private static Cipher decryptCipher;
125   
126    protected static byte[] encrypted;
127    protected static byte[] anotherEncrypted;
128   
 
129  7 toggle @BeforeClass
130    public static void cleanUpCaches() {
131  7 encryptCipher = null;
132  7 decryptCipher = null;
133  7 encrypted = null;
134  7 anotherEncrypted = null;
135    }
136   
137    /**
138    * Cache the cipher to avoid recreating it between test uselessly since cypher setup may be long.
139    * @return a cipher for encryption or decryption using KEY32 and IV16.
140    */
 
141  122 toggle protected Cipher getCipher(boolean forEncryption)
142    {
143  122 return getCipher(forEncryption, false);
144    }
145   
146    abstract Cipher getCipherInstance(boolean forEncryption);
147   
 
148  122 toggle private Cipher getCipher(boolean forEncryption, boolean reset)
149    {
150  122 Cipher cipher = (forEncryption) ? encryptCipher : decryptCipher;
151   
152  122 if (reset || cipher == null) {
153  14 cipher = getCipherInstance(forEncryption);
154  14 if (forEncryption) {
155  7 encryptCipher = cipher;
156    } else {
157  7 decryptCipher = cipher;
158    }
159    }
160  122 return cipher;
161    }
162   
163    /**
164    * Cache the encrypted results for BYTES, for faster comparison, or use initialized constant.
165    * @return the encrypted bytes of BYTES.
166    */
 
167  64 toggle protected byte[] getEncrypted() throws Exception
168    {
169  64 if (encrypted == null) {
170  5 encrypted = getCipher(true).doFinal(BYTES);
171    }
172  64 return encrypted;
173    }
174   
175    /**
176    * Cache the encrypted results for ANOTHER_BYTES, for faster comparison, or use initialized constant.
177    * @return the encrypted bytes of ANOTHER_BYTES.
178    */
 
179  64 toggle protected byte[] getAnotherEncrypted() throws Exception
180    {
181  64 if (anotherEncrypted == null) {
182  5 anotherEncrypted = getCipher(true).doFinal(ANOTHER_BYTES);
183    }
184  64 return anotherEncrypted;
185    }
186   
 
187  7 toggle @Test
188    public void testCipherOneShotEncryption() throws Exception
189    {
190  7 Cipher cipher = getCipher(true);
191   
192  7 assertThat(getEncrypted().length,equalTo(BYTES_ENCRYPTED_SIZE));
193  7 assertThat(cipher.doFinal(BYTES),equalTo(getEncrypted()));
194   
195  7 assertThat(getAnotherEncrypted().length,equalTo(ANOTHER_BYTES_ENCRYPTED_SIZE));
196  7 assertThat(cipher.doFinal(ANOTHER_BYTES),equalTo(getAnotherEncrypted()));
197    }
198   
 
199  7 toggle @Test
200    public void testCipherOneShotDecryption() throws Exception
201    {
202  7 Cipher cipher = getCipher(false);
203   
204  7 byte[] result = cipher.doFinal(getEncrypted());
205  7 assertThat(result.length, equalTo(BYTES.length));
206  7 assertThat(result,equalTo(BYTES));
207   
208  7 result = cipher.doFinal(getAnotherEncrypted());
209  7 assertThat(result.length, equalTo(ANOTHER_BYTES.length));
210  7 assertThat(result, equalTo(ANOTHER_BYTES));
211    }
212   
 
213  28 toggle private byte[] getProgressive(boolean forEncryption, byte[] bytes, int size) throws Exception
214    {
215  28 Cipher cipher = getCipher(forEncryption);
216  28 byte[] result = new byte[size];
217  28 byte[] tmp;
218  28 int len = 0;
219   
220  28 tmp = cipher.update(bytes, 0, BLOCK_SIZE + 1);
221  28 assertThat(tmp, not(nullValue()));
222  28 System.arraycopy(tmp, 0, result, 0, len = tmp.length);
223   
224  28 assertThat(cipher.update(bytes, BLOCK_SIZE + 1, BLOCK_SIZE - 1), nullValue());
225   
226  28 tmp = cipher.update(bytes, BLOCK_SIZE * 2, 1);
227  28 assertThat(tmp, not(nullValue()));
228  28 System.arraycopy(tmp, 0, result, len, tmp.length);
229  28 len += tmp.length;
230   
231  28 tmp = cipher.update(bytes, ((BLOCK_SIZE * 2) + 1), bytes.length - ((BLOCK_SIZE * 2) + 1));
232  28 assertThat(tmp, not(nullValue()));
233  28 System.arraycopy(tmp, 0, result, len, tmp.length);
234  28 len += tmp.length;
235   
236  28 tmp = cipher.doFinal();
237  28 if (forEncryption || tmp != null) {
238  21 assertThat(tmp, not(nullValue()));
239  21 System.arraycopy(tmp, 0, result, len, tmp.length);
240  21 len += tmp.length;
241    }
242   
243  28 return result;
244    }
245   
 
246  7 toggle @Test
247    public void testCipherProgressiveEncryption() throws Exception
248    {
249  7 assertThat(getProgressive(true, BYTES, BYTES_ENCRYPTED_SIZE), equalTo(getEncrypted()));
250  7 assertThat(getProgressive(true, ANOTHER_BYTES, ANOTHER_BYTES_ENCRYPTED_SIZE), equalTo(getAnotherEncrypted()));
251    }
252   
 
253  7 toggle @Test
254    public void testCipherProgressiveDecryption() throws Exception
255    {
256  7 assertThat(getProgressive(false, getEncrypted(), BYTES.length), equalTo(BYTES));
257  7 assertThat(getProgressive(false, getAnotherEncrypted(), ANOTHER_BYTES.length), equalTo(ANOTHER_BYTES));
258    }
259   
 
260  7 toggle @Test
261    public void testCipherOutputStreamEncryption() throws Exception
262    {
263  7 ByteArrayOutputStream baos = new ByteArrayOutputStream(BYTES_ENCRYPTED_SIZE);
264  7 OutputStream encos = getCipher(true).getOutputStream(baos);
265  7 encos.write(BYTES);
266  7 encos.close();
267   
268  7 assertThat(baos.toByteArray(), equalTo(getEncrypted()));
269   
270  7 baos = new ByteArrayOutputStream(ANOTHER_BYTES_ENCRYPTED_SIZE);
271  7 encos = getCipher(true).getOutputStream(baos);
272  7 encos.write(ANOTHER_BYTES);
273  7 encos.close();
274   
275  7 assertThat(baos.toByteArray(), equalTo(getAnotherEncrypted()));
276    }
277   
 
278  7 toggle @Test
279    public void testCipherOutputStreamDecryption() throws Exception
280    {
281  7 ByteArrayOutputStream baos = new ByteArrayOutputStream(BYTES.length);
282  7 OutputStream encos = getCipher(false).getOutputStream(baos);
283  7 encos.write(getEncrypted());
284  7 encos.close();
285   
286  7 assertThat(baos.toByteArray(), equalTo(BYTES));
287   
288  7 baos = new ByteArrayOutputStream(ANOTHER_BYTES.length);
289  7 encos = getCipher(false).getOutputStream(baos);
290  7 encos.write(getAnotherEncrypted());
291  7 encos.close();
292   
293  7 assertThat(baos.toByteArray(), equalTo(ANOTHER_BYTES));
294    }
295   
 
296  28 toggle private int readAll(InputStream decis, byte[] out) throws IOException
297    {
298  28 int readLen = 0, len = 0;
299  ? while( (readLen = decis.read(out, len, BLOCK_SIZE + 1)) > 0 ) {
300  555 len += readLen;
301    }
302  28 decis.close();
303  28 return len;
304    }
305   
 
306  7 toggle @Test
307    public void testCipheInputStreamEncryption() throws Exception
308    {
309  7 ByteArrayInputStream bais = new ByteArrayInputStream(BYTES);
310  7 InputStream decis = getCipher(true).getInputStream(bais);
311  7 byte[] buf = new byte[BYTES_ENCRYPTED_SIZE];
312  7 assertThat(readAll(decis, buf), equalTo(BYTES_ENCRYPTED_SIZE));
313  7 assertThat(buf, equalTo(getEncrypted()));
314   
315  7 bais = new ByteArrayInputStream(ANOTHER_BYTES);
316  7 decis = getCipher(true).getInputStream(bais);
317  7 buf = new byte[ANOTHER_BYTES_ENCRYPTED_SIZE];
318  7 assertThat(readAll(decis, buf), equalTo(ANOTHER_BYTES_ENCRYPTED_SIZE));
319  7 assertThat(buf, equalTo(getAnotherEncrypted()));
320    }
321   
 
322  7 toggle @Test
323    public void testCipheInputStreamDecryption() throws Exception
324    {
325  7 ByteArrayInputStream bais = new ByteArrayInputStream(getEncrypted());
326  7 InputStream decis = getCipher(false).getInputStream(bais);
327  7 byte[] buf = new byte[BYTES.length];
328  7 assertThat(readAll(decis, buf), equalTo(BYTES.length));
329  7 assertThat(buf, equalTo(BYTES));
330   
331  7 bais = new ByteArrayInputStream(getAnotherEncrypted());
332  7 decis = getCipher(false).getInputStream(bais);
333  7 buf = new byte[ANOTHER_BYTES.length];
334  7 assertThat(readAll(decis, buf), equalTo(ANOTHER_BYTES.length));
335  7 assertThat(buf, equalTo(ANOTHER_BYTES));
336    }
337   
338    @Rule public ExpectedException thrown = ExpectedException.none();
339   
 
340    class WrongParameters implements SymmetricCipherParameters
341    { }
342   
 
343  0 toggle @Test
344    public void testCipherWithWrongParameters() throws Exception
345    {
346  0 thrown.expect(IllegalArgumentException.class);
347  0 thrown.expectMessage("Invalid parameters for cipher: " + WrongParameters.class.getName());
348  0 Test failure here factory.getInstance(true, new WrongParameters());
349    }
350   
 
351    class AsymmetricParameters implements AsymmetricCipherParameters
352    { }
353   
 
354  0 toggle @Test
355    public void testCipherWithAsymmetricParameters() throws Exception
356    {
357  0 thrown.expect(IllegalArgumentException.class);
358  0 thrown.expectMessage("Unexpected parameters received for a symmetric cipher: "
359    + AsymmetricParameters.class.getName());
360  0 Test failure here factory.getInstance(true, new AsymmetricParameters());
361    }
362    }