Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: David Kornel <kornys@outlook.com>
  • Loading branch information
kornys committed Jul 25, 2024
1 parent eb4fb5d commit 88698d4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 88698d4

Please sign in to comment.