View Javadoc
1   package org.jastacry.layer;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.io.ByteArrayInputStream;
6   import java.io.ByteArrayOutputStream;
7   import java.io.InputStream;
8   import java.io.OutputStream;
9   
10  import org.jastacry.JastacryException;
11  import org.junit.jupiter.api.AfterEach;
12  import org.junit.jupiter.api.BeforeEach;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test class for AES Layer.
17   *
18   * @author Kai Kretschmann
19   *
20   */
21  public class TestLayerAesCbc
22  {
23      /**
24       * Test data to play with.
25       */
26      private final String testdata = "The quick brown fox jumps over the lazy dog.";
27  
28      /**
29       * Layer to test.
30       */
31      private AesCbcLayer layerEncrypt = null;
32  
33      /**
34       * Layer to test.
35       */
36      private AesCbcLayer layerDecrypt = null;
37  
38      /**
39       * Init value for random layer.
40       */
41      private static final String INITVALUE = "Passwort";
42  
43      /**
44       * Test Before method.
45       *
46       * @throws Exception
47       *             in case of error.
48       */
49      @BeforeEach
50      public void setUp() throws Exception
51      {
52          layerEncrypt = new AesCbcLayer();
53          layerEncrypt.init(INITVALUE);
54  
55          layerDecrypt = new AesCbcLayer();
56          layerDecrypt.init(INITVALUE);
57      }
58  
59      /**
60       * Test After method.
61       *
62       * @throws Exception
63       *             in case off error
64       */
65      @AfterEach
66      public void tearDown() throws Exception
67      {
68          layerEncrypt = null;
69          layerDecrypt = null;
70      }
71  
72      /**
73       * Testcase testEncDecStream.
74       *
75       * @throws JastacryException
76       *             in case off error
77       */
78      @Test
79      public void testEncDecStream() throws JastacryException
80      {
81          byte[] buf = testdata.getBytes();
82          final InputStream isEncode = new ByteArrayInputStream(buf);
83          final ByteArrayOutputStream osEncode = new ByteArrayOutputStream();
84          layerEncrypt.encStream(isEncode, osEncode);
85          buf = osEncode.toByteArray();
86  
87          final InputStream isDecode = new ByteArrayInputStream(buf);
88          final OutputStream osDecode = new ByteArrayOutputStream();
89          layerDecrypt.decStream(isDecode, osDecode);
90          assertEquals("decoding differs", testdata, osDecode.toString());
91  
92      }
93  
94      /**
95       * Testcase testToString.
96       */
97      @Test
98      public void testToString()
99      {
100         assertEquals("Layer name mismatch", AesCbcLayer.LAYERNAME, layerEncrypt.toString());
101     }
102 
103     /**
104      * Testcase equals.
105      */
106     @Test
107     public void testEquals()
108     {
109         AesCbcLayer l1 = new AesCbcLayer();
110         AesCbcLayer l2 = new AesCbcLayer();
111         l1.init(INITVALUE);
112         l2.init(INITVALUE);
113         assertEquals("Layer object equal", l1, l2);
114     }
115 
116     /**
117      * Testcase equals.
118      */
119     @Test
120     public void testEqualsSame()
121     {
122         AesCbcLayer l1 = new AesCbcLayer();
123         l1.init(INITVALUE);
124         assertEquals("Layer object same", l1, l1);
125     }
126 
127     /**
128      * Testcase equals.
129      */
130     @Test
131     public void testNotEqualsNull()
132     {
133         AesCbcLayer l1 = new AesCbcLayer();
134         l1.init(INITVALUE);
135         Object o = null;
136         assertEquals("Layer object null unequal", false, l1.equals(o));
137     }
138 
139     /**
140      * Testcase equals.
141      */
142     @Test
143     public void testNotEqualsWrongclass()
144     {
145         AesCbcLayer l1 = new AesCbcLayer();
146         l1.init(INITVALUE);
147         Object o = new Object();
148         assertEquals("Layer object wrong class unequal", false, l1.equals(o));
149     }
150 
151     /**
152      * Testcase hashcode.
153      */
154     @Test
155     public void testHashcode()
156     {
157         AesCbcLayer l1 = new AesCbcLayer();
158         AesCbcLayer l2 = new AesCbcLayer();
159         l1.init(INITVALUE);
160         l2.init(INITVALUE);
161         assertEquals("Layer hash equal", l1.hashCode(), l2.hashCode());
162     }
163 
164 }