Use Assertj instead of Hamcrest

This commit is contained in:
Richard Körber
2022-05-14 12:39:27 +02:00
parent f3c7e8a46c
commit a25b8c1b8d
53 changed files with 1382 additions and 1561 deletions

View File

@@ -13,12 +13,16 @@
*/
package org.shredzone.acme4j.smime;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.stream.Stream;
import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests of {@link EmailIdentifier}.
@@ -27,25 +31,27 @@ public class EmailIdentifierTest {
@Test
public void testConstants() {
assertThat(EmailIdentifier.TYPE_EMAIL, is("email"));
assertThat(EmailIdentifier.TYPE_EMAIL).isEqualTo("email");
}
@Test
public void testEmail() throws AddressException {
EmailIdentifier id1 = EmailIdentifier.email("email@example.com");
assertThat(id1.getType(), is(EmailIdentifier.TYPE_EMAIL));
assertThat(id1.getValue(), is("email@example.com"));
assertThat(id1.getEmailAddress().getAddress(), is("email@example.com"));
@ParameterizedTest
@MethodSource("provideTestEmails")
public void testEmail(Object input, String expected) {
EmailIdentifier id = input instanceof InternetAddress
? EmailIdentifier.email((InternetAddress) input)
: EmailIdentifier.email(input.toString());
EmailIdentifier id2 = EmailIdentifier.email(new InternetAddress("email@example.com"));
assertThat(id2.getType(), is(EmailIdentifier.TYPE_EMAIL));
assertThat(id2.getValue(), is("email@example.com"));
assertThat(id2.getEmailAddress().getAddress(), is("email@example.com"));
assertThat(id.getType()).isEqualTo(EmailIdentifier.TYPE_EMAIL);
assertThat(id.getValue()).isEqualTo(expected);
assertThat(id.getEmailAddress().getAddress()).isEqualTo(expected);
}
EmailIdentifier id3 = EmailIdentifier.email(new InternetAddress("Example Corp <info@example.com>"));
assertThat(id3.getType(), is(EmailIdentifier.TYPE_EMAIL));
assertThat(id3.getValue(), is("info@example.com"));
assertThat(id3.getEmailAddress().getAddress(), is("info@example.com"));
public static Stream<Arguments> provideTestEmails() throws AddressException {
return Stream.of(
Arguments.of("email@example.com", "email@example.com"),
Arguments.of(new InternetAddress("email@example.com"), "email@example.com"),
Arguments.of(new InternetAddress("Example Corp <info@example.com>"), "info@example.com")
);
}
}

View File

@@ -13,8 +13,7 @@
*/
package org.shredzone.acme4j.smime.challenge;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.net.URI;
@@ -40,8 +39,8 @@ public class EmailReply00ChallengeTest extends SMIMETests {
AcmeProvider provider = new TestAcmeProvider();
Challenge challenge = provider.createChallenge(mockLogin(), getJSON("emailReplyChallenge"));
assertThat(challenge, not(nullValue()));
assertThat(challenge, instanceOf(EmailReply00Challenge.class));
assertThat(challenge).isNotNull();
assertThat(challenge).isInstanceOf(EmailReply00Challenge.class);
}
/**
@@ -51,14 +50,14 @@ public class EmailReply00ChallengeTest extends SMIMETests {
public void testEmailReplyChallenge() {
EmailReply00Challenge challenge = new EmailReply00Challenge(mockLogin(), getJSON("emailReplyChallenge"));
assertThat(challenge.getType(), is(EmailReply00Challenge.TYPE));
assertThat(challenge.getStatus(), is(Status.PENDING));
assertThat(challenge.getToken(TOKEN_PART1), is(TOKEN_PART1 + TOKEN_PART2));
assertThat(challenge.getTokenPart2(), is(TOKEN_PART2));
assertThat(challenge.getAuthorization(TOKEN_PART1), is(KEY_AUTHORIZATION));
assertThat(challenge.getType()).isEqualTo(EmailReply00Challenge.TYPE);
assertThat(challenge.getStatus()).isEqualTo(Status.PENDING);
assertThat(challenge.getToken(TOKEN_PART1)).isEqualTo(TOKEN_PART1 + TOKEN_PART2);
assertThat(challenge.getTokenPart2()).isEqualTo(TOKEN_PART2);
assertThat(challenge.getAuthorization(TOKEN_PART1)).isEqualTo(KEY_AUTHORIZATION);
assertThat(challenge.getFrom(), is("acme-generator@example.org"));
assertThat(challenge.getExpectedSender().getAddress(), is("acme-generator@example.org"));
assertThat(challenge.getFrom()).isEqualTo("acme-generator@example.org");
assertThat(challenge.getExpectedSender().getAddress()).isEqualTo("acme-generator@example.org");
}
/**

View File

@@ -14,8 +14,7 @@
package org.shredzone.acme4j.smime.csr;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayOutputStream;
@@ -28,12 +27,12 @@ import java.util.Arrays;
import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress;
import org.assertj.core.api.AutoCloseableSoftAssertions;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DERIA5String;
import org.bouncycastle.asn1.pkcs.Attribute;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.x500.RDN;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x509.Extension;
@@ -44,9 +43,6 @@ import org.bouncycastle.asn1.x509.KeyUsage;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.shredzone.acme4j.util.KeyPairUtils;
@@ -88,18 +84,18 @@ public class SMIMECSRBuilderTest {
builder.setOrganizationalUnit("Testunit");
builder.setState("ABC");
assertThat(builder.toString(), is("CN=mail@example.com,C=XX,L=Testville,"
assertThat(builder.toString()).isEqualTo("CN=mail@example.com,C=XX,L=Testville,"
+ "O=Testing Co,OU=Testunit,ST=ABC,"
+ "EMAIL=mail@example.com,EMAIL=info@example.com,"
+ "EMAIL=sales@example.com,EMAIL=shop@example.com,"
+ "EMAIL=support@example.com,EMAIL=help@example.com,"
+ "TYPE=SIGNING_AND_ENCRYPTION"));
+ "TYPE=SIGNING_AND_ENCRYPTION");
builder.sign(testKey);
PKCS10CertificationRequest csr = builder.getCSR();
assertThat(csr, is(Matchers.notNullValue()));
assertThat(csr.getEncoded(), is(Matchers.equalTo(builder.getEncoded())));
assertThat(csr).isNotNull();
assertThat(csr.getEncoded()).isEqualTo(builder.getEncoded());
smimeCsrTest(csr);
keyUsageTest(csr, KeyUsage.digitalSignature | KeyUsage.keyEncipherment);
@@ -116,7 +112,7 @@ public class SMIMECSRBuilderTest {
builder.setKeyUsageType(KeyUsageType.ENCRYPTION_ONLY);
builder.sign(testKey);
PKCS10CertificationRequest csr = builder.getCSR();
assertThat(csr, is(Matchers.notNullValue()));
assertThat(csr).isNotNull();
keyUsageTest(csr, KeyUsage.keyEncipherment);
}
@@ -130,7 +126,7 @@ public class SMIMECSRBuilderTest {
builder.setKeyUsageType(KeyUsageType.SIGNING_ONLY);
builder.sign(testKey);
PKCS10CertificationRequest csr = builder.getCSR();
assertThat(csr, is(Matchers.notNullValue()));
assertThat(csr).isNotNull();
keyUsageTest(csr, KeyUsage.digitalSignature);
}
@@ -144,7 +140,7 @@ public class SMIMECSRBuilderTest {
builder.setKeyUsageType(KeyUsageType.SIGNING_AND_ENCRYPTION);
builder.sign(testKey);
PKCS10CertificationRequest csr = builder.getCSR();
assertThat(csr, is(Matchers.notNullValue()));
assertThat(csr).isNotNull();
keyUsageTest(csr, KeyUsage.digitalSignature | KeyUsage.keyEncipherment);
}
@@ -157,25 +153,41 @@ public class SMIMECSRBuilderTest {
*/
private void smimeCsrTest(PKCS10CertificationRequest csr) {
X500Name name = csr.getSubject();
assertThat(name.getRDNs(BCStyle.CN), Matchers.arrayContaining(new RDNMatcher("mail@example.com")));
assertThat(name.getRDNs(BCStyle.C), Matchers.arrayContaining(new RDNMatcher("XX")));
assertThat(name.getRDNs(BCStyle.L), Matchers.arrayContaining(new RDNMatcher("Testville")));
assertThat(name.getRDNs(BCStyle.O), Matchers.arrayContaining(new RDNMatcher("Testing Co")));
assertThat(name.getRDNs(BCStyle.OU), Matchers.arrayContaining(new RDNMatcher("Testunit")));
assertThat(name.getRDNs(BCStyle.ST), Matchers.arrayContaining(new RDNMatcher("ABC")));
try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
softly.assertThat(name.getRDNs(BCStyle.CN)).as("CN")
.extracting(rdn -> rdn.getFirst().getValue().toString())
.contains("mail@example.com");
softly.assertThat(name.getRDNs(BCStyle.C)).as("C")
.extracting(rdn -> rdn.getFirst().getValue().toString())
.contains("XX");
softly.assertThat(name.getRDNs(BCStyle.L)).as("L")
.extracting(rdn -> rdn.getFirst().getValue().toString())
.contains("Testville");
softly.assertThat(name.getRDNs(BCStyle.O)).as("O")
.extracting(rdn -> rdn.getFirst().getValue().toString())
.contains("Testing Co");
softly.assertThat(name.getRDNs(BCStyle.OU)).as("OU")
.extracting(rdn -> rdn.getFirst().getValue().toString())
.contains("Testunit");
softly.assertThat(name.getRDNs(BCStyle.ST)).as("ST")
.extracting(rdn -> rdn.getFirst().getValue().toString())
.contains("ABC");
}
Attribute[] attr = csr.getAttributes(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest);
assertThat(attr.length, is(1));
assertThat(attr).hasSize(1);
ASN1Encodable[] extensions = attr[0].getAttrValues().toArray();
assertThat(extensions.length, is(1));
assertThat(extensions).hasSize(1);
GeneralNames names = GeneralNames.fromExtensions((Extensions) extensions[0], Extension.subjectAlternativeName);
assertThat(names.getNames(), Matchers.arrayContaining(
new GeneralNameMatcher("mail@example.com", GeneralName.rfc822Name),
new GeneralNameMatcher("info@example.com", GeneralName.rfc822Name),
new GeneralNameMatcher("sales@example.com", GeneralName.rfc822Name),
new GeneralNameMatcher("shop@example.com", GeneralName.rfc822Name),
new GeneralNameMatcher("support@example.com", GeneralName.rfc822Name),
new GeneralNameMatcher("help@example.com", GeneralName.rfc822Name)));
assertThat(names.getNames())
.filteredOn(gn -> gn.getTagNo() == GeneralName.rfc822Name)
.extracting(gn -> DERIA5String.getInstance(gn.getName()).getString())
.containsExactlyInAnyOrder("mail@example.com", "info@example.com",
"sales@example.com", "shop@example.com", "support@example.com",
"help@example.com");
}
/**
@@ -189,14 +201,14 @@ public class SMIMECSRBuilderTest {
*/
private void keyUsageTest(PKCS10CertificationRequest csr, Integer expectedUsageBits) {
Attribute[] attr = csr.getAttributes(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest);
assertThat(attr.length, is(1));
assertThat(attr).hasSize(1);
ASN1Encodable[] extensions = attr[0].getAttrValues().toArray();
assertThat(extensions.length, is(1));
assertThat(extensions).hasSize(1);
DERBitString keyUsageBits = (DERBitString) ((Extensions) extensions[0]).getExtensionParsedValue(Extension.keyUsage);
if (expectedUsageBits != null) {
assertThat(keyUsageBits.intValue(), is(expectedUsageBits));
assertThat(keyUsageBits.intValue()).isEqualTo(expectedUsageBits);
} else {
assertThat(keyUsageBits, is(Matchers.nullValue()));
assertThat(keyUsageBits).isNull();
}
}
@@ -213,10 +225,10 @@ public class SMIMECSRBuilderTest {
}
// Make sure PEM file is properly formatted
assertThat(pem, Matchers.matchesPattern(
assertThat(pem).matches(
"-----BEGIN CERTIFICATE REQUEST-----[\\r\\n]+"
+ "([a-zA-Z0-9/+=]+[\\r\\n]+)+"
+ "-----END CERTIFICATE REQUEST-----[\\r\\n]*"));
+ "-----END CERTIFICATE REQUEST-----[\\r\\n]*");
// Read CSR from PEM
PKCS10CertificationRequest readCsr;
@@ -225,8 +237,8 @@ public class SMIMECSRBuilderTest {
}
// Verify that both keypairs are the same
assertThat(builder.getCSR(), Matchers.not(Matchers.sameInstance(readCsr)));
assertThat(builder.getEncoded(), is(Matchers.equalTo(readCsr.getEncoded())));
assertThat(builder.getCSR()).isNotSameAs(readCsr);
assertThat(builder.getEncoded()).isEqualTo(readCsr.getEncoded());
// OutputStream is identical?
byte[] pemBytes;
@@ -234,7 +246,7 @@ public class SMIMECSRBuilderTest {
builder.write(baos);
pemBytes = baos.toByteArray();
}
assertThat(new String(pemBytes, UTF_8), is(Matchers.equalTo(pem)));
assertThat(new String(pemBytes, UTF_8)).isEqualTo(pem);
}
/**
@@ -264,89 +276,4 @@ public class SMIMECSRBuilderTest {
},"write");
}
/**
* Matches {@link RDN} values.
*/
private static class RDNMatcher extends BaseMatcher<RDN> {
private final String expectedValue;
public RDNMatcher(String expectedValue) {
this.expectedValue = expectedValue;
}
@Override
public boolean matches(Object item) {
if (!(item instanceof RDN)) {
return false;
}
return expectedValue.equals(((RDN) item).getFirst().getValue().toString());
}
@Override
public void describeTo(Description description) {
description.appendValue(expectedValue);
}
@Override
public void describeMismatch(Object item, Description description) {
if (!(item instanceof RDN)) {
description.appendText("is a ").appendValue(item.getClass());
} else {
description.appendText("was ").appendValue(((RDN) item).getFirst().getValue());
}
}
}
/**
* Matches {@link GeneralName} DNS tagged values.
*/
private static class GeneralNameMatcher extends BaseMatcher<GeneralName> {
private final String expectedValue;
private final int expectedTag;
public GeneralNameMatcher(String expectedValue, int expectedTag) {
this.expectedTag = expectedTag;
this.expectedValue = expectedValue;
}
@Override
public boolean matches(Object item) {
if (!(item instanceof GeneralName)) {
return false;
}
GeneralName gn = (GeneralName) item;
if (gn.getTagNo() != expectedTag) {
return false;
}
if (gn.getTagNo() == GeneralName.rfc822Name) {
return expectedValue.equals(DERIA5String.getInstance(gn.getName()).getString());
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendValue(expectedValue);
}
@Override
public void describeMismatch(Object item, Description description) {
if (!(item instanceof GeneralName)) {
description.appendText("is a ").appendValue(item.getClass());
return;
}
GeneralName gn = (GeneralName) item;
if (gn.getTagNo() == GeneralName.rfc822Name) {
description.appendText("was EMAIL ").appendValue(DERIA5String.getInstance(gn.getName()).getString());
} else {
description.appendText("is not EMAIL, but has tag " + gn.getTagNo());
}
}
}
}

