diff options
author | 2020-06-17 14:54:51 +0200 | |
---|---|---|
committer | 2020-06-19 10:13:08 +0200 | |
commit | 0b278343630c1e3c7c92b3e5094570748692666e (patch) | |
tree | 7f859bedb889dfb46685e7ea71865fb3f62aff31 /certServiceClient/src/test/java/org/onap/aaf/certservice | |
parent | 212038b654728b79aa647e08da2562484c63c883 (diff) |
Add creation JKS artifact
Issue-ID: AAF-1152
Signed-off-by: Piotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Change-Id: I8b38dc07ddbf6758e0c4c036100572b350dceab9
Diffstat (limited to 'certServiceClient/src/test/java/org/onap/aaf/certservice')
-rw-r--r-- | certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java | 26 | ||||
-rw-r--r-- | certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ConvertedArtifactsCreatorTest.java (renamed from certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java) | 49 | ||||
-rw-r--r-- | certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemConverterTest.java (renamed from certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemToPKCS12ConverterTest.java) | 68 |
3 files changed, 94 insertions, 49 deletions
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java index 133d90d2..8a619991 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java @@ -20,6 +20,9 @@ package org.onap.aaf.certservice.client.certification.conversion; 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 static org.assertj.core.api.Assertions.assertThat; @@ -27,17 +30,19 @@ 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"; - @Test - void artifactsProviderShouldReturnP12Creator(){ + @ParameterizedTest + @ValueSource(strings = {JKS, P12}) + void artifactsProviderShouldReturnConvertedCreator(String outputType){ // when ArtifactsCreator artifactsCreator = - ArtifactsCreatorProvider.getCreator(P12, TEST_PATH); + ArtifactsCreatorProvider.getCreator(outputType, TEST_PATH); // then - assertThat(artifactsCreator).isInstanceOf(PKCS12ArtifactsCreator.class); + assertThat(artifactsCreator).isInstanceOf(ConvertedArtifactsCreator.class); } @Test @@ -49,4 +54,17 @@ class ArtifactsCreatorProviderTest { // 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/conversion/PKCS12ArtifactsCreatorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ConvertedArtifactsCreatorTest.java index 4a690e5f..2da4ab98 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ConvertedArtifactsCreatorTest.java @@ -30,10 +30,10 @@ import java.util.List; 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.PemToPKCS12ConverterException; +import org.onap.aaf.certservice.client.certification.exception.PemConversionException; import org.onap.aaf.certservice.client.certification.writer.CertFileWriter; -class PKCS12ArtifactsCreatorTest { +class ConvertedArtifactsCreatorTest { private static final int PASSWORD_LENGTH = 24; private static final String CERTIFICATE_ALIAS = "certificate"; @@ -44,33 +44,52 @@ class PKCS12ArtifactsCreatorTest { 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 PemToPKCS12Converter converter; + private PemConverter converter; private PrivateKey privateKey; - private PKCS12ArtifactsCreator artifactCreator; + private ConvertedArtifactsCreator artifactsCreator; @BeforeEach void setUp() { certFileWriter = mock(CertFileWriter.class); passwordGenerator = mock(RandomPasswordGenerator.class); - converter = mock(PemToPKCS12Converter.class); + converter = mock(PemConverter.class); privateKey = mock(PrivateKey.class); - artifactCreator = new PKCS12ArtifactsCreator(certFileWriter, passwordGenerator, converter); + artifactsCreator = new ConvertedArtifactsCreator(certFileWriter, passwordGenerator, converter, P12_EXTENSION); } @Test - void artifactsCreatorShouldCauseCallOfConvertAndDataSaveMethods() - throws PemToPKCS12ConverterException, CertFileWriterException { + 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 - mockPasswordGeneratorAndPKSC12Converter(); + mockPasswordGeneratorAndPemConverter(); final String keystoreP12 = "keystore.p12"; final String keystorePass = "keystore.pass"; //when - artifactCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey); + artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey); // then verify(converter, times(1)) @@ -84,19 +103,19 @@ class PKCS12ArtifactsCreatorTest { } @Test - void artifactsCreatorShouldCallPasswordGeneratorTwice() - throws PemToPKCS12ConverterException, CertFileWriterException { + void convertedArtifactsCreatorShouldCallPasswordGeneratorTwice() + throws PemConversionException, CertFileWriterException { // given - mockPasswordGeneratorAndPKSC12Converter(); + mockPasswordGeneratorAndPemConverter(); //when - artifactCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey); + artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey); // then verify(passwordGenerator, times(2)).generate(PASSWORD_LENGTH); } - private void mockPasswordGeneratorAndPKSC12Converter() throws PemToPKCS12ConverterException { + 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); diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemToPKCS12ConverterTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemConverterTest.java index 35043409..0d67fba7 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemToPKCS12ConverterTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemConverterTest.java @@ -42,10 +42,12 @@ import java.security.cert.CertificateException; import java.util.List; 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.PemToPKCS12ConverterException; +import org.onap.aaf.certservice.client.certification.exception.PemConversionException; -class PemToPKCS12ConverterTest { +class PemConverterTest { private static final String RESOURCES_PATH = "src/test/resources"; private static final String CERT1_PATH = RESOURCES_PATH + "/cert1.pem"; @@ -55,6 +57,7 @@ class PemToPKCS12ConverterTest { 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 (#)"; @@ -66,15 +69,16 @@ class PemToPKCS12ConverterTest { key = Files.readAllBytes(Path.of(KEY_PATH)); } - @Test - void convertKeystoreShouldReturnKeystoreWithGivenPrivateKeyAndCertificateChain() - throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, PemToPKCS12ConverterException { + @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 PemToPKCS12Converter converter = new PemToPKCS12Converter(); - final KeyStore expectedKeyStore = KeyStore.getInstance(PKCS12); + 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); @@ -84,7 +88,7 @@ class PemToPKCS12ConverterTest { final byte[] result = converter.convertKeystore(certificateChain, password, alias, privateKey); // then - final KeyStore actualKeyStore = KeyStore.getInstance(PKCS12); + final KeyStore actualKeyStore = KeyStore.getInstance(conversionTarget); actualKeyStore.load(new ByteArrayInputStream(result), password.toCharArray()); final Certificate[] actualChain = actualKeyStore.getCertificateChain(alias); @@ -93,17 +97,18 @@ class PemToPKCS12ConverterTest { assertArrayEquals(expectedChain, actualChain); } - @Test - void convertKeystoreShouldThrowPemToPKCS12ConverterExceptionBecauseOfWrongPassword() throws IOException { + @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 PemToPKCS12Converter converter = new PemToPKCS12Converter(); + final PemConverter converter = new PemConverter(conversionTarget); privateKeyMockSetup(); // when - Exception exception = assertThrows(PemToPKCS12ConverterException.class, () -> + Exception exception = assertThrows(PemConversionException.class, () -> converter.convertKeystore(certificateChain, password, alias, privateKey) ); @@ -111,18 +116,19 @@ class PemToPKCS12ConverterTest { assertEquals(PASSWORD_ERROR_MSG, exception.getMessage()); } - @Test - void convertTruststoreShouldReturnTruststoreWithGivenCertificatesArray() - throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException, PemToPKCS12ConverterException { + @ParameterizedTest + @ValueSource(strings = {PKCS12, JKS}) + void convertTruststoreShouldReturnTruststoreWithGivenCertificatesArray(String conversionTarget) + throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException, PemConversionException { // given - final PemToPKCS12Converter converter = new PemToPKCS12Converter(); + 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(PKCS12); + final KeyStore expectedTrustStore = KeyStore.getInstance(conversionTarget); expectedTrustStore.load(new ByteArrayInputStream(Files.readAllBytes(Path.of(EXPECTED_TRUSTSTORE_PATH))), password.toCharArray()); @@ -130,7 +136,7 @@ class PemToPKCS12ConverterTest { final byte[] result = converter.convertTruststore(trustedCertificates, password, alias); // then - final KeyStore actualKeyStore = KeyStore.getInstance(PKCS12); + final KeyStore actualKeyStore = KeyStore.getInstance(conversionTarget); actualKeyStore.load(new ByteArrayInputStream(result), password.toCharArray()); assertTrue(actualKeyStore.containsAlias(alias1)); @@ -139,45 +145,47 @@ class PemToPKCS12ConverterTest { assertEquals(expectedTrustStore.getCertificate(alias2), actualKeyStore.getCertificate(alias2)); } - @Test - void convertTruststoreShouldThrowPemToPKCS12ConverterExceptionBecauseOfWrongPassword() throws IOException { + @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 PemToPKCS12Converter converter = new PemToPKCS12Converter(); + final PemConverter converter = new PemConverter(conversionTarget); // when then assertThatThrownBy(() -> converter.convertTruststore(trustedCertificates, password, alias)) - .isInstanceOf(PemToPKCS12ConverterException.class).hasMessage(PASSWORD_ERROR_MSG); + .isInstanceOf(PemConversionException.class).hasMessage(PASSWORD_ERROR_MSG); } @Test - void convertKeystoreShouldThrowPemToPKCS12ConverterExceptionBecauseOfWrongPrivateKey() throws IOException { + void convertKeystoreShouldThrowPemConverterExceptionBecauseOfWrongPrivateKey() throws IOException { // given final String alias = "keystore-entry"; final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0"); final List<String> certificateChain = getCertificates(); - final PemToPKCS12Converter converter = new PemToPKCS12Converter(); + final PemConverter converter = new PemConverter(PKCS12); // when then assertThatThrownBy(() -> converter.convertKeystore(certificateChain, password, alias, privateKey)) - .isInstanceOf(PemToPKCS12ConverterException.class).hasMessage(KEY_ERROR_MSG); + .isInstanceOf(PemConversionException.class).hasMessage(KEY_ERROR_MSG); } - @Test - void convertKeystoreShouldThrowPemToPKCS12ConverterExceptionBecauseOfWrongCertificates() { + @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 PemToPKCS12Converter converter = new PemToPKCS12Converter(); + final PemConverter converter = new PemConverter(conversionTarget); privateKeyMockSetup(); // when then assertThatThrownBy(() -> converter.convertKeystore(certificateChain, password, alias, privateKey)) - .isInstanceOf(PemToPKCS12ConverterException.class).hasMessage(CERTIFICATES_ERROR_MSG); + .isInstanceOf(PemConversionException.class).hasMessage(CERTIFICATES_ERROR_MSG); } private void privateKeyMockSetup() { @@ -194,4 +202,4 @@ class PemToPKCS12ConverterTest { Path.of(CERT2_PATH), StandardCharsets.UTF_8) ); } -}
\ No newline at end of file +} |