|
|
@ -14,113 +14,143 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package org.hyperledger.besu.pki.keystore; |
|
|
|
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.nio.file.Path; |
|
|
|
import java.security.cert.Certificate; |
|
|
|
import java.security.cert.Certificate; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.jupiter.params.ParameterizedTest; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
import org.junit.jupiter.params.provider.MethodSource; |
|
|
|
import org.junit.runners.Parameterized; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(Parameterized.class) |
|
|
|
|
|
|
|
public abstract class BaseKeyStoreFileWrapperTest { |
|
|
|
public abstract class BaseKeyStoreFileWrapperTest { |
|
|
|
protected static final String KEYSTORE_VALID_KEY_ALIAS = "partner1client1"; |
|
|
|
protected static final String KEYSTORE_VALID_KEY_ALIAS = "partner1client1"; |
|
|
|
protected static final String KEYSTORE_INVALID_KEY_ALIAS = "partner1clientinvalid"; |
|
|
|
protected static final String KEYSTORE_INVALID_KEY_ALIAS = "partner1clientinvalid"; |
|
|
|
protected static final String TRUSTSTORE_VALID_CERTIFICATE_ALIAS = "interca"; |
|
|
|
protected static final String TRUSTSTORE_VALID_CERTIFICATE_ALIAS = "interca"; |
|
|
|
protected static final String TRUSTSTORE_INVALID_CERTIFICATE_ALIAS = "interca-invalid"; |
|
|
|
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 { |
|
|
|
protected static Path toPath(final String path) throws Exception { |
|
|
|
return null == path |
|
|
|
return null == path |
|
|
|
? null |
|
|
|
? null |
|
|
|
: Path.of(BaseKeyStoreFileWrapperTest.class.getResource(path).toURI()); |
|
|
|
: Path.of(BaseKeyStoreFileWrapperTest.class.getResource(path).toURI()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getPublicKey_WithValidAlias_ReturnsExpectedValue() { |
|
|
|
@MethodSource("data") |
|
|
|
assertThat(keyStoreWrapper.getPublicKey(KEYSTORE_VALID_KEY_ALIAS)) |
|
|
|
public void getPublicKey_WithValidAlias_ReturnsExpectedValue( |
|
|
|
.as("Public key is not null") |
|
|
|
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
.isNotNull(); |
|
|
|
assertNotNull( |
|
|
|
|
|
|
|
keyStoreWrapperTestParameter.keyStoreWrapper.getPublicKey(KEYSTORE_VALID_KEY_ALIAS)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getPublicKey_WithInvalidAlias_ReturnsExpectedValue() { |
|
|
|
@MethodSource("data") |
|
|
|
assertThat(keyStoreWrapper.getPublicKey(KEYSTORE_INVALID_KEY_ALIAS)) |
|
|
|
public void getPublicKey_WithInvalidAlias_ReturnsExpectedValue( |
|
|
|
.as("Public key is null") |
|
|
|
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
.isNull(); |
|
|
|
assertNull( |
|
|
|
|
|
|
|
keyStoreWrapperTestParameter.keyStoreWrapper.getPublicKey(KEYSTORE_INVALID_KEY_ALIAS)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getPrivateKey_WithValidAlias_ReturnsExpectedValue() { |
|
|
|
@MethodSource("data") |
|
|
|
assertThat(keyStoreWrapper.getPrivateKey(KEYSTORE_VALID_KEY_ALIAS)) |
|
|
|
public void getPrivateKey_WithValidAlias_ReturnsExpectedValue( |
|
|
|
.as("Private key is not null") |
|
|
|
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
.isNotNull(); |
|
|
|
assertNotNull( |
|
|
|
|
|
|
|
keyStoreWrapperTestParameter.keyStoreWrapper.getPrivateKey(KEYSTORE_VALID_KEY_ALIAS), |
|
|
|
|
|
|
|
"Private key is not null"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getPrivateKey_WithInvalidAlias_ReturnsExpectedValue() { |
|
|
|
@MethodSource("data") |
|
|
|
assertThat(keyStoreWrapper.getPrivateKey(KEYSTORE_INVALID_KEY_ALIAS)) |
|
|
|
public void getPrivateKey_WithInvalidAlias_ReturnsExpectedValue( |
|
|
|
.as("Private key is null") |
|
|
|
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
.isNull(); |
|
|
|
assertNull( |
|
|
|
|
|
|
|
keyStoreWrapperTestParameter.keyStoreWrapper.getPrivateKey(KEYSTORE_INVALID_KEY_ALIAS), |
|
|
|
|
|
|
|
"Private key is null"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getCertificate_WithValidAlias_ReturnsExpectedValue() { |
|
|
|
@MethodSource("data") |
|
|
|
assertThat(keyStoreWrapper.getCertificate(KEYSTORE_VALID_KEY_ALIAS)) |
|
|
|
public void getCertificate_WithValidAlias_ReturnsExpectedValue( |
|
|
|
.as("Certificate is not null") |
|
|
|
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
.isNotNull(); |
|
|
|
assertNotNull( |
|
|
|
|
|
|
|
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificate(KEYSTORE_VALID_KEY_ALIAS), |
|
|
|
|
|
|
|
"Certificate is not null"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getCertificate_WithInvalidAlias_ReturnsExpectedValue() { |
|
|
|
@MethodSource("data") |
|
|
|
assertThat(keyStoreWrapper.getCertificate(KEYSTORE_INVALID_KEY_ALIAS)) |
|
|
|
public void getCertificate_WithInvalidAlias_ReturnsExpectedValue( |
|
|
|
.as("Certificate is null") |
|
|
|
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
.isNull(); |
|
|
|
assertNull( |
|
|
|
|
|
|
|
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificate(KEYSTORE_INVALID_KEY_ALIAS), |
|
|
|
|
|
|
|
"Certificate is null"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getCertificateChain_WithValidAlias_ReturnsExpectedValue() { |
|
|
|
@MethodSource("data") |
|
|
|
assertThat(keyStoreWrapper.getCertificateChain(KEYSTORE_VALID_KEY_ALIAS)) |
|
|
|
public void getCertificateChain_WithValidAlias_ReturnsExpectedValue( |
|
|
|
.as("Certificate chain is not null") |
|
|
|
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
.isNotNull(); |
|
|
|
assertNotNull( |
|
|
|
|
|
|
|
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificateChain(KEYSTORE_VALID_KEY_ALIAS), |
|
|
|
|
|
|
|
"Certificate chain is not null"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getCertificateChain_WithInvalidAlias_ReturnsExpectedValue() { |
|
|
|
@MethodSource("data") |
|
|
|
assertThat(keyStoreWrapper.getCertificateChain(KEYSTORE_INVALID_KEY_ALIAS)) |
|
|
|
public void getCertificateChain_WithInvalidAlias_ReturnsExpectedValue( |
|
|
|
.as("Certificate is null") |
|
|
|
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
.isNull(); |
|
|
|
assertNull( |
|
|
|
|
|
|
|
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificateChain( |
|
|
|
|
|
|
|
KEYSTORE_INVALID_KEY_ALIAS), |
|
|
|
|
|
|
|
"Certificate is null"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getCertificate_FromTruststore_WithValidAlias_ReturnsExpectedValue() { |
|
|
|
@MethodSource("data") |
|
|
|
|
|
|
|
public void getCertificate_FromTruststore_WithValidAlias_ReturnsExpectedValue( |
|
|
|
|
|
|
|
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
final Certificate certificate = |
|
|
|
final Certificate certificate = |
|
|
|
keyStoreWrapper.getCertificate(TRUSTSTORE_VALID_CERTIFICATE_ALIAS); |
|
|
|
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificate( |
|
|
|
if (keystoreWrapperConfiguredWithTruststore) { |
|
|
|
TRUSTSTORE_VALID_CERTIFICATE_ALIAS); |
|
|
|
assertThat(certificate).as("Certificate is not null").isNotNull(); |
|
|
|
if (keyStoreWrapperTestParameter.keystoreWrapperConfiguredWithTruststore) { |
|
|
|
|
|
|
|
assertNotNull(certificate, "Certificate is not null"); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
assertThat(certificate).as("Certificate is null").isNull(); |
|
|
|
assertNull(certificate, "Certificate is null"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getCertificate_FromTruststore_WithInvalidAlias_ReturnsExpectedValue() { |
|
|
|
@MethodSource("data") |
|
|
|
assertThat(keyStoreWrapper.getPrivateKey(TRUSTSTORE_INVALID_CERTIFICATE_ALIAS)) |
|
|
|
public void getCertificate_FromTruststore_WithInvalidAlias_ReturnsExpectedValue( |
|
|
|
.as("Certificate is null") |
|
|
|
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
.isNull(); |
|
|
|
assertNull( |
|
|
|
|
|
|
|
keyStoreWrapperTestParameter.keyStoreWrapper.getPrivateKey( |
|
|
|
|
|
|
|
TRUSTSTORE_INVALID_CERTIFICATE_ALIAS), |
|
|
|
|
|
|
|
"Certificate is null"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ParameterizedTest |
|
|
|
public void getCRLS_Check() { |
|
|
|
@MethodSource("data") |
|
|
|
assertThat(keyStoreWrapper.getCRLs()).as("CRLs is not null").isNotNull(); |
|
|
|
public void getCRLS_Check(final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) { |
|
|
|
assertThat(keyStoreWrapper.getCRLs().size()).as("CRLs size matches").isEqualTo(2); |
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|