\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
passin = aes.decrypt(encrypted);\r
Assert.assertEquals(orig, new String(passin));\r
}\r
+ \r
+ @Test\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
+ System.out.println(new String(b64enc));\r
+ \r
+ 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 testInputStream() throws Exception {\r
\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
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";\r
+ byte[] passin = orig.getBytes();\r
+ byte[] encrypted = aes.encrypt(passin);\r
+ byte[] b64enc = Symm.base64.encode(encrypted);\r
+ System.out.println(new String(b64enc));\r
+ \r
+ encrypted = Symm.base64.decode(b64enc);\r
+ passin = aes.decrypt(encrypted);\r
+ Assert.assertEquals(orig, new String(passin));\r
+ }\r
+ \r
+ @Test\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
+ System.out.println(new String(b64enc));\r
+ \r
+ 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
+ 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
+ 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
+ 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