AT&T 2.0.19 Code drop, stage 2
[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 package org.onap.aaf.cadi.test;
23
24 import static org.junit.Assert.*;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
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;
35
36 import javax.crypto.CipherInputStream;
37 import javax.crypto.CipherOutputStream;
38 import javax.crypto.SecretKey;
39
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;
45
46 public class JU_AES {
47         private AES aes;
48         private ByteArrayInputStream baisEncrypt;
49         private ByteArrayInputStream baisDecrypt;
50         private ByteArrayOutputStream baosEncrypt;
51         private ByteArrayOutputStream baosDecrypt;
52
53         @Before
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];
60                 }
61                 aes = new AES(keyBytes,0,keyBytes.length);
62         }
63
64         @Test
65         public void newKeyTest() throws Exception {
66                 SecretKey secretKey = AES.newKey();
67                 assertEquals(secretKey.getAlgorithm(), AES.class.getSimpleName());
68         }
69
70         @Test
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));
76  
77                 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
78                 aeskeySpec_field.setAccessible(true);
79                 aeskeySpec_field.set(aes, null);
80
81                 try {
82                         aes.encrypt(orig.getBytes());
83                         fail("Should have thrown an exception");
84                 } catch (CadiException e) {
85                 }
86                 try {
87                         aes.decrypt(encrypted);
88                         fail("Should have thrown an exception");
89                 } catch (CadiException e) {
90                 }
91         }
92
93         @Test
94         public void saveToFileTest() throws Exception {
95                 String filePath = "test/output_key";
96                 File keyfile = new File(filePath);
97                 aes.save(keyfile);
98                 assertTrue(Files.isReadable(Paths.get(filePath)));
99                 assertFalse(Files.isWritable(Paths.get(filePath)));
100                 assertFalse(Files.isExecutable(Paths.get(filePath)));
101                 keyfile.delete();
102         }
103
104         @Test
105         public void encryptDecryptFromInputStream() throws Exception {
106                 String orig = "I'm a password, really";
107                 byte[] b64encrypted;
108                 String output;
109
110                 CipherInputStream cisEncrypt;
111                 CipherInputStream cisDecrypt;
112                 
113                 // Test CipherInputStream
114                 baisEncrypt = new ByteArrayInputStream(orig.getBytes());
115                 cisEncrypt = aes.inputStream(baisEncrypt, true);
116                 baosEncrypt = new ByteArrayOutputStream();
117                 transferFromInputStreamToOutputStream(cisEncrypt, baosEncrypt);
118                 cisEncrypt.close();
119
120                 b64encrypted = baosEncrypt.toByteArray();
121
122                 baisDecrypt = new ByteArrayInputStream(b64encrypted);
123                 cisDecrypt = aes.inputStream(baisDecrypt, false);
124                 baosDecrypt = new ByteArrayOutputStream();
125                 transferFromInputStreamToOutputStream(cisDecrypt, baosDecrypt);
126                 cisDecrypt.close();
127
128                 output = new String(baosDecrypt.toByteArray());
129                 assertEquals(orig, output);
130
131                 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
132                 aeskeySpec_field.setAccessible(true);
133                 aeskeySpec_field.set(aes, null);
134
135                 assertNull(aes.inputStream(baisEncrypt, true));
136         }
137
138         @Test
139         public void encryptDecryptFromOutputStream() throws Exception {
140                 String orig = "I'm a password, really";
141                 byte[] b64encrypted;
142                 String output;
143
144                 CipherOutputStream cosEncrypt;
145                 CipherOutputStream cosDecrypt;
146                 
147                 // Test CipherOutputStream
148                 baisEncrypt = new ByteArrayInputStream(orig.getBytes());
149                 baosEncrypt = new ByteArrayOutputStream();
150                 cosEncrypt = aes.outputStream(baosEncrypt, true);
151                 transferFromInputStreamToOutputStream(baisEncrypt, cosEncrypt);
152                 cosEncrypt.close();
153
154                 b64encrypted = baosEncrypt.toByteArray();
155
156                 baosDecrypt = new ByteArrayOutputStream();
157                 cosDecrypt = aes.outputStream(baosDecrypt, false);
158                 baisDecrypt = new ByteArrayInputStream(b64encrypted);
159                 transferFromInputStreamToOutputStream(baisDecrypt, cosDecrypt);
160                 cosDecrypt.close();
161
162                 output = new String(baosDecrypt.toByteArray());
163                 assertEquals(orig, output);
164
165                 Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
166                 aeskeySpec_field.setAccessible(true);
167                 aeskeySpec_field.set(aes, null);
168
169                 assertNull(aes.outputStream(baosEncrypt, true));
170         }
171
172         public void transferFromInputStreamToOutputStream(InputStream is, OutputStream os) throws IOException {
173                 byte[] buffer = new byte[200];
174                 int len;
175                 while ((len = is.read(buffer)) != -1) {
176                     os.write(buffer, 0, len);
177                 }
178         }
179         
180 }