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
diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_AES.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_AES.java
new file mode 100644 (file)
index 0000000..4fb9242
--- /dev/null
@@ -0,0 +1,180 @@
+/*******************************************************************************
+ * ============LICENSE_START====================================================
+ * * org.onap.aaf
+ * * ===========================================================================
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * * ===========================================================================
+ * * Licensed under the Apache License, Version 2.0 (the "License");
+ * * you may not use this file except in compliance with the License.
+ * * You may obtain a copy of the License at
+ * *
+ *  *      http://www.apache.org/licenses/LICENSE-2.0
+ * *
+ *  * Unless required by applicable law or agreed to in writing, software
+ * * distributed under the License is distributed on an "AS IS" BASIS,
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * * See the License for the specific language governing permissions and
+ * * limitations under the License.
+ * * ============LICENSE_END====================================================
+ * *
+ * *
+ ******************************************************************************/
+package org.onap.aaf.cadi.test;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import javax.crypto.CipherInputStream;
+import javax.crypto.CipherOutputStream;
+import javax.crypto.SecretKey;
+
+import org.junit.Test;
+import org.onap.aaf.cadi.AES;
+import org.onap.aaf.cadi.CadiException;
+import org.onap.aaf.cadi.Symm;
+import org.junit.Before;
+
+public class JU_AES {
+       private AES aes;
+       private ByteArrayInputStream baisEncrypt;
+       private ByteArrayInputStream baisDecrypt;
+       private ByteArrayOutputStream baosEncrypt;
+       private ByteArrayOutputStream baosDecrypt;
+
+       @Before
+       public void setup() throws Exception {
+               byte[] keyBytes = new byte[AES.AES_KEY_SIZE/8];
+               char[] codeset = Symm.base64.codeset;
+               int offset = (Math.abs(codeset[0])+47)%(codeset.length-keyBytes.length);
+               for(int i=0;i<keyBytes.length;++i) {
+                       keyBytes[i] = (byte)codeset[i+offset];
+               }
+               aes = new AES(keyBytes,0,keyBytes.length);
+       }
+
+       @Test
+       public void newKeyTest() throws Exception {
+               SecretKey secretKey = AES.newKey();
+               assertEquals(secretKey.getAlgorithm(), AES.class.getSimpleName());
+       }
+
+       @Test
+       public void encryptDecrpytFromBytes() throws Exception {
+               String orig = "I'm a password, really";
+               byte[] encrypted = aes.encrypt(orig.getBytes());
+               byte[] decrypted = aes.decrypt(encrypted);
+               assertEquals(orig, new String(decrypted));
+               Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
+               aeskeySpec_field.setAccessible(true);
+               aeskeySpec_field.set(aes, null);
+
+               try {
+                       aes.encrypt(orig.getBytes());
+                       fail("Should have thrown an exception");
+               } catch (CadiException e) {
+               }
+               try {
+                       aes.decrypt(encrypted);
+                       fail("Should have thrown an exception");
+               } catch (CadiException e) {
+               }
+       }
+
+       @Test
+       public void saveToFileTest() throws Exception {
+               String filePath = "test/output_key";
+               File keyfile = new File(filePath);
+               aes.save(keyfile);
+               assertTrue(Files.isReadable(Paths.get(filePath)));
+               assertFalse(Files.isWritable(Paths.get(filePath)));
+               assertFalse(Files.isExecutable(Paths.get(filePath)));
+               keyfile.delete();
+       }
+
+       @Test
+       public void encryptDecryptFromInputStream() throws Exception {
+               String orig = "I'm a password, really";
+               byte[] b64encrypted;
+               String output;
+
+               CipherInputStream cisEncrypt;
+               CipherInputStream cisDecrypt;
+               
+               // Test CipherInputStream
+               baisEncrypt = new ByteArrayInputStream(orig.getBytes());
+               cisEncrypt = aes.inputStream(baisEncrypt, true);
+               baosEncrypt = new ByteArrayOutputStream();
+               transferFromInputStreamToOutputStream(cisEncrypt, baosEncrypt);
+               cisEncrypt.close();
+
+               b64encrypted = baosEncrypt.toByteArray();
+
+               baisDecrypt = new ByteArrayInputStream(b64encrypted);
+               cisDecrypt = aes.inputStream(baisDecrypt, false);
+               baosDecrypt = new ByteArrayOutputStream();
+               transferFromInputStreamToOutputStream(cisDecrypt, baosDecrypt);
+               cisDecrypt.close();
+
+               output = new String(baosDecrypt.toByteArray());
+               assertEquals(orig, output);
+
+               Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
+               aeskeySpec_field.setAccessible(true);
+               aeskeySpec_field.set(aes, null);
+
+               assertNull(aes.inputStream(baisEncrypt, true));
+       }
+
+       @Test
+       public void encryptDecryptFromOutputStream() throws Exception {
+               String orig = "I'm a password, really";
+               byte[] b64encrypted;
+               String output;
+
+               CipherOutputStream cosEncrypt;
+               CipherOutputStream cosDecrypt;
+               
+               // Test CipherOutputStream
+               baisEncrypt = new ByteArrayInputStream(orig.getBytes());
+               baosEncrypt = new ByteArrayOutputStream();
+               cosEncrypt = aes.outputStream(baosEncrypt, true);
+               transferFromInputStreamToOutputStream(baisEncrypt, cosEncrypt);
+               cosEncrypt.close();
+
+               b64encrypted = baosEncrypt.toByteArray();
+
+               baosDecrypt = new ByteArrayOutputStream();
+               cosDecrypt = aes.outputStream(baosDecrypt, false);
+               baisDecrypt = new ByteArrayInputStream(b64encrypted);
+               transferFromInputStreamToOutputStream(baisDecrypt, cosDecrypt);
+               cosDecrypt.close();
+
+               output = new String(baosDecrypt.toByteArray());
+               assertEquals(orig, output);
+
+               Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
+               aeskeySpec_field.setAccessible(true);
+               aeskeySpec_field.set(aes, null);
+
+               assertNull(aes.outputStream(baosEncrypt, true));
+       }
+
+       public void transferFromInputStreamToOutputStream(InputStream is, OutputStream os) throws IOException {
+               byte[] buffer = new byte[200];
+               int len;
+               while ((len = is.read(buffer)) != -1) {
+                   os.write(buffer, 0, len);
+               }
+       }
+       
+}