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.configure.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 caName = "onap";
57 private static final String dirName = "src/test/resources/artifacts";
58 private static final String nsName = "org.onap.test";
59 private static final String mechID = "m12345";
60 private static final String luggagePassword = "12345"; // That's the stupidest combination I've ever heard in my life
62 private static String privateKeyString;
63 private static String x509Chain;
64 private static String x509String;
66 private List<String> certs;
69 public void setup() throws FileNotFoundException, IOException, CertificateException {
70 MockitoAnnotations.initMocks(this);
72 x509Chain = fromFile(new File("src/test/resources/cert.pem"));
73 x509String = fromFile(new File("src/test/resources/exampleCertificate.cer"));
74 privateKeyString = fromFile(new File("src/test/resources/key.pem"));
76 certs = new ArrayList<>();
78 when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
79 when(certInfoMock.getCerts()).thenReturn(certs);
81 when(artiMock.getCa()).thenReturn(caName);
82 when(artiMock.getDir()).thenReturn(dirName);
83 when(artiMock.getNs()).thenReturn(nsName);
84 when(artiMock.getMechid()).thenReturn(mechID);
86 when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString);
88 when(transMock.start("Reconstitute Private Key", Env.SUB)).thenReturn(mock(TimeTaken.class));
92 public static void tearDownOnce() {
94 PlaceArtifactInKeystore.clear();
98 public void test() throws CadiException {
99 // Note: PKCS12 can't be tested in JDK 7 and earlier. Can't handle Trusting Certificates.
100 PlaceArtifactInKeystore placer = new PlaceArtifactInKeystore("jks");
102 certs.add(x509String);
103 certs.add(x509Chain);
104 assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
105 for (String ext : new String[] {"chal", "keyfile", "jks", "trust.jks", "cred.props"}) {
106 File f = new File(dirName + '/' + nsName + '.' + ext);
107 assertThat(f.exists(), is(true));
111 assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
113 when(certInfoMock.getCerts()).thenReturn(null);
115 placer._place(transMock, certInfoMock, artiMock);
116 fail("Should've thrown an exception");
117 } catch (Exception e) {
122 private static void cleanup() {
123 File dir = new File(dirName);
125 for (File f : dir.listFiles()) {
132 public String fromFile(File file) throws IOException {
133 BufferedReader br = new BufferedReader(new FileReader(file));
134 ByteArrayOutputStream baos = new ByteArrayOutputStream();
136 baos.write(br.readLine().getBytes());
137 // Here comes the hacky part
138 baos.write("\n".getBytes());
139 while((line=br.readLine())!=null) {
140 if(line.length()>0) {
141 baos.write(line.getBytes());
142 baos.write("\n".getBytes());
146 return baos.toString();