2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi.cm.test;
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
28 import java.io.BufferedReader;
29 import java.io.ByteArrayOutputStream;
31 import java.io.FileNotFoundException;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.List;
37 import java.security.cert.CertificateException;
41 import org.onap.aaf.cadi.CadiException;
42 import org.onap.aaf.cadi.cm.PlaceArtifactInKeystore;
43 import org.onap.aaf.misc.env.Env;
44 import org.onap.aaf.misc.env.TimeTaken;
45 import org.onap.aaf.misc.env.Trans;
47 import certman.v1_0.Artifacts.Artifact;
48 import certman.v1_0.CertInfo;
50 public class JU_PlaceArtifactInKeystore {
52 @Mock private Trans transMock;
53 @Mock private CertInfo certInfoMock;
54 @Mock private Artifact artiMock;
56 private static final String dirName = "src/test/resources/artifacts";
57 private static final String nsName = "org.onap.test";
58 private static final String mechID = "m12345";
59 private static final String luggagePassword = "12345"; // That's the stupidest combination I've ever heard in my life
61 private static String privateKeyString;
62 private static String x509Chain;
63 private static String x509String;
65 private List<String> certs;
68 public void setup() throws FileNotFoundException, IOException, CertificateException {
69 MockitoAnnotations.initMocks(this);
71 x509Chain = fromFile(new File("src/test/resources/cert.pem"));
72 x509String = fromFile(new File("src/test/resources/exampleCertificate.cer"));
73 privateKeyString = fromFile(new File("src/test/resources/key.pem"));
75 certs = new ArrayList<>();
77 when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
78 when(certInfoMock.getCerts()).thenReturn(certs);
80 when(artiMock.getDir()).thenReturn(dirName);
81 when(artiMock.getNs()).thenReturn(nsName);
82 when(artiMock.getMechid()).thenReturn(mechID);
84 when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString);
86 when(transMock.start("Reconstitute Private Key", Env.SUB)).thenReturn(mock(TimeTaken.class));
90 public static void tearDownOnce() {
92 PlaceArtifactInKeystore.clear();
96 public void test() throws CadiException {
97 PlaceArtifactInKeystore placer = new PlaceArtifactInKeystore("pkcs12");
99 certs.add(x509String);
100 certs.add(x509Chain);
101 assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
102 for (String ext : new String[] {"chal", "keyfile", "pkcs12", "props", "trust.pkcs12"}) {
103 assertThat(new File(dirName + '/' + nsName + '.' + ext).exists(), is(true));
107 assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
109 when(certInfoMock.getCerts()).thenReturn(null);
111 placer._place(transMock, certInfoMock, artiMock);
112 fail("Should've thrown an exception");
113 } catch (Exception e) {
118 private static void cleanup() {
119 File dir = new File(dirName);
121 for (File f : dir.listFiles()) {
128 public String fromFile(File file) throws IOException {
129 BufferedReader br = new BufferedReader(new FileReader(file));
130 ByteArrayOutputStream baos = new ByteArrayOutputStream();
132 baos.write(br.readLine().getBytes());
133 // Here comes the hacky part
134 baos.write("\n".getBytes());
135 while((line=br.readLine())!=null) {
136 if(line.length()>0) {
137 baos.write(line.getBytes());
138 baos.write("\n".getBytes());
142 return baos.toString();