Add of config for copier
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / certification / file / TestCertificateProvider.java
1 /*============LICENSE_START=======================================================
2  * oom-truststore-merger
3  * ================================================================================
4  * Copyright (C) 2020 Nokia. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.oom.truststoremerger.certification.file;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.nio.file.StandardCopyOption;
28 import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException;
29 import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException;
30 import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore;
31 import org.onap.oom.truststoremerger.certification.file.model.PemTruststore;
32 import org.onap.oom.truststoremerger.certification.file.provider.JavaCertificateStoreController;
33 import org.onap.oom.truststoremerger.certification.file.provider.CertificateStoreControllerFactory;
34 import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController;
35
36 public class TestCertificateProvider {
37
38     public static final String SAMPLE_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-p12.p12";
39     public static final String SAMPLE_P12_TRUSTSTORE_PASSWORD = "88y9v5D8H3SG6bZWRVHDfOAo";
40     public static final String TMP_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-p12.p12";
41
42     public static final String SAMPLE_P12_KEYSTORE_FILE_PATH = "src/test/resources/keystore.p12";
43     public static final String SAMPLE_P12_KEYSTORE_PASSWORD = "Foh49MJNYI7S_pEzE9gvUDSu";
44
45     public static final String SAMPLE_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-jks.jks";
46     public static final String SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH = "src/test/resources/truststore-jks-uniq.jks";
47     public static final String SAMPLE_JKS_TRUSTSTORE_PASSWORD = "EOyuFbuYDyq_EhpboM72RHua";
48     public static final String TMP_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-jks.jks";
49
50     public static final String SAMPLE_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore.pem";
51     public static final String EMPTY_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/empty-truststore.pem";
52     public static final String TMP_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore.pem";
53     public static final String SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH = "src/test/resources/truststore-with-private-key.pem";
54
55     private static final CertificateStoreControllerFactory certificateStoreControllerFactory = new CertificateStoreControllerFactory();
56
57     public static JavaTruststore getSampleP12Truststore() throws LoadTruststoreException, KeystoreInstanceException {
58         return createP12TruststoreInstance(SAMPLE_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD);
59     }
60
61     public static JavaTruststore getSampleP12Keystore() throws LoadTruststoreException, KeystoreInstanceException {
62         return createP12TruststoreInstance(SAMPLE_P12_KEYSTORE_FILE_PATH, SAMPLE_P12_KEYSTORE_PASSWORD);
63     }
64
65     public static JavaTruststore createTmpP12TruststoreFile()
66         throws IOException, LoadTruststoreException, KeystoreInstanceException {
67         copyFile(SAMPLE_P12_TRUSTSTORE_FILE_PATH, TMP_P12_TRUSTSTORE_FILE_PATH);
68         return createP12TruststoreInstance(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD);
69     }
70
71     public static JavaTruststore getTmpP12TruststoreFile() throws LoadTruststoreException, KeystoreInstanceException {
72         return createP12TruststoreInstance(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD);
73     }
74
75     private static JavaTruststore createP12TruststoreInstance(String filePath, String password)
76         throws LoadTruststoreException, KeystoreInstanceException {
77         File certFile = getFile(filePath);
78         JavaCertificateStoreController storeController = certificateStoreControllerFactory
79             .createLoadedPkcs12CertificateStoreController(certFile, password);
80         return new JavaTruststore(certFile, storeController);
81     }
82
83     public static PemTruststore getSamplePemTruststoreFile() {
84         return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_FILE_PATH);
85     }
86
87     public static PemTruststore getEmptyPemTruststoreFile() {
88         return getPemTruststoreInstance(EMPTY_PEM_TRUSTSTORE_FILE_PATH);
89     }
90
91     public static PemTruststore createEmptyTmpPemTruststoreFile() throws IOException {
92         copyFile(EMPTY_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH);
93         return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH);
94     }
95
96     public static PemTruststore createTmpPemTruststoreFile() throws IOException {
97         copyFile(SAMPLE_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH);
98         return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH);
99     }
100
101     public static PemTruststore getTmpPemTruststoreFile() {
102         return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH);
103     }
104
105     public static PemTruststore getPemWithPrivateKeyTruststoreFile() {
106         return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH);
107     }
108
109     public static String getExpectedPemCertificateAsString() throws IOException {
110         Path samplePemFilePath = Paths.get(SAMPLE_PEM_TRUSTSTORE_FILE_PATH);
111         return Files.readString(samplePemFilePath);
112     }
113
114     public static JavaTruststore getSampleJksTruststoreFile()
115         throws LoadTruststoreException, KeystoreInstanceException {
116         return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD);
117     }
118
119     public static JavaTruststore getSampleJksTruststoreFileWithUniqueAlias()
120         throws LoadTruststoreException, KeystoreInstanceException {
121         return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH,
122             SAMPLE_JKS_TRUSTSTORE_PASSWORD);
123     }
124
125     public static JavaTruststore createTmpJksTruststoreFileWithUniqAlias()
126         throws IOException, LoadTruststoreException, KeystoreInstanceException {
127         copyFile(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, TMP_JKS_TRUSTSTORE_FILE_PATH);
128         return createJksTruststoreInstance(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD);
129     }
130
131     public static void removeTemporaryFiles() throws IOException {
132         Files.deleteIfExists(Paths.get(TMP_PEM_TRUSTSTORE_FILE_PATH));
133         Files.deleteIfExists(Paths.get(TMP_JKS_TRUSTSTORE_FILE_PATH));
134         Files.deleteIfExists(Paths.get(TMP_P12_TRUSTSTORE_FILE_PATH));
135     }
136
137     private static JavaTruststore createJksTruststoreInstance(String filePath, String password)
138         throws LoadTruststoreException, KeystoreInstanceException {
139         File certFile = getFile(filePath);
140         JavaCertificateStoreController storeController = certificateStoreControllerFactory
141             .createLoadedJksCertificateStoreController(certFile, password);
142         return new JavaTruststore(certFile, storeController);
143     }
144
145     private static PemTruststore getPemTruststoreInstance(String tmpPemTruststoreFilePath) {
146         File file = getFile(tmpPemTruststoreFilePath);
147         return new PemTruststore(file, new PemCertificateController(file));
148     }
149
150     private static void copyFile(String sourcePath, String destPath) throws IOException {
151         Files.copy(Paths.get(sourcePath), Paths.get(destPath), StandardCopyOption.REPLACE_EXISTING);
152     }
153
154     private static File getFile(String path) {
155         return new File(path);
156     }
157 }