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:17 UTC

[james-project] 02/09: JAMES-3034 SMTPMessageSender cannot handle UTF-8 characters in body

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 277dd466796cb1d1fd29c45c0ccc9f86741cfe2b
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Mon Feb 3 15:35:17 2020 +0700

    JAMES-3034 SMTPMessageSender cannot handle UTF-8 characters in body
---
 pom.xml                                            |  5 ++
 server/mailet/mock-smtp-server/pom.xml             |  1 -
 .../test/resources/cucumber/GetMessages.feature    |  2 +-
 server/testing/pom.xml                             |  5 ++
 .../org/apache/james/utils/SMTPMessageSender.java  | 13 ++-
 .../apache/james/utils/SMTPMessageSenderTest.java  | 94 ++++++++++++++++++++++
 6 files changed, 115 insertions(+), 5 deletions(-)

diff --git a/pom.xml b/pom.xml
index c491d1b..1b387a6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2672,6 +2672,11 @@
                 </exclusions>
             </dependency>
             <dependency>
+                <groupId>org.subethamail</groupId>
+                <artifactId>subethasmtp</artifactId>
+                <version>3.1.7</version>
+            </dependency>
+            <dependency>
                 <groupId>org.testcontainers</groupId>
                 <artifactId>testcontainers</artifactId>
                 <version>${testcontainers.version}</version>
diff --git a/server/mailet/mock-smtp-server/pom.xml b/server/mailet/mock-smtp-server/pom.xml
index 0f81583..4f7f78c 100644
--- a/server/mailet/mock-smtp-server/pom.xml
+++ b/server/mailet/mock-smtp-server/pom.xml
@@ -113,7 +113,6 @@
         <dependency>
             <groupId>org.subethamail</groupId>
             <artifactId>subethasmtp</artifactId>
-            <version>3.1.7</version>
         </dependency>
     </dependencies>
 
diff --git a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/resources/cucumber/GetMessages.feature b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
index a497504..d58c6a1 100644
--- a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
+++ b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/resources/cucumber/GetMessages.feature
@@ -447,6 +447,6 @@ Feature: GetMessages method
     And the first attachment is:
     |key      | value                        |
     |type     |"text/calendar"               |
-    |size     |1056                          |
+    |size     |1096                          |
     |name     |"event.ics"                   |
     |isInline |false                         |
diff --git a/server/testing/pom.xml b/server/testing/pom.xml
index d981af7..20fc034 100644
--- a/server/testing/pom.xml
+++ b/server/testing/pom.xml
@@ -74,6 +74,11 @@
             <artifactId>awaitility</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.subethamail</groupId>
+            <artifactId>subethasmtp</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.testcontainers</groupId>
             <artifactId>testcontainers</artifactId>
         </dependency>
