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 'io.tmio:tuweni-bytes'
implementation 'org.bouncycastle:bcpkix-jdk18on' implementation 'org.bouncycastle:bcpkix-jdk18on'
testImplementation 'junit:junit'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.junit.jupiter:junit-jupiter' testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.mockito:mockito-core' testImplementation 'org.mockito:mockito-core'
testImplementation 'org.mockito:mockito-junit-jupiter'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine' testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
} }

@ -15,9 +15,10 @@
package org.hyperledger.besu.pki.cms; 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.EC;
import static org.hyperledger.besu.pki.util.TestCertificateUtils.Algorithm.RSA; 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; import org.hyperledger.besu.pki.util.TestCertificateUtils.Algorithm;
@ -56,8 +57,7 @@ public class CmsCreationAndValidationTest {
public void cmsValidationWithEmptyCmsMessage(final Algorithm algorithm) { public void cmsValidationWithEmptyCmsMessage(final Algorithm algorithm) {
final Bytes data = Bytes.random(32); final Bytes data = Bytes.random(32);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(Bytes.EMPTY, data)) assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(Bytes.EMPTY, data));
.isFalse();
} }
@ParameterizedTest @ParameterizedTest
@ -69,7 +69,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data); final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isTrue(); assertTrue(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
} }
@ParameterizedTest @ParameterizedTest
@ -81,7 +81,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data); final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse(); assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
} }
@ParameterizedTest @ParameterizedTest
@ -93,7 +93,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data); final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isTrue(); assertTrue(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
} }
@ParameterizedTest @ParameterizedTest
@ -105,7 +105,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data); final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse(); assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
} }
@ParameterizedTest @ParameterizedTest
@ -117,7 +117,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data); final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse(); assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
} }
@ParameterizedTest @ParameterizedTest
@ -129,7 +129,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(data); final Bytes cms = cmsCreator.create(data);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse(); assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
} }
@ParameterizedTest @ParameterizedTest
@ -144,7 +144,7 @@ public class CmsCreationAndValidationTest {
CmsValidator cmsValidator = getCmsTestKeystores(algorithm).getCmsValidatorWithoutCrl(); CmsValidator cmsValidator = getCmsTestKeystores(algorithm).getCmsValidatorWithoutCrl();
// Because we don't have a CRL CertStore, revocation is not checked // Because we don't have a CRL CertStore, revocation is not checked
assertThat(cmsValidator.validate(cms, data)).isTrue(); assertTrue(cmsValidator.validate(cms, data));
} }
@ParameterizedTest @ParameterizedTest
@ -156,8 +156,7 @@ public class CmsCreationAndValidationTest {
final Bytes cms = cmsCreator.create(otherData); final Bytes cms = cmsCreator.create(otherData);
final Bytes expectedData = Bytes.random(32); final Bytes expectedData = Bytes.random(32);
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, expectedData)) assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, expectedData));
.isFalse();
} }
@ParameterizedTest @ParameterizedTest
@ -198,7 +197,6 @@ public class CmsCreationAndValidationTest {
final CMSSignedData cmsSignedData = cmsGenerator.generate(cmsData, true); final CMSSignedData cmsSignedData = cmsGenerator.generate(cmsData, true);
final Bytes cmsBytes = Bytes.wrap(cmsSignedData.getEncoded()); final Bytes cmsBytes = Bytes.wrap(cmsSignedData.getEncoded());
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cmsBytes, expectedData)) assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cmsBytes, expectedData));
.isFalse();
} }
} }

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

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

