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.CertFileWriterException;
33 import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException;
34 import org.onap.aaf.certservice.client.certification.writer.CertFileWriter;
36 class PKCS12ArtifactsCreatorTest {
38 private static final int PASSWORD_LENGTH = 24;
39 private static final String CERTIFICATE_ALIAS = "certificate";
40 private static final String TRUSTED_CERTIFICATE_ALIAS = "trusted-certificate-";
42 private static final Password SAMPLE_PASSWORD = new Password("d9D_u8LooYaXH4G48DtN#vw0");
43 private static final List<String> SAMPLE_KEYSTORE_CERTIFICATE_CHAIN = List.of("a", "b");
44 private static final List<String> SAMPLE_TRUSTED_CERTIFICATE_CHAIN = List.of("c", "d");
45 private static final byte[] SAMPLE_KEYSTORE_BYTES = "this is a keystore test".getBytes();
46 private static final byte[] SAMPLE_TRUSTSTORE_BYTES = "this is a truststore test".getBytes();
48 private CertFileWriter certFileWriter;
49 private RandomPasswordGenerator passwordGenerator;
50 private PemToPKCS12Converter converter;
51 private PrivateKey privateKey;
52 private PKCS12ArtifactsCreator artifactCreator;
57 certFileWriter = mock(CertFileWriter.class);
58 passwordGenerator = mock(RandomPasswordGenerator.class);
59 converter = mock(PemToPKCS12Converter.class);
60 privateKey = mock(PrivateKey.class);
61 artifactCreator = new PKCS12ArtifactsCreator(certFileWriter, passwordGenerator, converter);
65 void artifactsCreatorShouldCauseCallOfConvertAndDataSaveMethods()
66 throws PemToPKCS12ConverterException, CertFileWriterException {
68 mockPasswordGeneratorAndPKSC12Converter();
69 final String keystoreP12 = "keystore.p12";
70 final String keystorePass = "keystore.pass";
73 artifactCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
76 verify(converter, times(1))
77 .convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey);
78 verify(certFileWriter, times(1))
79 .saveData(SAMPLE_KEYSTORE_BYTES, keystoreP12);
80 verify(certFileWriter, times(1))
81 .saveData(SAMPLE_PASSWORD.getCurrentPassword().getBytes(), keystorePass);
82 verify(converter, times(1))
83 .convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS);
87 void artifactsCreatorShouldCallPasswordGeneratorTwice()
88 throws PemToPKCS12ConverterException, CertFileWriterException {
90 mockPasswordGeneratorAndPKSC12Converter();
93 artifactCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
96 verify(passwordGenerator, times(2)).generate(PASSWORD_LENGTH);
99 private void mockPasswordGeneratorAndPKSC12Converter() throws PemToPKCS12ConverterException {
100 when(passwordGenerator.generate(PASSWORD_LENGTH)).thenReturn(SAMPLE_PASSWORD);
101 when(converter.convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey))
102 .thenReturn(SAMPLE_KEYSTORE_BYTES);
103 when(converter.convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS))
104 .thenReturn(SAMPLE_TRUSTSTORE_BYTES);