fc960be8f9058e80cbe821ffbbe5e5f14cd4e03c
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / 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  * *\r
21  ******************************************************************************/\r
22 package org.onap.aaf.cadi.test;\r
23 \r
24 import static org.hamcrest.CoreMatchers.*;\r
25 import static org.junit.Assert.*;\r
26 import org.junit.*;\r
27 \r
28 \r
29 import java.io.ByteArrayInputStream;\r
30 import java.io.ByteArrayOutputStream;\r
31 import java.io.File;\r
32 import java.io.IOException;\r
33 import java.io.InputStream;\r
34 import java.io.OutputStream;\r
35 import java.io.PrintStream;\r
36 import java.lang.reflect.Field;\r
37 import java.nio.file.Files;\r
38 import java.nio.file.Paths;\r
39 \r
40 import javax.crypto.CipherInputStream;\r
41 import javax.crypto.CipherOutputStream;\r
42 import javax.crypto.SecretKey;\r
43 \r
44 import org.onap.aaf.cadi.AES;\r
45 import org.onap.aaf.cadi.CadiException;\r
46 import org.onap.aaf.cadi.Symm;\r
47 \r
48 public class JU_AES {\r
49         private AES aes;\r
50         private ByteArrayInputStream baisEncrypt;\r
51         private ByteArrayInputStream baisDecrypt;\r
52         private ByteArrayOutputStream baosEncrypt;\r
53         private ByteArrayOutputStream baosDecrypt;\r
54 \r
55         private ByteArrayOutputStream errStream;\r
56 \r
57         @Before\r
58         public void setup() throws Exception {\r
59                 byte[] keyBytes = new byte[AES.AES_KEY_SIZE/8];\r
60                 char[] codeset = Symm.base64.codeset;\r
61                 int offset = (Math.abs(codeset[0]) + 47) % (codeset.length - keyBytes.length);\r
62                 for(int i = 0; i < keyBytes.length; ++i) {\r
63                         keyBytes[i] = (byte)codeset[i+offset];\r
64                 }\r
65                 aes = new AES(keyBytes, 0, keyBytes.length);\r
66 \r
67                 errStream = new ByteArrayOutputStream();\r
68                 System.setErr(new PrintStream(errStream));\r
69         }\r
70 \r
71         @After\r
72         public void tearDown() {\r
73                 System.setErr(System.err);\r
74         }\r
75 \r
76         @Test\r
77         public void newKeyTest() throws Exception {\r
78                 SecretKey secretKey = AES.newKey();\r
79                 assertThat(secretKey.getAlgorithm(), is(AES.class.getSimpleName()));\r
80         }\r
81 \r
82         @Test\r
83         public void encryptDecrpytFromBytes() throws Exception {\r
84                 String orig = "I'm a password, really";\r
85                 byte[] encrypted = aes.encrypt(orig.getBytes());\r
86                 byte[] decrypted = aes.decrypt(encrypted);\r
87                 assertThat(new String(decrypted), is(orig));\r
88  \r
89                 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");\r
90                 aeskeySpec_field.setAccessible(true);\r
91                 aeskeySpec_field.set(aes, null);\r
92 \r
93                 try {\r
94                         aes.encrypt(orig.getBytes());\r
95                         fail("Should have thrown an exception");\r
96                 } catch (CadiException e) {\r
97                 }\r
98                 try {\r
99                         aes.decrypt(encrypted);\r
100                         fail("Should have thrown an exception");\r
101                 } catch (CadiException e) {\r
102                 }\r
103         }\r
104 \r
105         @Test\r
106         public void saveToFileTest() throws Exception {\r
107                 String filePath = "test/output_key";\r
108                 File keyfile = new File(filePath);\r
109                 aes.save(keyfile);\r
110                 assertTrue(Files.isReadable(Paths.get(filePath)));\r
111                 assertFalse(Files.isWritable(Paths.get(filePath)));\r
112                 assertFalse(Files.isExecutable(Paths.get(filePath)));\r
113                 keyfile.delete();\r
114         }\r
115 \r
116         @Test\r
117         public void encryptDecryptFromInputStream() throws Exception {\r
118                 String orig = "I'm a password, really";\r
119                 byte[] b64encrypted;\r
120                 String output;\r
121 \r
122                 CipherInputStream cisEncrypt;\r
123                 CipherInputStream cisDecrypt;\r
124                 \r
125                 // Test CipherInputStream\r
126                 baisEncrypt = new ByteArrayInputStream(orig.getBytes());\r
127                 cisEncrypt = aes.inputStream(baisEncrypt, true);\r
128                 baosEncrypt = new ByteArrayOutputStream();\r
129                 transferFromInputStreamToOutputStream(cisEncrypt, baosEncrypt);\r
130                 cisEncrypt.close();\r
131 \r
132                 b64encrypted = baosEncrypt.toByteArray();\r
133 \r
134                 baisDecrypt = new ByteArrayInputStream(b64encrypted);\r
135                 cisDecrypt = aes.inputStream(baisDecrypt, false);\r
136                 baosDecrypt = new ByteArrayOutputStream();\r
137                 transferFromInputStreamToOutputStream(cisDecrypt, baosDecrypt);\r
138                 cisDecrypt.close();\r
139 \r
140                 output = new String(baosDecrypt.toByteArray());\r
141                 assertThat(output, is(orig));\r
142 \r
143                 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");\r
144                 aeskeySpec_field.setAccessible(true);\r
145                 aeskeySpec_field.set(aes, null);\r
146 \r
147                 assertNull(aes.inputStream(baisEncrypt, true));\r
148                 assertThat(errStream.toString(), is("Error creating Aes CipherInputStream\n"));\r
149         }\r
150 \r
151         @Test\r
152         public void encryptDecryptFromOutputStream() throws Exception {\r
153                 String orig = "I'm a password, really";\r
154                 byte[] b64encrypted;\r
155                 String output;\r
156 \r
157                 CipherOutputStream cosEncrypt;\r
158                 CipherOutputStream cosDecrypt;\r
159                 \r
160                 // Test CipherOutputStream\r
161                 baisEncrypt = new ByteArrayInputStream(orig.getBytes());\r
162                 baosEncrypt = new ByteArrayOutputStream();\r
163                 cosEncrypt = aes.outputStream(baosEncrypt, true);\r
164                 transferFromInputStreamToOutputStream(baisEncrypt, cosEncrypt);\r
165                 cosEncrypt.close();\r
166 \r
167                 b64encrypted = baosEncrypt.toByteArray();\r
168 \r
169                 baosDecrypt = new ByteArrayOutputStream();\r
170                 cosDecrypt = aes.outputStream(baosDecrypt, false);\r
171                 baisDecrypt = new ByteArrayInputStream(b64encrypted);\r
172                 transferFromInputStreamToOutputStream(baisDecrypt, cosDecrypt);\r
173                 cosDecrypt.close();\r
174 \r
175                 output = new String(baosDecrypt.toByteArray());\r
176                 assertThat(output, is(orig));\r
177 \r
178                 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");\r
179                 aeskeySpec_field.setAccessible(true);\r
180                 aeskeySpec_field.set(aes, null);\r
181 \r
182                 assertNull(aes.outputStream(baosEncrypt, true));\r
183                 assertThat(errStream.toString(), is("Error creating Aes CipherOutputStream\n"));\r
184         }\r
185 \r
186         public void transferFromInputStreamToOutputStream(InputStream is, OutputStream os) throws IOException {\r
187                 byte[] buffer = new byte[200];\r
188                 int len;\r
189                 while ((len = is.read(buffer)) != -1) {\r
190                     os.write(buffer, 0, len);\r
191                 }\r
192         }\r
193         \r
194 }\r