1 /*============LICENSE_START=======================================================
2 * aaf-certservice-client
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.aaf.certservice.client.certification.conversion;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
27 import java.security.PrivateKey;
28 import java.util.List;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException;
34 class PKCS12ArtifactsCreatorTest {
36 private static final int PASSWORD_LENGTH = 24;
37 private static final String CERTIFICATE_ALIAS = "certificate";
38 private static final String TRUSTED_CERTIFICATE_ALIAS = "trusted-certificate-";
40 private static final Password SAMPLE_PASSWORD = new Password("d9D_u8LooYaXH4G48DtN#vw0");
41 private static final List<String> SAMPLE_KEYSTORE_CERTIFICATE_CHAIN = List.of("a", "b");
42 private static final List<String> SAMPLE_TRUSTED_CERTIFICATE_CHAIN = List.of("c", "d");
43 private static final byte[] SAMPLE_KEYSTORE_BYTES = "this is a keystore test".getBytes();
44 private static final byte[] SAMPLE_TRUSTSTORE_BYTES = "this is a truststore test".getBytes();
46 private PKCS12FilesCreator filesCreator;
47 private RandomPasswordGenerator passwordGenerator;
48 private PemToPKCS12Converter converter;
49 private PrivateKey privateKey;
50 private PKCS12ArtifactsCreator artifactCreator;
55 filesCreator = mock(PKCS12FilesCreator.class);
56 passwordGenerator = mock(RandomPasswordGenerator.class);
57 converter = mock(PemToPKCS12Converter.class);
58 privateKey = mock(PrivateKey.class);
59 artifactCreator = new PKCS12ArtifactsCreator(filesCreator, passwordGenerator, converter);
63 void generateArtifactsShouldCallConverterAndFilesCreatorMethods() throws PemToPKCS12ConverterException {
65 mockPasswordGeneratorAndPKSC12Converter();
68 artifactCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
71 verify(converter, times(1))
72 .convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey);
73 verify(filesCreator, times(1))
74 .saveKeystoreData(SAMPLE_KEYSTORE_BYTES, SAMPLE_PASSWORD.getCurrentPassword());
75 verify(converter, times(1))
76 .convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS);
77 verify(filesCreator, times(1))
78 .saveTruststoreData(SAMPLE_TRUSTSTORE_BYTES, SAMPLE_PASSWORD.getCurrentPassword());
82 void generateArtifactsMethodShouldCallPasswordGeneratorTwice() throws PemToPKCS12ConverterException {
84 mockPasswordGeneratorAndPKSC12Converter();
87 artifactCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
90 verify(passwordGenerator, times(2)).generate(PASSWORD_LENGTH);
93 private void mockPasswordGeneratorAndPKSC12Converter() throws PemToPKCS12ConverterException {
94 when(passwordGenerator.generate(PASSWORD_LENGTH)).thenReturn(SAMPLE_PASSWORD);
95 when(converter.convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey))
96 .thenReturn(SAMPLE_KEYSTORE_BYTES);
97 when(converter.convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS))
98 .thenReturn(SAMPLE_TRUSTSTORE_BYTES);