summaryrefslogtreecommitdiffstats
path: root/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification
diff options
context:
space:
mode:
authorbaniewsk <pawel.baniewski@nokia.com>2020-07-29 16:01:27 +0200
committerPawel <pawel.kasperkiewicz@nokia.com>2020-08-05 14:18:54 +0200
commitb8c4e6867d6b26652f4382e93665c220769cdc9f (patch)
treebb60a44b012731e3ee6fdffe2466f5ed7d6b5c7b /certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification
parentfc31c9e47b3e08f8914dcd1f0c5b6d18aa625567 (diff)
Removing AAF references from Cert-Service in OOM repo.
Certificates regenerated External files (from legacy AAF) removed Still left: * Sonar link, * Link to documentation, * Names of K8s resources in RTD documentation, * Link to CSITs Issue-ID: OOM-2526 Signed-off-by: Pawel Baniewski <pawel.baniewski@nokia.com> Change-Id: I675f7485160b9b8e46e9ea573550e62ed28ca607
Diffstat (limited to 'certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification')
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/ArtifactsCreatorProviderTest.java73
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/CsrFactoryTest.java56
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/KeyPairFactoryTest.java53
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/PrivateKeyToPemEncoderTest.java66
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ConvertedArtifactsCreatorTest.java125
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java58
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemConverterTest.java206
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/RandomPasswordGeneratorTest.java33
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java76
9 files changed, 0 insertions, 746 deletions
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/ArtifactsCreatorProviderTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/ArtifactsCreatorProviderTest.java
deleted file mode 100644
index 46bacef8..00000000
--- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/ArtifactsCreatorProviderTest.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*============LICENSE_START=======================================================
- * aaf-certservice-client
- * ================================================================================
- * Copyright (C) 2020 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.aaf.certservice.client.certification;
-
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.CsvSource;
-import org.junit.jupiter.params.provider.ValueSource;
-import org.onap.aaf.certservice.client.certification.conversion.ArtifactsCreator;
-import org.onap.aaf.certservice.client.certification.conversion.ConvertedArtifactsCreator;
-import org.onap.aaf.certservice.client.certification.conversion.PemArtifactsCreator;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-
-class ArtifactsCreatorProviderTest {
-
- private static final String P12 = "P12";
- private static final String JKS = "JKS";
- private static final String PEM = "PEM";
- private static final String TEST_PATH = "testPath";
-
- @ParameterizedTest
- @ValueSource(strings = {JKS, P12})
- void artifactsProviderShouldReturnConvertedCreator(String outputType) {
-
- // when
- ArtifactsCreator artifactsCreator =
- ArtifactsCreatorProvider.get(outputType, TEST_PATH);
- // then
- assertThat(artifactsCreator).isInstanceOf(ConvertedArtifactsCreator.class);
- }
-
- @Test
- void artifactsProviderShouldReturnPemCreator() {
-
- // when
- ArtifactsCreator artifactsCreator =
- ArtifactsCreatorProvider.get(PEM, TEST_PATH);
- // then
- assertThat(artifactsCreator).isInstanceOf(PemArtifactsCreator.class);
- }
-
- @ParameterizedTest
- @CsvSource({
- "JKS, jks",
- "P12, p12"})
- void getExtensionShouldProvideExtensionBasedOnArtifactType(String artifactType, String expectedExtension) {
-
- //when
- String actualExtension = ArtifactsCreatorProvider.valueOf(artifactType).getExtension();
- //then
- assertThat(actualExtension).isEqualTo(expectedExtension);
- }
-
-}
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/CsrFactoryTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/CsrFactoryTest.java
deleted file mode 100644
index ae18e6fb..00000000
--- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/CsrFactoryTest.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*============LICENSE_START=======================================================
- * aaf-certservice-client
- * ================================================================================
- * Copyright (C) 2020 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.aaf.certservice.client.certification;
-
-
-import org.junit.jupiter.api.Test;
-import org.onap.aaf.certservice.client.certification.exception.CsrGenerationException;
-import org.onap.aaf.certservice.client.certification.exception.KeyPairGenerationException;
-import org.onap.aaf.certservice.client.configuration.model.CsrConfiguration;
-
-import java.security.KeyPair;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-class CsrFactoryTest {
-
- CsrConfiguration config = mock(CsrConfiguration.class);
-
-
- @Test
- void createEncodedCsr_shouldSucceedWhenAllFieldsAreSetCorrectly() throws KeyPairGenerationException, CsrGenerationException {
-
- KeyPair keyPair =
- new KeyPairFactory(EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM, EncryptionAlgorithmConstants.KEY_SIZE).create();
-
- when(config.getCommonName()).thenReturn("onap.org");
- when(config.getSans()).thenReturn("onapexample.com:onapexample.com.pl:onapexample.pl");
- when(config.getCountry()).thenReturn("US");
- when(config.getLocation()).thenReturn("San-Francisco");
- when(config.getOrganization()).thenReturn("Linux-Foundation");
- when(config.getOrganizationUnit()).thenReturn("ONAP");
- when(config.getState()).thenReturn("California");
-
- assertThat(new CsrFactory(config).createCsrInPem(keyPair)).isNotEmpty();
- }
-}
-
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/KeyPairFactoryTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/KeyPairFactoryTest.java
deleted file mode 100644
index 3d3d3c17..00000000
--- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/KeyPairFactoryTest.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*============LICENSE_START=======================================================
- * aaf-certservice-client
- * ================================================================================
- * Copyright (C) 2020 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.aaf.certservice.client.certification;
-
-import org.junit.jupiter.api.Test;
-import org.onap.aaf.certservice.client.certification.exception.KeyPairGenerationException;
-
-import java.security.KeyPair;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-
-class KeyPairFactoryTest {
- private static final String NOT_EXISTING_ENCRYPTION_ALGORITHM = "FAKE_ALGORITHM";
-
- @Test
- public void shouldProvideKeyPair_whenCreateKeyPairCalledWithCorrectArguments() throws KeyPairGenerationException {
- // given
- KeyPairFactory keyPairFactory = new KeyPairFactory(EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM,
- EncryptionAlgorithmConstants.KEY_SIZE);
- // when
- KeyPair keyPair = keyPairFactory.create();
- // then
- assertThat(keyPair).isInstanceOf(KeyPair.class);
- }
-
- @Test
- public void shouldThrowKeyPairGenerationException_whenCreateTryCalledOnNotExistingAlgorithm() {
- // given
- KeyPairFactory keyPairFactory = new KeyPairFactory(NOT_EXISTING_ENCRYPTION_ALGORITHM,
- EncryptionAlgorithmConstants.KEY_SIZE);
- // when, then
- assertThatThrownBy(keyPairFactory::create).isInstanceOf(KeyPairGenerationException.class);
- }
-
-} \ No newline at end of file
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/PrivateKeyToPemEncoderTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/PrivateKeyToPemEncoderTest.java
deleted file mode 100644
index 41f15bb7..00000000
--- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/PrivateKeyToPemEncoderTest.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*============LICENSE_START=======================================================
- * aaf-certservice-client
- * ================================================================================
- * Copyright (C) 2020 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.aaf.certservice.client.certification;
-
-
-import org.bouncycastle.util.io.pem.PemObject;
-import org.bouncycastle.util.io.pem.PemReader;
-import org.junit.jupiter.api.Test;
-import org.onap.aaf.certservice.client.certification.exception.PkEncodingException;
-
-import java.io.IOException;
-import java.io.StringReader;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.security.KeyFactory;
-import java.security.NoSuchAlgorithmException;
-import java.security.PrivateKey;
-import java.security.spec.InvalidKeySpecException;
-import java.security.spec.PKCS8EncodedKeySpec;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-class PrivateKeyToPemEncoderTest {
-
- private static final String ENCRYPTION_ALGORITHM = "RSA";
- private static final String RESOURCES_DIR = "src/test/resources/";
- private static final String PRIVATE_KEY_PEM_PATH = RESOURCES_DIR + "rsaPrivateKeyPem";
-
- @Test
- void shouldReturnProperlyEncodedPrivateKey() throws InvalidKeySpecException, NoSuchAlgorithmException, PkEncodingException, IOException {
- //given
- String expectedPem = Files.readString(Paths.get(PRIVATE_KEY_PEM_PATH));
- PrivateKeyToPemEncoder testedPkEncoder = new PrivateKeyToPemEncoder();
- //when
- PrivateKey privateKey = extractPrivateKeyFromPem(expectedPem);
- String resultPkInPem = testedPkEncoder.encodePrivateKeyToPem(privateKey);
- //then
- assertThat(resultPkInPem).isEqualTo(expectedPem);
- }
-
- private PrivateKey extractPrivateKeyFromPem(String pem) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
- PemReader pemReader = new PemReader(new StringReader(pem));
- PemObject pemObject = pemReader.readPemObject();
- pemReader.close();
- PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(pemObject.getContent());
- KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
- return kf.generatePrivate(spec);
- }
-}
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ConvertedArtifactsCreatorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ConvertedArtifactsCreatorTest.java
deleted file mode 100644
index e13f8be8..00000000
--- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ConvertedArtifactsCreatorTest.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/*============LICENSE_START=======================================================
- * aaf-certservice-client
- * ================================================================================
- * Copyright (C) 2020 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.aaf.certservice.client.certification.conversion;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException;
-import org.onap.aaf.certservice.client.certification.exception.PemConversionException;
-import org.onap.aaf.certservice.client.certification.writer.CertFileWriter;
-
-import java.security.PrivateKey;
-import java.util.List;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-class ConvertedArtifactsCreatorTest {
-
- private static final int PASSWORD_LENGTH = 24;
- private static final String CERTIFICATE_ALIAS = "certificate";
- private static final String TRUSTED_CERTIFICATE_ALIAS = "trusted-certificate-";
-
- private static final Password SAMPLE_PASSWORD = new Password("d9D_u8LooYaXH4G48DtN#vw0");
- private static final List<String> SAMPLE_KEYSTORE_CERTIFICATE_CHAIN = List.of("a", "b");
- private static final List<String> SAMPLE_TRUSTED_CERTIFICATE_CHAIN = List.of("c", "d");
- private static final byte[] SAMPLE_KEYSTORE_BYTES = "this is a keystore test".getBytes();
- private static final byte[] SAMPLE_TRUSTSTORE_BYTES = "this is a truststore test".getBytes();
- private static final String P12_EXTENSION = "p12";
-
- private CertFileWriter certFileWriter;
- private RandomPasswordGenerator passwordGenerator;
- private PemConverter converter;
- private PrivateKey privateKey;
- private ConvertedArtifactsCreator artifactsCreator;
-
-
- @BeforeEach
- void setUp() {
- certFileWriter = mock(CertFileWriter.class);
- passwordGenerator = mock(RandomPasswordGenerator.class);
- converter = mock(PemConverter.class);
- privateKey = mock(PrivateKey.class);
- artifactsCreator = new ConvertedArtifactsCreator(certFileWriter, passwordGenerator, converter, P12_EXTENSION);
- }
-
- @Test
- void convertedArtifactCreatorShouldTryCreateFileWithGivenExtension()
- throws CertFileWriterException, PemConversionException {
- //given
- mockPasswordGeneratorAndPemConverter();
- final String keystore = "keystore";
- final String testExtension = "testExt";
- final String keystoreFileName = String.format("%s.%s", keystore, testExtension);
- artifactsCreator = new ConvertedArtifactsCreator(certFileWriter, passwordGenerator, converter, testExtension);
-
- //when
- artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
-
- //then
- verify(certFileWriter, times(1))
- .saveData(SAMPLE_KEYSTORE_BYTES, keystoreFileName);
- }
-
- @Test
- void convertedArtifactsCreatorShouldCallConverterAndFilesCreatorMethods()
- throws PemConversionException, CertFileWriterException {
- // given
- mockPasswordGeneratorAndPemConverter();
- final String keystoreP12 = "keystore.p12";
- final String keystorePass = "keystore.pass";
-
- //when
- artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
-
- // then
- verify(converter, times(1))
- .convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey);
- verify(certFileWriter, times(1))
- .saveData(SAMPLE_KEYSTORE_BYTES, keystoreP12);
- verify(certFileWriter, times(1))
- .saveData(SAMPLE_PASSWORD.getCurrentPassword().getBytes(), keystorePass);
- verify(converter, times(1))
- .convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS);
- }
-
- @Test
- void convertedArtifactsCreatorShouldCallPasswordGeneratorTwice()
- throws PemConversionException, CertFileWriterException {
- // given
- mockPasswordGeneratorAndPemConverter();
-
- //when
- artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
-
- // then
- verify(passwordGenerator, times(2)).generate(PASSWORD_LENGTH);
- }
-
- private void mockPasswordGeneratorAndPemConverter() throws PemConversionException {
- when(passwordGenerator.generate(PASSWORD_LENGTH)).thenReturn(SAMPLE_PASSWORD);
- when(converter.convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey))
- .thenReturn(SAMPLE_KEYSTORE_BYTES);
- when(converter.convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS))
- .thenReturn(SAMPLE_TRUSTSTORE_BYTES);
- }
-}
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java
deleted file mode 100644
index 5e79b96f..00000000
--- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*============LICENSE_START=======================================================
- * aaf-certservice-client
- * ================================================================================
- * Copyright (C) 2020 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.aaf.certservice.client.certification.conversion;
-
-import org.junit.jupiter.api.Test;
-import org.onap.aaf.certservice.client.api.ExitableException;
-import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder;
-import org.onap.aaf.certservice.client.certification.writer.CertFileWriter;
-
-import java.security.PrivateKey;
-import java.util.List;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-class PemArtifactsCreatorTest {
- private static final String KEYSTORE_PEM = "keystore.pem";
- private static final String TRUSTSTORE_PEM = "truststore.pem";
- private static final String KEY_PEM = "key.pem";
- private static final String KEY = "my private key";
- private CertFileWriter certFileWriter = mock(CertFileWriter.class);
- private PrivateKey privateKey = mock(PrivateKey.class);
- private PrivateKeyToPemEncoder pkEncoder = mock(PrivateKeyToPemEncoder.class);
-
- @Test
- void pemArtifactsCreatorShouldCallRequiredMethods() throws ExitableException {
- // given
- final PemArtifactsCreator creator = new PemArtifactsCreator(certFileWriter, pkEncoder);
-
- // when
- when(pkEncoder.encodePrivateKeyToPem(privateKey)).thenReturn(KEY);
- creator.create(List.of("one", "two"), List.of("three", "four"), privateKey);
-
- // then
- verify(certFileWriter, times(1)).saveData("one\ntwo".getBytes(), KEYSTORE_PEM);
- verify(certFileWriter, times(1)).saveData("three\nfour".getBytes(), TRUSTSTORE_PEM);
- verify(certFileWriter, times(1)).saveData(KEY.getBytes(), KEY_PEM);
- }
-}
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemConverterTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemConverterTest.java
deleted file mode 100644
index e3a58c3f..00000000
--- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemConverterTest.java
+++ /dev/null
@@ -1,206 +0,0 @@
-/*============LICENSE_START=======================================================
- * aaf-certservice-client
- * ================================================================================
- * Copyright (C) 2020 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.aaf.certservice.client.certification.conversion;
-
-import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.ValueSource;
-import org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants;
-import org.onap.aaf.certservice.client.certification.exception.PemConversionException;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.security.KeyStore;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
-import java.security.PrivateKey;
-import java.security.UnrecoverableKeyException;
-import java.security.cert.Certificate;
-import java.security.cert.CertificateException;
-import java.util.List;
-
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-class PemConverterTest {
-
- private static final String RESOURCES_PATH = "src/test/resources";
- private static final String CERT1_PATH = RESOURCES_PATH + "/cert1.pem";
- private static final String CERT2_PATH = RESOURCES_PATH + "/cert2.pem";
- private static final String KEY_PATH = RESOURCES_PATH + "/privateKey";
- private static final String EXPECTED_KEYSTORE_PATH = RESOURCES_PATH + "/expectedKeystore.jks";
- private static final String EXPECTED_TRUSTSTORE_PATH = RESOURCES_PATH + "/expectedTruststore.jks";
- private static final String PKCS12 = "PKCS12";
- private static final String PKCS8 = "PKCS#8";
- private static final String JKS = "JKS";
- private static final String KEY_ERROR_MSG = "java.security.KeyStoreException: Key protection algorithm not found: java.lang.NullPointerException";
- private static final String CERTIFICATES_ERROR_MSG = "The certificate couldn't be parsed correctly. certificate1";
- private static final String PASSWORD_ERROR_MSG = "Password should be min. 16 chars long and should contain only alphanumeric characters and special characters like Underscore (_), Dollar ($) and Pound (#)";
- private static byte[] key;
- private PrivateKey privateKey = mock(PrivateKey.class);
-
- @BeforeAll
- static void setUpForAll() throws IOException {
- key = Files.readAllBytes(Path.of(KEY_PATH));
- }
-
- @ParameterizedTest
- @ValueSource(strings = {PKCS12, JKS})
- void convertKeystoreShouldReturnKeystoreWithGivenPrivateKeyAndCertificateChain(String conversionTarget)
- throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, PemConversionException {
- // given
- final String alias = "keystore-entry";
- final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0");
- final List<String> certificateChain = getCertificates();
- final PemConverter converter = new PemConverter(conversionTarget);
- final KeyStore expectedKeyStore = KeyStore.getInstance(conversionTarget);
- expectedKeyStore.load(new ByteArrayInputStream(Files.readAllBytes(Path.of(EXPECTED_KEYSTORE_PATH))),
- password.toCharArray());
- final Certificate[] expectedChain = expectedKeyStore.getCertificateChain(alias);
- privateKeyMockSetup();
-
- // when
- final byte[] result = converter.convertKeystore(certificateChain, password, alias, privateKey);
-
- // then
- final KeyStore actualKeyStore = KeyStore.getInstance(conversionTarget);
- actualKeyStore.load(new ByteArrayInputStream(result), password.toCharArray());
- final Certificate[] actualChain = actualKeyStore.getCertificateChain(alias);
-
- assertArrayEquals(key, actualKeyStore.getKey(alias, password.toCharArray()).getEncoded());
- assertEquals(2, expectedChain.length);
- assertArrayEquals(expectedChain, actualChain);
- }
-
- @ParameterizedTest
- @ValueSource(strings = {PKCS12, JKS})
- void convertKeystoreShouldThrowPemConverterExceptionBecauseOfWrongPassword(String conversionTarget) throws IOException {
- // given
- final String alias = "keystore-entry";
- final Password password = new Password("apple");
- final List<String> certificateChain = getCertificates();
- final PemConverter converter = new PemConverter(conversionTarget);
- privateKeyMockSetup();
-
- // when
- Exception exception = assertThrows(PemConversionException.class, () ->
- converter.convertKeystore(certificateChain, password, alias, privateKey)
- );
-
- // then
- assertEquals(PASSWORD_ERROR_MSG, exception.getMessage());
- }
-
- @ParameterizedTest
- @ValueSource(strings = {PKCS12, JKS})
- void convertTruststoreShouldReturnTruststoreWithGivenCertificatesArray(String conversionTarget)
- throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException, PemConversionException {
-
- // given
- final PemConverter converter = new PemConverter(conversionTarget);
- final String alias = "trusted-certificate-";
- final String alias1 = alias + 1;
- final String alias2 = alias + 2;
- final Password password = new Password("9z6oFx1epRSCuBWU4Er8i_0y");
- final List<String> trustedCertificates = getCertificates();
- final KeyStore expectedTrustStore = KeyStore.getInstance(conversionTarget);
- expectedTrustStore.load(new ByteArrayInputStream(Files.readAllBytes(Path.of(EXPECTED_TRUSTSTORE_PATH))),
- password.toCharArray());
-
- // when
- final byte[] result = converter.convertTruststore(trustedCertificates, password, alias);
-
- // then
- final KeyStore actualKeyStore = KeyStore.getInstance(conversionTarget);
- actualKeyStore.load(new ByteArrayInputStream(result), password.toCharArray());
-
- assertTrue(actualKeyStore.containsAlias(alias1));
- assertTrue(actualKeyStore.containsAlias(alias2));
- assertEquals(expectedTrustStore.getCertificate(alias1), actualKeyStore.getCertificate(alias1));
- assertEquals(expectedTrustStore.getCertificate(alias2), actualKeyStore.getCertificate(alias2));
- }
-
- @ParameterizedTest
- @ValueSource(strings = {PKCS12, JKS})
- void convertTruststoreShouldThrowPemConverterExceptionBecauseOfWrongPassword(String conversionTarget) throws IOException {
- // given
- final String alias = "trusted-certificate-";
- final Password password = new Password("nokia");
- final List<String> trustedCertificates = getCertificates();
- final PemConverter converter = new PemConverter(conversionTarget);
-
- // when then
- assertThatThrownBy(() ->
- converter.convertTruststore(trustedCertificates, password, alias))
- .isInstanceOf(PemConversionException.class).hasMessage(PASSWORD_ERROR_MSG);
- }
-
- @Test
- void convertKeystoreShouldThrowPemConverterExceptionBecauseOfWrongPrivateKey() throws IOException {
- // given
- final String alias = "keystore-entry";
- final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0");
- final List<String> certificateChain = getCertificates();
- final PemConverter converter = new PemConverter(PKCS12);
-
- // when then
- assertThatThrownBy(() -> converter.convertKeystore(certificateChain, password, alias, privateKey))
- .isInstanceOf(PemConversionException.class).hasMessage(KEY_ERROR_MSG);
- }
-
- @ParameterizedTest
- @ValueSource(strings = {PKCS12, JKS})
- void convertKeystoreShouldThrowPemConverterExceptionBecauseOfWrongCertificates(String conversionTarget) {
- // given
- final String alias = "keystore-entry";
- final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0");
- final List<String> certificateChain = List.of("certificate1", "certificate2");
- final PemConverter converter = new PemConverter(conversionTarget);
- privateKeyMockSetup();
-
- // when then
- assertThatThrownBy(() -> converter.convertKeystore(certificateChain, password, alias, privateKey))
- .isInstanceOf(PemConversionException.class).hasMessage(CERTIFICATES_ERROR_MSG);
- }
-
- private void privateKeyMockSetup() {
- when(privateKey.getEncoded()).thenReturn(key);
- when(privateKey.getAlgorithm()).thenReturn(EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM);
- when(privateKey.getFormat()).thenReturn(PKCS8);
- }
-
- private List<String> getCertificates() throws IOException {
- return List.of(
- Files.readString(
- Path.of(CERT1_PATH), StandardCharsets.UTF_8),
- Files.readString(
- Path.of(CERT2_PATH), StandardCharsets.UTF_8)
- );
- }
-}
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/RandomPasswordGeneratorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/RandomPasswordGeneratorTest.java
deleted file mode 100644
index 483e35e0..00000000
--- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/RandomPasswordGeneratorTest.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*============LICENSE_START=======================================================
- * aaf-certservice-client
- * ================================================================================
- * Copyright (C) 2020 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.aaf.certservice.client.certification.conversion;
-
-import org.junit.jupiter.api.Test;
-
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-class RandomPasswordGeneratorTest {
-
- @Test
- void shouldGenerateRandomPasswordOfGivenLengthMatchingThePattern() {
- Password password = new RandomPasswordGenerator().generate(24);
- assertTrue(password.isCorrectPasswordPattern());
- }
-} \ No newline at end of file
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java
deleted file mode 100644
index c45876ec..00000000
--- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*============LICENSE_START=======================================================
- * aaf-certservice-client
- * ================================================================================
- * Copyright (C) 2020 Nokia. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.aaf.certservice.client.certification.writer;
-
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.ValueSource;
-import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.List;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-
-class CertFileWriterTest {
-
- private static final String RESOURCES_PATH = "src/test/resources/";
- private static final String OUTPUT_PATH = RESOURCES_PATH + "generatedFiles/";
- private static final String NOT_EXISTING_OUTPUT_PATH = OUTPUT_PATH + "directoryDoesNotExist/";
- private static final String TRUSTSTORE_P12 = "truststore.p12";
- private File outputDirectory = new File(OUTPUT_PATH);
-
- @AfterEach
- void cleanUpFiles() {
- deleteDirectoryRecursive(outputDirectory);
- }
-
- @ParameterizedTest
- @ValueSource(strings = {OUTPUT_PATH, NOT_EXISTING_OUTPUT_PATH})
- void certFileWriterShouldCreateFilesWithDataInGivenLocation(String outputPath)
- throws IOException, CertFileWriterException {
- // given
- File truststore = new File(outputPath + TRUSTSTORE_P12);
- CertFileWriter certFileWriter = CertFileWriter.createWithDir(outputPath);
- final byte[] data = new byte[]{-128, 1, 2, 3, 127};
-
- // when
- certFileWriter.saveData(data, TRUSTSTORE_P12);
-
- // then
- assertThat(truststore.exists()).isTrue();
- assertThat(Files.readAllBytes(Path.of(outputPath + TRUSTSTORE_P12))).isEqualTo(data);
- }
-
- private void deleteDirectoryRecursive(File dirForDeletion) {
- List.of(dirForDeletion.listFiles()).forEach(file -> {
- if (file.isDirectory()) {
- deleteDirectoryRecursive(file);
- }
- file.delete();
- });
- dirForDeletion.delete();
- }
-
-}