Skip to content

Commit 4cc2eed

Browse files
macfarladelehefgaryschulte
authored
Pki - migrate to junit 5 (#6235)
* migrate to junit5 Signed-off-by: Sally MacFarlane <[email protected]> * fix: double calls to trace{Start,End}Transaction (#6247) Signed-off-by: Franklin Delehelle <[email protected]> * migrate to junit5 (#6234) Signed-off-by: Sally MacFarlane <[email protected]> * fixes for problems discovered in main (#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]>
1 parent 017f9aa commit 4cc2eed

7 files changed

+172
-155
lines changed

pki/build.gradle

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ dependencies {
3434
implementation 'io.tmio:tuweni-bytes'
3535
implementation 'org.bouncycastle:bcpkix-jdk18on'
3636

37-
testImplementation 'junit:junit'
38-
testImplementation 'org.assertj:assertj-core'
3937
testImplementation 'org.junit.jupiter:junit-jupiter'
4038
testImplementation 'org.mockito:mockito-core'
39+
testImplementation 'org.mockito:mockito-junit-jupiter'
4140

4241
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
4342
}

pki/src/test/java/org/hyperledger/besu/pki/cms/CmsCreationAndValidationTest.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
package org.hyperledger.besu.pki.cms;
1717

18-
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
1918
import static org.hyperledger.besu.pki.util.TestCertificateUtils.Algorithm.EC;
2019
import static org.hyperledger.besu.pki.util.TestCertificateUtils.Algorithm.RSA;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2122

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

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

59-
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(Bytes.EMPTY, data))
60-
.isFalse();
60+
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(Bytes.EMPTY, data));
6161
}
6262

6363
@ParameterizedTest
@@ -69,7 +69,7 @@ public void cmsValidationWithTrustedSelfSignedCertificate(final Algorithm algori
6969

7070
final Bytes cms = cmsCreator.create(data);
7171

72-
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isTrue();
72+
assertTrue(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
7373
}
7474

7575
@ParameterizedTest
@@ -81,7 +81,7 @@ public void cmsValidationWithUntrustedSelfSignedCertificate(final Algorithm algo
8181

8282
final Bytes cms = cmsCreator.create(data);
8383

84-
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
84+
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
8585
}
8686

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

9494
final Bytes cms = cmsCreator.create(data);
9595

96-
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isTrue();
96+
assertTrue(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
9797
}
9898

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

106106
final Bytes cms = cmsCreator.create(data);
107107

108-
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
108+
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
109109
}
110110

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

118118
final Bytes cms = cmsCreator.create(data);
119119

120-
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
120+
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
121121
}
122122

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

130130
final Bytes cms = cmsCreator.create(data);
131131

132-
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data)).isFalse();
132+
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, data));
133133
}
134134

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

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

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

158158
final Bytes expectedData = Bytes.random(32);
159-
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, expectedData))
160-
.isFalse();
159+
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cms, expectedData));
161160
}
162161

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

201-
assertThat(getCmsTestKeystores(algorithm).getCmsValidator().validate(cmsBytes, expectedData))
202-
.isFalse();
200+
assertFalse(getCmsTestKeystores(algorithm).getCmsValidator().validate(cmsBytes, expectedData));
203201
}
204202
}

pki/src/test/java/org/hyperledger/besu/pki/keystore/BaseKeyStoreFileWrapperTest.java

+98-68
Original file line numberDiff line numberDiff line change
@@ -14,113 +14,143 @@
1414
*/
1515
package org.hyperledger.besu.pki.keystore;
1616

17-
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
1820

1921
import java.nio.file.Path;
2022
import java.security.cert.Certificate;
2123

22-
import org.junit.Test;
23-
import org.junit.runner.RunWith;
24-
import org.junit.runners.Parameterized;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.MethodSource;
2526

