You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ro...@apache.org on 2019/06/18 08:28:05 UTC
[james-project] 08/14: JAMES-1975 Update dkim to JUnit 5 and new
mailet API
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 5cff6146f9c16ea31ebc9219d8322f44fbfb2430
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Tue May 21 14:07:31 2019 +0200
JAMES-1975 Update dkim to JUnit 5 and new mailet API
---
server/mailet/dkim/pom.xml | 57 ++++++++++++++++++----
.../org/apache/james/jdkim/mailets/DKIMVerify.java | 17 ++++---
.../apache/james/jdkim/mailets/DKIMSignTest.java | 34 ++++++-------
.../apache/james/jdkim/mailets/DKIMVerifyTest.java | 40 ++++++++-------
4 files changed, 98 insertions(+), 50 deletions(-)
diff --git a/server/mailet/dkim/pom.xml b/server/mailet/dkim/pom.xml
index 3dfbbf6..1db8160 100644
--- a/server/mailet/dkim/pom.xml
+++ b/server/mailet/dkim/pom.xml
@@ -56,13 +56,25 @@
<version>${jdkim.version}</version>
<exclusions>
<exclusion>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
+ <artifactId>apache-mailet</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mime4j-core</artifactId>
</exclusion>
<exclusion>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mime4j-dom</artifactId>
</exclusion>
+ <exclusion>
+ <groupId>org.apache.geronimo.javamail</groupId>
+ <artifactId>geronimo-javamail_1.4_mail</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
</exclusions>
</dependency>
<dependency>
@@ -73,25 +85,52 @@
<scope>test</scope>
<exclusions>
<exclusion>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
+ <artifactId>apache-mailet</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mime4j-core</artifactId>
</exclusion>
<exclusion>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mime4j-dom</artifactId>
</exclusion>
+ <exclusion>
+ <groupId>org.apache.geronimo.javamail</groupId>
+ <artifactId>geronimo-javamail_1.4_mail</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
</exclusions>
</dependency>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
<groupId>ca.juliusdavies</groupId>
<artifactId>not-yet-commons-ssl</artifactId>
<version>0.3.11</version>
</dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.platform</groupId>
+ <artifactId>junit-platform-launcher</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.vintage</groupId>
+ <artifactId>junit-vintage-engine</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
index baddb09..a98c12b 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
@@ -27,11 +27,15 @@ import java.util.Optional;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
+import com.google.common.annotations.VisibleForTesting;
import org.apache.james.jdkim.DKIMVerifier;
import org.apache.james.jdkim.api.BodyHasher;
import org.apache.james.jdkim.api.Headers;
import org.apache.james.jdkim.api.SignatureRecord;
import org.apache.james.jdkim.exceptions.FailException;
+import org.apache.mailet.Attribute;
+import org.apache.mailet.AttributeName;
+import org.apache.mailet.AttributeValue;
import org.apache.mailet.Mail;
import org.apache.mailet.base.GenericMailet;
@@ -50,9 +54,9 @@ import org.apache.mailet.base.GenericMailet;
*/
public class DKIMVerify extends GenericMailet {
- public static final String DKIM_AUTH_RESULT_ATTRIBUTE = "jDKIM.AUTHRESULT";
+ public static final AttributeName DKIM_AUTH_RESULT = AttributeName.of("jDKIM.AUTHRESULT");
- protected DKIMVerifier verifier = null;
+ @VisibleForTesting DKIMVerifier verifier = null;
private boolean forceCRLF;
@Override
@@ -67,7 +71,7 @@ public class DKIMVerify extends GenericMailet {
List<SignatureRecord> res = verify(verifier, message, forceCRLF);
if (res == null || res.isEmpty()) {
// neutral
- mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, "neutral (no signatures)");
+ mail.setAttribute(new Attribute(DKIM_AUTH_RESULT, AttributeValue.of("neutral (no signatures)")));
} else {
// pass
StringBuilder msg = new StringBuilder();
@@ -78,18 +82,19 @@ public class DKIMVerify extends GenericMailet {
msg.append(rec.getIdentity().toString());
msg.append(")");
}
- mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, msg.toString());
+ mail.setAttribute(new Attribute(DKIM_AUTH_RESULT, AttributeValue.of(msg.toString())));
}
} catch (FailException e) {
// fail
String relatedRecordIdentity = Optional.ofNullable(e.getRelatedRecordIdentity())
.map(value -> "identity" + value + ":")
.orElse("");
- mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, "fail (" + relatedRecordIdentity + e.getMessage() + ")");
+ mail.setAttribute(new Attribute(DKIM_AUTH_RESULT, AttributeValue.of("fail (" + relatedRecordIdentity + e.getMessage() + ")")));
}
}
- protected static List<SignatureRecord> verify(DKIMVerifier verifier, MimeMessage message, boolean forceCRLF)
+ @VisibleForTesting
+ static List<SignatureRecord> verify(DKIMVerifier verifier, MimeMessage message, boolean forceCRLF)
throws MessagingException, FailException {
Headers headers = new MimeMessageHeaders(message);
BodyHasher bh = verifier.newBodyHasher(headers);
diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
index 731941a..ee69b4c 100644
--- a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
+++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
@@ -19,8 +19,8 @@
package org.apache.james.jdkim.mailets;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Fail.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -45,8 +45,8 @@ import org.apache.mailet.Mailet;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMailetConfig;
-import org.junit.Assert;
-import org.junit.Test;
+
+import org.junit.jupiter.api.Test;
public class DKIMSignTest {
@@ -68,7 +68,7 @@ public class DKIMSignTest {
private static final FakeMailContext FAKE_MAIL_CONTEXT = FakeMailContext.defaultContext();
@Test
- public void testDKIMSign() throws MessagingException, IOException,
+ void testDKIMSign() throws MessagingException, IOException,
FailException {
String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
@@ -111,13 +111,13 @@ public class DKIMSignTest {
MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever)
throws MessagingException, FailException {
List<SignatureRecord> signs = DKIMVerify.verify(new DKIMVerifier(mockPublicKeyRecordRetriever), new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(rawMessage.toByteArray())), true);
- assertNotNull(signs);
- assertEquals(1, signs.size());
+ assertThat(signs).hasSize(1);
+
return signs;
}
@Test
- public void testDKIMSignFuture() throws MessagingException, IOException,
+ void testDKIMSignFuture() throws MessagingException, IOException,
FailException {
String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
@@ -155,7 +155,7 @@ public class DKIMSignTest {
"selector", "example.com");
try {
verify(rawMessage, mockPublicKeyRecordRetriever);
- Assert.fail("Expecting signature to be ignored");
+ fail("Expecting signature to be ignored");
} catch (PermFailException e) {
// signature ignored, so fail for missing signatures.
}
@@ -163,7 +163,7 @@ public class DKIMSignTest {
@Test
- public void testDKIMSignTime() throws MessagingException, IOException,
+ void testDKIMSignTime() throws MessagingException, IOException,
FailException {
String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
@@ -204,15 +204,15 @@ public class DKIMSignTest {
List<SignatureRecord> rs = verify(rawMessage, mockPublicKeyRecordRetriever);
// check we have a valued signatureTimestamp
- Assert.assertNotNull(rs.get(0).getSignatureTimestamp());
+ assertThat(rs.get(0).getSignatureTimestamp()).isNotNull();
long ref = System.currentTimeMillis() / 1000;
// Chech that the signature timestamp is in the past 60 seconds.
- Assert.assertTrue(rs.get(0).getSignatureTimestamp() <= ref);
- Assert.assertTrue(rs.get(0).getSignatureTimestamp() >= ref - 60);
+ assertThat(rs.get(0).getSignatureTimestamp() <= ref).isTrue();
+ assertThat(rs.get(0).getSignatureTimestamp() >= ref - 60).isTrue();
}
@Test
- public void testDKIMSignMessageAsText() throws MessagingException,
+ void testDKIMSignMessageAsText() throws MessagingException,
IOException, FailException {
MimeMessage mm = new MimeMessage(Session
.getDefaultInstance(new Properties()));
@@ -256,7 +256,7 @@ public class DKIMSignTest {
}
@Test
- public void testDKIMSignMessageAsObjectConvertedTo7Bit()
+ void testDKIMSignMessageAsObjectConvertedTo7Bit()
throws MessagingException, IOException, FailException {
MimeMessage mm = new MimeMessage(Session
.getDefaultInstance(new Properties()));
@@ -304,7 +304,7 @@ public class DKIMSignTest {
}
@Test
- public void testDKIMSignMessageAsObjectNotConverted()
+ void testDKIMSignMessageAsObjectNotConverted()
throws MessagingException, IOException, FailException {
MimeMessage mm = new MimeMessage(Session
.getDefaultInstance(new Properties()));
@@ -350,7 +350,7 @@ public class DKIMSignTest {
"selector", "example.com");
try {
verify(rawMessage, mockPublicKeyRecordRetriever);
- Assert.fail("Expected PermFail");
+ fail("Expected PermFail");
} catch (PermFailException e) {
}
diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
index 2011965..27650ab 100644
--- a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
+++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
@@ -19,7 +19,10 @@
package org.apache.james.jdkim.mailets;
+import static org.assertj.core.api.Assertions.assertThat;
+
import java.io.ByteArrayInputStream;
+import java.util.Optional;
import java.util.Properties;
import javax.mail.Session;
@@ -27,65 +30,66 @@ import javax.mail.internet.MimeMessage;
import org.apache.james.jdkim.DKIMVerifier;
import org.apache.james.jdkim.MockPublicKeyRecordRetriever;
+import org.apache.mailet.AttributeUtils;
import org.apache.mailet.Mail;
import org.apache.mailet.Mailet;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMailetConfig;
-import org.junit.Assert;
-import org.junit.Test;
+
+import org.junit.jupiter.api.Test;
public class DKIMVerifyTest {
@Test
- public void testDKIMVerifyPass() throws Exception {
+ void testDKIMVerifyPass() throws Exception {
String message = "DKIM-Signature: v=1; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
Mail mail = process(message);
- String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
- Assert.assertNotNull(attr);
- Assert.assertTrue(attr.startsWith("pass"));
+ Optional<String> attr = AttributeUtils.getValueAndCastFromMail(mail, DKIMVerify.DKIM_AUTH_RESULT, String.class);
+ assertThat(attr)
+ .hasValueSatisfying(result -> assertThat(result).startsWith("pass"));
}
@Test
- public void testDKIMVerifyFail() throws Exception {
+ void testDKIMVerifyFail() throws Exception {
// altered message body
String message = "DKIM-Signature: v=1; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova altered\r\n";
Mail mail = process(message);
- String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
- Assert.assertNotNull(attr);
- Assert.assertTrue(attr.startsWith("fail"));
+ Optional<String> attr = AttributeUtils.getValueAndCastFromMail(mail, DKIMVerify.DKIM_AUTH_RESULT, String.class);
+ assertThat(attr)
+ .hasValueSatisfying(result -> assertThat(result).startsWith("fail"));
}
@Test
- public void testDKIMVerifyFailInvalid() throws Exception {
+ void testDKIMVerifyFailInvalid() throws Exception {
// invalid version v=2
String message = "DKIM-Signature: v=2; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
Mail mail = process(message);
- String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
- Assert.assertNotNull(attr);
- Assert.assertTrue(attr.startsWith("fail"));
+ Optional<String> attr = AttributeUtils.getValueAndCastFromMail(mail, DKIMVerify.DKIM_AUTH_RESULT, String.class);
+ assertThat(attr)
+ .hasValueSatisfying(result -> assertThat(result).startsWith("fail"));
}
@Test
- public void testDKIMVerifyNeutral() throws Exception {
+ void testDKIMVerifyNeutral() throws Exception {
// no signatures!
String message = ""
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova altered\r\n";
Mail mail = process(message);
- String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
- Assert.assertNotNull(attr);
- Assert.assertTrue(attr.startsWith("neutral"));
+ Optional<String> attr = AttributeUtils.getValueAndCastFromMail(mail, DKIMVerify.DKIM_AUTH_RESULT, String.class);
+ assertThat(attr)
+ .hasValueSatisfying(result -> assertThat(result).startsWith("neutral"));
}
private Mail process(String message) throws Exception {
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org