Sonar Fixes, Formatting
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / cm / test / JU_PlaceArtifactInKeystore.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.cm.test;
23
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;
29
30 import java.io.BufferedReader;
31 import java.io.ByteArrayOutputStream;
32 import java.io.File;
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;
39
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;
51
52 import certman.v1_0.Artifacts.Artifact;
53 import certman.v1_0.CertInfo;
54
55 public class JU_PlaceArtifactInKeystore {
56
57     @Mock private Trans transMock;
58     @Mock private CertInfo certInfoMock;
59     @Mock private Artifact artiMock;
60
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
66
67     private static String privateKeyString;
68     private static String x509Chain;
69     private static String x509String;
70
71     private List<String> certs;
72
73     @Before
74     public void setup() throws FileNotFoundException, IOException, CertificateException {
75         MockitoAnnotations.initMocks(this);
76
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"));
80
81         certs = new ArrayList<>();
82
83         when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
84         when(certInfoMock.getCerts()).thenReturn(certs);
85
86         when(artiMock.getCa()).thenReturn(caName);
87         when(artiMock.getDir()).thenReturn(dirName);
88         when(artiMock.getNs()).thenReturn(nsName);
89         when(artiMock.getMechid()).thenReturn(mechID);
90
91         when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString);
92
93         when(transMock.start("Reconstitute Private Key", Env.SUB)).thenReturn(mock(TimeTaken.class));
94     }
95
96     @AfterClass
97     public static void tearDownOnce() {
98         cleanup();
99         PlaceArtifactInKeystore.clear();
100     }
101
102     @Test
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);
106
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));
113         }
114
115         // coverage
116         assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
117
118         when(certInfoMock.getCerts()).thenReturn(null);
119         try {
120             placer._place(transMock, certInfoMock, artiMock);
121             fail("Should've thrown an exception");
122         } catch (Exception e) {
123         }
124
125     }
126
127     private static void cleanup() {
128         File dir = new File(dirName);
129         if (dir.exists()) {
130             for (File f : dir.listFiles()) {
131                 f.delete();
132             }
133             dir.delete();
134         }
135     }
136
137     public String fromFile(File file) throws IOException {
138         BufferedReader br = new BufferedReader(new FileReader(file));
139         ByteArrayOutputStream baos = new ByteArrayOutputStream();
140         String line;
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());
148             }
149         }
150         br.close();
151         return baos.toString();
152     }
153 }