@ -15,7 +15,8 @@
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.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.security.KeyStore; import java.security.KeyStore;
@ -23,13 +24,13 @@ import java.security.PrivateKey;
import java.security.PublicKey; import java.security.PublicKey;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class HardwareKeyStoreWrapperTest { public class HardwareKeyStoreWrapperTest {
private static final String KEY_ALIAS = "keyalias"; private static final String KEY_ALIAS = "keyalias";
@ -43,7 +44,7 @@ public class HardwareKeyStoreWrapperTest {
private HardwareKeyStoreWrapper keyStoreWrapper; private HardwareKeyStoreWrapper keyStoreWrapper;
@Before @BeforeEach
public void before() { public void before() {
keyStoreWrapper = new HardwareKeyStoreWrapper(null, keyStore, new String(PASSWORD)); keyStoreWrapper = new HardwareKeyStoreWrapper(null, keyStore, new String(PASSWORD));
} }
@ -52,7 +53,7 @@ public class HardwareKeyStoreWrapperTest {
public void getPrivateKey() throws Exception { public void getPrivateKey() throws Exception {
when(keyStore.getKey(KEY_ALIAS, PASSWORD)).thenReturn(privateKey); when(keyStore.getKey(KEY_ALIAS, PASSWORD)).thenReturn(privateKey);
assertThat(keyStoreWrapper.getPrivateKey(KEY_ALIAS)).isNotNull(); assertNotNull(keyStoreWrapper.getPrivateKey(KEY_ALIAS));
} }
@Test @Test
@ -61,14 +62,14 @@ public class HardwareKeyStoreWrapperTest {
when(keyStore.getCertificate(KEY_ALIAS)).thenReturn(certificate); when(keyStore.getCertificate(KEY_ALIAS)).thenReturn(certificate);
when(certificate.getPublicKey()).thenReturn(publicKey); when(certificate.getPublicKey()).thenReturn(publicKey);
assertThat(keyStoreWrapper.getPublicKey(KEY_ALIAS)).isNotNull(); assertNotNull(keyStoreWrapper.getPublicKey(KEY_ALIAS));
} }
@Test @Test
public void getCertificate() throws Exception { public void getCertificate() throws Exception {
when(keyStore.getCertificate(CERTIFICATE_ALIAS)).thenReturn(certificate); when(keyStore.getCertificate(CERTIFICATE_ALIAS)).thenReturn(certificate);
assertThat(keyStoreWrapper.getCertificate(CERTIFICATE_ALIAS)).isNotNull(); assertNotNull(keyStoreWrapper.getCertificate(CERTIFICATE_ALIAS));
} }
@Test @Test
@ -76,6 +77,6 @@ public class HardwareKeyStoreWrapperTest {
when(keyStore.getCertificateChain(CERTIFICATE_ALIAS)) when(keyStore.getCertificateChain(CERTIFICATE_ALIAS))
.thenReturn(new Certificate[] {certificate}); .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.Arrays;
import java.util.Collection; import java.util.Collection;
import org.junit.runners.Parameterized;
public class SoftwareKeyStoreFileWrapperTest extends BaseKeyStoreFileWrapperTest { public class SoftwareKeyStoreFileWrapperTest extends BaseKeyStoreFileWrapperTest {
private static final String p12KeyStore = "/keystore/partner1client1/keys.p12"; 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 crl = "/keystore/partner1client1/crl.pem";
private static final String validKeystorePassword = "test123"; private static final String validKeystorePassword = "test123";
@Parameterized.Parameters(name = "{index}: {0}") public static Collection<KeyStoreWrapperTestParameter> data() {
public static Collection<Object[]> data() {
return Arrays.asList( return Arrays.asList(
new Object[][] { new KeyStoreWrapperTestParameter(
{
"SoftwareKeyStoreWrapper[PKCS12 keystore only]", "SoftwareKeyStoreWrapper[PKCS12 keystore only]",
false, false,
getPKCS12SoftwareKeyStoreWrapper() getPKCS12SoftwareKeyStoreWrapper()),
}, new KeyStoreWrapperTestParameter(
{
"SoftwareKeyStoreWrapper[JKS keystore only]", "SoftwareKeyStoreWrapper[JKS keystore only]",
false, false,
getJKSSoftwareKeyStoreWrapper(false) getJKSSoftwareKeyStoreWrapper(false)),
}, new KeyStoreWrapperTestParameter(
{
"SoftwareKeyStoreWrapper[JKS keystore/truststore]", "SoftwareKeyStoreWrapper[JKS keystore/truststore]",
true, true,
getJKSSoftwareKeyStoreWrapper(true) getJKSSoftwareKeyStoreWrapper(true)));
}
});
} }
private static KeyStoreWrapper getPKCS12SoftwareKeyStoreWrapper() { private static KeyStoreWrapper getPKCS12SoftwareKeyStoreWrapper() {

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

Loading…
Cancel
Save