Check Perm Instance ending in colon
[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 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
61
62         private static String privateKeyString;
63         private static String x509Chain;
64         private static String x509String;
65
66         private List<String> certs;
67
68         @Before
69         public void setup() throws FileNotFoundException, IOException, CertificateException {
70                 MockitoAnnotations.initMocks(this);
71
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"));
75
76                 certs = new ArrayList<>();
77
78                 when(certInfoMock.getChallenge()).thenReturn(luggagePassword);
79                 when(certInfoMock.getCerts()).thenReturn(certs);
80
81                 when(artiMock.getCa()).thenReturn(caName);
82                 when(artiMock.getDir()).thenReturn(dirName);
83                 when(artiMock.getNs()).thenReturn(nsName);
84                 when(artiMock.getMechid()).thenReturn(mechID);
85
86                 when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString);
87
88                 when(transMock.start("Reconstitute Private Key", Env.SUB)).thenReturn(mock(TimeTaken.class));
89         }
90
91         @AfterClass
92         public static void tearDownOnce() {
93                 cleanup();
94                 PlaceArtifactInKeystore.clear();
95         }
96
97         @Test
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");
101
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", "props", "trust.jks"}) {
106                         assertThat(new File(dirName + '/' + nsName + '.' + ext).exists(), is(true));
107                 }
108
109                 // coverage
110                 assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true));
111                 
112                 when(certInfoMock.getCerts()).thenReturn(null);
113                 try {
114                         placer._place(transMock, certInfoMock, artiMock);
115                         fail("Should've thrown an exception");
116                 } catch (Exception e) {
117                 }
118
119         }
120
121         private static void cleanup() {
122                 File dir = new File(dirName);
123                 if (dir.exists()) {
124                         for (File f : dir.listFiles()) {
125                                 f.delete();
126                         }
127                         dir.delete();
128                 }
129         }
130
131         public String fromFile(File file) throws IOException {
132                 BufferedReader br = new BufferedReader(new FileReader(file));
133                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
134                 String line;
135                 baos.write(br.readLine().getBytes());
136                 // Here comes the hacky part
137                 baos.write("\n".getBytes());
138                 while((line=br.readLine())!=null) {
139                         if(line.length()>0) {
140                                 baos.write(line.getBytes());
141                                 baos.write("\n".getBytes());
142                         }
143                 }
144                 br.close();
145                 return baos.toString();
146         }
147 }