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