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 ma...@apache.org on 2018/04/25 08:55:29 UTC

[10/24] james-project git commit: JAMES-2381 Enhance complete and correct ValidRcptHandlerTests

JAMES-2381 Enhance complete and correct ValidRcptHandlerTests


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/dc7d05aa
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/dc7d05aa
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/dc7d05aa

Branch: refs/heads/master
Commit: dc7d05aa6245b04ca70fe3a10f5095964ecea983
Parents: d1573fc
Author: benwa <bt...@linagora.com>
Authored: Fri Apr 20 11:50:54 2018 +0700
Committer: Matthieu Baechler <ma...@apache.org>
Committed: Tue Apr 24 15:34:30 2018 +0200

----------------------------------------------------------------------
 .../james/smtpserver/ValidRcptHandlerTest.java  | 125 ++++++++++++-------
 1 file changed, 83 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/dc7d05aa/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
index 8ab0360..de4c3fa 100644
--- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
+++ b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
@@ -18,6 +18,7 @@
  ****************************************************************/
 package org.apache.james.smtpserver;
 
+import static org.apache.mailet.base.MailAddressFixture.SENDER;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 
@@ -26,48 +27,55 @@ import java.util.HashMap;
 import org.apache.james.core.Domain;
 import org.apache.james.core.MailAddress;
 import org.apache.james.dnsservice.api.DNSService;
-import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.memory.MemoryDomainList;
-import org.apache.james.protocols.api.ProtocolSession.State;
 import org.apache.james.protocols.smtp.SMTPSession;
 import org.apache.james.protocols.smtp.hook.HookReturnCode;
 import org.apache.james.protocols.smtp.utils.BaseFakeSMTPSession;
