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 MD5DES Layer.
17   *
18   * @author Kai Kretschmann
19   *
20   */
21  public class TestLayerMd5Des
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 Md5DesLayer layerEncrypt = null;
32  
33      /**
34       * Layer to test.
35       */
36      private Md5DesLayer layerDecrypt = null;
37  
38      /**
39       * Init value for random layer.
40       */
41      private static final String INITVALUE = "Passwort123";
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 Md5DesLayer();
53          layerEncrypt.init(INITVALUE);
54          layerDecrypt = new Md5DesLayer();
55          layerDecrypt.init(INITVALUE);
56      }
57  
58      /**
59       * Test After method.
60       *
61       * @throws Exception
62       *             in case off error
63       */
64      @AfterEach
65      public void tearDown() throws Exception
66      {
67          layerEncrypt = null;
68          layerDecrypt = null;
69      }
70  
71      /**
72       * Testcase testEncDecStream.
73       *
74       * @throws JastacryException
75       *             in case off error
76       */
77      @Test
78      // TestLink(externalId = "JAS-4")
79      public void testEncDecStream() throws JastacryException
80      {
81          // tooling.listProviders();
82  
83          byte[] buf = testdata.getBytes();
84          final InputStream isEncode = new ByteArrayInputStream(buf);
85          final ByteArrayOutputStream osEncode = new ByteArrayOutputStream();
86          layerEncrypt.encStream(isEncode, osEncode);
87          buf = osEncode.toByteArray();
88  
89          final InputStream isDecode = new ByteArrayInputStream(buf);
90          final OutputStream osDecode = new ByteArrayOutputStream();
91          layerDecrypt.decStream(isDecode, osDecode);
92          assertEquals("decoding differs", testdata, osDecode.toString());
93      }
94  
95      /**
96       * Testcase testToString.
97       */
98      @Test
99      // TestLink(externalId = "JAS-5")
100     public void testToString()
101     {
102         assertEquals("Layer name mismatch", Md5DesLayer.LAYERNAME, layerEncrypt.toString());
103     }
104 
105     /**
106      * Testcase equals.
107      */
108     @Test
109     public void testEquals()
110     {
111         Md5DesLayer l1 = new Md5DesLayer();
112         Md5DesLayer l2 = new Md5DesLayer();
113         l1.init(INITVALUE);
114         l2.init(INITVALUE);
115         assertEquals("Layer object equal", l1, l2);
116     }
117 
118     /**
119      * Testcase equals.
120      */
121     @Test
122     public void testEqualsSame()
123     {
124         Md5DesLayer l1 = new Md5DesLayer();
125         l1.init(INITVALUE);
126         assertEquals("Layer object same", l1, l1);
127     }
128 
129     /**
130      * Testcase equals.
131      */
132     @Test
133     public void testNotEqualsNull()
134     {
135         Md5DesLayer l1 = new Md5DesLayer();
136         l1.init(INITVALUE);
137         Object o = null;
138         assertEquals("Layer object null unequal", l1.equals(o), false);
139     }
140 
141     /**
142      * Testcase equals.
143      */
144     @Test
145     public void testNotEqualsWrongclass()
146     {
147         Md5DesLayer l1 = new Md5DesLayer();
148         l1.init(INITVALUE);
149         Object o = new Object();
150         assertEquals("Layer object wrong class unequal", l1.equals(o), false);
151     }
152 
153     /**
154      * Testcase hashcode.
155      */
156     @Test
157     public void testHashcode()
158     {
159         Md5DesLayer l1 = new Md5DesLayer();
160         Md5DesLayer l2 = new Md5DesLayer();
161         l1.init(INITVALUE);
162         l2.init(INITVALUE);
163         assertEquals("Layer hash equal", l1.hashCode(), l2.hashCode());
164     }
165 
166 }