2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nokia.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.oom.certservice.certification.conversion;
23 import org.bouncycastle.util.encoders.Base64;
24 import org.junit.jupiter.api.Test;
25 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
27 import java.security.PrivateKey;
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertNotNull;
31 import static org.junit.jupiter.api.Assertions.assertThrows;
32 import static org.junit.jupiter.api.Assertions.assertTrue;
33 import static org.onap.oom.certservice.certification.TestData.TEST_PEM;
34 import static org.onap.oom.certservice.certification.TestData.TEST_PK;
36 class StringBase64ToPrivateKeyConverterTest {
38 private static final String RSA = "RSA";
39 public static final String PKCS_8 = "PKCS#8";
42 void shouldUseProperAlgorithmWhenConverting() throws KeyDecryptionException {
44 StringBase64ToPrivateKeyConverter stringBase64ToPrivateKeyConverter = new StringBase64ToPrivateKeyConverter();
45 String encodedPK = new String(Base64.encode(TEST_PK.getBytes()));
47 PrivateKey privateKey = stringBase64ToPrivateKeyConverter.convert(new StringBase64(encodedPK));
49 assertEquals(RSA, privateKey.getAlgorithm());
53 void shouldUsePkcs8FormatWhenConverting() throws KeyDecryptionException {
55 StringBase64ToPrivateKeyConverter stringBase64ToPrivateKeyConverter = new StringBase64ToPrivateKeyConverter();
56 String encodedPK = new String(Base64.encode(TEST_PK.getBytes()));
58 PrivateKey privateKey = stringBase64ToPrivateKeyConverter.convert(new StringBase64(encodedPK));
60 assertEquals(PKCS_8, privateKey.getFormat());
64 void shouldCorrectlyConvertWhenPrivateKeyPemIsProper() throws KeyDecryptionException {
66 StringBase64ToPrivateKeyConverter stringBase64ToPrivateKeyConverter = new StringBase64ToPrivateKeyConverter();
67 String encodedPK = new String(Base64.encode(TEST_PK.getBytes()));
69 PrivateKey privateKey = stringBase64ToPrivateKeyConverter.convert(new StringBase64(encodedPK));
71 assertNotNull(privateKey.getEncoded());
75 void shouldThrowExceptionWhenPrivateKeyPemIsNotProperPrivateKey() {
77 StringBase64ToPrivateKeyConverter stringBase64ToPrivateKeyConverter = new StringBase64ToPrivateKeyConverter();
78 StringBase64 privateKey = new StringBase64(TEST_PEM);
80 Exception exception = assertThrows(
81 KeyDecryptionException.class, () -> stringBase64ToPrivateKeyConverter.convert(privateKey));
83 String expectedMessage = "Incorrect Key, decryption failed";
84 String actualMessage = exception.getMessage();
86 assertTrue(actualMessage.contains(expectedMessage));