diff --git a/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java b/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
index cdae5c8..7563db1 100644
--- a/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
+++ b/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
@@ -41,15 +41,18 @@ import com.github.fge.lambdas.Throwing;
 
 public class SMTPMessageSender extends ExternalResource implements Closeable {
 
+    private static final String DEFAULT_PROTOCOL = "TLS";
+    private static final String UTF_8_ENCODING = "UTF-8";
+
     public static SMTPMessageSender noAuthentication(String ip, int port, String senderDomain) throws IOException {
-        AuthenticatingSMTPClient smtpClient = new AuthenticatingSMTPClient();
+        AuthenticatingSMTPClient smtpClient = newUtf8AuthenticatingClient();
         smtpClient.connect(ip, port);
         return new SMTPMessageSender(smtpClient, senderDomain);
     }
 
     public static SMTPMessageSender authentication(String ip, int port, String senderDomain, String username, String password)
         throws NoSuchAlgorithmException, IOException, InvalidKeySpecException, InvalidKeyException {
-        AuthenticatingSMTPClient smtpClient = new AuthenticatingSMTPClient();
+        AuthenticatingSMTPClient smtpClient = newUtf8AuthenticatingClient();
         smtpClient.connect(ip, port);
         if (!smtpClient.auth(AuthenticatingSMTPClient.AUTH_METHOD.PLAIN, username, password)) {
             throw new RuntimeException("auth failed");
@@ -57,6 +60,10 @@ public class SMTPMessageSender extends ExternalResource implements Closeable {
         return new SMTPMessageSender(smtpClient, senderDomain);
     }
 
+    private static AuthenticatingSMTPClient newUtf8AuthenticatingClient() {
+        return new AuthenticatingSMTPClient(DEFAULT_PROTOCOL, UTF_8_ENCODING);
+    }
+
     private final AuthenticatingSMTPClient smtpClient;
     private final String senderDomain;
 
@@ -66,7 +73,7 @@ public class SMTPMessageSender extends ExternalResource implements Closeable {
     }
 
     public SMTPMessageSender(String senderDomain) {
-        this(new AuthenticatingSMTPClient(), senderDomain);
+        this(newUtf8AuthenticatingClient(), senderDomain);
     }
 
     public SMTPMessageSender connect(String ip, Port port) throws IOException {
diff --git a/server/testing/src/test/java/org/apache/james/utils/SMTPMessageSenderTest.java b/server/testing/src/test/java/org/apache/james/utils/SMTPMessageSenderTest.java
new file mode 100644
index 0000000..08565be
--- /dev/null
+++ b/server/testing/src/test/java/org/apache/james/utils/SMTPMessageSenderTest.java
@@ -0,0 +1,94 @@
+/****************************************************************
+ * 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.utils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.james.core.Domain;
+import org.apache.james.util.Port;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.subethamail.wiser.Wiser;
+
+class SMTPMessageSenderTest {
+
+    private static final int RANDOM_PORT = 0;
+    private static final String LOCALHOST = "localhost";
+    private static final String SENDER = "sender@localhost";
+    private static final String RECIPIENT = "receiver@localhost";
+    private static final String UNICODE_BODY = "Unicode characters Ê Â á tiếng việt";
+    private static final String ASCII_BODY = "ASCII characters E A A tieng viet";
+
+    private Wiser testingSMTPServer;
+    private SMTPMessageSender testee;
+
+    @BeforeEach
+    void setUp() throws IOException {
+        testingSMTPServer = new Wiser(RANDOM_PORT);
+        testingSMTPServer.start();
+
+        testee = new SMTPMessageSender(Domain.LOCALHOST.asString())
+            .connect(LOCALHOST, Port.of(testingSMTPServer.getServer().getPort()));
+    }
+
+    @AfterEach
+    void teardown() {
+        testingSMTPServer.stop();
+    }
+
+    @Test
+    void sendMessageWithHeadersShouldDeliverUnicodeBodyCharacters() throws IOException {
+        testee.sendMessageWithHeaders(SENDER, RECIPIENT, UNICODE_BODY);
+
+        assertThat(testingSMTPServer.getMessages())
+            .extracting(message -> new String(message.getData(), StandardCharsets.UTF_8))
+            .hasOnlyOneElementSatisfying(messageContent ->
+                assertThat(messageContent)
+                    .contains(UNICODE_BODY));
+    }
+
+    @Test
+    void sendMessageWithHeadersShouldDeliverASCIIBodyCharacters() throws IOException {
+        testee.sendMessageWithHeaders(SENDER, RECIPIENT, ASCII_BODY);
+
+        assertThat(testingSMTPServer.getMessages())
+            .extracting(message -> new String(message.getData(), StandardCharsets.UTF_8))
+            .hasOnlyOneElementSatisfying(messageContent ->
+                assertThat(messageContent)
+                    .contains(ASCII_BODY));
+    }
+
+    @Test
+    void sendMessageWithHeadersShouldPreserveRightEnvelop() throws IOException {
+        testee.sendMessageWithHeaders(SENDER, RECIPIENT, ASCII_BODY);
+
+        assertThat(testingSMTPServer.getMessages())
+            .hasOnlyOneElementSatisfying(message -> {
+                assertThat(message.getEnvelopeReceiver())
+                    .isEqualTo(RECIPIENT);
+                assertThat(message.getEnvelopeSender())
+                    .isEqualTo(SENDER);
+            });
+    }
+}
\ No newline at end of file


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