Improve code coverage for aaf cadi modules
[aaf/cadi.git] / core / src / test / java / org / onap / aaf / cadi / JU_AES.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cadi;\r
24 \r
25 import java.io.ByteArrayInputStream;\r
26 import java.io.ByteArrayOutputStream;\r
27 \r
28 import javax.crypto.CipherInputStream;\r
29 import javax.crypto.CipherOutputStream;\r
30 \r
31 import org.junit.Test;\r
32 import org.onap.aaf.cadi.AES;\r
33 import org.onap.aaf.cadi.Symm;\r
34 \r
35 import junit.framework.Assert;\r
36 \r
37 import static org.junit.Assert.assertFalse;\r
38 import static org.junit.Assert.assertTrue;\r
39 \r
40 import java.io.ByteArrayInputStream;\r
41 import java.io.ByteArrayOutputStream;\r
42 import java.io.File;\r
43 import java.io.OutputStream;\r
44 \r
45 import javax.crypto.CipherInputStream;\r
46 import javax.crypto.CipherOutputStream;\r
47 \r
48 import org.junit.Test;\r
49 import org.onap.aaf.cadi.AES;\r
50 import org.onap.aaf.cadi.Symm;\r
51 \r
52 import junit.framework.Assert;\r
53 \r
54 public class JU_AES {\r
55 \r
56         @Test\r
57         public void test() throws Exception {\r
58                 AES aes = new AES();\r
59                 String orig = "I'm a password, really";\r
60                 byte[] passin = orig.getBytes();\r
61                 byte[] encrypted = aes.encrypt(passin);\r
62                 byte[] b64enc = Symm.base64.encode(encrypted);\r
63                 System.out.println(new String(b64enc));\r
64                 \r
65                 encrypted = Symm.base64.decode(b64enc);\r
66                 passin = aes.decrypt(encrypted);\r
67                 Assert.assertEquals(orig, new String(passin));\r
68         }\r
69         \r
70         @Test\r
71         public void testAESFileStream() throws Exception {\r
72                 File fi = new File("test/AESKeyFile");\r
73                 AES aes = new AES(fi);\r
74                 \r
75                 String orig = "I'm a password, really";\r
76                 byte[] passin = orig.getBytes();\r
77                 byte[] encrypted = aes.encrypt(passin);\r
78                 byte[] b64enc = Symm.base64.encode(encrypted);\r
79                 System.out.println(new String(b64enc));\r
80                 \r
81                 encrypted = Symm.base64.decode(b64enc);\r
82                 passin = aes.decrypt(encrypted);\r
83                 Assert.assertEquals(orig, new String(passin));\r
84                 \r
85                 File temp = new File("tempFile");\r
86                 try {\r
87                         assertFalse(temp.exists());\r
88                         aes.save(temp);\r
89                         assertTrue(temp.exists());\r
90                 } catch (Exception e) {\r
91                 } finally {\r
92                         temp.delete();\r
93                 }               \r
94                 \r
95         }\r
96 \r
97         @Test\r
98         public void testInputStream() throws Exception {\r
99                 AES aes = new AES();\r
100                 String orig = "I'm a password, really";\r
101                 ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
102                 CipherInputStream cis = aes.inputStream(bais, true);\r
103                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
104                 Symm.base64.encode(cis, baos);\r
105                 cis.close();\r
106 \r
107                 byte[] b64encrypted;\r
108                 System.out.println(new String(b64encrypted=baos.toByteArray()));\r
109 \r
110                 CipherInputStream cis1 = aes.inputStream(bais, false);\r
111                 ByteArrayOutputStream baos1 = new ByteArrayOutputStream();\r
112                 Symm.base64.encode(cis1, baos1);\r
113                 cis.close();\r
114 \r
115                 byte[] b64encrypted1;\r
116                 System.out.println(new String(b64encrypted1=baos1.toByteArray()));\r
117                 \r
118                 baos.reset();\r
119                 CipherOutputStream cos = aes.outputStream(baos, false);\r
120                 Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
121                 cos.close();\r
122                 Assert.assertEquals(orig, new String(baos.toByteArray()));\r
123                 \r
124                 OutputStream stream = aes.outputStream(System.out, true);\r
125                 assertTrue(stream instanceof CipherOutputStream);\r
126 \r
127         }\r
128 \r
129         @Test\r
130         public void testObtain() throws Exception {\r
131                 byte[] keygen = Symm.baseCrypt().keygen();\r
132                 \r
133                 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
134                 \r
135                 String orig ="Another Password, please";\r
136                 String encrypted = symm.enpass(orig);\r
137                 System.out.println(encrypted);\r
138                 String decrypted = symm.depass(encrypted);\r
139                 System.out.println(decrypted);\r
140                 Assert.assertEquals(orig, decrypted);\r
141         }\r
142         \r
143         @Test\r
144         public void test1() throws Exception {\r
145                 AES aes = new AES();\r
146                 String orig = "I'm a password, really";\r
147                 byte[] passin = orig.getBytes();\r
148                 byte[] encrypted = aes.encrypt(passin);\r
149                 byte[] b64enc = Symm.base64.encode(encrypted);\r
150                 System.out.println(new String(b64enc));\r
151                 \r
152                 encrypted = Symm.base64.decode(b64enc);\r
153                 passin = aes.decrypt(encrypted);\r
154                 Assert.assertEquals(orig, new String(passin));\r
155         }\r
156         \r
157         @Test\r
158         public void testAESFileStream1() throws Exception {\r
159                 File fi = new File("test/AESKeyFile");\r
160                 AES aes = new AES(fi);\r
161                 \r
162                 String orig = "I'm a password, really";\r
163                 byte[] passin = orig.getBytes();\r
164                 byte[] encrypted = aes.encrypt(passin);\r
165                 byte[] b64enc = Symm.base64.encode(encrypted);\r
166                 System.out.println(new String(b64enc));\r
167                 \r
168                 encrypted = Symm.base64.decode(b64enc);\r
169                 passin = aes.decrypt(encrypted);\r
170                 Assert.assertEquals(orig, new String(passin));\r
171                 \r
172                 File temp = new File("tempFile");\r
173                 try {\r
174                         assertFalse(temp.exists());\r
175                         aes.save(temp);\r
176                         assertTrue(temp.exists());\r
177                 } catch (Exception e) {\r
178                 } finally {\r
179                         temp.delete();\r
180                 }               \r
181                 \r
182         }\r
183 \r
184         @Test\r
185         public void testInputStream1() throws Exception {\r
186                 AES aes = new AES();\r
187                 String orig = "I'm a password, really";\r
188                 ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
189                 CipherInputStream cis = aes.inputStream(bais, true);\r
190                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
191                 Symm.base64.encode(cis, baos);\r
192                 cis.close();\r
193 \r
194                 byte[] b64encrypted;\r
195                 System.out.println(new String(b64encrypted=baos.toByteArray()));\r
196 \r
197                 CipherInputStream cis1 = aes.inputStream(bais, false);\r
198                 ByteArrayOutputStream baos1 = new ByteArrayOutputStream();\r
199                 Symm.base64.encode(cis1, baos1);\r
200                 cis.close();\r
201 \r
202                 byte[] b64encrypted1;\r
203                 System.out.println(new String(b64encrypted1=baos1.toByteArray()));\r
204                 \r
205                 baos.reset();\r
206                 CipherOutputStream cos = aes.outputStream(baos, false);\r
207                 Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
208                 cos.close();\r
209                 Assert.assertEquals(orig, new String(baos.toByteArray()));\r
210                 \r
211                 OutputStream stream = aes.outputStream(System.out, true);\r
212                 assertTrue(stream instanceof CipherOutputStream);\r
213 \r
214         }\r
215 \r
216         @Test\r
217         public void testObtain1() throws Exception {\r
218                 byte[] keygen = Symm.baseCrypt().keygen();\r
219                 \r
220                 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
221                 \r
222                 String orig ="Another Password, please";\r
223                 String encrypted = symm.enpass(orig);\r
224                 System.out.println(encrypted);\r
225                 String decrypted = symm.depass(encrypted);\r
226                 System.out.println(decrypted);\r
227                 Assert.assertEquals(orig, decrypted);\r
228         }\r
229 }\r