2 * ============LICENSE_START=======================================================
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
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.cmpv2client.impl;
23 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
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;
36 public class CmpMessageHelperTest {
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});
44 void shouldSetSansInExtensions() throws CmpClientException {
46 Extensions extensions = CmpMessageHelper.generateExtension(getTestSans());
48 GeneralName[] sans = GeneralNames.fromExtensions(extensions, Extension.subjectAlternativeName).getNames();
49 assertArrayEquals(sans, getTestSans());
53 void shouldSetKeyUsagesInExtensions() throws CmpClientException {
55 Extensions extensions = CmpMessageHelper.generateExtension(getTestSans());
57 KeyUsage actualKeyUsage = KeyUsage.fromExtensions(extensions);
58 ExtendedKeyUsage actualExtendedKeyUsage = ExtendedKeyUsage.fromExtensions(extensions);
59 assertEquals(this.expectedKeyUsage, actualKeyUsage);
60 assertEquals(this.expectedExtendedKeyUsage, actualExtendedKeyUsage);
63 private GeneralName[] getTestSans() {
64 return new GeneralName[]{
65 new GeneralName(GeneralName.dNSName, "tetHostName"),
66 new GeneralName(GeneralName.iPAddress, "1.2.3.4")