package org.onap.aaf.cadi.cm.test;
-import static org.junit.Assert.*;
-import static org.hamcrest.CoreMatchers.*;
-import static org.mockito.Mockito.*;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
+import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
-import java.security.cert.CertificateException;
-
-import org.junit.*;
-import org.mockito.*;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import org.onap.aaf.cadi.CadiException;
-import org.onap.aaf.cadi.cm.PlaceArtifactInKeystore;
+import org.onap.aaf.cadi.configure.Agent;
+import org.onap.aaf.cadi.configure.PlaceArtifactInKeystore;
import org.onap.aaf.misc.env.Env;
import org.onap.aaf.misc.env.TimeTaken;
import org.onap.aaf.misc.env.Trans;
public class JU_PlaceArtifactInKeystore {
- @Mock private Trans transMock;
- @Mock private CertInfo certInfoMock;
- @Mock private Artifact artiMock;
-
- private static final String dirName = "src/test/resources/artifacts";
- private static final String nsName = "org.onap.test";
- private static final String mechID = "m12345";
- private static final String luggagePassword = "12345"; // That's the stupidest combination I've ever heard in my life
-
- private static String privateKeyString;
- private static String x509Chain;
- private static String x509String;
-
- private List<String> certs;
-
- @Before
- public void setup() throws FileNotFoundException, IOException, CertificateException {
- MockitoAnnotations.initMocks(this);
-
- x509Chain = fromFile(new File("src/test/resources/cert.pem"));
- x509String = fromFile(new File("src/test/resources/exampleCertificate.cer"));
- privateKeyString = fromFile(new File("src/test/resources/key.pem"));
-
- certs = new ArrayList<>();
-
- when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
- when(certInfoMock.getCerts()).thenReturn(certs);
-
- when(artiMock.getDir()).thenReturn(dirName);
- when(artiMock.getNs()).thenReturn(nsName);
- when(artiMock.getMechid()).thenReturn(mechID);
-
- when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString);
-
- when(transMock.start("Reconstitute Private Key", Env.SUB)).thenReturn(mock(TimeTaken.class));
- }
-
- @AfterClass
- public static void tearDownOnce() {
- cleanup();
- PlaceArtifactInKeystore.clear();
- }
-
- @Test
- public void test() throws CadiException {
- PlaceArtifactInKeystore placer = new PlaceArtifactInKeystore("pkcs12");
-
- certs.add(x509String);
- certs.add(x509Chain);
- assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
- for (String ext : new String[] {"chal", "keyfile", "pkcs12", "props", "trust.pkcs12"}) {
- assertThat(new File(dirName + '/' + nsName + '.' + ext).exists(), is(true));
- }
-
- // coverage
- assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
-
- when(certInfoMock.getCerts()).thenReturn(null);
- try {
- placer._place(transMock, certInfoMock, artiMock);
- fail("Should've thrown an exception");
- } catch (Exception e) {
- }
-
- }
-
- private static void cleanup() {
- File dir = new File(dirName);
- if (dir.exists()) {
- for (File f : dir.listFiles()) {
- f.delete();
- }
- dir.delete();
- }
- }
-
- public String fromFile(File file) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader(file));
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- String line;
- baos.write(br.readLine().getBytes());
- // Here comes the hacky part
- baos.write("\n".getBytes());
- while((line=br.readLine())!=null) {
- if(line.length()>0) {
- baos.write(line.getBytes());
- baos.write("\n".getBytes());
- }
- }
- br.close();
- return baos.toString();
- }
+ @Mock private Trans transMock;
+ @Mock private CertInfo certInfoMock;
+ @Mock private Artifact artiMock;
+
+ private static final String caName = "onap";
+ private static final String dirName = "src/test/resources/artifacts";
+ private static final String nsName = "org.onap.test";
+ private static final String mechID = "m12345";
+ private static final String luggagePassword = "12345"; // That's the stupidest combination I've ever heard in my life
+
+ private static String privateKeyString;
+ private static String x509Chain;
+ private static String x509String;
+
+ private List<String> certs;
+
+ @Before
+ public void setup() throws FileNotFoundException, IOException, CertificateException {
+ MockitoAnnotations.initMocks(this);
+
+ x509Chain = fromFile(new File("src/test/resources/cert.pem"));
+ x509String = fromFile(new File("src/test/resources/exampleCertificate.cer"));
+ privateKeyString = fromFile(new File("src/test/resources/key.pem"));
+
+ certs = new ArrayList<>();
+
+ when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
+ when(certInfoMock.getCerts()).thenReturn(certs);
+
+ when(artiMock.getCa()).thenReturn(caName);
+ when(artiMock.getDir()).thenReturn(dirName);
+ when(artiMock.getNs()).thenReturn(nsName);
+ when(artiMock.getMechid()).thenReturn(mechID);
+
+ when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString);
+
+ when(transMock.start("Reconstitute Private Key", Env.SUB)).thenReturn(mock(TimeTaken.class));
+ }
+
+ @AfterClass
+ public static void tearDownOnce() {
+ cleanup();
+ PlaceArtifactInKeystore.clear();
+ }
+
+ @Test
+ public void test() throws CadiException {
+ // Note: PKCS12 can't be tested in JDK 7 and earlier. Can't handle Trusting Certificates.
+ PlaceArtifactInKeystore placer = new PlaceArtifactInKeystore(Agent.JKS);
+
+ certs.add(x509String);
+ certs.add(x509Chain);
+ assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
+ for (String ext : new String[] {"chal", "keyfile", Agent.JKS, "trust.jks", "cred.props"}) {
+ File f = new File(dirName + '/' + nsName + '.' + ext);
+ assertThat(f.exists(), is(true));
+ }
+
+ // coverage
+ assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
+
+ when(certInfoMock.getCerts()).thenReturn(null);
+ try {
+ placer._place(transMock, certInfoMock, artiMock);
+ fail("Should've thrown an exception");
+ } catch (Exception e) {
+ }
+
+ }
+
+ private static void cleanup() {
+ File dir = new File(dirName);
+ if (dir.exists()) {
+ for (File f : dir.listFiles()) {
+ f.delete();
+ }
+ dir.delete();
+ }
+ }
+
+ public String fromFile(File file) throws IOException {
+ BufferedReader br = new BufferedReader(new FileReader(file));
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ String line;
+ baos.write(br.readLine().getBytes());
+ // Here comes the hacky part
+ baos.write("\n".getBytes());
+ while ((line=br.readLine())!=null) {
+ if (line.length()>0) {
+ baos.write(line.getBytes());
+ baos.write("\n".getBytes());
+ }
+ }
+ br.close();
+ return baos.toString();
+ }
}