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