Pki - migrate to junit 5 (#6235)

* migrate to junit5

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

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

Signed-off-by: Franklin Delehelle <franklin.delehelle@odena.eu>

* migrate to junit5 (#6234)

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

* fixes for problems discovered in main (#6248)

Signed-off-by: garyschulte <garyschulte@gmail.com>

* fixed test comparing size of collection

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

---------

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Signed-off-by: Franklin Delehelle <franklin.delehelle@odena.eu>
Signed-off-by: garyschulte <garyschulte@gmail.com>
Co-authored-by: delehef <franklin.delehelle@odena.eu>
Co-authored-by: garyschulte <garyschulte@gmail.com>
pull/6253/head
Sally MacFarlane 12 months ago committed by GitHub
parent 017f9aa224
commit 4cc2eed4e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      pki/build.gradle
  2. 26
      pki/src/test/java/org/hyperledger/besu/pki/cms/CmsCreationAndValidationTest.java
  3. 166
      pki/src/test/java/org/hyperledger/besu/pki/keystore/BaseKeyStoreFileWrapperTest.java
  4. 48
      pki/src/test/java/org/hyperledger/besu/pki/keystore/HardwareKeyStoreFileWrapperTest.java
  5. 23
      pki/src/test/java/org/hyperledger/besu/pki/keystore/HardwareKeyStoreWrapperTest.java
  6. 22
      pki/src/test/java/org/hyperledger/besu/pki/keystore/SoftwareKeyStoreFileWrapperTest.java
  7. 39
      pki/src/test/java/org/hyperledger/besu/pki/keystore/SoftwareKeyStoreWrapperTest.java

@ -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'
}

@ -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;
@ -56,8 +57,7 @@ public class CmsCreationAndValidationTest {
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
@ -69,7 +69,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isTrue();
assertTrue(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}
@ParameterizedTest
@ -81,7 +81,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}
@ParameterizedTest
@ -93,7 +93,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isTrue();
assertTrue(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}
@ParameterizedTest
@ -105,7 +105,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}
@ParameterizedTest
@ -117,7 +117,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}
@ParameterizedTest
@ -129,7 +129,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
}
@ParameterizedTest
@ -144,7 +144,7 @@ public class CmsCreationAndValidationTest {
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
@ -156,8 +156,7 @@ public class CmsCreationAndValidationTest {
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
@ -198,7 +197,6 @@ public class CmsCreationAndValidationTest {
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));
}
}

@ -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;
}
}
}

