From a7fb5826abbf912a01230f5f5c09a8683d743f9c Mon Sep 17 00:00:00 2001 From: IanHowell Date: Fri, 11 May 2018 11:25:44 -0500 Subject: Improve coverage of cadi-aaf Issue-ID: AAF-223 Change-Id: I1b8330bb0c6bd436fd6836dc7ed69447057cdd5c Signed-off-by: IanHowell --- .../java/org/onap/aaf/cadi/cm/test/JU_CmAgent.java | 123 +++++++++++++++++++++ .../java/org/onap/aaf/cadi/cm/test/JU_Factory.java | 110 +++++++++--------- 2 files changed, 179 insertions(+), 54 deletions(-) create mode 100644 cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_CmAgent.java diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_CmAgent.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_CmAgent.java new file mode 100644 index 00000000..34ccf57b --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_CmAgent.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * ============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==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.cm.test; + +import java.io.ByteArrayInputStream; +import java.io.File; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.aaf.cadi.cm.CmAgent; + +public class JU_CmAgent { + + private static final String resourceDirString = "src/test/resources"; + private static final String aafDir = resourceDirString + "/aaf"; + + private ByteArrayInputStream inStream; + + @Before + public void setup() { + System.setProperty("user.home", aafDir); + + // Simulate user input + inStream = new ByteArrayInputStream("test\nhttp://example.com\nhttp://example.com".getBytes()); + System.setIn(inStream); + } + + @After + public void tearDown() { + recursiveDelete(new File(aafDir)); + } + + @Test + public void test() { + String[] args; + args = new String[] { + "-login", + "-noexit", + }; + CmAgent.main(args); + + inStream.reset(); + args = new String[] { + "noexit=true", + }; + CmAgent.main(args); + + inStream.reset(); + args = new String[] { + "place", + }; + CmAgent.main(args); + + inStream.reset(); + args = new String[] { + "create" + }; + CmAgent.main(args); + + inStream.reset(); + args = new String[] { + "read" + }; + CmAgent.main(args); + + inStream.reset(); + args = new String[] { + "copy" + }; + CmAgent.main(args); + + inStream.reset(); + args = new String[] { + "update" + }; + CmAgent.main(args); + + inStream.reset(); + args = new String[] { + "delete" + }; + CmAgent.main(args); + + inStream.reset(); + args = new String[] { + "showpass" + }; + CmAgent.main(args); + + } + + private void recursiveDelete(File file) { + for (File f : file.listFiles()) { + if (f.isDirectory()) { + recursiveDelete(f); + } + f.delete(); + } + file.delete(); + } + +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_Factory.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_Factory.java index 27eeddc5..fb186b89 100644 --- a/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_Factory.java +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_Factory.java @@ -7,9 +7,9 @@ * * 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. @@ -19,13 +19,22 @@ * * * * ******************************************************************************/ + package org.onap.aaf.cadi.cm.test; -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import static org.mockito.Mockito.*; -import org.junit.*; -import org.mockito.*; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.anyString; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import java.io.BufferedReader; import java.io.File; @@ -50,6 +59,7 @@ import javax.crypto.Cipher; import org.onap.aaf.cadi.cm.CertException; import org.onap.aaf.cadi.cm.Factory; +import org.onap.aaf.cadi.cm.Factory.Base64InputStream; import org.onap.aaf.cadi.cm.Factory.StripperInputStream; import org.onap.aaf.misc.env.Env; @@ -59,34 +69,23 @@ import org.onap.aaf.misc.env.Trans; public class JU_Factory { - @Mock - Trans transMock; - - @Mock - TimeTaken timeTakenMock; - - @Mock - LogTarget logTargetMock; - - @Mock - X509Certificate x509CertMock; - - @Mock - Certificate certMock; - - @Mock - Principal subjectDN; + private static final String message = "The quick brown fox jumps over the lazy dog."; + private static final String subjectDNText = "subjectDN"; + private static final String certText = "Some text that might be included in a certificate"; + private static final String resourceDirName = "src/test/resources"; - private final String resourceDirName = "src/test/resources"; private File resourceDir; private File publicKeyFile; private File privateKeyFile; private File certFile; - - private static final String message = "The quick brown fox jumps over the lazy dog."; - private static final String subjectDNText = "subjectDN"; - private static final String certText = "Some text that might be included in a certificate"; + @Mock private Trans transMock; + @Mock private TimeTaken timeTakenMock; + @Mock private LogTarget logTargetMock; + @Mock private X509Certificate x509CertMock; + @Mock private Certificate certMock; + @Mock private Principal subjectDN; + @Before public void setup() throws CertificateEncodingException { @@ -118,10 +117,6 @@ public class JU_Factory { privateKeyFile = new File(resourceDirName, "/privateKey"); publicKeyFile.delete(); privateKeyFile.delete(); - - if (resourceDir.list().length == 0) { - resourceDir.delete(); - } } @Test @@ -295,25 +290,32 @@ public class JU_Factory { assertThat(Factory.verify(transMock, signedString.getBytes(), signedBytes, kp.getPublic()), is(true)); } - // TODO: Ian - finish these tests - // @Test - // public void base64ISTest() throws Exception { - // KeyPair kp = Factory.generateKeyPair(transMock); - - // String privateKeyString = Factory.toString(transMock, kp.getPrivate()); - // String cleaned = cleanupString(privateKeyString); - // System.out.println(cleaned); - // writeToFile(privateKeyFile, cleaned); - // Base64InputStream b64is = new Base64InputStream(privateKeyFile); - // byte[] buffer = new byte[10000]; - // b64is.read(buffer); - // System.out.println(new String(buffer)); - // b64is.close(); - // } - - // @Test - // public void getSecurityProviderTest() { - // } + @Test + public void base64ISTest() throws Exception { + KeyPair kp = Factory.generateKeyPair(transMock); + + String privateKeyString = Factory.toString(transMock, kp.getPrivate()); + String cleaned = cleanupString(privateKeyString); + writeToFile(privateKeyFile, cleaned, null); + Base64InputStream b64is = new Base64InputStream(privateKeyFile); + byte[] buffer = new byte[10000]; + b64is.read(buffer); + b64is.close(); + + FileInputStream fis = new FileInputStream(privateKeyFile); + b64is = new Base64InputStream(fis); + b64is.close(); + fis.close(); + } + + @Test + public void getSecurityProviderTest() throws CertException { + String[][] params = { + {"test", "test"}, + {"test", "test"}, + }; + assertThat(Factory.getSecurityProvider("PKCS12", params), is(nullValue())); + } private String cleanupString(String str) { String[] lines = str.split("\n", 0); @@ -324,7 +326,7 @@ public class JU_Factory { return join("", rawLines); } - /** + /** * Note: String.join is not part of JDK 7, which is what we compile to for CADI */ private String join(String delim, List rawLines) { @@ -340,7 +342,7 @@ public class JU_Factory { } return sb.toString(); } - + private void writeToFile(File file, String contents, String header) throws Exception { PrintWriter writer = new PrintWriter(file, "UTF-8"); if (header != null) { -- cgit 1.2.3-korg