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.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import java.io.BufferedReader;
31 import java.io.ByteArrayOutputStream;
33 import java.io.FileNotFoundException;
34 import java.io.FileReader;
35 import java.io.IOException;
36 import java.security.cert.CertificateException;
37 import java.util.ArrayList;
38 import java.util.List;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.aaf.cadi.CadiException;
46 import org.onap.aaf.cadi.configure.Agent;
47 import org.onap.aaf.cadi.configure.PlaceArtifactInKeystore;
48 import org.onap.aaf.misc.env.Env;
49 import org.onap.aaf.misc.env.TimeTaken;
50 import org.onap.aaf.misc.env.Trans;
52 import certman.v1_0.Artifacts.Artifact;
53 import certman.v1_0.CertInfo;
55 public class JU_PlaceArtifactInKeystore {
57 @Mock private Trans transMock;
58 @Mock private CertInfo certInfoMock;
59 @Mock private Artifact artiMock;
61 private static final String caName = "onap";
62 private static final String dirName = "src/test/resources/artifacts";
63 private static final String nsName = "org.onap.test";
64 private static final String mechID = "m12345";
65 private static final String luggagePassword = "12345"; // That's the stupidest combination I've ever heard in my life
67 private static String privateKeyString;
68 private static String x509Chain;
69 private static String x509String;
71 private List<String> certs;
74 public void setup() throws FileNotFoundException, IOException, CertificateException {
75 MockitoAnnotations.initMocks(this);
77 x509Chain = fromFile(new File("src/test/resources/cert.pem"));
78 x509String = fromFile(new File("src/test/resources/exampleCertificate.cer"));
79 privateKeyString = fromFile(new File("src/test/resources/key.pem"));
81 certs = new ArrayList<>();
83 when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
84 when(certInfoMock.getCerts()).thenReturn(certs);
86 when(artiMock.getCa()).thenReturn(caName);
87 when(artiMock.getDir()).thenReturn(dirName);
88 when(artiMock.getNs()).thenReturn(nsName);
89 when(artiMock.getMechid()).thenReturn(mechID);
91 when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString);
93 when(transMock.start("Reconstitute Private Key", Env.SUB)).thenReturn(mock(TimeTaken.class));
97 public static void tearDownOnce() {
99 PlaceArtifactInKeystore.clear();
103 public void test() throws CadiException {
104 // Note: PKCS12 can't be tested in JDK 7 and earlier. Can't handle Trusting Certificates.
105 PlaceArtifactInKeystore placer = new PlaceArtifactInKeystore(Agent.JKS);
107 certs.add(x509String);
108 certs.add(x509Chain);
109 assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
110 for (String ext : new String[] { Agent.JKS, "trust.jks"}) {
111 File f = new File(dirName + '/' + nsName + '.' + ext);
112 assertThat(f.exists(), is(true));
116 assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
118 when(certInfoMock.getCerts()).thenReturn(null);
120 placer._place(transMock, certInfoMock, artiMock);
121 fail("Should've thrown an exception");
122 } catch (Exception e) {
127 private static void cleanup() {
128 File dir = new File(dirName);
130 for (File f : dir.listFiles()) {
137 public String fromFile(File file) throws IOException {
138 BufferedReader br = new BufferedReader(new FileReader(file));
139 ByteArrayOutputStream baos = new ByteArrayOutputStream();
141 baos.write(br.readLine().getBytes());
142 // Here comes the hacky part
143 baos.write("\n".getBytes());
144 while ((line=br.readLine())!=null) {
145 if (line.length()>0) {
146 baos.write(line.getBytes());
147 baos.write("\n".getBytes());
151 return baos.toString();