-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Kornel <kornys@outlook.com>
- Loading branch information
Showing
3 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
test-frame-common/src/test/java/io/skodjob/testframe/security/CertAndKeyBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
test-frame-common/src/test/java/io/skodjob/testframe/security/SecurityUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |