1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (28) |
Complexity: 5 |
Complexity Density: 0.22 |
|
35 |
|
public class BcRc2CbcPaddedCipherFactoryTest extends AbstractSymmetricCipherFactoryTest |
36 |
|
{ |
37 |
|
@Rule |
38 |
|
public final MockitoComponentMockingRule<CipherFactory> mocker = |
39 |
|
new MockitoComponentMockingRule<CipherFactory>(BcRc2CbcPaddedCipherFactory.class); |
40 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
41 |
14 |
{... |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
53 |
14 |
@Before... |
54 |
|
public void configure() throws Exception |
55 |
|
{ |
56 |
14 |
factory = mocker.getComponentUnderTest(); |
57 |
|
} |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
59 |
2 |
@Override... |
60 |
|
Cipher getCipherInstance(boolean forEncryption) |
61 |
|
{ |
62 |
2 |
return factory.getInstance(forEncryption, new KeyWithIVParameters(KEY32, IV8)); |
63 |
|
} |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
65 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
79 |
1 |
@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 |
|
} |