Skip to content

Commit

Permalink
Pki - migrate to junit 5 (hyperledger#6235)
Browse files Browse the repository at this point in the history
* migrate to junit5

Signed-off-by: Sally MacFarlane <[email protected]>

* fix: double calls to trace{Start,End}Transaction (hyperledger#6247)

Signed-off-by: Franklin Delehelle <[email protected]>

* migrate to junit5 (hyperledger#6234)

Signed-off-by: Sally MacFarlane <[email protected]>

* fixes for problems discovered in main (hyperledger#6248)

Signed-off-by: garyschulte <[email protected]>

* fixed test comparing size of collection

Signed-off-by: Sally MacFarlane <[email protected]>

---------

Signed-off-by: Sally MacFarlane <[email protected]>
Signed-off-by: Franklin Delehelle <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Co-authored-by: delehef <[email protected]>
Co-authored-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
3 people committed Dec 7, 2023
1 parent 248462f commit 41f3f75
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 155 deletions.
3 changes: 1 addition & 2 deletions pki/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ dependencies {
implementation 'io.tmio:tuweni-bytes'
implementation 'org.bouncycastle:bcpkix-jdk18on'

testImplementation 'junit:junit'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.mockito:mockito-core'
testImplementation 'org.mockito:mockito-junit-jupiter'

testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

package org.hyperledger.besu.pki.cms;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.hyperledger.besu.pki.util.TestCertificateUtils.Algorithm.EC;
import static org.hyperledger.besu.pki.util.TestCertificateUtils.Algorithm.RSA;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.hyperledger.besu.pki.util.TestCertificateUtils.Algorithm;

Expand Down Expand Up @@ -56,8 +57,7 @@ private CmsTestKeystores getCmsTestKeystores(final Algorithm algorithm) {
public void cmsValidationWithEmptyCmsMessage(final Algorithm algorithm) {
final Bytes data = Bytes.random(32);

assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(Bytes.EMPTY, data))
.isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(Bytes.EMPTY, data));
}

@ParameterizedTest
Expand All @@ -69,7 +69,7 @@ public void cmsValidationWithTrustedSelfSignedCertificate(final Algorithm algori

final Bytes cms = cmsCreator.create(data);

assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isTrue();
assertTrue(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}

@ParameterizedTest
Expand All @@ -81,7 +81,7 @@ public void cmsValidationWithUntrustedSelfSignedCertificate(final Algorithm algo

final Bytes cms = cmsCreator.create(data);

assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}

@ParameterizedTest
Expand All @@ -93,7 +93,7 @@ public void cmsValidationWithTrustedChain(final Algorithm algorithm) {

final Bytes cms = cmsCreator.create(data);

assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isTrue();
assertTrue(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}

@ParameterizedTest
Expand All @@ -105,7 +105,7 @@ public void cmsValidationWithUntrustedChain(final Algorithm algorithm) {

final Bytes cms = cmsCreator.create(data);

assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}

@ParameterizedTest
Expand All @@ -117,7 +117,7 @@ public void cmsValidationWithExpiredCertificate(final Algorithm algorithm) {

final Bytes cms = cmsCreator.create(data);

assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}

@ParameterizedTest
Expand All @@ -129,7 +129,7 @@ public void cmsValidationWithRevokedCertificate(final Algorithm algorithm) {

final Bytes cms = cmsCreator.create(data);

assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}

@ParameterizedTest
Expand All @@ -144,7 +144,7 @@ public void cmsValidationWithoutCRLConfigDisablesCRLCheck(final Algorithm algori
CmsValidator cmsValidator = getCmsTestKeystores(algorithm).getCmsValidatorWithoutCrl();

// Because we don't have a CRL CertStore, revocation is not checked
assertThat(cmsValidator.validate(cms, data)).isTrue();
assertTrue(cmsValidator.validate(cms, data));
}

@ParameterizedTest
Expand All @@ -156,8 +156,7 @@ public void cmsValidationWithWrongSignedData(final Algorithm algorithm) {
final Bytes cms = cmsCreator.create(otherData);

final Bytes expectedData = Bytes.random(32);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, expectedData))
.isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, expectedData));
}

@ParameterizedTest
Expand Down Expand Up @@ -198,7 +197,6 @@ public void cmsValidationWithInvalidSignature(final Algorithm algorithm) throws
final CMSSignedData cmsSignedData = cmsGenerator.generate(cmsData, true);
final Bytes cmsBytes = Bytes.wrap(cmsSignedData.getEncoded());

assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cmsBytes, expectedData))
.isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cmsBytes, expectedData));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,113 +14,143 @@
*/
package org.hyperledger.besu.pki.keystore;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.nio.file.Path;
import java.security.cert.Certificate;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

