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