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