From b06f7e948bb2e4a7b34d436b5a4c2505b8a58f23 Mon Sep 17 00:00:00 2001 From: sg481n Date: Wed, 4 Oct 2017 16:01:39 +0000 Subject: Improve code coverage for aaf cadi modules INcluded new test case Issue-ID: AAF-79 Change-Id: I6edba798b1b3f7f37a9f51bc3f2c65aa16a959c4 Signed-off-by: sg481n --- core/src/test/java/org/onap/aaf/cadi/JU_AES.java | 174 +++++++++--------- .../java/org/onap/aaf/cadi/JU_CadiWrapTest.java | 197 +++++++++++++++++++++ .../java/org/onap/aaf/cadi/test/JU_Passcode.java | 17 ++ 3 files changed, 297 insertions(+), 91 deletions(-) create mode 100644 core/src/test/java/org/onap/aaf/cadi/JU_CadiWrapTest.java (limited to 'core/src') 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); } - } diff --git a/core/src/test/java/org/onap/aaf/cadi/JU_CadiWrapTest.java b/core/src/test/java/org/onap/aaf/cadi/JU_CadiWrapTest.java new file mode 100644 index 0000000..da81e0c --- /dev/null +++ b/core/src/test/java/org/onap/aaf/cadi/JU_CadiWrapTest.java @@ -0,0 +1,197 @@ +/******************************************************************************* + * ============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==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package org.onap.aaf.cadi; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.when; + +import java.security.Principal; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.CachedPrincipal.Resp; +import org.onap.aaf.cadi.filter.MapPermConverter; +import org.onap.aaf.cadi.lur.EpiLur; +import org.onap.aaf.cadi.taf.TafResp; + +public class JU_CadiWrapTest { + + @Mock + private HttpServletRequest request; + + @Mock + private TafResp tafResp; + + @Mock + private Principal principle; + + @Mock + private Lur lur; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testInstantiate() throws CadiException { + Access a = new PropAccess(); + when(tafResp.getAccess()).thenReturn(a); + + lur.fishAll(isA(Principal.class), isA(List.class)); + + EpiLur lur1 = new EpiLur(lur); + + CadiWrap wrap = new CadiWrap(request, tafResp, lur1); + + assertNull(wrap.getUserPrincipal()); + assertNull(wrap.getRemoteUser()); + assertNull(wrap.getUser()); + assertEquals(wrap.getPermissions(principle).size(), 0); + + byte[] arr = {'1','2'}; + wrap.setCred(arr); + + assertEquals(arr, wrap.getCred()); + + wrap.setUser("User1"); + assertEquals("User1", wrap.getUser()); + + wrap.invalidate("1"); + + assertFalse(wrap.isUserInRole(null)); + + wrap.set(tafResp, lur); + + wrap.invalidate("2"); + + wrap.isUserInRole("User1"); + } + + @Test + public void testInstantiateWithPermConverter() throws CadiException { + Access a = new PropAccess(); + when(tafResp.getAccess()).thenReturn(a); + when(tafResp.getPrincipal()).thenReturn(principle); + + + CachingLur lur1 = new CachingLur() { + + @Override + public Permission createPerm(String p) { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean fish(Principal bait, Permission pond) { + // TODO Auto-generated method stub + return true; + } + + @Override + public void fishAll(Principal bait, List permissions) { + // TODO Auto-generated method stub + + } + + @Override + public void destroy() { + // TODO Auto-generated method stub + + } + + @Override + public boolean handlesExclusively(Permission pond) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean supports(String userName) { + // TODO Auto-generated method stub + return false; + } + + @Override + public void remove(String user) { + // TODO Auto-generated method stub + + } + + @Override + public Resp reload(User user) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setDebug(String commaDelimIDsOrNull) { + // TODO Auto-generated method stub + + } + + @Override + public void clear(Principal p, StringBuilder sb) { + // TODO Auto-generated method stub + + } + }; + + MapPermConverter pc = new MapPermConverter(); + + CadiWrap wrap = new CadiWrap(request, tafResp, lur1, pc); + + assertNotNull(wrap.getUserPrincipal()); + assertNull(wrap.getRemoteUser()); + assertNull(wrap.getUser()); + + byte[] arr = {'1','2'}; + wrap.setCred(arr); + + assertEquals(arr, wrap.getCred()); + + wrap.setUser("User1"); + assertEquals("User1", wrap.getUser()); + + wrap.invalidate("1"); + wrap.setPermConverter(new MapPermConverter()); + + assertTrue(wrap.getLur() instanceof CachingLur); + assertTrue(wrap.isUserInRole("User1")); + + wrap.set(tafResp, lur); + assertFalse(wrap.isUserInRole("Perm1")); + } +} diff --git a/core/src/test/java/org/onap/aaf/cadi/test/JU_Passcode.java b/core/src/test/java/org/onap/aaf/cadi/test/JU_Passcode.java index 40ceaa1..f51b131 100644 --- a/core/src/test/java/org/onap/aaf/cadi/test/JU_Passcode.java +++ b/core/src/test/java/org/onap/aaf/cadi/test/JU_Passcode.java @@ -32,6 +32,17 @@ import org.onap.aaf.cadi.Symm; import junit.framework.Assert; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; + +import org.junit.AfterClass; +import org.junit.Test; +import org.onap.aaf.cadi.CmdLine; +import org.onap.aaf.cadi.Symm; + +import junit.framework.Assert; + public class JU_Passcode { @Test public void test() throws Exception { @@ -50,6 +61,7 @@ public class JU_Passcode { symm = Symm.obtain(fis); } finally { fis.close(); + fi.delete(); } String samples[] = {"activevos","ThisIsATestPassword","I have spaces","I have 's, /s and &s and _s"}; ByteArrayOutputStream baos; @@ -85,5 +97,10 @@ public class JU_Passcode { } + + @AfterClass + public static void tearDown() { + + } } -- cgit 1.2.3-korg