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
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.CertificateConstants;
42 import org.onap.oom.certservice.postprocessor.api.ExitableException;
43 import org.onap.oom.certservice.postprocessor.merger.exception.CreateBackupException;
44 import org.onap.oom.certservice.postprocessor.merger.exception.KeystoreInstanceException;
45 import org.onap.oom.certservice.postprocessor.merger.exception.LoadTruststoreException;
46 import org.onap.oom.certservice.postprocessor.merger.exception.MissingTruststoreException;
47 import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
48 import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreDataOperationException;
49 import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAlias;
50 import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAliasFactory;
52 class TruststoreTest {
54 private static final String BACKUP_EXTENSION = ".bak";
56 private static final int EXPECTED_ONE = 1;
57 public static final int EXPECTED_THREE = 3;
58 public static final int FIRST_ELEMENT = 0;
60 private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory();
63 void createBackupShouldCreateFileWithExtension() throws CreateBackupException {
65 File pemFile = new File(TestCertificateProvider.PEM_FILE_PATH);
66 Truststore truststore = new PemTruststore(pemFile);
68 truststore.createBackup();
71 File backupFile = new File(PEM_BACKUP_FILE_PATH);
72 assertThat(backupFile.getName().endsWith(BACKUP_EXTENSION)).isTrue();
73 assertThat(backupFile.isFile()).isTrue();
77 @MethodSource("truststoreProvider")
78 void truststoreShouldReadCertificatesFromFile(Truststore truststore) throws ExitableException {
81 List<CertificateWithAlias> certificates = truststore.getCertificates();
82 Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate();
85 assertThat(certificates).hasSize(EXPECTED_ONE);
86 assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
90 void jksTruststoreShouldAddDifferentCertificates() throws Exception {
92 Truststore jksTruststore = TestCertificateProvider.createTmpJksTruststoreFileWithUniqAlias();
94 List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore()
97 List<CertificateWithAlias> certificateFromPem = TestCertificateProvider
98 .getSamplePemTruststoreFile().getCertificates();
102 jksTruststore.addCertificates(certificateFromP12);
104 jksTruststore.addCertificates(certificateFromPem);
106 jksTruststore.saveFile();
110 assertThat(jksTruststore.getCertificates()).hasSize(EXPECTED_THREE);
114 void p12TruststoreShouldAddDifferentCertificates() throws Exception {
116 Truststore p12Truststore = TestCertificateProvider.createTmpP12TruststoreFile();
118 List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
119 .getSampleJksTruststoreFileWithUniqueAlias().getCertificates();
121 List<CertificateWithAlias> certificateFromPem = TestCertificateProvider
122 .getSamplePemTruststoreFile().getCertificates();
126 p12Truststore.addCertificates(certificateFromJks);
127 p12Truststore.addCertificates(certificateFromPem);
128 p12Truststore.saveFile();
132 assertThat(p12Truststore.getCertificates()).hasSize(EXPECTED_THREE);
136 void pemTruststoreShouldAddDifferentCertificates() throws IOException, ExitableException {
138 Truststore pemTruststore = TestCertificateProvider
139 .createTmpPemTruststoreFile();
141 List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
142 .getSampleJksTruststoreFileWithUniqueAlias().getCertificates();
144 List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore()
149 pemTruststore.addCertificates(certificateFromJks);
151 pemTruststore.addCertificates(certificateFromP12);
153 pemTruststore.saveFile();
157 List<CertificateWithAlias> addedCertificates = pemTruststore.getCertificates();
158 Certificate certificate = addedCertificates.get(FIRST_ELEMENT).getCertificate();
160 assertThat(pemTruststore.getCertificates()).hasSize(EXPECTED_THREE);
161 assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
165 void shouldThrowExceptionWhenFileNotContainsCertificate() throws IOException {
167 Truststore tmpPemTruststoreFile = TestCertificateProvider
168 .createEmptyTmpPemTruststoreFile();
170 assertThatExceptionOfType(MissingTruststoreException.class)
171 .isThrownBy(() -> tmpPemTruststoreFile.getCertificates());
175 void shouldThrowExceptionWhenCannotConvertCertificateToPem() throws Exception {
177 Truststore pemTruststore = TestCertificateProvider.createTmpPemTruststoreFile();
178 Certificate certificate = mock(Certificate.class);
180 when(certificate.getEncoded()).thenThrow(new CertificateEncodingException());
182 List<CertificateWithAlias> certificateFromPem = new ArrayList<>();
183 certificateFromPem.add(factory.createPemCertificate(certificate));
185 pemTruststore.addCertificates(certificateFromPem);
188 assertThatExceptionOfType(TruststoreDataOperationException.class)
189 .isThrownBy(() -> pemTruststore.saveFile());
193 void removeTemporaryFiles() throws IOException {
194 TestCertificateProvider.removeTemporaryFiles();
197 private static Stream<Arguments> truststoreProvider()
198 throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
200 Arguments.of(TestCertificateProvider.getSampleJksTruststoreFile()),
201 Arguments.of(TestCertificateProvider.getSampleP12Truststore()),
202 Arguments.of(TestCertificateProvider.getSamplePemTruststoreFile())