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

File BcRc2CbcPaddedCipherFactoryTest.java

 

Code metrics

0
23
5
1
94
61
5
0.22
4.6
5
1

Classes

Class Line # Actions
BcRc2CbcPaddedCipherFactoryTest 35 23 0% 5 0
1.0100%
 

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.cipher.internal.symmetric.factory;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.crypto.cipher.Cipher;
26    import org.xwiki.crypto.cipher.CipherFactory;
27    import org.xwiki.crypto.params.cipher.symmetric.RC2KeyParameters;
28    import org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters;
29    import org.xwiki.test.mockito.MockitoComponentMockingRule;
30   
31    import static org.hamcrest.CoreMatchers.equalTo;
32    import static org.hamcrest.CoreMatchers.not;
33    import static org.junit.Assert.assertThat;
34   
 
35    public class BcRc2CbcPaddedCipherFactoryTest extends AbstractSymmetricCipherFactoryTest
36    {
37    @Rule
38    public final MockitoComponentMockingRule<CipherFactory> mocker =
39    new MockitoComponentMockingRule<CipherFactory>(BcRc2CbcPaddedCipherFactory.class);
40   
 
41  14 toggle {
42  14 CIPHER_ALGO = "RC2/CBC/PKCS5Padding";
43  14 BLOCK_SIZE = 8;
44  14 KEY_SIZE = 64;
45  14 SUPPORTED_KEY_SIZE = new int[] { 1, 2 , 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
46    23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
47    50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64 };
48   
49  14 BYTES_ENCRYPTED_SIZE = ((BYTES.length / BLOCK_SIZE) * BLOCK_SIZE) + BLOCK_SIZE;
50  14 ANOTHER_BYTES_ENCRYPTED_SIZE = ((ANOTHER_BYTES.length / BLOCK_SIZE) * BLOCK_SIZE) + BLOCK_SIZE;
51    }
52   
 
53  14 toggle @Before
54    public void configure() throws Exception
55    {
56  14 factory = mocker.getComponentUnderTest();
57    }
58   
 
59  2 toggle @Override
60    Cipher getCipherInstance(boolean forEncryption)
61    {
62  2 return factory.getInstance(forEncryption, new KeyWithIVParameters(KEY32, IV8));
63    }
64   
 
65  1 toggle @Test
66    public void testRC2EffectiveBitsEncription() throws Exception
67    {
68  1 Cipher cipher27 = factory.getInstance(true, new KeyWithIVParameters(new RC2KeyParameters(KEY32, 27), IV8));
69   
70  1 byte[] result = cipher27.doFinal(BYTES);
71  1 assertThat(result.length,equalTo(BYTES_ENCRYPTED_SIZE));
72  1 assertThat(result,not(equalTo(getEncrypted())));
73   
74  1 result = cipher27.doFinal(ANOTHER_BYTES);
75  1 assertThat(result.length,equalTo(ANOTHER_BYTES_ENCRYPTED_SIZE));
76  1 assertThat(result,not(equalTo(getAnotherEncrypted())));
77    }
78   
 
79  1 toggle @Test
80    public void testRC2EffectiveBitsDecription() throws Exception
81    {
82  1 Cipher cipher27Enc = factory.getInstance(true, new KeyWithIVParameters(new RC2KeyParameters(KEY32, 27), IV8));
83  1 Cipher cipher27 = factory.getInstance(false, new KeyWithIVParameters(new RC2KeyParameters(KEY32, 27), IV8));
84   
85  1 byte[] result = cipher27.doFinal(cipher27Enc.doFinal(BYTES));
86  1 assertThat(result.length, equalTo(BYTES.length));
87  1 assertThat(result,equalTo(BYTES));
88   
89  1 result = cipher27.doFinal(cipher27Enc.doFinal(ANOTHER_BYTES));
90  1 assertThat(result.length, equalTo(ANOTHER_BYTES.length));
91  1 assertThat(result, equalTo(ANOTHER_BYTES));
92    }
93   
94    }