Improve code coverage for aaf cadi modules
[aaf/cadi.git] / core / src / test / java / org / onap / aaf / cadi / JU_AES.java
index 4779f09..50c4f27 100644 (file)
@@ -34,6 +34,23 @@ import org.onap.aaf.cadi.Symm;
 \r
 import junit.framework.Assert;\r
 \r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.assertTrue;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.File;\r
+import java.io.OutputStream;\r
+\r
+import javax.crypto.CipherInputStream;\r
+import javax.crypto.CipherOutputStream;\r
+\r
+import org.junit.Test;\r
+import org.onap.aaf.cadi.AES;\r
+import org.onap.aaf.cadi.Symm;\r
+\r
+import junit.framework.Assert;\r
+\r
 public class JU_AES {\r
 \r
        @Test\r
@@ -49,46 +66,13 @@ public class JU_AES {
                passin = aes.decrypt(encrypted);\r
                Assert.assertEquals(orig, new String(passin));\r
        }\r
-\r
-       @Test\r
-       public void testInputStream() throws Exception {\r
-               AES aes = new AES();\r
-               String orig = "I'm a password, really";\r
-               ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
-               CipherInputStream cis = aes.inputStream(bais, true);\r
-               ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
-               Symm.base64.encode(cis, baos);\r
-               cis.close();\r
-\r
-               byte[] b64encrypted;\r
-               System.out.println(new String(b64encrypted=baos.toByteArray()));\r
-               \r
-               \r
-               baos.reset();\r
-               CipherOutputStream cos = aes.outputStream(baos, false);\r
-               Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
-               cos.close();\r
-               Assert.assertEquals(orig, new String(baos.toByteArray()));\r
-       }\r
-\r
-       @Test\r
-       public void testObtain() throws Exception {\r
-               byte[] keygen = Symm.baseCrypt().keygen();\r
-               \r
-               Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
-               \r
-               String orig ="Another Password, please";\r
-               String encrypted = symm.enpass(orig);\r
-               System.out.println(encrypted);\r
-               String decrypted = symm.depass(encrypted);\r
-               System.out.println(decrypted);\r
-               Assert.assertEquals(orig, decrypted);\r
-       }\r
        \r
        @Test\r
-       public void test1() throws Exception {\r
-               AES aes = new AES();\r
-               String orig = "I'm a password, really Cool";\r
+       public void testAESFileStream() throws Exception {\r
+               File fi = new File("test/AESKeyFile");\r
+               AES aes = new AES(fi);\r
+               \r
+               String orig = "I'm a password, really";\r
                byte[] passin = orig.getBytes();\r
                byte[] encrypted = aes.encrypt(passin);\r
                byte[] b64enc = Symm.base64.encode(encrypted);\r
@@ -97,12 +81,23 @@ public class JU_AES {
                encrypted = Symm.base64.decode(b64enc);\r
                passin = aes.decrypt(encrypted);\r
                Assert.assertEquals(orig, new String(passin));\r
+               \r
+               File temp = new File("tempFile");\r
+               try {\r
+                       assertFalse(temp.exists());\r
+                       aes.save(temp);\r
+                       assertTrue(temp.exists());\r
+               } catch (Exception e) {\r
+               } finally {\r
+                       temp.delete();\r
+               }               \r
+               \r
        }\r
 \r
        @Test\r
-       public void testInputStream1() throws Exception {\r
+       public void testInputStream() throws Exception {\r
                AES aes = new AES();\r
-               String orig = "I'm a password, really cool";\r
+               String orig = "I'm a password, really";\r
                ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
                CipherInputStream cis = aes.inputStream(bais, true);\r
                ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
@@ -111,34 +106,44 @@ public class JU_AES {
 \r
                byte[] b64encrypted;\r
                System.out.println(new String(b64encrypted=baos.toByteArray()));\r
-               \r
+\r
+               CipherInputStream cis1 = aes.inputStream(bais, false);\r
+               ByteArrayOutputStream baos1 = new ByteArrayOutputStream();\r
+               Symm.base64.encode(cis1, baos1);\r
+               cis.close();\r
+\r
+               byte[] b64encrypted1;\r
+               System.out.println(new String(b64encrypted1=baos1.toByteArray()));\r
                \r
                baos.reset();\r
                CipherOutputStream cos = aes.outputStream(baos, false);\r
                Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
                cos.close();\r
                Assert.assertEquals(orig, new String(baos.toByteArray()));\r
+               \r
+               OutputStream stream = aes.outputStream(System.out, true);\r
+               assertTrue(stream instanceof CipherOutputStream);\r
+\r
        }\r
 \r
        @Test\r
-       public void testObtain1() throws Exception {\r
+       public void testObtain() throws Exception {\r
                byte[] keygen = Symm.baseCrypt().keygen();\r
                \r
                Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
                \r
-               String orig ="Another Password, please cool";\r
+               String orig ="Another Password, please";\r
                String encrypted = symm.enpass(orig);\r
                System.out.println(encrypted);\r
                String decrypted = symm.depass(encrypted);\r
                System.out.println(decrypted);\r
                Assert.assertEquals(orig, decrypted);\r
        }\r
-\r
        \r
        @Test\r
-       public void test2() throws Exception {\r
+       public void test1() throws Exception {\r
                AES aes = new AES();\r
-               String orig = "I'm a password, really Nice";\r
+               String orig = "I'm a password, really";\r
                byte[] passin = orig.getBytes();\r
                byte[] encrypted = aes.encrypt(passin);\r
                byte[] b64enc = Symm.base64.encode(encrypted);\r
@@ -148,47 +153,13 @@ public class JU_AES {
                passin = aes.decrypt(encrypted);\r
                Assert.assertEquals(orig, new String(passin));\r
        }\r
-\r
-       @Test\r
-       public void testInputStream2() throws Exception {\r
-               AES aes = new AES();\r
-               String orig = "I'm a password, really Nice";\r
-               ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
-               CipherInputStream cis = aes.inputStream(bais, true);\r
-               ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
-               Symm.base64.encode(cis, baos);\r
-               cis.close();\r
-\r
-               byte[] b64encrypted;\r
-               System.out.println(new String(b64encrypted=baos.toByteArray()));\r
-               \r
-               \r
-               baos.reset();\r
-               CipherOutputStream cos = aes.outputStream(baos, false);\r
-               Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
-               cos.close();\r
-               Assert.assertEquals(orig, new String(baos.toByteArray()));\r
-       }\r
-\r
-       @Test\r
-       public void testObtain2() throws Exception {\r
-               byte[] keygen = Symm.baseCrypt().keygen();\r
-               \r
-               Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
-               \r
-               String orig ="Another Password, please Nice";\r
-               String encrypted = symm.enpass(orig);\r
-               System.out.println(encrypted);\r
-               String decrypted = symm.depass(encrypted);\r
-               System.out.println(decrypted);\r
-               Assert.assertEquals(orig, decrypted);\r
-       }\r
-\r
        \r
        @Test\r
-       public void test3() throws Exception {\r
-               AES aes = new AES();\r
-               String orig = "I'm a password, magic";\r
+       public void testAESFileStream1() throws Exception {\r
+               File fi = new File("test/AESKeyFile");\r
+               AES aes = new AES(fi);\r
+               \r
+               String orig = "I'm a password, really";\r
                byte[] passin = orig.getBytes();\r
                byte[] encrypted = aes.encrypt(passin);\r
                byte[] b64enc = Symm.base64.encode(encrypted);\r
@@ -197,12 +168,23 @@ public class JU_AES {
                encrypted = Symm.base64.decode(b64enc);\r
                passin = aes.decrypt(encrypted);\r
                Assert.assertEquals(orig, new String(passin));\r
+               \r
+               File temp = new File("tempFile");\r
+               try {\r
+                       assertFalse(temp.exists());\r
+                       aes.save(temp);\r
+                       assertTrue(temp.exists());\r
+               } catch (Exception e) {\r
+               } finally {\r
+                       temp.delete();\r
+               }               \r
+               \r
        }\r
 \r
        @Test\r
-       public void testInputStream3() throws Exception {\r
+       public void testInputStream1() throws Exception {\r
                AES aes = new AES();\r
-               String orig = "I'm a password, magic";\r
+               String orig = "I'm a password, really";\r
                ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
                CipherInputStream cis = aes.inputStream(bais, true);\r
                ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
@@ -211,27 +193,37 @@ public class JU_AES {
 \r
                byte[] b64encrypted;\r
                System.out.println(new String(b64encrypted=baos.toByteArray()));\r
-               \r
+\r
+               CipherInputStream cis1 = aes.inputStream(bais, false);\r
+               ByteArrayOutputStream baos1 = new ByteArrayOutputStream();\r
+               Symm.base64.encode(cis1, baos1);\r
+               cis.close();\r
+\r
+               byte[] b64encrypted1;\r
+               System.out.println(new String(b64encrypted1=baos1.toByteArray()));\r
                \r
                baos.reset();\r
                CipherOutputStream cos = aes.outputStream(baos, false);\r
                Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
                cos.close();\r
                Assert.assertEquals(orig, new String(baos.toByteArray()));\r
+               \r
+               OutputStream stream = aes.outputStream(System.out, true);\r
+               assertTrue(stream instanceof CipherOutputStream);\r
+\r
        }\r
 \r
        @Test\r
-       public void testObtain3() throws Exception {\r
+       public void testObtain1() throws Exception {\r
                byte[] keygen = Symm.baseCrypt().keygen();\r
                \r
                Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
                \r
-               String orig ="Another Password, magic";\r
+               String orig ="Another Password, please";\r
                String encrypted = symm.enpass(orig);\r
                System.out.println(encrypted);\r
                String decrypted = symm.depass(encrypted);\r
                System.out.println(decrypted);\r
                Assert.assertEquals(orig, decrypted);\r
        }\r
-\r
 }\r