From b5985fb5667b3ee9b6a4a0675fb57f55e7a288b7 Mon Sep 17 00:00:00 2001 From: sg481n Date: Sun, 1 Oct 2017 21:06:52 +0000 Subject: Improve code coverage for aaf cadi modules Updated few testcases. Issue-ID: AAF-79 Change-Id: If0760b6f3e74d52fa5132224bf1e6f5e3099f0e4 Signed-off-by: sg481n --- core/pom.xml | 11 +- .../java/org/onap/aaf/cadi/CadiExceptionTest.java | 64 +++++++ core/src/test/java/org/onap/aaf/cadi/JU_AES.java | 149 +++++++++++++++ core/src/test/java/org/onap/aaf/cadi/UserTest.java | 54 ++++++ .../org/onap/aaf/cadi/lur/test/JU_LocalLur.java | 53 ++++++ .../java/org/onap/aaf/cadi/test/JU_Base64.java | 212 +++++++++++++++++++++ .../test/java/org/onap/aaf/cadi/test/JU_Hash.java | 55 ++++++ .../java/org/onap/aaf/cadi/util/JU_NetMask.java | 24 +++ .../test/java/org/onap/aaf/cadi/util/JU_Split.java | 19 ++ .../java/org/onap/aaf/cadi/util/JU_TheConsole.java | 1 + .../org/onap/aaf/cadi/util/JU_UserChainManip.java | 1 + 11 files changed, 638 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/pom.xml b/core/pom.xml index 3088b20..32ad054 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -51,11 +51,12 @@ /content/sites/site/org/onap/aaf/cadi/${project.artifactId}/${project.version} - - - org.mockito - mockito-all - + + org.mockito + mockito-all + 1.9.5 + test + javax.servlet servlet-api diff --git a/core/src/test/java/org/onap/aaf/cadi/CadiExceptionTest.java b/core/src/test/java/org/onap/aaf/cadi/CadiExceptionTest.java index 29a0a8b..f994b60 100644 --- a/core/src/test/java/org/onap/aaf/cadi/CadiExceptionTest.java +++ b/core/src/test/java/org/onap/aaf/cadi/CadiExceptionTest.java @@ -22,6 +22,10 @@ ******************************************************************************/ package org.onap.aaf.cadi; +import static org.junit.Assert.*; + +import org.junit.Test; + import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; @@ -57,5 +61,65 @@ public class CadiExceptionTest { assertThat(exception.getMessage(), is("New Exception")); } + + @Test + public void testCadiException1() { + CadiException exception = new CadiException(); + + assertNotNull(exception); + } + + @Test + public void testCadiExceptionString1() { + CadiException exception = new CadiException("New Exception"); + assertNotNull(exception); + assertThat(exception.getMessage(), is("New Exception")); + } + + @Test + public void testCadiExceptionThrowable1() { + CadiException exception = new CadiException(new Throwable("New Exception")); + assertNotNull(exception); + assertThat(exception.getMessage(), is("java.lang.Throwable: New Exception")); + } + + @Test + public void testCadiExceptionStringThrowable1() { + CadiException exception = new CadiException("New Exception",new Throwable("New Exception")); + assertNotNull(exception); + assertThat(exception.getMessage(), is("New Exception")); + + } + + @Test + public void testCadiException2() { + CadiException exception = new CadiException(); + + assertNotNull(exception); + } + + @Test + public void testCadiExceptionString2() { + CadiException exception = new CadiException("New Exception"); + assertNotNull(exception); + assertThat(exception.getMessage(), is("New Exception")); + } + + @Test + public void testCadiExceptionThrowable2() { + CadiException exception = new CadiException(new Throwable("New Exception")); + assertNotNull(exception); + assertThat(exception.getMessage(), is("java.lang.Throwable: New Exception")); + } + + @Test + public void testCadiExceptionStringThrowable2() { + CadiException exception = new CadiException("New Exception",new Throwable("New Exception")); + assertNotNull(exception); + assertThat(exception.getMessage(), is("New Exception")); + + } + + } 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 11e40a6..4779f09 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 @@ -84,5 +84,154 @@ public class JU_AES { 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"; + byte[] passin = orig.getBytes(); + byte[] encrypted = aes.encrypt(passin); + byte[] b64enc = Symm.base64.encode(encrypted); + System.out.println(new String(b64enc)); + + encrypted = Symm.base64.decode(b64enc); + passin = aes.decrypt(encrypted); + Assert.assertEquals(orig, new String(passin)); + } + + @Test + public void testInputStream1() throws Exception { + AES aes = new AES(); + String orig = "I'm a password, really cool"; + 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 testObtain1() throws Exception { + byte[] keygen = Symm.baseCrypt().keygen(); + + Symm symm = Symm.obtain(new ByteArrayInputStream(keygen)); + + String orig ="Another Password, please cool"; + 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 { + AES aes = new AES(); + String orig = "I'm a password, really Nice"; + byte[] passin = orig.getBytes(); + byte[] encrypted = aes.encrypt(passin); + byte[] b64enc = Symm.base64.encode(encrypted); + System.out.println(new String(b64enc)); + + encrypted = Symm.base64.decode(b64enc); + 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"; + byte[] passin = orig.getBytes(); + byte[] encrypted = aes.encrypt(passin); + byte[] b64enc = Symm.base64.encode(encrypted); + System.out.println(new String(b64enc)); + + encrypted = Symm.base64.decode(b64enc); + passin = aes.decrypt(encrypted); + Assert.assertEquals(orig, new String(passin)); + } + + @Test + public void testInputStream3() throws Exception { + AES aes = new AES(); + String orig = "I'm a password, magic"; + 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 testObtain3() throws Exception { + byte[] keygen = Symm.baseCrypt().keygen(); + + Symm symm = Symm.obtain(new ByteArrayInputStream(keygen)); + + String orig ="Another Password, magic"; + String encrypted = symm.enpass(orig); + System.out.println(encrypted); + String decrypted = symm.depass(encrypted); + System.out.println(decrypted); + Assert.assertEquals(orig, decrypted); + } } diff --git a/core/src/test/java/org/onap/aaf/cadi/UserTest.java b/core/src/test/java/org/onap/aaf/cadi/UserTest.java index 3e8254b..f5ea6fc 100644 --- a/core/src/test/java/org/onap/aaf/cadi/UserTest.java +++ b/core/src/test/java/org/onap/aaf/cadi/UserTest.java @@ -22,6 +22,11 @@ ******************************************************************************/ package org.onap.aaf.cadi; +import static org.junit.Assert.*; + +import org.junit.Test; + + import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; import static org.mockito.Mockito.when; @@ -64,6 +69,15 @@ public class UserTest { user.incCount(); assertThat(user.count, is(1)); } + + @Test + public void testCountCheck1() { + User user = new User(principal); + user.resetCount(); + assertThat(user.count, is(0)); + user.incCount(); + assertThat(user.count, is(1)); + } @Test public void testPerm() throws InterruptedException { @@ -84,6 +98,25 @@ public class UserTest { assertTrue(user.noPerms()); } + @Test + public void testPerm1() throws InterruptedException { + User user = new User(principal); + assertThat(user.permExpires(), is(Long.MAX_VALUE)); + user.renewPerm(); + Thread.sleep(1); + assertThat(user.permExpired(), is(true)); + user = new User(principal,100); + assertTrue(user.noPerms()); + user.add(permission); + assertFalse(user.noPerms()); + user.setNoPerms(); + assertThat(user.permExpired(), is(false)); + assertFalse(user.permsUnloaded()); + user.perms = null; + assertTrue(user.permsUnloaded()); + assertTrue(user.noPerms()); + } + @Test public void testAddValuesToNewMap() { User user = new User(principal); @@ -104,4 +137,25 @@ public class UserTest { assertThat(user.toString(), is("Principal|:NewKey")); } + + @Test + public void testAddValuesToNewMap1() { + User user = new User(principal); + Map newMap = new HashMap(); + + assertFalse(user.contains(permission)); + + user.add(newMap, permission); + user.setMap(newMap); + + assertTrue(user.contains(permission)); + + List sink = new ArrayList(); + user.copyPermsTo(sink); + + assertThat(sink.size(), is(1)); + assertTrue(sink.contains(permission)); + + assertThat(user.toString(), is("Principal|:NewKey")); + } } diff --git a/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalLur.java b/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalLur.java index f050a6b..a5ea5f2 100644 --- a/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalLur.java +++ b/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalLur.java @@ -97,4 +97,57 @@ public class JU_LocalLur { } } + @Test + public void test1() throws IOException { + Symm symmetric = Symm.baseCrypt().obtain(); + LocalLur up; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + baos.write(Symm.ENC.getBytes()); + symmetric.enpass("", baos); + PropAccess ta = new PropAccess(); + Lur ml = up = new LocalLur(ta,"myname:groupC,groupD","admin:myname,yourname;suser:hisname1,hername2,m1234%"+baos.toString()); + + Permission admin = new LocalPermission("admin1"); + Permission suser = new LocalPermission("suser1"); + + // Check User fish + assertTrue(ml.fish(new JUPrincipal("myname1"),admin)); + assertTrue(ml.fish(new JUPrincipal("hisname1"),admin)); + assertFalse(ml.fish(new JUPrincipal("noname1"),admin)); + assertTrue(ml.fish(new JUPrincipal("itsname1"),suser)); + assertTrue(ml.fish(new JUPrincipal("hername1"),suser)); + assertFalse(ml.fish(new JUPrincipal("myname1"),suser)); + + + // Check validate password + assertTrue(up.validate("m1234",Type.PASSWORD, "".getBytes())); + assertFalse(up.validate("m1234",Type.PASSWORD, "badPass".getBytes())); + + // Check fishAll + Set set = new TreeSet(); + List perms = new ArrayList(); + ml.fishAll(new JUPrincipal("myname"), perms); + for(Permission p : perms) { + set.add(p.getKey()); + } + assertEquals("[admin, groupA, groupB]",set.toString()); + UsersDump.write(System.out, up); + System.out.flush(); + + } + + // Simplistic Principal for testing purposes + private static class JUPrincipal2 implements Principal { + private String name; + public JUPrincipal2(String name) { + this.name = name; + } +// @Override + public String getName() { + return name; + } + } + + + } diff --git a/core/src/test/java/org/onap/aaf/cadi/test/JU_Base64.java b/core/src/test/java/org/onap/aaf/cadi/test/JU_Base64.java index 4d49c92..3596619 100644 --- a/core/src/test/java/org/onap/aaf/cadi/test/JU_Base64.java +++ b/core/src/test/java/org/onap/aaf/cadi/test/JU_Base64.java @@ -153,4 +153,216 @@ public class JU_Base64 { assertEquals(toEncode,result); } + + @Test + public void test1() throws Exception { + // Test with different Padding + encode("leas", "bGVhcw=="); + encode("leasu", "bGVhc3U="); + encode("leasur", "bGVhc3Vy"); + encode("leasure", "bGVhc3VyZQ=="); + encode("leasure.","bGVhc3VyZS4="); + + // Test with line ends + encode(encoding, expected); + + int ITER = 2000; + System.out.println("Priming by Encoding Base64 " + ITER + " times"); + long start = System.nanoTime(); + for(int i=0;i