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 rc...@apache.org on 2020/02/10 03:54:21 UTC

[james-project] 06/09: JAMES-3016 Extract new DKIMVerifier to be using in integration test

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit f7e83f4ee11d0d2e6a34844fd6b692335c7ab742
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Wed Jan 22 13:20:03 2020 +0700

    JAMES-3016 Extract new DKIMVerifier to be using in integration test
---
 .../apache/james/jdkim/mailets/DKIMVerifier.java   | 74 ++++++++++++++++++++++
 .../org/apache/james/jdkim/mailets/DKIMVerify.java | 38 +----------
 .../apache/james/jdkim/mailets/DKIMSignTest.java   |  8 ++-
 3 files changed, 80 insertions(+), 40 deletions(-)

diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerifier.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerifier.java
new file mode 100644
index 0000000..0699402
--- /dev/null
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerifier.java
@@ -0,0 +1,74 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jdkim.mailets;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.List;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.james.jdkim.api.BodyHasher;
+import org.apache.james.jdkim.api.Headers;
+import org.apache.james.jdkim.api.PublicKeyRecordRetriever;
+import org.apache.james.jdkim.api.SignatureRecord;
+import org.apache.james.jdkim.exceptions.FailException;
+
+public class DKIMVerifier {
+    private final org.apache.james.jdkim.DKIMVerifier originalVerifier;
+
+    public DKIMVerifier(PublicKeyRecordRetriever publicKeyRecordRetriever) {
+        this.originalVerifier = new org.apache.james.jdkim.DKIMVerifier(publicKeyRecordRetriever);
+    }
+
+    public List<SignatureRecord> verifyUsingCRLF(MimeMessage message) throws MessagingException, FailException {
+        return verify(message, true);
+    }
+
+    public List<SignatureRecord> verify(MimeMessage message, boolean forceCRLF) throws MessagingException, FailException {
+        Headers headers = new MimeMessageHeaders(message);
+        BodyHasher bh = originalVerifier.newBodyHasher(headers);
+        try {
+            if (bh != null) {
+                OutputStream os = new HeaderSkippingOutputStream(bh
+                    .getOutputStream());
+                if (forceCRLF) {
+                    os = new CRLFOutputStream(os);
+                }
+                message.writeTo(os);
+            }
+
+        } catch (IOException e) {
+            throw new MessagingException("Exception calculating bodyhash: "
+                    + e.getMessage(), e);
+        } finally {
+            try {
+                if (bh != null) {
+                    bh.getOutputStream().close();
+                }
+            } catch (IOException e) {
+                throw new MessagingException("Exception calculating bodyhash: "
+                        + e.getMessage(), e);
+            }
+        }
+        return originalVerifier.verify(bh);
+    }
+}
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 2a2b11a..37eee98 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
@@ -19,8 +19,6 @@
 
 package org.apache.james.jdkim.mailets;
 
-import java.io.IOException;
-import java.io.OutputStream;
 import java.util.List;
 import java.util.Optional;
 
@@ -28,9 +26,6 @@ import javax.inject.Inject;
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 
-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.PublicKeyRecordRetriever;
 import org.apache.james.jdkim.api.SignatureRecord;
 import org.apache.james.jdkim.exceptions.FailException;
@@ -77,7 +72,7 @@ public class DKIMVerify extends GenericMailet {
     public void service(Mail mail) throws MessagingException {
         try {
             MimeMessage message = mail.getMessage();
-            List<SignatureRecord> res = verify(verifier, message, forceCRLF);
+            List<SignatureRecord> res = verifier.verify(message, forceCRLF);
             if (res == null || res.isEmpty()) {
                 // neutral
                 mail.setAttribute(new Attribute(DKIM_AUTH_RESULT, AttributeValue.of("neutral (no signatures)")));
@@ -101,35 +96,4 @@ public class DKIMVerify extends GenericMailet {
             mail.setAttribute(new Attribute(DKIM_AUTH_RESULT, AttributeValue.of("fail (" + relatedRecordIdentity + e.getMessage() + ")")));
         }
     }
-
-    @VisibleForTesting
-    static List<SignatureRecord> verify(DKIMVerifier verifier, MimeMessage message, boolean forceCRLF)
-        throws MessagingException, FailException {
-        Headers headers = new MimeMessageHeaders(message);
-        BodyHasher bh = verifier.newBodyHasher(headers);
-        try {
-            if (bh != null) {
-                OutputStream os = new HeaderSkippingOutputStream(bh
-                    .getOutputStream());
-                if (forceCRLF) {
-                    os = new CRLFOutputStream(os);
-                }
-                message.writeTo(os);
-            }
-
-        } catch (IOException e) {
-            throw new MessagingException("Exception calculating bodyhash: "
-                    + e.getMessage(), e);
-        } finally {
-            try {
-                if (bh != null) {
-                    bh.getOutputStream().close();
-                }
-            } catch (IOException e) {
-                throw new MessagingException("Exception calculating bodyhash: "
-                        + e.getMessage(), e);
-            }
-        }
-        return verifier.verify(bh);
-    }
 }
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 4a1c992..bd9fac9 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
@@ -35,10 +35,10 @@ import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMessage.RecipientType;
 
-import org.apache.james.jdkim.DKIMVerifier;
 import org.apache.james.jdkim.api.SignatureRecord;
 import org.apache.james.jdkim.exceptions.FailException;
 import org.apache.james.jdkim.exceptions.PermFailException;
+import org.apache.james.util.MimeMessageUtil;
 import org.apache.mailet.Mail;
 import org.apache.mailet.Mailet;
 import org.apache.mailet.base.test.FakeMail;
@@ -47,7 +47,7 @@ import org.apache.mailet.base.test.FakeMailetConfig;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ValueSource;
 
-public class DKIMSignTest {
+class DKIMSignTest {
 
     private static final String PKCS1_PEM_FILE = "test-dkim-pkcs1.pem";
     private static final String PKCS8_PEM_FILE = "test-dkim-pkcs8.pem";
@@ -97,7 +97,9 @@ public class DKIMSignTest {
     private List<SignatureRecord> verify(ByteArrayOutputStream rawMessage,
                                          MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever)
             throws MessagingException, FailException {
-        List<SignatureRecord> signs = DKIMVerify.verify(new DKIMVerifier(mockPublicKeyRecordRetriever), new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(rawMessage.toByteArray())), true);
+        List<SignatureRecord> signs = new DKIMVerifier(mockPublicKeyRecordRetriever)
+            .verifyUsingCRLF(MimeMessageUtil.mimeMessageFromStream(
+                new ByteArrayInputStream(rawMessage.toByteArray())));
         assertThat(signs).hasSize(1);
 
         return signs;


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org