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 ******************************************************************************/
23 package org.onap.aaf.cadi.test;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.junit.Assert.*;
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
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;
41 import javax.crypto.CipherInputStream;
42 import javax.crypto.CipherOutputStream;
43 import javax.crypto.SecretKey;
45 import org.onap.aaf.cadi.AES;
46 import org.onap.aaf.cadi.CadiException;
47 import org.onap.aaf.cadi.Symm;
51 private ByteArrayInputStream baisEncrypt;
52 private ByteArrayInputStream baisDecrypt;
53 private ByteArrayOutputStream baosEncrypt;
54 private ByteArrayOutputStream baosDecrypt;
56 private ByteArrayOutputStream errStream;
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];
66 aes = new AES(keyBytes, 0, keyBytes.length);
68 errStream = new ByteArrayOutputStream();
69 System.setErr(new PrintStream(errStream));
73 public void tearDown() {
74 System.setErr(System.err);
78 public void newKeyTest() throws Exception {
79 SecretKey secretKey = AES.newKey();
80 assertThat(secretKey.getAlgorithm(), is(AES.class.getSimpleName()));
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));
90 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
91 aeskeySpec_field.setAccessible(true);
92 aeskeySpec_field.set(aes, null);
95 aes.encrypt(orig.getBytes());
96 fail("Should have thrown an exception");
97 } catch (CadiException e) {
100 aes.decrypt(encrypted);
101 fail("Should have thrown an exception");
102 } catch (CadiException e) {
107 public void saveToFileTest() throws Exception {
108 String filePath = "src/test/resources/output_key";
109 File keyfile = new File(filePath);
111 assertTrue(Files.isReadable(Paths.get(filePath)));
112 assertFalse(Files.isWritable(Paths.get(filePath)));
113 assertFalse(Files.isExecutable(Paths.get(filePath)));
118 public void encryptDecryptFromInputStream() throws Exception {
119 String orig = "I'm a password, really";
123 CipherInputStream cisEncrypt;
124 CipherInputStream cisDecrypt;
126 // Test CipherInputStream
127 baisEncrypt = new ByteArrayInputStream(orig.getBytes());
128 cisEncrypt = aes.inputStream(baisEncrypt, true);
129 baosEncrypt = new ByteArrayOutputStream();
130 transferFromInputStreamToOutputStream(cisEncrypt, baosEncrypt);
133 b64encrypted = baosEncrypt.toByteArray();
135 baisDecrypt = new ByteArrayInputStream(b64encrypted);
136 cisDecrypt = aes.inputStream(baisDecrypt, false);
137 baosDecrypt = new ByteArrayOutputStream();
138 transferFromInputStreamToOutputStream(cisDecrypt, baosDecrypt);
141 output = new String(baosDecrypt.toByteArray());
142 assertThat(output, is(orig));
144 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
145 aeskeySpec_field.setAccessible(true);
146 aeskeySpec_field.set(aes, null);
148 assertNull(aes.inputStream(baisEncrypt, true));
149 assertThat(errStream.toString(), is("Error creating Aes CipherInputStream\n"));
153 public void encryptDecryptFromOutputStream() throws Exception {
154 String orig = "I'm a password, really";
158 CipherOutputStream cosEncrypt;
159 CipherOutputStream cosDecrypt;
161 // Test CipherOutputStream
162 baisEncrypt = new ByteArrayInputStream(orig.getBytes());
163 baosEncrypt = new ByteArrayOutputStream();
164 cosEncrypt = aes.outputStream(baosEncrypt, true);
165 transferFromInputStreamToOutputStream(baisEncrypt, cosEncrypt);
168 b64encrypted = baosEncrypt.toByteArray();
170 baosDecrypt = new ByteArrayOutputStream();
171 cosDecrypt = aes.outputStream(baosDecrypt, false);
172 baisDecrypt = new ByteArrayInputStream(b64encrypted);
173 transferFromInputStreamToOutputStream(baisDecrypt, cosDecrypt);
176 output = new String(baosDecrypt.toByteArray());
177 assertThat(output, is(orig));
179 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
180 aeskeySpec_field.setAccessible(true);
181 aeskeySpec_field.set(aes, null);
183 assertNull(aes.outputStream(baosEncrypt, true));
184 assertThat(errStream.toString(), is("Error creating Aes CipherOutputStream\n"));
187 public void transferFromInputStreamToOutputStream(InputStream is, OutputStream os) throws IOException {
188 byte[] buffer = new byte[200];
190 while ((len = is.read(buffer)) != -1) {
191 os.write(buffer, 0, len);