@ -14,7 +14,8 @@
*/
package org.hyperledger.besu.pki.keystore;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.hyperledger.besu.pki.PkiException;
@ -26,11 +27,9 @@ import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.OS;
import org.junit.runners.Parameterized;
public class HardwareKeyStoreFileWrapperTest extends BaseKeyStoreFileWrapperTest {
@ -39,16 +38,12 @@ public class HardwareKeyStoreFileWrapperTest extends BaseKeyStoreFileWrapperTest
private static final String configName = "NSScrypto-partner1client1";
private static final String validKeystorePassword = "test123";
@Parameterized.Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() {
public static Collection<KeyStoreWrapperTestParameter> data() {
return Arrays.asList(
new Object[][] {
{
new KeyStoreWrapperTestParameter(
"HardwareKeyStoreWrapper[PKCS11 keystore/truststore]",
true,
CryptoTestUtil.isNSSLibInstalled() ? getHardwareKeyStoreWrapper(configName) : null
}
});
CryptoTestUtil.isNSSLibInstalled() ? getHardwareKeyStoreWrapper(configName) : null));
}
private static KeyStoreWrapper getHardwareKeyStoreWrapper(final String cfgName) {
@ -66,26 +61,26 @@ public class HardwareKeyStoreFileWrapperTest extends BaseKeyStoreFileWrapperTest
if (OS.MAC.isCurrentOs()) {
// nss3 is difficult to setup on mac correctly, don't let it break unit tests for dev
// machines.
Assume.assumeNoException("Failed to initialize hardware keystore", e);
System.out.println("Failed to initialize hardware keystore " + e.getLocalizedMessage());
}
// Not a mac, probably a production build. Full failure.
throw new PkiException("Failed to initialize hardware keystore", e);
}
}
@Before
@BeforeEach
public void beforeMethod() {
Assume.assumeTrue(
"Test ignored due to NSS library not being installed/detected.",
CryptoTestUtil.isNSSLibInstalled());
assumeTrue(
CryptoTestUtil.isNSSLibInstalled(),
"Test ignored due to NSS library not being installed/detected.");
}
@Test
public void getPkcs11Provider() throws Exception {
final HardwareKeyStoreWrapper sut =
(HardwareKeyStoreWrapper) getHardwareKeyStoreWrapper(configName);
assertThatThrownBy(() -> sut.getPkcs11ProviderForConfig("no-library"))
.isInstanceOf(IllegalArgumentException.class);
assertThrows(
IllegalArgumentException.class, () -> sut.getPkcs11ProviderForConfig("no-library"));
}
@Test
@ -96,21 +91,22 @@ public class HardwareKeyStoreFileWrapperTest extends BaseKeyStoreFileWrapperTest
@Test
public void init_keystorePassword_config_invalid() throws Exception {
final String config = "invalid";
assertThatThrownBy(
() -> new HardwareKeyStoreWrapper(validKeystorePassword, toPath(config), toPath(crl)))
.isInstanceOf(NullPointerException.class);
assertThrows(
NullPointerException.class,
() -> new HardwareKeyStoreWrapper(validKeystorePassword, toPath(config), toPath(crl)));
}
@Test
public void init_keystorePassword_config_missing_pw() throws Exception {
assertThatThrownBy(() -> new HardwareKeyStoreWrapper(null, toPath(config), toPath(crl)))
.isInstanceOf(PkiException.class);
assertThrows(
PkiException.class, () -> new HardwareKeyStoreWrapper(null, toPath(config), toPath(crl)));
}
@Test
public void init_keystorePassword_provider_missing_pw() throws Exception {
final Provider p = null;
assertThatThrownBy(() -> new HardwareKeyStoreWrapper(validKeystorePassword, p, toPath(crl)))
.isInstanceOf(PkiException.class);
assertThrows(
PkiException.class,
() -> new HardwareKeyStoreWrapper(validKeystorePassword, p, toPath(crl)));
}
}

