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.assertj.core.api.Assertions.assertThatThrownBy;
23 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import java.io.IOException;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.util.List;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException;
38 class PKCS12FilesCreatorTest {
40 private static final String RESOURCES_PATH = "src/test/resources";
41 private static final String OUTPUT_PATH = RESOURCES_PATH + "/generatedFiles/";
42 private static final String KEYSTORE_PATH = OUTPUT_PATH + "keystore.jks";
43 private static final String KEYSTORE_PASS_PATH = OUTPUT_PATH + "keystore.pass";
44 private static final String TRUSTSTORE_PATH = OUTPUT_PATH + "truststore.jks";
45 private static final String TRUSTSTORE_PASS_PATH = OUTPUT_PATH + "truststore.pass";
46 private static final String ERROR_MESSAGE = "java.io.FileNotFoundException: src/test/resources/generatedFiles/thisPathDoesNotExist/keystore.jks (No such file or directory)";
48 private File outputDirectory = new File(OUTPUT_PATH);
51 void createDirectory() {
52 outputDirectory.mkdir();
57 List.of(outputDirectory.listFiles()).forEach(f -> f.delete());
58 outputDirectory.delete();
62 void saveKeystoreDataShouldCreateFilesWithDataInGivenLocation() throws PemToPKCS12ConverterException, IOException {
64 final byte[] data = new byte[]{-128, 1, 127};
65 final String password = "onap123";
66 File keystore = new File(KEYSTORE_PATH);
67 File keystorePass = new File(KEYSTORE_PASS_PATH);
68 PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH);
71 filesCreator.saveKeystoreData(data, password);
74 assertTrue(keystore.exists());
75 assertTrue(keystorePass.exists());
76 assertArrayEquals(data, Files.readAllBytes(Path.of(KEYSTORE_PATH)));
77 assertEquals(password, Files.readString(Path.of(KEYSTORE_PASS_PATH), StandardCharsets.UTF_8));
81 void saveTruststoreDataShouldCreateFilesWithDataInGivenLocation()
82 throws PemToPKCS12ConverterException, IOException {
84 final byte[] data = new byte[]{-128, 1, 2, 3, 127};
85 final String password = "nokia321";
86 File truststore = new File(TRUSTSTORE_PATH);
87 File truststorePass = new File(TRUSTSTORE_PASS_PATH);
88 PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH);
91 filesCreator.saveTruststoreData(data, password);
94 assertTrue(truststore.exists());
95 assertTrue(truststorePass.exists());
96 assertArrayEquals(data, Files.readAllBytes(Path.of(TRUSTSTORE_PATH)));
97 assertEquals(password, Files.readString(Path.of(TRUSTSTORE_PASS_PATH), StandardCharsets.UTF_8));
101 void saveKeystoreDataShouldThrowPemToPKCS12ConverterExceptionWhenOutputDirectoryDoesNotExist() {
103 final byte[] data = new byte[]{-128, 1, 2, 3, 0};
104 final String password = "123aikon";
105 PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH + "thisPathDoesNotExist/");
108 assertThatThrownBy(() -> filesCreator.saveKeystoreData(data, password))
109 .isInstanceOf(PemToPKCS12ConverterException.class).hasMessage(ERROR_MESSAGE);