26-
@RunWith(Parameterized.class)
2727
public abstract class BaseKeyStoreFileWrapperTest {
2828
protected static final String KEYSTORE_VALID_KEY_ALIAS = "partner1client1";
2929
protected static final String KEYSTORE_INVALID_KEY_ALIAS = "partner1clientinvalid";
3030
protected static final String TRUSTSTORE_VALID_CERTIFICATE_ALIAS = "interca";
3131
protected static final String TRUSTSTORE_INVALID_CERTIFICATE_ALIAS = "interca-invalid";
3232

33-
@Parameterized.Parameter public String keyStoreWrapperDescription;
34-
35-
@Parameterized.Parameter(1)
36-
public boolean keystoreWrapperConfiguredWithTruststore;
37-
38-
@Parameterized.Parameter(2)
39-
public KeyStoreWrapper keyStoreWrapper;
40-
4133
protected static Path toPath(final String path) throws Exception {
4234
return null == path
4335
? null
4436
: Path.of(BaseKeyStoreFileWrapperTest.class.getResource(path).toURI());
4537
}
4638

47-
@Test
48-
public void getPublicKey_WithValidAlias_ReturnsExpectedValue() {
49-
assertThat(keyStoreWrapper.getPublicKey(KEYSTORE_VALID_KEY_ALIAS))
50-
.as("Public key is not null")
51-
.isNotNull();
39+
@ParameterizedTest
40+
@MethodSource("data")
41+
public void getPublicKey_WithValidAlias_ReturnsExpectedValue(
42+
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
43+
assertNotNull(
44+
keyStoreWrapperTestParameter.keyStoreWrapper.getPublicKey(KEYSTORE_VALID_KEY_ALIAS));
5245
}
5346

54-
@Test
55-
public void getPublicKey_WithInvalidAlias_ReturnsExpectedValue() {
56-
assertThat(keyStoreWrapper.getPublicKey(KEYSTORE_INVALID_KEY_ALIAS))
57-
.as("Public key is null")
58-
.isNull();
47+
@ParameterizedTest
48+
@MethodSource("data")
49+
public void getPublicKey_WithInvalidAlias_ReturnsExpectedValue(
50+
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
51+
assertNull(
52+
keyStoreWrapperTestParameter.keyStoreWrapper.getPublicKey(KEYSTORE_INVALID_KEY_ALIAS));
5953
}
6054

61-
@Test
62-
public void getPrivateKey_WithValidAlias_ReturnsExpectedValue() {
63-
assertThat(keyStoreWrapper.getPrivateKey(KEYSTORE_VALID_KEY_ALIAS))
64-
.as("Private key is not null")
65-
.isNotNull();
55+
@ParameterizedTest
56+
@MethodSource("data")
57+
public void getPrivateKey_WithValidAlias_ReturnsExpectedValue(
58+
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
59+
assertNotNull(
60+
keyStoreWrapperTestParameter.keyStoreWrapper.getPrivateKey(KEYSTORE_VALID_KEY_ALIAS),
61+
"Private key is not null");
6662
}
6763

68-
@Test
69-
public void getPrivateKey_WithInvalidAlias_ReturnsExpectedValue() {
70-
assertThat(keyStoreWrapper.getPrivateKey(KEYSTORE_INVALID_KEY_ALIAS))
71-
.as("Private key is null")
72-
.isNull();
64+
@ParameterizedTest
65+
@MethodSource("data")
66+
public void getPrivateKey_WithInvalidAlias_ReturnsExpectedValue(
67+
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
68+
assertNull(
69+
keyStoreWrapperTestParameter.keyStoreWrapper.getPrivateKey(KEYSTORE_INVALID_KEY_ALIAS),
70+
"Private key is null");
7371
}
7472

75-
@Test
76-
public void getCertificate_WithValidAlias_ReturnsExpectedValue() {
77-
assertThat(keyStoreWrapper.getCertificate(KEYSTORE_VALID_KEY_ALIAS))
78-
.as("Certificate is not null")
79-
.isNotNull();
73+
@ParameterizedTest
74+
@MethodSource("data")
75+
public void getCertificate_WithValidAlias_ReturnsExpectedValue(
76+
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
77+
assertNotNull(
78+
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificate(KEYSTORE_VALID_KEY_ALIAS),
79+
"Certificate is not null");
8080
}
8181

82-
@Test
83-
public void getCertificate_WithInvalidAlias_ReturnsExpectedValue() {
84-
assertThat(keyStoreWrapper.getCertificate(KEYSTORE_INVALID_KEY_ALIAS))
85-
.as("Certificate is null")
86-
.isNull();
82+
@ParameterizedTest
83+
@MethodSource("data")
84+
public void getCertificate_WithInvalidAlias_ReturnsExpectedValue(
85+
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
86+
assertNull(
87+
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificate(KEYSTORE_INVALID_KEY_ALIAS),
88+
"Certificate is null");
8789
}
8890

89-
@Test
90-
public void getCertificateChain_WithValidAlias_ReturnsExpectedValue() {
91-
assertThat(keyStoreWrapper.getCertificateChain(KEYSTORE_VALID_KEY_ALIAS))
92-
.as("Certificate chain is not null")
93-
.isNotNull();
91+
@ParameterizedTest
92+
@MethodSource("data")
93+
public void getCertificateChain_WithValidAlias_ReturnsExpectedValue(
94+
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
95+
assertNotNull(
96+
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificateChain(KEYSTORE_VALID_KEY_ALIAS),
97+
"Certificate chain is not null");
9498
}
9599

96-
@Test
97-
public void getCertificateChain_WithInvalidAlias_ReturnsExpectedValue() {
98-
assertThat(keyStoreWrapper.getCertificateChain(KEYSTORE_INVALID_KEY_ALIAS))
99-
.as("Certificate is null")
100-
.isNull();
100+
@ParameterizedTest
101+
@MethodSource("data")
102+
public void getCertificateChain_WithInvalidAlias_ReturnsExpectedValue(
103+
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
104+
assertNull(
105+
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificateChain(
106+
KEYSTORE_INVALID_KEY_ALIAS),
107+
"Certificate is null");
101108
}
102109

103-
@Test
104-
public void getCertificate_FromTruststore_WithValidAlias_ReturnsExpectedValue() {
110+
@ParameterizedTest
111+
@MethodSource("data")
112+
public void getCertificate_FromTruststore_WithValidAlias_ReturnsExpectedValue(
113+
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
105114
final Certificate certificate =
106-
keyStoreWrapper.getCertificate(TRUSTSTORE_VALID_CERTIFICATE_ALIAS);
107-
if (keystoreWrapperConfiguredWithTruststore) {
108-
assertThat(certificate).as("Certificate is not null").isNotNull();
115+
keyStoreWrapperTestParameter.keyStoreWrapper.getCertificate(
116+
TRUSTSTORE_VALID_CERTIFICATE_ALIAS);
117+
if (keyStoreWrapperTestParameter.keystoreWrapperConfiguredWithTruststore) {
118+
assertNotNull(certificate, "Certificate is not null");
109119
} else {
110-
assertThat(certificate).as("Certificate is null").isNull();
120+
assertNull(certificate, "Certificate is null");
111121
}
112122
}
113123

114-
@Test
115-
public void getCertificate_FromTruststore_WithInvalidAlias_ReturnsExpectedValue() {
116-
assertThat(keyStoreWrapper.getPrivateKey(TRUSTSTORE_INVALID_CERTIFICATE_ALIAS))
117-
.as("Certificate is null")
118-
.isNull();
124+
@ParameterizedTest
125+
@MethodSource("data")
126+
public void getCertificate_FromTruststore_WithInvalidAlias_ReturnsExpectedValue(
127+
final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
128+
assertNull(
129+
keyStoreWrapperTestParameter.keyStoreWrapper.getPrivateKey(
130+
TRUSTSTORE_INVALID_CERTIFICATE_ALIAS),
131+
"Certificate is null");
119132
}
120133

121-
@Test
122-
public void getCRLS_Check() {
123-
assertThat(keyStoreWrapper.getCRLs()).as("CRLs is not null").isNotNull();
124-
assertThat(keyStoreWrapper.getCRLs().size()).as("CRLs size matches").isEqualTo(2);
134+
@ParameterizedTest
135+
@MethodSource("data")
136+
public void getCRLS_Check(final KeyStoreWrapperTestParameter keyStoreWrapperTestParameter) {
137+
assertNotNull(keyStoreWrapperTestParameter.keyStoreWrapper.getCRLs(), "CRLs is not null");
138+
assertEquals(
139+
keyStoreWrapperTestParameter.keyStoreWrapper.getCRLs().size(), 2, "CRLs size matches");
140+
}
141+
142+
public static class KeyStoreWrapperTestParameter {
143+
public String keyStoreWrapperDescription;
144+
public boolean keystoreWrapperConfiguredWithTruststore;
145+
public KeyStoreWrapper keyStoreWrapper;
146+
147+
public KeyStoreWrapperTestParameter(
148+
final String keyStoreWrapperDescription,
149+
final boolean keystoreWrapperConfiguredWithTruststore,
150+
final KeyStoreWrapper keyStoreWrapper) {
151+
this.keyStoreWrapperDescription = keyStoreWrapperDescription;
152+
this.keystoreWrapperConfiguredWithTruststore = keystoreWrapperConfiguredWithTruststore;
153+
this.keyStoreWrapper = keyStoreWrapper;
154+
}
125155
}
126156
}

0 commit comments

Comments
 (0)