From 88698d4ef435d7077e7da630b0fe949fced308f4 Mon Sep 17 00:00:00 2001 From: David Kornel Date: Thu, 25 Jul 2024 17:09:11 +0200 Subject: [PATCH] Add unit tests Signed-off-by: David Kornel --- .../TestEnvironmentVariablesTest.java | 3 +- .../security/CertAndKeyBuilderTest.java | 59 ++++++++++++++++ .../testframe/security/SecurityUtilsTest.java | 69 +++++++++++++++++++ 3 files changed, 129 insertions(+), 2 deletions(-) rename test-frame-common/src/test/java/io/skodjob/testframe/{clients => environment}/TestEnvironmentVariablesTest.java (94%) create mode 100644 test-frame-common/src/test/java/io/skodjob/testframe/security/CertAndKeyBuilderTest.java create mode 100644 test-frame-common/src/test/java/io/skodjob/testframe/security/SecurityUtilsTest.java diff --git a/test-frame-common/src/test/java/io/skodjob/testframe/clients/TestEnvironmentVariablesTest.java b/test-frame-common/src/test/java/io/skodjob/testframe/environment/TestEnvironmentVariablesTest.java similarity index 94% rename from test-frame-common/src/test/java/io/skodjob/testframe/clients/TestEnvironmentVariablesTest.java rename to test-frame-common/src/test/java/io/skodjob/testframe/environment/TestEnvironmentVariablesTest.java index 781906d..3891ac4 100644 --- a/test-frame-common/src/test/java/io/skodjob/testframe/clients/TestEnvironmentVariablesTest.java +++ b/test-frame-common/src/test/java/io/skodjob/testframe/environment/TestEnvironmentVariablesTest.java @@ -2,9 +2,8 @@ * Copyright Skodjob authors. * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). */ -package io.skodjob.testframe.clients; +package io.skodjob.testframe.environment; -import io.skodjob.testframe.environment.TestEnvironmentVariables; import org.junit.jupiter.api.Test; import java.io.IOException; diff --git a/test-frame-common/src/test/java/io/skodjob/testframe/security/CertAndKeyBuilderTest.java b/test-frame-common/src/test/java/io/skodjob/testframe/security/CertAndKeyBuilderTest.java new file mode 100644 index 0000000..76cbfdb --- /dev/null +++ b/test-frame-common/src/test/java/io/skodjob/testframe/security/CertAndKeyBuilderTest.java @@ -0,0 +1,59 @@ +/* + * Copyright Skodjob authors. + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). + */ +package io.skodjob.testframe.security; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CertAndKeyBuilderTest { + static final String ROOT_CA = "C=COM, L=Boston, O=Example, CN=ExampleRootCA"; + static final String INTERMEDIATE_CA = "C=COM, L=Boston, O=Example, CN=ExampleIntermediateCA"; + static final String END_SUBJECT = "C=COM, L=Boston, O=Example, CN=end-app.example.io"; + static final String APP_SUBJECT = "C=COM, L=Boston, O=Example, CN=app.example.io"; + + static final String COMPARE_ROOT_DN = "CN=ExampleRootCA,O=Example,L=Boston,C=COM"; + static final String COMPARE_INTERMEDIATE_DN = "CN=ExampleIntermediateCA,O=Example,L=Boston,C=COM"; + + @Test + void testGenerateCerts() { + CertAndKey ca = CertAndKeyBuilder.rootCaCertBuilder() + .withIssuerDn(ROOT_CA) + .withSubjectDn(ROOT_CA) + .build(); + + assertEquals(COMPARE_ROOT_DN, ca.certificate().getIssuerX500Principal().getName()); + assertDoesNotThrow(() -> ca.certificate().checkValidity()); + + CertAndKey intermediateCa = CertAndKeyBuilder.intermediateCaCertBuilder(ca) + .withIssuerDn(INTERMEDIATE_CA) + .withSubjectDn(INTERMEDIATE_CA) + .build(); + + assertEquals(COMPARE_INTERMEDIATE_DN, intermediateCa.certificate().getIssuerX500Principal().getName()); + assertDoesNotThrow(() -> intermediateCa.certificate().checkValidity()); + + CertAndKey appCert = CertAndKeyBuilder.appCaCertBuilder(ca) + .withSubjectDn(APP_SUBJECT) + .build(); + + assertEquals(COMPARE_ROOT_DN, appCert.certificate().getIssuerX500Principal().getName()); + assertDoesNotThrow(() -> appCert.certificate().checkValidity()); + + CertAndKey endAppCert = CertAndKeyBuilder.endEntityCertBuilder(intermediateCa) + .withSubjectDn(END_SUBJECT) + .withSanDnsName("*.example.io") + .build(); + + assertEquals(COMPARE_INTERMEDIATE_DN, endAppCert.certificate().getIssuerX500Principal().getName()); + assertDoesNotThrow(() -> endAppCert.certificate().checkValidity()); + + // check cert signing + assertDoesNotThrow(() -> appCert.certificate().verify(ca.getPublicKey())); + assertDoesNotThrow(() -> endAppCert.certificate().verify(intermediateCa.getPublicKey())); + assertDoesNotThrow(() -> intermediateCa.certificate().verify(ca.getPublicKey())); + } +} diff --git a/test-frame-common/src/test/java/io/skodjob/testframe/security/SecurityUtilsTest.java b/test-frame-common/src/test/java/io/skodjob/testframe/security/SecurityUtilsTest.java new file mode 100644 index 0000000..41ae40f --- /dev/null +++ b/test-frame-common/src/test/java/io/skodjob/testframe/security/SecurityUtilsTest.java @@ -0,0 +1,69 @@ +/* + * Copyright Skodjob authors. + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). + */ +package io.skodjob.testframe.security; + +import io.skodjob.testframe.utils.SecurityUtils; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.cert.CertificateEncodingException; + +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class SecurityUtilsTest { + static final String ROOT_CA = "C=COM, L=Boston, O=Example, CN=ExampleRootCA"; + static final String INTERMEDIATE_CA = "C=COM, L=Boston, O=Example, CN=ExampleIntermediateCA"; + static final String END_SUBJECT = "C=COM, L=Boston, O=Example, CN=end-app.example.io"; + static final String APP_SUBJECT = "C=COM, L=Boston, O=Example, CN=app.example.io"; + + CertAndKey ca; + CertAndKey intermediateCa; + CertAndKey appCert; + CertAndKey endAppCert; + + @BeforeAll + void setup() { + ca = CertAndKeyBuilder.rootCaCertBuilder() + .withIssuerDn(ROOT_CA) + .withSubjectDn(ROOT_CA) + .build(); + + intermediateCa = CertAndKeyBuilder.intermediateCaCertBuilder(ca) + .withIssuerDn(INTERMEDIATE_CA) + .withSubjectDn(INTERMEDIATE_CA) + .build(); + + appCert = CertAndKeyBuilder.appCaCertBuilder(ca) + .withSubjectDn(APP_SUBJECT) + .build(); + + endAppCert = CertAndKeyBuilder.endEntityCertBuilder(intermediateCa) + .withSubjectDn(END_SUBJECT) + .withSanDnsName("*.example.io") + .build(); + } + + @Test + void testExportCertsToPem() throws IOException, CertificateEncodingException { + CertAndKeyFiles all = SecurityUtils.exportToPemFiles(ca, intermediateCa, appCert); + + String content = Files.readString(Paths.get(all.getCertPath())); + assertNotEquals("", content); + } + + @Test + void testExportDataToCa() throws IOException { + File caCert = SecurityUtils.exportCaDataToFile(ca.getPublicKey().toString(), "ca", ".crt"); + + String content = Files.readString(caCert.toPath()); + assertNotEquals("", content); + } +}