Automation adds INFO.yaml
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / certification / model / CsrModelTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.oom.certservice.certification.model;
22
23 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
24 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
25 import org.bouncycastle.util.io.pem.PemObject;
26 import org.junit.jupiter.api.Test;
27 import org.onap.oom.certservice.certification.Pkcs10CertificationRequestFactory;
28 import org.onap.oom.certservice.certification.PemObjectFactory;
29 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
30 import org.onap.oom.certservice.certification.exception.DecryptionException;
31 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
32
33 import java.io.IOException;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.junit.jupiter.api.Assertions.assertThrows;
37 import static org.junit.jupiter.api.Assertions.assertTrue;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40 import static org.onap.oom.certservice.certification.TestData.TEST_CSR;
41 import static org.onap.oom.certservice.certification.TestData.TEST_PEM;
42 import static org.onap.oom.certservice.certification.TestData.TEST_PK;
43
44
45 class CsrModelTest {
46
47     private final Pkcs10CertificationRequestFactory certificationRequestFactory
48             = new Pkcs10CertificationRequestFactory();
49     private final PemObjectFactory pemObjectFactory
50             = new PemObjectFactory();
51
52     @Test
53     void shouldByConstructedAndReturnProperFields() throws DecryptionException, IOException {
54         // Given
55         PemObject testPrivateKey = getPemPrivateKey();
56         PemObject testPublicKey = generateTestPublicKey();
57         PKCS10CertificationRequest testCsr = generateTestCertificationRequest();
58
59         // When
60         CsrModel csrModel = generateTestCsrModel(testCsr);
61
62         // Then
63         assertThat(csrModel.getCsr())
64                 .isEqualTo(testCsr);
65         assertThat(csrModel.getPrivateKey().getEncoded())
66                 .contains(testPrivateKey.getContent());
67         assertThat(csrModel.getPublicKey().getEncoded())
68                 .contains(testPublicKey.getContent());
69         assertThat(csrModel.getSans())
70                 .contains(
71                         "gerrit.onap.org", "test.onap.org", "onap.com");
72         assertThat(csrModel.getSubjectData().toString())
73                 .contains(
74                         "C=US,ST=California,L=San-Francisco,O=Linux-Foundation,OU=ONAP,CN=onap.org,E=tester@onap.org");
75     }
76
77     @Test
78     void shouldThrowExceptionWhenPublicKeyIsNotCorrect() throws DecryptionException, IOException {
79         // Given
80         PemObject testPrivateKey = getPemPrivateKey();
81         PKCS10CertificationRequest testCsr = mock(PKCS10CertificationRequest.class);
82         SubjectPublicKeyInfo wrongKryInfo = mock(SubjectPublicKeyInfo.class);
83         when(testCsr.getSubjectPublicKeyInfo())
84                 .thenReturn(wrongKryInfo);
85         when(wrongKryInfo.getEncoded())
86                 .thenThrow(new IOException());
87
88         // When
89         Exception exception = assertThrows(
90                 CsrDecryptionException.class,
91                 () -> new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build()
92         );
93
94         String expectedMessage = "Reading Public Key from CSR failed";
95         String actualMessage = exception.getMessage();
96
97         // Then
98         assertTrue(actualMessage.contains(expectedMessage));
99     }
100
101     @Test
102     void shouldThrowExceptionWhenPrivateKeyPemIsNotProperPrivateKey() throws KeyDecryptionException, IOException {
103         // Given
104         PemObject testPrivateKey = getPemWrongKey();
105         PKCS10CertificationRequest testCsr = mock(PKCS10CertificationRequest.class);
106         SubjectPublicKeyInfo wrongKryInfo = mock(SubjectPublicKeyInfo.class);
107         when(testCsr.getSubjectPublicKeyInfo())
108                 .thenReturn(wrongKryInfo);
109         when(wrongKryInfo.getEncoded())
110                 .thenThrow(new IOException());
111
112         // When
113         Exception exception = assertThrows(
114                 KeyDecryptionException.class,
115                 () -> new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build()
116         );
117
118         String expectedMessage = "Converting Private Key failed";
119         String actualMessage = exception.getMessage();
120
121         // Then
122         assertTrue(actualMessage.contains(expectedMessage));
123     }
124
125     @Test
126     void shouldThrowExceptionWhenPublicKeyPemIsNotProperPublicKey() throws KeyDecryptionException, IOException {
127         // Given
128         PemObject testPrivateKey = getPemPrivateKey();
129         PemObject testPublicKey = getPemWrongKey();
130         PKCS10CertificationRequest testCsr = mock(PKCS10CertificationRequest.class);
131         SubjectPublicKeyInfo wrongKryInfo = mock(SubjectPublicKeyInfo.class);
132         when(testCsr.getSubjectPublicKeyInfo())
133                 .thenReturn(wrongKryInfo);
134         when(wrongKryInfo.getEncoded())
135                 .thenReturn(testPublicKey.getContent());
136
137         // When
138         Exception exception = assertThrows(
139                 KeyDecryptionException.class,
140                 () -> new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build()
141         );
142
143         String expectedMessage = "Converting Public Key from CSR failed";
144         String actualMessage = exception.getMessage();
145
146         // Then
147         assertTrue(actualMessage.contains(expectedMessage));
148     }
149
150     private PemObject getPemPrivateKey() throws KeyDecryptionException {
151         PemObjectFactory pemObjectFactory = new PemObjectFactory();
152         return pemObjectFactory.createPemObject(TEST_PK).orElseThrow(
153                 () -> new KeyDecryptionException("Private key decoding fail")
154         );
155     }
156
157     private PemObject getPemWrongKey() throws KeyDecryptionException {
158         PemObjectFactory pemObjectFactory = new PemObjectFactory();
159         return pemObjectFactory.createPemObject(TEST_PEM).orElseThrow(
160                 () -> new KeyDecryptionException("Private key decoding fail")
161         );
162     }
163
164     private CsrModel generateTestCsrModel(PKCS10CertificationRequest testCsr) throws DecryptionException {
165         PemObject testPrivateKey = pemObjectFactory.createPemObject(TEST_PK).orElseThrow(
166                 () -> new DecryptionException("Incorrect Private Key, decryption failed")
167         );
168         return new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build();
169     }
170
171     private PemObject generateTestPublicKey() throws DecryptionException, IOException {
172         PKCS10CertificationRequest testCsr = generateTestCertificationRequest();
173         return new PemObject("PUBLIC KEY", testCsr.getSubjectPublicKeyInfo().getEncoded());
174     }
175
176     private PKCS10CertificationRequest generateTestCertificationRequest() throws DecryptionException {
177         return pemObjectFactory.createPemObject(TEST_CSR)
178                 .flatMap(
179                         certificationRequestFactory::createPkcs10CertificationRequest
180                 ).orElseThrow(
181                         () -> new DecryptionException("Incorrect CSR, decryption failed")
182                 );
183     }
184
185 }