1 /*============LICENSE_START=======================================================
2 * oom-truststore-merger
3 * ================================================================================
4 * Copyright (C) 2020-2021 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.oom.certservice.postprocessor.merger.model;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.X_509_CERTIFICATE;
27 import static org.onap.oom.certservice.postprocessor.merger.model.TestCertificateProvider.PEM_BACKUP_FILE_PATH;
30 import java.io.IOException;
31 import java.security.cert.Certificate;
32 import java.security.cert.CertificateEncodingException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.stream.Stream;
36 import org.junit.jupiter.api.AfterEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.params.ParameterizedTest;
39 import org.junit.jupiter.params.provider.Arguments;
40 import org.junit.jupiter.params.provider.MethodSource;
41 import org.onap.oom.certservice.postprocessor.api.ExitableException;
42 import org.onap.oom.certservice.postprocessor.merger.exception.CreateBackupException;
43 import org.onap.oom.certservice.postprocessor.merger.exception.KeystoreInstanceException;
44 import org.onap.oom.certservice.postprocessor.merger.exception.LoadTruststoreException;
45 import org.onap.oom.certservice.postprocessor.merger.exception.MissingTruststoreException;
46 import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
47 import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreDataOperationException;
48 import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAlias;
49 import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAliasFactory;
51 class TruststoreTest {
53 private static final String BACKUP_EXTENSION = ".bak";
55 private static final int EXPECTED_ONE = 1;
56 public static final int EXPECTED_THREE = 3;
57 public static final int FIRST_ELEMENT = 0;
59 private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory();
62 void createBackupShouldCreateFileWithExtension() throws CreateBackupException {
64 File pemFile = new File(TestCertificateProvider.PEM_FILE_PATH);
65 Truststore truststore = new PemTruststore(pemFile);
67 truststore.createBackup();
70 File backupFile = new File(PEM_BACKUP_FILE_PATH);
71 assertThat(backupFile.getName()).endsWith(BACKUP_EXTENSION);
72 assertThat(backupFile).isFile();
76 @MethodSource("truststoreProvider")
77 void truststoreShouldReadCertificatesFromFile(Truststore truststore) throws ExitableException {
80 List<CertificateWithAlias> certificates = truststore.getCertificates();
81 Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate();
84 assertThat(certificates).hasSize(EXPECTED_ONE);
85 assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
89 void jksTruststoreShouldAddDifferentCertificates() throws Exception {
91 Truststore jksTruststore = TestCertificateProvider.createTmpJksTruststoreFileWithUniqAlias();
93 List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore()
96 List<CertificateWithAlias> certificateFromPem = TestCertificateProvider
97 .getSamplePemTruststoreFile().getCertificates();
101 jksTruststore.addCertificates(certificateFromP12);
103 jksTruststore.addCertificates(certificateFromPem);
105 jksTruststore.saveFile();
109 assertThat(jksTruststore.getCertificates()).hasSize(EXPECTED_THREE);
113 void p12TruststoreShouldAddDifferentCertificates() throws Exception {
115 Truststore p12Truststore = TestCertificateProvider.createTmpP12TruststoreFile();
117 List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
118 .getSampleJksTruststoreFileWithUniqueAlias().getCertificates();
120 List<CertificateWithAlias> certificateFromPem = TestCertificateProvider
121 .getSamplePemTruststoreFile().getCertificates();
125 p12Truststore.addCertificates(certificateFromJks);
126 p12Truststore.addCertificates(certificateFromPem);
127 p12Truststore.saveFile();
131 assertThat(p12Truststore.getCertificates()).hasSize(EXPECTED_THREE);
135 void pemTruststoreShouldAddDifferentCertificates() throws IOException, ExitableException {
137 Truststore pemTruststore = TestCertificateProvider
138 .createTmpPemTruststoreFile();
140 List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
141 .getSampleJksTruststoreFileWithUniqueAlias().getCertificates();
143 List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore()
148 pemTruststore.addCertificates(certificateFromJks);
150 pemTruststore.addCertificates(certificateFromP12);
152 pemTruststore.saveFile();
156 List<CertificateWithAlias> addedCertificates = pemTruststore.getCertificates();
157 Certificate certificate = addedCertificates.get(FIRST_ELEMENT).getCertificate();
159 assertThat(pemTruststore.getCertificates()).hasSize(EXPECTED_THREE);
160 assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
164 void shouldThrowExceptionWhenFileNotContainsCertificate() throws IOException {
166 Truststore tmpPemTruststoreFile = TestCertificateProvider
167 .createEmptyTmpPemTruststoreFile();
169 assertThatExceptionOfType(MissingTruststoreException.class)
170 .isThrownBy(() -> tmpPemTruststoreFile.getCertificates());
174 void shouldThrowExceptionWhenCannotConvertCertificateToPem() throws Exception {
176 Truststore pemTruststore = TestCertificateProvider.createTmpPemTruststoreFile();
177 Certificate certificate = mock(Certificate.class);
179 when(certificate.getEncoded()).thenThrow(new CertificateEncodingException());
181 List<CertificateWithAlias> certificateFromPem = new ArrayList<>();
182 certificateFromPem.add(factory.createPemCertificate(certificate));
184 pemTruststore.addCertificates(certificateFromPem);
187 assertThatExceptionOfType(TruststoreDataOperationException.class)
188 .isThrownBy(() -> pemTruststore.saveFile());
192 void removeTemporaryFiles() throws IOException {
193 TestCertificateProvider.removeTemporaryFiles();