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.junit.Assert.*;
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.lang.reflect.Field;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
36 import javax.crypto.CipherInputStream;
37 import javax.crypto.CipherOutputStream;
38 import javax.crypto.SecretKey;
40 import org.junit.Test;
41 import org.onap.aaf.cadi.AES;
42 import org.onap.aaf.cadi.CadiException;
43 import org.onap.aaf.cadi.Symm;
44 import org.junit.Before;
48 private ByteArrayInputStream baisEncrypt;
49 private ByteArrayInputStream baisDecrypt;
50 private ByteArrayOutputStream baosEncrypt;
51 private ByteArrayOutputStream baosDecrypt;
54 public void setup() throws Exception {
55 byte[] keyBytes = new byte[AES.AES_KEY_SIZE/8];
56 char[] codeset = Symm.base64.codeset;
57 int offset = (Math.abs(codeset[0])+47)%(codeset.length-keyBytes.length);
58 for(int i=0;i<keyBytes.length;++i) {
59 keyBytes[i] = (byte)codeset[i+offset];
61 aes = new AES(keyBytes,0,keyBytes.length);
65 public void newKeyTest() throws Exception {
66 SecretKey secretKey = AES.newKey();
67 assertEquals(secretKey.getAlgorithm(), AES.class.getSimpleName());
71 public void encryptDecrpytFromBytes() throws Exception {
72 String orig = "I'm a password, really";
73 byte[] encrypted = aes.encrypt(orig.getBytes());
74 byte[] decrypted = aes.decrypt(encrypted);
75 assertEquals(orig, new String(decrypted));
77 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
78 aeskeySpec_field.setAccessible(true);
79 aeskeySpec_field.set(aes, null);
82 aes.encrypt(orig.getBytes());
83 fail("Should have thrown an exception");
84 } catch (CadiException e) {
87 aes.decrypt(encrypted);
88 fail("Should have thrown an exception");
89 } catch (CadiException e) {
94 public void saveToFileTest() throws Exception {
95 String filePath = "test/output_key";
96 File keyfile = new File(filePath);
98 assertTrue(Files.isReadable(Paths.get(filePath)));
99 assertFalse(Files.isWritable(Paths.get(filePath)));
100 assertFalse(Files.isExecutable(Paths.get(filePath)));
105 public void encryptDecryptFromInputStream() throws Exception {
106 String orig = "I'm a password, really";
110 CipherInputStream cisEncrypt;
111 CipherInputStream cisDecrypt;
113 // Test CipherInputStream
114 baisEncrypt = new ByteArrayInputStream(orig.getBytes());
115 cisEncrypt = aes.inputStream(baisEncrypt, true);
116 baosEncrypt = new ByteArrayOutputStream();
117 transferFromInputStreamToOutputStream(cisEncrypt, baosEncrypt);
120 b64encrypted = baosEncrypt.toByteArray();
122 baisDecrypt = new ByteArrayInputStream(b64encrypted);
123 cisDecrypt = aes.inputStream(baisDecrypt, false);
124 baosDecrypt = new ByteArrayOutputStream();
125 transferFromInputStreamToOutputStream(cisDecrypt, baosDecrypt);
128 output = new String(baosDecrypt.toByteArray());
129 assertEquals(orig, output);
131 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
132 aeskeySpec_field.setAccessible(true);
133 aeskeySpec_field.set(aes, null);
135 assertNull(aes.inputStream(baisEncrypt, true));
139 public void encryptDecryptFromOutputStream() throws Exception {
140 String orig = "I'm a password, really";
144 CipherOutputStream cosEncrypt;
145 CipherOutputStream cosDecrypt;
147 // Test CipherOutputStream
148 baisEncrypt = new ByteArrayInputStream(orig.getBytes());
149 baosEncrypt = new ByteArrayOutputStream();
150 cosEncrypt = aes.outputStream(baosEncrypt, true);
151 transferFromInputStreamToOutputStream(baisEncrypt, cosEncrypt);
154 b64encrypted = baosEncrypt.toByteArray();
156 baosDecrypt = new ByteArrayOutputStream();
157 cosDecrypt = aes.outputStream(baosDecrypt, false);
158 baisDecrypt = new ByteArrayInputStream(b64encrypted);
159 transferFromInputStreamToOutputStream(baisDecrypt, cosDecrypt);
162 output = new String(baosDecrypt.toByteArray());
163 assertEquals(orig, output);
165 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
166 aeskeySpec_field.setAccessible(true);
167 aeskeySpec_field.set(aes, null);
169 assertNull(aes.outputStream(baosEncrypt, true));
172 public void transferFromInputStreamToOutputStream(InputStream is, OutputStream os) throws IOException {
173 byte[] buffer = new byte[200];
175 while ((len = is.read(buffer)) != -1) {
176 os.write(buffer, 0, len);