[OOM-CERT-SERVICE] Fix KeyUsage extention sent to CMPv2 server
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / cmpv2client / impl / CmpMessageHelperTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-api
4  * ================================================================================
5  * Copyright (C) 2021 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.cmpv2client.impl;
22
23 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25
26 import org.bouncycastle.asn1.x509.ExtendedKeyUsage;
27 import org.bouncycastle.asn1.x509.Extension;
28 import org.bouncycastle.asn1.x509.Extensions;
29 import org.bouncycastle.asn1.x509.GeneralName;
30 import org.bouncycastle.asn1.x509.GeneralNames;
31 import org.bouncycastle.asn1.x509.KeyPurposeId;
32 import org.bouncycastle.asn1.x509.KeyUsage;
33 import org.junit.jupiter.api.Test;
34 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
35
36 public class CmpMessageHelperTest {
37
38     private final KeyUsage expectedKeyUsage = new KeyUsage(
39         KeyUsage.digitalSignature | KeyUsage.keyEncipherment | KeyUsage.nonRepudiation);
40     private final ExtendedKeyUsage expectedExtendedKeyUsage = new ExtendedKeyUsage(
41         new KeyPurposeId[]{KeyPurposeId.id_kp_clientAuth, KeyPurposeId.id_kp_serverAuth});
42
43     @Test
44     void shouldSetSansInExtensions() throws CmpClientException {
45         //when
46         Extensions extensions = CmpMessageHelper.generateExtension(getTestSans());
47         //then
48         GeneralName[] sans = GeneralNames.fromExtensions(extensions, Extension.subjectAlternativeName).getNames();
49         assertArrayEquals(sans, getTestSans());
50     }
51
52     @Test
53     void shouldSetKeyUsagesInExtensions() throws CmpClientException {
54         //when
55         Extensions extensions = CmpMessageHelper.generateExtension(getTestSans());
56         //then
57         KeyUsage actualKeyUsage = KeyUsage.fromExtensions(extensions);
58         ExtendedKeyUsage actualExtendedKeyUsage = ExtendedKeyUsage.fromExtensions(extensions);
59         assertEquals(this.expectedKeyUsage, actualKeyUsage);
60         assertEquals(this.expectedExtendedKeyUsage, actualExtendedKeyUsage);
61     }
62
63     private GeneralName[] getTestSans() {
64         return new GeneralName[]{
65             new GeneralName(GeneralName.dNSName, "tetHostName"),
66             new GeneralName(GeneralName.iPAddress, "1.2.3.4")
67         };
68     }
69
70 }