-import org.apache.james.rrt.api.RecipientRewriteTable;
-import org.apache.james.rrt.api.RecipientRewriteTableException;
 import org.apache.james.rrt.lib.MappingSource;
 import org.apache.james.rrt.memory.MemoryRecipientRewriteTable;
 import org.apache.james.smtpserver.fastfail.ValidRcptHandler;
 import org.apache.james.user.api.UsersRepository;
 import org.apache.james.user.memory.MemoryUsersRepository;
 import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
 
 public class ValidRcptHandlerTest {
-
     private static final String VALID_USER = "postmaster";
     private static final String INVALID_USER = "invalid";
     private static final String USER1 = "user1";
     private static final String USER2 = "user2";
-    UsersRepository users;
-    ValidRcptHandler handler;
+    private static final String PASSWORD = "xxx";
+    private static final boolean RELAYING_ALLOWED = true;
+
+    private ValidRcptHandler handler;
+    private MemoryRecipientRewriteTable memoryRecipientRewriteTable;
+    private MailAddress validUserEmail;
+    private MailAddress user1mail;
+    private MailAddress invalidUserEmail;
 
     @Before
     public void setUp() throws Exception {
-
-        users = MemoryUsersRepository.withoutVirtualHosting();
-        users.addUser(VALID_USER, "xxx");
+        UsersRepository users = MemoryUsersRepository.withoutVirtualHosting();
+        users.addUser(VALID_USER, PASSWORD);
 
         MemoryDomainList memoryDomainList = new MemoryDomainList(mock(DNSService.class));
         memoryDomainList.setDefaultDomain(Domain.LOCALHOST);
 
+        memoryRecipientRewriteTable = new MemoryRecipientRewriteTable();
+        memoryRecipientRewriteTable.setDomainList(memoryDomainList);
+
         handler = new ValidRcptHandler();
         handler.setUsersRepository(users);
-        handler.setRecipientRewriteTable(setUpRecipientRewriteTable(memoryDomainList));
+        handler.setRecipientRewriteTable(memoryRecipientRewriteTable);
         handler.setDomainList(memoryDomainList);
-    }
 
-    private SMTPSession setupMockedSMTPSession(final boolean relayingAllowed) {
+        validUserEmail = new MailAddress(VALID_USER + "@localhost");
+        user1mail = new MailAddress(USER1 + "@localhost");
+        invalidUserEmail = new MailAddress(INVALID_USER + "@localhost");
+    }
 
+    private SMTPSession setupMockedSMTPSession(boolean relayingAllowed) {
         return new BaseFakeSMTPSession() {
 
             @Override
@@ -104,63 +112,96 @@ public class ValidRcptHandlerTest {
         };
     }
 
-    private RecipientRewriteTable setUpRecipientRewriteTable(DomainList domainList) throws RecipientRewriteTableException {
-        MemoryRecipientRewriteTable memoryRecipientRewriteTable = new MemoryRecipientRewriteTable();
-        memoryRecipientRewriteTable.setDomainList(domainList);
-        memoryRecipientRewriteTable.addAddressMapping(MappingSource.fromUser(USER1, Domain.LOCALHOST), "address");
-        memoryRecipientRewriteTable.addErrorMapping(MappingSource.fromUser(USER2, Domain.LOCALHOST), "554 BOUNCE");
-        return memoryRecipientRewriteTable;
+    @Test
+    public void doRcptShouldRejectNotExistingLocalUsersWhenNoRelay() {
+        SMTPSession session = setupMockedSMTPSession(!RELAYING_ALLOWED);
+
+        int rCode = handler.doRcpt(session, SENDER, invalidUserEmail).getResult();
+
+        assertThat(rCode).isEqualTo(HookReturnCode.DENY);
     }
 
     @Test
-    public void testRejectInvalidUser() throws Exception {
-        MailAddress mailAddress = new MailAddress(INVALID_USER + "@localhost");
-        SMTPSession session = setupMockedSMTPSession(false);
+    public void doRcptShouldDenyNotExistingLocalUsersWhenRelay() {
+        SMTPSession session = setupMockedSMTPSession(RELAYING_ALLOWED);
 
-        int rCode = handler.doRcpt(session, null, mailAddress).getResult();
+        int rCode = handler.doRcpt(session, SENDER, invalidUserEmail).getResult();
 
         assertThat(rCode).isEqualTo(HookReturnCode.DENY);
     }
 
     @Test
-    public void testRejectInvalidUserRelay() throws Exception {
-        MailAddress mailAddress = new MailAddress(INVALID_USER + "@localhost");
-        SMTPSession session = setupMockedSMTPSession(true);
+    public void doRcptShouldDeclineNonLocalUsersWhenRelay() throws Exception {
+        MailAddress mailAddress = new MailAddress(INVALID_USER + "@otherdomain");
+        SMTPSession session = setupMockedSMTPSession(RELAYING_ALLOWED);
 
-        int rCode = handler.doRcpt(session, null, mailAddress).getResult();
+        int rCode = handler.doRcpt(session, SENDER, mailAddress).getResult();
 
-        assertThat(rCode).isEqualTo(HookReturnCode.DENY);
+        assertThat(rCode).isEqualTo(HookReturnCode.DECLINED);
     }
 
     @Test
-    public void testNotRejectValidUser() throws Exception {
-        MailAddress mailAddress = new MailAddress(VALID_USER + "@localhost");
-        SMTPSession session = setupMockedSMTPSession(false);
+    public void doRcptShouldDeclineNonLocalUsersWhenNoRelay() throws Exception {
+        MailAddress mailAddress = new MailAddress(INVALID_USER + "@otherdomain");
+        SMTPSession session = setupMockedSMTPSession(!RELAYING_ALLOWED);
 
-        int rCode = handler.doRcpt(session, null, mailAddress).getResult();
+        int rCode = handler.doRcpt(session, SENDER, mailAddress).getResult();
 
         assertThat(rCode).isEqualTo(HookReturnCode.DECLINED);
     }
 
     @Test
-    public void testHasAddressMapping() throws Exception {
-        MailAddress mailAddress = new MailAddress(USER1 + "@localhost");
-        SMTPSession session = setupMockedSMTPSession(false);
+    public void doRcptShouldDeclineValidUsersWhenNoRelay() throws Exception {
+        SMTPSession session = setupMockedSMTPSession(!RELAYING_ALLOWED);
 
-        int rCode = handler.doRcpt(session, null, mailAddress).getResult();
+        int rCode = handler.doRcpt(session, SENDER, validUserEmail).getResult();
 
         assertThat(rCode).isEqualTo(HookReturnCode.DECLINED);
     }
 
     @Test
-    public void testHasErrorMapping() throws Exception {
-        MailAddress mailAddress = new MailAddress(USER2 + "@localhost");
-        SMTPSession session = setupMockedSMTPSession(false);
+    public void doRcptShouldDeclineValidUsersWhenRelay() throws Exception {
+        SMTPSession session = setupMockedSMTPSession(RELAYING_ALLOWED);
 
-        int rCode = handler.doRcpt(session, null, mailAddress).getResult();
+        int rCode = handler.doRcpt(session, SENDER, validUserEmail).getResult();
 
-        assertThat(session.getAttachment("VALID_USER", State.Transaction)).isNull();
-        assertThat(rCode).isEqualTo(HookReturnCode.DENY);
+        assertThat(rCode).isEqualTo(HookReturnCode.DECLINED);
+    }
+
+    @Test
+    public void doRcptShouldDeclineWhenHasAddressMapping() throws Exception {
+        memoryRecipientRewriteTable.addAddressMapping(MappingSource.fromUser(USER1, Domain.LOCALHOST), "address");
+
+        SMTPSession session = setupMockedSMTPSession(!RELAYING_ALLOWED);
+
+        int rCode = handler.doRcpt(session, SENDER, validUserEmail).getResult();
+
+        assertThat(rCode).isEqualTo(HookReturnCode.DECLINED);
+    }
+
+    @Ignore
+    @Test
+    public void doRcptShouldDenyWhenHasMappingLoop() throws Exception {
+        memoryRecipientRewriteTable.addAddressMapping(MappingSource.fromUser(USER1, Domain.LOCALHOST), USER2 + "@localhost");
+        memoryRecipientRewriteTable.addAddressMapping(MappingSource.fromUser(USER2, Domain.LOCALHOST), USER1 + "@localhost");
+
+        SMTPSession session = setupMockedSMTPSession(!RELAYING_ALLOWED);
+
+        int rCode = handler.doRcpt(session, SENDER, user1mail).getResult();
+
+        assertThat(rCode).isEqualTo(HookReturnCode.DECLINED);
+    }
+
+    @Ignore
+    @Test
+    public void doRcptShouldDeclineWhenHasErrorMapping() throws Exception {
+        memoryRecipientRewriteTable.addErrorMapping(MappingSource.fromUser(USER1, Domain.LOCALHOST), "554 BOUNCE");
+
+        SMTPSession session = setupMockedSMTPSession(!RELAYING_ALLOWED);
+
+        int rCode = handler.doRcpt(session, SENDER, user1mail).getResult();
+
+        assertThat(rCode).isEqualTo(HookReturnCode.DECLINED);
     }
     
 }


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