2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.oom.certservice.certification.model;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.stream.Collectors;
26 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
27 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
28 import org.bouncycastle.util.io.pem.PemObject;
29 import org.junit.jupiter.api.Test;
30 import org.onap.oom.certservice.certification.conversion.Pkcs10CertificationRequestFactory;
31 import org.onap.oom.certservice.certification.conversion.PemObjectFactory;
32 import org.onap.oom.certservice.certification.TestData;
33 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
34 import org.onap.oom.certservice.certification.exception.DecryptionException;
35 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
37 import java.io.IOException;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.junit.jupiter.api.Assertions.assertThrows;
41 import static org.junit.jupiter.api.Assertions.assertTrue;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.onap.oom.certservice.certification.TestData.LOCALHOST_IP_IN_HEX;
45 import static org.onap.oom.certservice.certification.TestData.TEST_CSR;
46 import static org.onap.oom.certservice.certification.TestData.TEST_PEM;
47 import static org.onap.oom.certservice.certification.TestData.TEST_PK;
52 private final Pkcs10CertificationRequestFactory certificationRequestFactory
53 = new Pkcs10CertificationRequestFactory();
54 private final PemObjectFactory pemObjectFactory
55 = new PemObjectFactory();
58 void shouldByConstructedAndReturnProperFields() throws DecryptionException, IOException {
60 PemObject testPrivateKey = getPemPrivateKey();
61 PemObject testPublicKey = generateTestPublicKey();
62 PKCS10CertificationRequest testCsr = generateTestCertificationRequest();
65 CsrModel csrModel = generateTestCsrModel(testCsr);
66 List<String> sansList = Arrays.stream(csrModel.getSans())
67 .map(generalName -> generalName.getName().toString())
68 .collect(Collectors.toList());
70 assertThat(csrModel.getCsr())
72 assertThat(csrModel.getPrivateKey().getEncoded())
73 .contains(testPrivateKey.getContent());
74 assertThat(csrModel.getPublicKey().getEncoded())
75 .contains(testPublicKey.getContent());
77 .contains("localhost", "onap.org", "test.onap.org", "onap@onap.org", LOCALHOST_IP_IN_HEX,
78 "onap://cluster.local/");
80 assertThat(csrModel.getSubjectData().toString())
81 .contains(TestData.EXPECTED_CERT_SUBJECT);
85 void shouldThrowExceptionWhenPublicKeyIsNotCorrect() throws DecryptionException, IOException {
87 PemObject testPrivateKey = getPemPrivateKey();
88 PKCS10CertificationRequest testCsr = mock(PKCS10CertificationRequest.class);
89 SubjectPublicKeyInfo wrongKryInfo = mock(SubjectPublicKeyInfo.class);
90 when(testCsr.getSubjectPublicKeyInfo())
91 .thenReturn(wrongKryInfo);
92 when(wrongKryInfo.getEncoded())
93 .thenThrow(new IOException());
96 Exception exception = assertThrows(
97 CsrDecryptionException.class,
98 () -> new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build()
101 String expectedMessage = "Reading Public Key from CSR failed";
102 String actualMessage = exception.getMessage();
105 assertTrue(actualMessage.contains(expectedMessage));
109 void shouldThrowExceptionWhenPrivateKeyPemIsNotProperPrivateKey() throws KeyDecryptionException, IOException {
111 PemObject testPrivateKey = getPemWrongKey();
112 PKCS10CertificationRequest testCsr = mock(PKCS10CertificationRequest.class);
113 SubjectPublicKeyInfo wrongKryInfo = mock(SubjectPublicKeyInfo.class);
114 when(testCsr.getSubjectPublicKeyInfo())
115 .thenReturn(wrongKryInfo);
116 when(wrongKryInfo.getEncoded())
117 .thenThrow(new IOException());
120 Exception exception = assertThrows(
121 KeyDecryptionException.class,
122 () -> new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build()
125 String expectedMessage = "Converting Private Key failed";
126 String actualMessage = exception.getMessage();
129 assertTrue(actualMessage.contains(expectedMessage));
133 void shouldThrowExceptionWhenPublicKeyPemIsNotProperPublicKey() throws KeyDecryptionException, IOException {
135 PemObject testPrivateKey = getPemPrivateKey();
136 PemObject testPublicKey = getPemWrongKey();
137 PKCS10CertificationRequest testCsr = mock(PKCS10CertificationRequest.class);
138 SubjectPublicKeyInfo wrongKryInfo = mock(SubjectPublicKeyInfo.class);
139 when(testCsr.getSubjectPublicKeyInfo())
140 .thenReturn(wrongKryInfo);
141 when(wrongKryInfo.getEncoded())
142 .thenReturn(testPublicKey.getContent());
145 Exception exception = assertThrows(
146 KeyDecryptionException.class,
147 () -> new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build()
150 String expectedMessage = "Converting Public Key from CSR failed";
151 String actualMessage = exception.getMessage();
154 assertTrue(actualMessage.contains(expectedMessage));
157 private PemObject getPemPrivateKey() throws KeyDecryptionException {
158 PemObjectFactory pemObjectFactory = new PemObjectFactory();
159 return pemObjectFactory.createPemObject(TEST_PK).orElseThrow(
160 () -> new KeyDecryptionException("Private key decoding fail")
164 private PemObject getPemWrongKey() throws KeyDecryptionException {
165 PemObjectFactory pemObjectFactory = new PemObjectFactory();
166 return pemObjectFactory.createPemObject(TEST_PEM).orElseThrow(
167 () -> new KeyDecryptionException("Private key decoding fail")
171 private CsrModel generateTestCsrModel(PKCS10CertificationRequest testCsr) throws DecryptionException {
172 PemObject testPrivateKey = pemObjectFactory.createPemObject(TEST_PK).orElseThrow(
173 () -> new DecryptionException("Incorrect Private Key, decryption failed")
175 return new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build();
178 private PemObject generateTestPublicKey() throws DecryptionException, IOException {
179 PKCS10CertificationRequest testCsr = generateTestCertificationRequest();
180 return new PemObject("PUBLIC KEY", testCsr.getSubjectPublicKeyInfo().getEncoded());
183 private PKCS10CertificationRequest generateTestCertificationRequest() throws DecryptionException {
184 return pemObjectFactory.createPemObject(TEST_CSR)
186 certificationRequestFactory::createPkcs10CertificationRequest
188 () -> new DecryptionException("Incorrect CSR, decryption failed")