X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cadi%2Faaf%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2Fcm%2Ftest%2FJU_Factory.java;h=5827e8c088aa807f79a5f6a28b4393fc1e89aa53;hb=f85f0889b3b0e5e9694afab4dd01a4a97a155188;hp=6f0397479ade1b8589d3399da8e290b8c95d3299;hpb=4094dee63ac8324fdc47a8b8b04d99a0227b7ba6;p=aaf%2Fauthz.git 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 6f039747..5827e8c0 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; @@ -48,10 +57,10 @@ import java.util.List; 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.StripperInputStream; - +import org.onap.aaf.cadi.configure.CertException; +import org.onap.aaf.cadi.configure.Factory; +import org.onap.aaf.cadi.configure.Factory.Base64InputStream; +import org.onap.aaf.cadi.configure.Factory.StripperInputStream; import org.onap.aaf.misc.env.Env; import org.onap.aaf.misc.env.LogTarget; import org.onap.aaf.misc.env.TimeTaken; @@ -59,34 +68,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 +116,6 @@ public class JU_Factory { privateKeyFile = new File(resourceDirName, "/privateKey"); publicKeyFile.delete(); privateKeyFile.delete(); - - if (resourceDir.list().length == 0) { - resourceDir.delete(); - } } @Test @@ -167,8 +161,8 @@ public class JU_Factory { assertThat(privateKeyString.startsWith("-----BEGIN PRIVATE KEY-----"), is(true)); assertThat(privateKeyString.endsWith("-----END PRIVATE KEY-----\n"), is(true)); - PublicKey publicKey = Factory.toPublicKey(transMock, cleanupString(publicKeyString)); - PrivateKey privateKey = Factory.toPrivateKey(transMock, cleanupString(privateKeyString)); + PublicKey publicKey = Factory.toPublicKey(transMock, publicKeyString); + PrivateKey privateKey = Factory.toPrivateKey(transMock, privateKeyString); Cipher encryptor = Factory.pkCipher(publicKey, true); Cipher decryptor = Factory.pkCipher(privateKey, false); @@ -244,7 +238,7 @@ public class JU_Factory { output = Factory.toString(transMock, certs.toArray(new Certificate[0])[0]); assertThat(output, is(certString)); - List certStrings = new ArrayList(); + List certStrings = new ArrayList<>(); certStrings.add(certString); certStrings.add(certString); certs = Factory.toX509Certificate(certStrings); @@ -295,36 +289,43 @@ 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); - List rawLines = new ArrayList(); + List rawLines = new ArrayList<>(); for (int i = 0; i < lines.length - 2; i++) { rawLines.add(lines[i + 1]); } 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,12 +341,6 @@ public class JU_Factory { } return sb.toString(); } - - /* Not used locally - private void writeToFile(File file, String contents) throws Exception { - writeToFile(file, contents, null); - } - */ private void writeToFile(File file, String contents, String header) throws Exception { PrintWriter writer = new PrintWriter(file, "UTF-8");