1 package org.apache.commons.jcs3.utils.serialization;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertThrows;
23
24 import java.io.IOException;
25
26 import junit.framework.TestCase;
27
28
29
30
31 public class EncryptingSerializerUnitTest
32 extends TestCase
33 {
34 private EncryptingSerializer serializer;
35
36 @Override
37 protected void setUp() throws Exception
38 {
39 this.serializer = new EncryptingSerializer();
40 this.serializer.setPreSharedKey("my_secret_key");
41 }
42
43
44
45
46
47
48
49 public void testDeserialize_NullInput()
50 throws IOException, ClassNotFoundException
51 {
52
53 final Object result = serializer.deSerialize( null, null );
54
55
56 assertNull( "Should have nothing.", result );
57 }
58
59
60
61
62
63
64
65
66 public void testSimpleBackAndForth()
67 throws Exception
68 {
69
70 final String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
71 final String after = serializer.deSerialize( serializer.serialize( before ), null );
72
73
74 assertEquals( "Before and after should be the same.", before, after );
75 }
76
77
78
79
80
81
82
83
84 public void testGCMBackAndForth()
85 throws Exception
86 {
87 this.serializer.setAesCipherTransformation("AES/GCM/NoPadding");
88
89
90 final String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
91 final String after = serializer.deSerialize( serializer.serialize( before ), null );
92
93
94 assertEquals( "Before and after should be the same.", before, after );
95 }
96
97
98
99
100
101
102 public void testDifferentKey()
103 throws Exception
104 {
105
106 final String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
107 byte[] serialized = serializer.serialize(before);
108 serializer.setPreSharedKey("another_key");
109
110 assertThrows(IOException.class, () -> serializer.deSerialize(serialized, null));
111 }
112
113
114
115
116
117
118 public void testSerialize_NullInput()
119 throws Exception
120 {
121 final String before = null;
122
123
124 final byte[] serialized = serializer.serialize( before );
125 final String after = (String) serializer.deSerialize( serialized, null );
126
127
128 assertNull( "Should have nothing. after =" + after, after );
129 }
130 }