@RunWith(Parameterized.class)
public abstract class BaseKeyStoreFileWrapperTest {
protected static final String KEYSTORE_VALID_KEY_ALIAS = "partner1client1";
protected static final String KEYSTORE_INVALID_KEY_ALIAS = "partner1clientinvalid";
protected static final String TRUSTSTORE_VALID_CERTIFICATE_ALIAS = "interca";
protected static final String TRUSTSTORE_INVALID_CERTIFICATE_ALIAS = "interca-invalid";

@Parameterized.Parameter public String keyStoreWrapperDescription;

@Parameterized.Parameter(1)
public boolean keystoreWrapperConfiguredWithTruststore;

@Parameterized.Parameter(2)
public KeyStoreWrapper keyStoreWrapper;

protected static Path toPath(final String path) throws Exception {
return null == path
? null
: Path.of(BaseKeyStoreFileWrapperTest.class.getResource(path).toURI());
}

@Test
public void getPublicKey_WithValidAlias_ReturnsExpectedValue() {
assertThat(keyStoreWrapper.getPublicKey(KEYSTORE_VALID_KEY_ALIAS))
.as("Public key is not null")
.isNotNull();
@ParameterizedTest
@MethodSource("data")
public void getPublicKey_WithValidAlias_ReturnsExpectedValue(
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
assertNotNull(
keyStoreWrapperTestParameter.keyStoreWrapper.getPublicKey(KEYSTORE_VALID_KEY_ALIAS));
}

@Test
public void getPublicKey_WithInvalidAlias_ReturnsExpectedValue() {
assertThat(keyStoreWrapper.getPublicKey(KEYSTORE_INVALID_KEY_ALIAS))
.as("Public key is null")
.isNull();
@ParameterizedTest
@MethodSource("data")
public void getPublicKey_WithInvalidAlias_ReturnsExpectedValue(
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
assertNull(
keyStoreWrapperTestParameter.keyStoreWrapper.getPublicKey(KEYSTORE_INVALID_KEY_ALIAS));
}

@Test
public void getPrivateKey_WithValidAlias_ReturnsExpectedValue() {
assertThat(keyStoreWrapper.getPrivateKey(KEYSTORE_VALID_KEY_ALIAS))
.as("Private key is not null")
.isNotNull();
@ParameterizedTest
@MethodSource("data")
public void getPrivateKey_WithValidAlias_ReturnsExpectedValue(
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
assertNotNull(
keyStoreWrapperTestParameter.keyStoreWrapper.getPrivateKey(KEYSTORE_VALID_KEY_ALIAS),
"Private key is not null");
}

@Test
public void getPrivateKey_WithInvalidAlias_ReturnsExpectedValue() {
assertThat(keyStoreWrapper.getPrivateKey(KEYSTORE_INVALID_KEY_ALIAS))
.as("Private key is null")
.isNull();
@ParameterizedTest
@MethodSource("data")
public void getPrivateKey_WithInvalidAlias_ReturnsExpectedValue(
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
assertNull(
keyStoreWrapperTestParameter.keyStoreWrapper.getPrivateKey(KEYSTORE_INVALID_KEY_ALIAS),
"Private key is null");
}

@Test
public void getCertificate_WithValidAlias_ReturnsExpectedValue() {
assertThat(keyStoreWrapper.getCertificate(KEYSTORE_VALID_KEY_ALIAS))
.as("Certificate is not null")
.isNotNull();
@ParameterizedTest
@MethodSource("data")
public void getCertificate_WithValidAlias_ReturnsExpectedValue(
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
assertNotNull(
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificate(KEYSTORE_VALID_KEY_ALIAS),
"Certificate is not null");
}

@Test
public void getCertificate_WithInvalidAlias_ReturnsExpectedValue() {
assertThat(keyStoreWrapper.getCertificate(KEYSTORE_INVALID_KEY_ALIAS))
.as("Certificate is null")
.isNull();
@ParameterizedTest
@MethodSource("data")
public void getCertificate_WithInvalidAlias_ReturnsExpectedValue(
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
assertNull(
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificate(KEYSTORE_INVALID_KEY_ALIAS),
"Certificate is null");
}

@Test
public void getCertificateChain_WithValidAlias_ReturnsExpectedValue() {
assertThat(keyStoreWrapper.getCertificateChain(KEYSTORE_VALID_KEY_ALIAS))
.as("Certificate chain is not null")
.isNotNull();
@ParameterizedTest
@MethodSource("data")
public void getCertificateChain_WithValidAlias_ReturnsExpectedValue(
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
assertNotNull(
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificateChain(KEYSTORE_VALID_KEY_ALIAS),
"Certificate chain is not null");
}

@Test
public void getCertificateChain_WithInvalidAlias_ReturnsExpectedValue() {
assertThat(keyStoreWrapper.getCertificateChain(KEYSTORE_INVALID_KEY_ALIAS))
.as("Certificate is null")
.isNull();
@ParameterizedTest
@MethodSource("data")
public void getCertificateChain_WithInvalidAlias_ReturnsExpectedValue(
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
assertNull(
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificateChain(
KEYSTORE_INVALID_KEY_ALIAS),
"Certificate is null");
}

@Test
public void getCertificate_FromTruststore_WithValidAlias_ReturnsExpectedValue() {
@ParameterizedTest
@MethodSource("data")
public void getCertificate_FromTruststore_WithValidAlias_ReturnsExpectedValue(
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
final Certificate certificate =
keyStoreWrapper.getCertificate(TRUSTSTORE_VALID_CERTIFICATE_ALIAS);
if (keystoreWrapperConfiguredWithTruststore) {
assertThat(certificate).as("Certificate is not null").isNotNull();
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificate(
TRUSTSTORE_VALID_CERTIFICATE_ALIAS);
if (keyStoreWrapperTestParameter.keystoreWrapperConfiguredWithTruststore) {
assertNotNull(certificate, "Certificate is not null");
} else {
assertThat(certificate).as("Certificate is null").isNull();
assertNull(certificate, "Certificate is null");
}
}

@Test
public void getCertificate_FromTruststore_WithInvalidAlias_ReturnsExpectedValue() {
assertThat(keyStoreWrapper.getPrivateKey(TRUSTSTORE_INVALID_CERTIFICATE_ALIAS))
.as("Certificate is null")
.isNull();
@ParameterizedTest
@MethodSource("data")
public void getCertificate_FromTruststore_WithInvalidAlias_ReturnsExpectedValue(
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
assertNull(
keyStoreWrapperTestParameter.keyStoreWrapper.getPrivateKey(
TRUSTSTORE_INVALID_CERTIFICATE_ALIAS),
"Certificate is null");
}

@Test
public void getCRLS_Check() {
assertThat(keyStoreWrapper.getCRLs()).as("CRLs is not null").isNotNull();
assertThat(keyStoreWrapper.getCRLs().size()).as("CRLs size matches").isEqualTo(2);
@ParameterizedTest
@MethodSource("data")
public void getCRLS_Check(final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
assertNotNull(keyStoreWrapperTestParameter.keyStoreWrapper.getCRLs(), "CRLs is not null");
assertEquals(
keyStoreWrapperTestParameter.keyStoreWrapper.getCRLs().size(), 2, "CRLs size matches");
}

public static class KeyStoreWrapperTestParameter {
public String keyStoreWrapperDescription;
public boolean keystoreWrapperConfiguredWithTruststore;
public KeyStoreWrapper keyStoreWrapper;

public KeyStoreWrapperTestParameter(
final String keyStoreWrapperDescription,
final boolean keystoreWrapperConfiguredWithTruststore,
final KeyStoreWrapper keyStoreWrapper) {
this.keyStoreWrapperDescription = keyStoreWrapperDescription;
this.keystoreWrapperConfiguredWithTruststore = keystoreWrapperConfiguredWithTruststore;
this.keyStoreWrapper = keyStoreWrapper;
}
}
}
Loading

0 comments on commit 41f3f75

Please sign in to comment.