02054cbfb00dbfc640d8bcf3081b55aa81668364
[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.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27
28 import java.io.BufferedReader;
29 import java.io.ByteArrayOutputStream;
30 import java.io.File;
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;
36
37 import java.security.cert.CertificateException;
38
39 import org.junit.*;
40 import org.mockito.*;
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;
46
47 import certman.v1_0.Artifacts.Artifact;
48 import certman.v1_0.CertInfo;
49
50 public class JU_PlaceArtifactInKeystore {
51
52         @Mock private Trans transMock;
53         @Mock private CertInfo certInfoMock;
54         @Mock private Artifact artiMock;
55
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
60
61         private static String privateKeyString;
62         private static String x509Chain;
63         private static String x509String;
64
65         private List<String> certs;
66
67         @Before
68         public void setup() throws FileNotFoundException, IOException, CertificateException {
69                 MockitoAnnotations.initMocks(this);
70
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"));
74
75                 certs = new ArrayList<>();
76
77                 when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
78                 when(certInfoMock.getCerts()).thenReturn(certs);
79
80                 when(artiMock.getDir()).thenReturn(dirName);
81                 when(artiMock.getNs()).thenReturn(nsName);
82                 when(artiMock.getMechid()).thenReturn(mechID);
83
84                 when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString);
85
86                 when(transMock.start("Reconstitute Private Key", Env.SUB)).thenReturn(mock(TimeTaken.class));
87         }
88
89         @AfterClass
90         public static void tearDownOnce() {
91                 cleanup();
92                 PlaceArtifactInKeystore.clear();
93         }
94
95         @Test
96         public void test() throws CadiException {
97                 PlaceArtifactInKeystore placer = new PlaceArtifactInKeystore("pkcs12");
98
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));
104                 }
105
106                 // coverage
107                 assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
108                 
109                 when(certInfoMock.getCerts()).thenReturn(null);
110                 try {
111                         placer._place(transMock, certInfoMock, artiMock);
112                         fail("Should've thrown an exception");
113                 } catch (Exception e) {
114                 }
115
116         }
117
118         private static void cleanup() {
119                 File dir = new File(dirName);
120                 if (dir.exists()) {
121                         for (File f : dir.listFiles()) {
122                                 f.delete();
123                         }
124                         dir.delete();
125                 }
126         }
127
128         public String fromFile(File file) throws IOException {
129                 BufferedReader br = new BufferedReader(new FileReader(file));
130                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
131                 String line;
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());
139                         }
140                 }
141                 br.close();
142                 return baos.toString();
143         }
144 }