View Javadoc
1   package org.apache.commons.jcs3.utils.serialization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertThrows;
23  
24  import java.io.IOException;
25  
26  import junit.framework.TestCase;
27  
28  /**
29   * Tests the encrypting serializer.
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       * Verify that we don't get any errors for null input.
45       * <p>
46       * @throws ClassNotFoundException
47       * @throws IOException
48       */
49      public void testDeserialize_NullInput()
50          throws IOException, ClassNotFoundException
51      {
52          // DO WORK
53          final Object result = serializer.deSerialize( null, null );
54  
55          // VERIFY
56          assertNull( "Should have nothing.", result );
57      }
58  
59      /**
60       * Test simple back and forth with a string.
61       * <p>
62       * ))&lt;=&gt;((
63       * <p>
64       * @throws Exception on error
65       */
66      public void testSimpleBackAndForth()
67          throws Exception
68      {
69          // DO WORK
70          final String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
71          final String after = serializer.deSerialize( serializer.serialize( before ), null );
72  
73          // VERIFY
74          assertEquals( "Before and after should be the same.", before, after );
75      }
76  
77      /**
78       * Test simple back and forth with a string.
79       * <p>
80       * ))&lt;=&gt;((
81       * <p>
82       * @throws Exception on error
83       */
84      public void testGCMBackAndForth()
85          throws Exception
86      {
87          this.serializer.setAesCipherTransformation("AES/GCM/NoPadding");
88  
89          // DO WORK
90          final String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
91          final String after = serializer.deSerialize( serializer.serialize( before ), null );
92  
93          // VERIFY
94          assertEquals( "Before and after should be the same.", before, after );
95      }
96  
97      /**
98       * Test different key.
99       * <p>
100      * @throws Exception on error
101      */
102     public void testDifferentKey()
103         throws Exception
104     {
105         // DO WORK
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      * Test serialization with a null object. Verify that we don't get an error.
115      * <p>
116      * @throws Exception on error
117      */
118     public void testSerialize_NullInput()
119         throws Exception
120     {
121         final String before = null;
122 
123         // DO WORK
124         final byte[] serialized = serializer.serialize( before );
125         final String after = (String) serializer.deSerialize( serialized, null );
126 
127         // VERIFY
128         assertNull( "Should have nothing. after =" + after, after );
129     }
130 }