@ -15,7 +15,8 @@
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.mockito.Mockito.when;
import java.security.KeyStore;
@ -23,13 +24,13 @@ import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class HardwareKeyStoreWrapperTest {
private static final String KEY_ALIAS = "keyalias";
@ -43,7 +44,7 @@ public class HardwareKeyStoreWrapperTest {
private HardwareKeyStoreWrapper keyStoreWrapper;
@Before
@BeforeEach
public void before() {
keyStoreWrapper = new HardwareKeyStoreWrapper(null, keyStore, new String(PASSWORD));
}
@ -52,7 +53,7 @@ public class HardwareKeyStoreWrapperTest {
public void getPrivateKey() throws Exception {
when(keyStore.getKey(KEY_ALIAS, PASSWORD)).thenReturn(privateKey);
assertThat(keyStoreWrapper.getPrivateKey(KEY_ALIAS)).isNotNull();
assertNotNull(keyStoreWrapper.getPrivateKey(KEY_ALIAS));
}
@Test
@ -61,14 +62,14 @@ public class HardwareKeyStoreWrapperTest {
when(keyStore.getCertificate(KEY_ALIAS)).thenReturn(certificate);
when(certificate.getPublicKey()).thenReturn(publicKey);
assertThat(keyStoreWrapper.getPublicKey(KEY_ALIAS)).isNotNull();
assertNotNull(keyStoreWrapper.getPublicKey(KEY_ALIAS));
}
@Test
public void getCertificate() throws Exception {
when(keyStore.getCertificate(CERTIFICATE_ALIAS)).thenReturn(certificate);
assertThat(keyStoreWrapper.getCertificate(CERTIFICATE_ALIAS)).isNotNull();
assertNotNull(keyStoreWrapper.getCertificate(CERTIFICATE_ALIAS));
}
@Test
@ -76,6 +77,6 @@ public class HardwareKeyStoreWrapperTest {
when(keyStore.getCertificateChain(CERTIFICATE_ALIAS))
.thenReturn(new Certificate[] {certificate});
assertThat(keyStoreWrapper.getCertificateChain(CERTIFICATE_ALIAS)).hasSize(1);
assertEquals(keyStoreWrapper.getCertificateChain(CERTIFICATE_ALIAS).length, 1);
}
}

@ -20,8 +20,6 @@ import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import org.junit.runners.Parameterized;
public class SoftwareKeyStoreFileWrapperTest extends BaseKeyStoreFileWrapperTest {
private static final String p12KeyStore = "/keystore/partner1client1/keys.p12";
@ -30,26 +28,20 @@ public class SoftwareKeyStoreFileWrapperTest extends BaseKeyStoreFileWrapperTest
private static final String crl = "/keystore/partner1client1/crl.pem";
private static final String validKeystorePassword = "test123";
@Parameterized.Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() {
public static Collection<KeyStoreWrapperTestParameter> data() {
return Arrays.asList(
new Object[][] {
{
new KeyStoreWrapperTestParameter(
"SoftwareKeyStoreWrapper[PKCS12 keystore only]",
false,
getPKCS12SoftwareKeyStoreWrapper()
},
{
getPKCS12SoftwareKeyStoreWrapper()),
new KeyStoreWrapperTestParameter(
"SoftwareKeyStoreWrapper[JKS keystore only]",
false,
getJKSSoftwareKeyStoreWrapper(false)
},
{
getJKSSoftwareKeyStoreWrapper(false)),
new KeyStoreWrapperTestParameter(
"SoftwareKeyStoreWrapper[JKS keystore/truststore]",
true,
getJKSSoftwareKeyStoreWrapper(true)
}
});
getJKSSoftwareKeyStoreWrapper(true)));
}
private static KeyStoreWrapper getPKCS12SoftwareKeyStoreWrapper() {

@ -15,8 +15,9 @@
package org.hyperledger.besu.pki.keystore;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.pki.keystore.KeyStoreWrapper.KEYSTORE_TYPE_PKCS12;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@ -28,13 +29,13 @@ import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class SoftwareKeyStoreWrapperTest {
private static final String KEY_ALIAS = "keyalias";
@ -49,7 +50,7 @@ public class SoftwareKeyStoreWrapperTest {
@Mock private PublicKey publicKey;
@Mock private Certificate certificate;
@Before
@BeforeEach
public void before() {
keyStoreWrapper = new SoftwareKeyStoreWrapper(keyStore, new String(PASSWORD), null, "");
}
@ -59,7 +60,7 @@ public class SoftwareKeyStoreWrapperTest {
when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(true);
when(keyStore.getKey(KEY_ALIAS, PASSWORD)).thenReturn(privateKey);
assertThat(keyStoreWrapper.getPrivateKey(KEY_ALIAS)).isNotNull();
assertNotNull(keyStoreWrapper.getPrivateKey(KEY_ALIAS));
}
@Test
@ -83,7 +84,7 @@ public class SoftwareKeyStoreWrapperTest {
when(trustStore.containsAlias(KEY_ALIAS)).thenReturn(true);
when(trustStore.getKey(KEY_ALIAS, PASSWORD)).thenReturn(privateKey);
assertThat(keyStoreWrapper.getPrivateKey(KEY_ALIAS)).isNotNull();
assertNotNull(keyStoreWrapper.getPrivateKey(KEY_ALIAS));
verify(trustStore).getKey(eq(KEY_ALIAS), eq(PASSWORD));
}
@ -93,7 +94,7 @@ public class SoftwareKeyStoreWrapperTest {
when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(true);
when(keyStore.getKey(KEY_ALIAS, PASSWORD)).thenReturn(publicKey);
assertThat(keyStoreWrapper.getPublicKey(KEY_ALIAS)).isNotNull();
assertNotNull(keyStoreWrapper.getPublicKey(KEY_ALIAS));
}
@Test
@ -117,7 +118,7 @@ public class SoftwareKeyStoreWrapperTest {
when(trustStore.containsAlias(KEY_ALIAS)).thenReturn(true);
when(trustStore.getKey(KEY_ALIAS, PASSWORD)).thenReturn(publicKey);
assertThat(keyStoreWrapper.getPublicKey(KEY_ALIAS)).isNotNull();
assertNotNull(keyStoreWrapper.getPublicKey(KEY_ALIAS));
verify(trustStore).getKey(eq(KEY_ALIAS), eq(PASSWORD));
}
@ -126,7 +127,7 @@ public class SoftwareKeyStoreWrapperTest {
public void getCertificate() throws Exception {
when(keyStore.getCertificate(CERTIFICATE_ALIAS)).thenReturn(certificate);
assertThat(keyStoreWrapper.getCertificate(CERTIFICATE_ALIAS)).isNotNull();
assertNotNull(keyStoreWrapper.getCertificate(CERTIFICATE_ALIAS));
}
@Test
@ -148,7 +149,7 @@ public class SoftwareKeyStoreWrapperTest {
when(keyStore.getCertificate(CERTIFICATE_ALIAS)).thenReturn(null);
when(trustStore.getCertificate(CERTIFICATE_ALIAS)).thenReturn(certificate);
assertThat(keyStoreWrapper.getCertificate(CERTIFICATE_ALIAS)).isNotNull();
assertNotNull(keyStoreWrapper.getCertificate(CERTIFICATE_ALIAS));
verify(trustStore).getCertificate(eq(CERTIFICATE_ALIAS));
}
@ -158,7 +159,7 @@ public class SoftwareKeyStoreWrapperTest {
when(keyStore.getCertificateChain(CERTIFICATE_ALIAS))
.thenReturn(new Certificate[] {certificate});
assertThat(keyStoreWrapper.getCertificateChain(CERTIFICATE_ALIAS)).hasSize(1);
assertEquals(keyStoreWrapper.getCertificateChain(CERTIFICATE_ALIAS).length, 1);
}
@Test
@ -171,7 +172,7 @@ public class SoftwareKeyStoreWrapperTest {
when(trustStore.getCertificateChain(CERTIFICATE_ALIAS))
.thenReturn(new Certificate[] {certificate});
assertThat(keyStoreWrapper.getCertificateChain(CERTIFICATE_ALIAS)).hasSize(1);
assertEquals(keyStoreWrapper.getCertificateChain(CERTIFICATE_ALIAS).length, 1);
verify(trustStore).getCertificateChain(eq(CERTIFICATE_ALIAS));
}
@ -188,10 +189,10 @@ public class SoftwareKeyStoreWrapperTest {
"validator",
null);
assertThat(loadedKeyStore.getPublicKey("validator")).isNotNull();
assertThat(loadedKeyStore.getPrivateKey("validator")).isNotNull();
assertThat(loadedKeyStore.getCertificate("validator")).isNotNull();
assertNotNull(loadedKeyStore.getPublicKey("validator"));
assertNotNull(loadedKeyStore.getPrivateKey("validator"));
assertNotNull(loadedKeyStore.getCertificate("validator"));
// CA -> INTERCA -> PARTNERACA -> VALIDATOR
assertThat(loadedKeyStore.getCertificateChain("validator")).hasSize(4);
assertEquals(loadedKeyStore.getCertificateChain("validator").length, 4);
}
}

Loading…
Cancel
Save