Add Certs, Docker Build
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_AES.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * *
21  ******************************************************************************/
22 package org.onap.aaf.cadi.test;
23
24 import static org.hamcrest.CoreMatchers.*;
25 import static org.junit.Assert.*;
26 import org.junit.*;
27
28
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.io.File;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.io.PrintStream;
36 import java.lang.reflect.Field;
37 import java.nio.file.Files;
38 import java.nio.file.Paths;
39
40 import javax.crypto.CipherInputStream;
41 import javax.crypto.CipherOutputStream;
42 import javax.crypto.SecretKey;
43
44 import org.onap.aaf.cadi.AES;
45 import org.onap.aaf.cadi.CadiException;
46 import org.onap.aaf.cadi.Symm;
47
48 public class JU_AES {
49         private AES aes;
50         private ByteArrayInputStream baisEncrypt;
51         private ByteArrayInputStream baisDecrypt;
52         private ByteArrayOutputStream baosEncrypt;
53         private ByteArrayOutputStream baosDecrypt;
54
55         private ByteArrayOutputStream errStream;
56
57         @Before
58         public void setup() throws Exception {
59                 byte[] keyBytes = new byte[AES.AES_KEY_SIZE/8];
60                 char[] codeset = Symm.base64.codeset;
61                 int offset = (Math.abs(codeset[0]) + 47) % (codeset.length - keyBytes.length);
62                 for(int i = 0; i < keyBytes.length; ++i) {
63                         keyBytes[i] = (byte)codeset[i+offset];
64                 }
65                 aes = new AES(keyBytes, 0, keyBytes.length);
66
67                 errStream = new ByteArrayOutputStream();
68                 System.setErr(new PrintStream(errStream));
69         }
70
71         @After
72         public void tearDown() {
73                 System.setErr(System.err);
74         }
75
76         @Test
77         public void newKeyTest() throws Exception {
78                 SecretKey secretKey = AES.newKey();
79                 assertThat(secretKey.getAlgorithm(), is(AES.class.getSimpleName()));
80         }
81
82         @Test
83         public void encryptDecrpytFromBytes() throws Exception {
84                 String orig = "I'm a password, really";
85                 byte[] encrypted = aes.encrypt(orig.getBytes());
86                 byte[] decrypted = aes.decrypt(encrypted);
87                 assertThat(new String(decrypted), is(orig));
88  
89                 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
90                 aeskeySpec_field.setAccessible(true);
91                 aeskeySpec_field.set(aes, null);
92
93                 try {
94                         aes.encrypt(orig.getBytes());
95                         fail("Should have thrown an exception");
96                 } catch (CadiException e) {
97                 }
98                 try {
99                         aes.decrypt(encrypted);
100                         fail("Should have thrown an exception");
101                 } catch (CadiException e) {
102                 }
103         }
104
105         @Test
106         public void saveToFileTest() throws Exception {
107                 String filePath = "test/output_key";
108                 File keyfile = new File(filePath);
109                 aes.save(keyfile);
110                 assertTrue(Files.isReadable(Paths.get(filePath)));
111                 assertFalse(Files.isWritable(Paths.get(filePath)));
112                 assertFalse(Files.isExecutable(Paths.get(filePath)));
113                 keyfile.delete();
114         }
115
116         @Test
117         public void encryptDecryptFromInputStream() throws Exception {
118                 String orig = "I'm a password, really";
119                 byte[] b64encrypted;
120                 String output;
121
122                 CipherInputStream cisEncrypt;
123                 CipherInputStream cisDecrypt;
124                 
125                 // Test CipherInputStream
126                 baisEncrypt = new ByteArrayInputStream(orig.getBytes());
127                 cisEncrypt = aes.inputStream(baisEncrypt, true);
128                 baosEncrypt = new ByteArrayOutputStream();
129                 transferFromInputStreamToOutputStream(cisEncrypt, baosEncrypt);
130                 cisEncrypt.close();
131
132                 b64encrypted = baosEncrypt.toByteArray();
133
134                 baisDecrypt = new ByteArrayInputStream(b64encrypted);
135                 cisDecrypt = aes.inputStream(baisDecrypt, false);
136                 baosDecrypt = new ByteArrayOutputStream();
137                 transferFromInputStreamToOutputStream(cisDecrypt, baosDecrypt);
138                 cisDecrypt.close();
139
140                 output = new String(baosDecrypt.toByteArray());
141                 assertThat(output, is(orig));
142
143                 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
144                 aeskeySpec_field.setAccessible(true);
145                 aeskeySpec_field.set(aes, null);
146
147                 assertNull(aes.inputStream(baisEncrypt, true));
148                 assertThat(errStream.toString(), is("Error creating Aes CipherInputStream\n"));
149         }
150
151         @Test
152         public void encryptDecryptFromOutputStream() throws Exception {
153                 String orig = "I'm a password, really";
154                 byte[] b64encrypted;
155                 String output;
156
157                 CipherOutputStream cosEncrypt;
158                 CipherOutputStream cosDecrypt;
159                 
160                 // Test CipherOutputStream
161                 baisEncrypt = new ByteArrayInputStream(orig.getBytes());
162                 baosEncrypt = new ByteArrayOutputStream();
163                 cosEncrypt = aes.outputStream(baosEncrypt, true);
164                 transferFromInputStreamToOutputStream(baisEncrypt, cosEncrypt);
165                 cosEncrypt.close();
166
167                 b64encrypted = baosEncrypt.toByteArray();
168
169                 baosDecrypt = new ByteArrayOutputStream();
170                 cosDecrypt = aes.outputStream(baosDecrypt, false);
171                 baisDecrypt = new ByteArrayInputStream(b64encrypted);
172                 transferFromInputStreamToOutputStream(baisDecrypt, cosDecrypt);
173                 cosDecrypt.close();
174
175                 output = new String(baosDecrypt.toByteArray());
176                 assertThat(output, is(orig));
177
178                 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
179                 aeskeySpec_field.setAccessible(true);
180                 aeskeySpec_field.set(aes, null);
181
182                 assertNull(aes.outputStream(baosEncrypt, true));
183                 assertThat(errStream.toString(), is("Error creating Aes CipherOutputStream\n"));
184         }
185
186         public void transferFromInputStreamToOutputStream(InputStream is, OutputStream os) throws IOException {
187                 byte[] buffer = new byte[200];
188                 int len;
189                 while ((len = is.read(buffer)) != -1) {
190                     os.write(buffer, 0, len);
191                 }
192         }
193         
194 }