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

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


The following commit(s) were added to refs/heads/master by this push:
     new 6f4084b05b JAMES-3491 Explicitly add tests for subaddressing (#1694)
6f4084b05b is described below

commit 6f4084b05bb94a7a0ce21199caae7f151bcffad9
Author: Benoit TELLIER <[email protected]>
AuthorDate: Thu Aug 24 13:07:14 2023 +0700

    JAMES-3491 Explicitly add tests for subaddressing (#1694)
---
 .../org/apache/james/core/MailAddressTest.java     |   3 +-
 .../org/apache/james/SubAddressingRelayTest.java   | 164 +++++++++++++++++++++
 .../james/webadmin/routes/ForwardRoutesTest.java   |  15 ++
 3 files changed, 181 insertions(+), 1 deletion(-)

diff --git a/core/src/test/java/org/apache/james/core/MailAddressTest.java 
b/core/src/test/java/org/apache/james/core/MailAddressTest.java
index 1920c357b9..bebde4703c 100644
--- a/core/src/test/java/org/apache/james/core/MailAddressTest.java
+++ b/core/src/test/java/org/apache/james/core/MailAddressTest.java
@@ -53,6 +53,7 @@ class MailAddressTest {
                 "[email protected]",
                 "[email protected]",
                 "user+mailbox/[email protected]",
+                "[email protected]",
                 "\"Abc@def\"@example.com",
                 "\"Fred Bloggs\"@example.com",
                 "\"Joe.\\Blow\"@example.com",
@@ -95,7 +96,7 @@ class MailAddressTest {
                 "server-dev\\[email protected]", // Javax.mail is unable to 
handle this so we better reject it
                 "[email protected]",
                 // According to wikipedia these addresses are valid but as 
javax.mail is unable
-                // to work with thenm we shall rather reject them (note that 
this is not breaking retro-compatibility)
+                // to work with them we shall rather reject them (note that 
this is not breaking retro-compatibility)
                 "Loïc.Accentué@voilà.fr8",
                 "pelé@exemple.com",
                 "δοκιμή@παράδειγμα.δοκιμή",
diff --git 
a/server/mailet/remote-delivery-integration-testing/src/test/java/org/apache/james/SubAddressingRelayTest.java
 
b/server/mailet/remote-delivery-integration-testing/src/test/java/org/apache/james/SubAddressingRelayTest.java
new file mode 100644
index 0000000000..896d9d44da
--- /dev/null
+++ 
b/server/mailet/remote-delivery-integration-testing/src/test/java/org/apache/james/SubAddressingRelayTest.java
@@ -0,0 +1,164 @@
+/****************************************************************
+ * 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;
+
+import static org.apache.james.MemoryJamesServerMain.SMTP_AND_IMAP_MODULE;
+import static org.apache.james.mailets.configuration.Constants.DEFAULT_DOMAIN;
+import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP;
+import static org.apache.james.mailets.configuration.Constants.PASSWORD;
+import static org.apache.james.mailets.configuration.Constants.calmlyAwait;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.awaitility.Durations.TEN_SECONDS;
+
+import java.io.File;
+
+import org.apache.commons.net.smtp.AuthenticatingSMTPClient;
+import org.apache.james.core.MailAddress;
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.james.dnsservice.api.InMemoryDNSService;
+import org.apache.james.mailets.TemporaryJamesServer;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetConfiguration;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.mailets.configuration.SmtpConfiguration;
+import org.apache.james.mock.smtp.server.model.Mail;
+import org.apache.james.mock.smtp.server.model.SMTPExtension;
+import org.apache.james.mock.smtp.server.model.SMTPExtensions;
+import org.apache.james.mock.smtp.server.testing.MockSmtpServerExtension;
+import 
org.apache.james.mock.smtp.server.testing.MockSmtpServerExtension.DockerMockSmtp;
+import org.apache.james.modules.protocols.SmtpGuiceProbe;
+import org.apache.james.smtpserver.dsn.DSNEhloHook;
+import org.apache.james.smtpserver.dsn.DSNMailParameterHook;
+import org.apache.james.smtpserver.dsn.DSNMessageHook;
+import org.apache.james.smtpserver.dsn.DSNRcptParameterHook;
+import org.apache.james.transport.mailets.RecipientRewriteTable;
+import org.apache.james.transport.mailets.RemoteDelivery;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.SMTPMessageSender;
+import org.apache.james.utils.TestIMAPClient;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.jupiter.api.io.TempDir;
+
+class SubAddressingRelayTest {
+    private static final String ANOTHER_DOMAIN = "other.com";
+    private static final String FROM = "from@" + DEFAULT_DOMAIN;
+    private static final String RECIPIENT = "touser+tag@" + ANOTHER_DOMAIN;
+
+    private InMemoryDNSService inMemoryDNSService;
+
+    @RegisterExtension
+    public TestIMAPClient testIMAPClient = new TestIMAPClient();
+    @RegisterExtension
+    public SMTPMessageSender messageSender = new 
SMTPMessageSender(DEFAULT_DOMAIN);
+    @RegisterExtension
+    public static MockSmtpServerExtension mockSmtpExtension = new 
MockSmtpServerExtension();
+
+    private TemporaryJamesServer jamesServer;
+
+    @BeforeEach
+    void setUp(@TempDir File temporaryFolder, DockerMockSmtp mockSmtp) throws 
Exception {
+        inMemoryDNSService = new InMemoryDNSService()
+            .registerMxRecord(DEFAULT_DOMAIN, LOCALHOST_IP)
+            .registerMxRecord(ANOTHER_DOMAIN, mockSmtp.getIPAddress());
+
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(SMTP_AND_IMAP_MODULE)
+            .withOverrides(binder -> 
binder.bind(DNSService.class).toInstance(inMemoryDNSService))
+            .withMailetContainer(MailetContainer.builder()
+                .putProcessor(CommonProcessors.simpleRoot())
+                .putProcessor(CommonProcessors.error())
+                .putProcessor(directResolutionTransport())
+                .putProcessor(CommonProcessors.bounces()))
+            .withSmtpConfiguration(SmtpConfiguration.builder()
+                .addHook(DSNEhloHook.class.getName())
+                .addHook(DSNMailParameterHook.class.getName())
+                .addHook(DSNRcptParameterHook.class.getName())
+                .addHook(DSNMessageHook.class.getName())
+                .withAutorizedAddresses("0.0.0.0/0.0.0.0"))
+            .build(temporaryFolder);
+        jamesServer.start();
+
+        jamesServer.getProbe(DataProbeImpl.class)
+            .fluent()
+            .addDomain(DEFAULT_DOMAIN)
+            .addUser(FROM, PASSWORD);
+
+        
mockSmtp.getConfigurationClient().setSMTPExtensions(SMTPExtensions.of(SMTPExtension.of("dsn")));
+
+        
assertThat(mockSmtp.getConfigurationClient().version()).isEqualTo("0.4");
+    }
+
+    @AfterEach
+    void tearDown() {
+        jamesServer.shutdown();
+    }
+
+    @Test
+    void remoteDeliveryShouldAcceptSubAddressing(DockerMockSmtp mockSmtp) 
throws Exception {
+        AuthenticatingSMTPClient smtpClient = new 
AuthenticatingSMTPClient("TLS", "UTF-8");
+
+        try {
+            smtpClient.connect("localhost", 
jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort().getValue());
+            smtpClient.ehlo(DEFAULT_DOMAIN);
+            smtpClient.mail("<" + FROM + ">");
+            smtpClient.rcpt("<" + RECIPIENT + "> NOTIFY=FAILURE,DELAY");
+            smtpClient.sendShortMessageData("A short message...");
+        } finally {
+            smtpClient.disconnect();
+        }
+
+        calmlyAwait.atMost(TEN_SECONDS).untilAsserted(() -> 
assertThat(mockSmtp.getConfigurationClient().listMails())
+            .hasSize(1)
+            .extracting(Mail::getEnvelope)
+            .containsOnly(Mail.Envelope.builder()
+                .from(new MailAddress(FROM))
+                .addRecipient(Mail.Recipient.builder()
+                    .address(new MailAddress(RECIPIENT))
+                    .addParameter(Mail.Parameter.builder()
+                        .name("NOTIFY")
+                        .value("FAILURE,DELAY")
+                        .build())
+                    .build())
+                .build()));
+    }
+
+    private ProcessorConfiguration.Builder directResolutionTransport() {
+        return ProcessorConfiguration.transport()
+            .addMailet(MailetConfiguration.BCC_STRIPPER)
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(RecipientRewriteTable.class))
+            .addMailet(MailetConfiguration.builder()
+                .mailet(RemoteDelivery.class)
+                .matcher(All.class)
+                .addProperty("outgoingQueue", "outgoing")
+                .addProperty("delayTime", "3 * 10 ms")
+                .addProperty("maxRetries", "3")
+                .addProperty("maxDnsProblemRetries", "0")
+                .addProperty("deliveryThreads", "2")
+                .addProperty("sendpartial", "true"));
+    }
+}
diff --git 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/ForwardRoutesTest.java
 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/ForwardRoutesTest.java
index 38547a3279..4816746395 100644
--- 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/ForwardRoutesTest.java
+++ 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/ForwardRoutesTest.java
@@ -33,6 +33,8 @@ import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Map;
 
@@ -155,6 +157,19 @@ class ForwardRoutesTest {
             assertThat(addresses).containsExactly(ALICE, CEDRIC);
         }
 
+        @Test
+        void shouldSupportSubAddressing() {
+            with()
+                .put(ALICE + SEPARATOR + "targets" + SEPARATOR + 
URLEncoder.encode("bob+tag@" + DOMAIN.name(), StandardCharsets.UTF_8));
+
+            when()
+                .get(ALICE)
+            .then()
+                .contentType(ContentType.JSON)
+                .statusCode(HttpStatus.OK_200)
+                .body("mailAddress", hasItems("bob+tag@" + DOMAIN.name()));
+        }
+
         @Test
         void getShouldNotResolveRecurseForwards() {
             with()


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to