View File

@@ -14,8 +14,7 @@
package org.shredzone.acme4j.smime.email;
import static jakarta.mail.Message.RecipientType.TO;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
@@ -49,11 +48,11 @@ public class EmailProcessorTest extends SMIMETests {
processor.expectedIdentifier(EmailIdentifier.email(expectedTo));
processor.expectedIdentifier(new Identifier("email", expectedTo.getAddress()));
assertThat(processor.getSender(), is(expectedFrom));
assertThat(processor.getRecipient(), is(expectedTo));
assertThat(processor.getMessageId(), is(Optional.of("<A2299BB.FF7788@example.org>")));
assertThat(processor.getToken1(), is(TOKEN_PART1));
assertThat(processor.getReplyTo(), contains(email("acme-validator@example.org")));
assertThat(processor.getSender()).isEqualTo(expectedFrom);
assertThat(processor.getRecipient()).isEqualTo(expectedTo);
assertThat(processor.getMessageId()).isEqualTo(Optional.of("<A2299BB.FF7788@example.org>"));
assertThat(processor.getToken1()).isEqualTo(TOKEN_PART1);
assertThat(processor.getReplyTo()).contains(email("acme-validator@example.org"));
}
@Test
@@ -118,9 +117,9 @@ public class EmailProcessorTest extends SMIMETests {
EmailProcessor processor = new EmailProcessor(message);
processor.withChallenge(challenge);
assertThat(processor.getToken(), is(TOKEN));
assertThat(processor.getAuthorization(), is(KEY_AUTHORIZATION));
assertThat(processor.respond(), is(notNullValue()));
assertThat(processor.getToken()).isEqualTo(TOKEN);
assertThat(processor.getAuthorization()).isEqualTo(KEY_AUTHORIZATION);
assertThat(processor.respond()).isNotNull();
}
@Test
@@ -176,22 +175,22 @@ public class EmailProcessorTest extends SMIMETests {
private void assertResponse(Message response, String expectedBody)
throws MessagingException, IOException {
assertThat(response.getContentType(), is("text/plain"));
assertThat(response.getContent().toString(), is(expectedBody));
assertThat(response.getContentType()).isEqualTo("text/plain");
assertThat(response.getContent().toString()).isEqualTo(expectedBody);
// This is a response, so the expected sender is the recipient of the challenge
assertThat(response.getFrom().length, is(1));
assertThat(response.getFrom()[0], is(expectedTo));
assertThat(response.getFrom()).hasSize(1);
assertThat(response.getFrom()[0]).isEqualTo(expectedTo);
// There is a Reply-To header, so we expect the mail to go only there
assertThat(response.getRecipients(TO).length, is(1));
assertThat(response.getRecipients(TO)[0], is(expectedReplyTo));
assertThat(response.getRecipients(TO)).hasSize(1);
assertThat(response.getRecipients(TO)[0]).isEqualTo(expectedReplyTo);
assertThat(response.getSubject(), is("Re: ACME: " + TOKEN_PART1));
assertThat(response.getSubject()).isEqualTo("Re: ACME: " + TOKEN_PART1);
String[] inReplyToHeader = response.getHeader("In-Reply-To");
assertThat(inReplyToHeader.length, is(1));
assertThat(inReplyToHeader[0], is("<A2299BB.FF7788@example.org>"));
assertThat(inReplyToHeader).hasSize(1);
assertThat(inReplyToHeader[0]).isEqualTo("<A2299BB.FF7788@example.org>");
}
}