1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
21 ******************************************************************************/
\r
22 package org.onap.aaf.cadi.test;
\r
24 import static org.hamcrest.CoreMatchers.*;
\r
25 import static org.junit.Assert.*;
\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
40 import javax.crypto.CipherInputStream;
\r
41 import javax.crypto.CipherOutputStream;
\r
42 import javax.crypto.SecretKey;
\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
48 public class JU_AES {
\r
50 private ByteArrayInputStream baisEncrypt;
\r
51 private ByteArrayInputStream baisDecrypt;
\r
52 private ByteArrayOutputStream baosEncrypt;
\r
53 private ByteArrayOutputStream baosDecrypt;
\r
55 private ByteArrayOutputStream errStream;
\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
65 aes = new AES(keyBytes, 0, keyBytes.length);
\r
67 errStream = new ByteArrayOutputStream();
\r
68 System.setErr(new PrintStream(errStream));
\r
72 public void tearDown() {
\r
73 System.setErr(System.err);
\r
77 public void newKeyTest() throws Exception {
\r
78 SecretKey secretKey = AES.newKey();
\r
79 assertThat(secretKey.getAlgorithm(), is(AES.class.getSimpleName()));
\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
89 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
\r
90 aeskeySpec_field.setAccessible(true);
\r
91 aeskeySpec_field.set(aes, null);
\r
94 aes.encrypt(orig.getBytes());
\r
95 fail("Should have thrown an exception");
\r
96 } catch (CadiException e) {
\r
99 aes.decrypt(encrypted);
\r
100 fail("Should have thrown an exception");
\r
101 } catch (CadiException e) {
\r
106 public void saveToFileTest() throws Exception {
\r
107 String filePath = "test/output_key";
\r
108 File keyfile = new File(filePath);
\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
117 public void encryptDecryptFromInputStream() throws Exception {
\r
118 String orig = "I'm a password, really";
\r
119 byte[] b64encrypted;
\r
122 CipherInputStream cisEncrypt;
\r
123 CipherInputStream cisDecrypt;
\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
132 b64encrypted = baosEncrypt.toByteArray();
\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
140 output = new String(baosDecrypt.toByteArray());
\r
141 assertThat(output, is(orig));
\r
143 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
\r
144 aeskeySpec_field.setAccessible(true);
\r
145 aeskeySpec_field.set(aes, null);
\r
147 assertNull(aes.inputStream(baisEncrypt, true));
\r
148 assertThat(errStream.toString(), is("Error creating Aes CipherInputStream\n"));
\r
152 public void encryptDecryptFromOutputStream() throws Exception {
\r
153 String orig = "I'm a password, really";
\r
154 byte[] b64encrypted;
\r
157 CipherOutputStream cosEncrypt;
\r
158 CipherOutputStream cosDecrypt;
\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
167 b64encrypted = baosEncrypt.toByteArray();
\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
175 output = new String(baosDecrypt.toByteArray());
\r
176 assertThat(output, is(orig));
\r
178 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
\r
179 aeskeySpec_field.setAccessible(true);
\r
180 aeskeySpec_field.set(aes, null);
\r
182 assertNull(aes.outputStream(baosEncrypt, true));
\r
183 assertThat(errStream.toString(), is("Error creating Aes CipherOutputStream\n"));
\r
186 public void transferFromInputStreamToOutputStream(InputStream is, OutputStream os) throws IOException {
\r
187 byte[] buffer = new byte[200];
\r
189 while ((len = is.read(buffer)) != -1) {
\r
190 os.write(buffer, 0, len);
\r