james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1226220 - in /james/protocols/trunk/smtp/src: main/java/org/apache/james/protocols/smtp/ main/java/org/apache/james/protocols/smtp/core/fastfail/ test/java/org/apache/james/protocols/smtp/ test/java/org/apache/james/protocols/smtp/core/fas...
Date Sun, 01 Jan 2012 09:16:20 GMT
Author: norman
Date: Sun Jan  1 09:16:19 2012
New Revision: 1226220

URL: http://svn.apache.org/viewvc?rev=1226220&view=rev
Log:
Remove DNSService interface. See PROTOCOLS-79

Removed:
    james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/DNSService.java
    james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/BaseFakeDNSService.java
Modified:
    james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/DNSRBLHandler.java
    james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ResolvableEhloHeloHandler.java
    james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ReverseEqualsEhloHeloHandler.java
    james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ValidSenderDomainHandler.java
    james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/DNSRBLHandlerTest.java
    james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/ResolvableEhloHeloHandlerTest.java
    james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/ValidSenderDomainHandlerTest.java

Modified: james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/DNSRBLHandler.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/DNSRBLHandler.java?rev=1226220&r1=1226219&r2=1226220&view=diff
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/DNSRBLHandler.java
(original)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/DNSRBLHandler.java
Sun Jan  1 09:16:19 2012
@@ -21,11 +21,13 @@
 
 package org.apache.james.protocols.smtp.core.fastfail;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.StringTokenizer;
 
 import org.apache.james.protocols.api.ProtocolSession.State;
-import org.apache.james.protocols.smtp.DNSService;
 import org.apache.james.protocols.smtp.MailAddress;
 import org.apache.james.protocols.smtp.SMTPSession;
 import org.apache.james.protocols.smtp.dsn.DSNStatus;
@@ -44,9 +46,7 @@ public class DNSRBLHandler implements  R
      */
     private String[] whitelist;
     private String[] blacklist;
-    
-    private DNSService dnsService = null;
-    
+        
     private boolean getDetail = false;
     
     private String blocklistedDetail = null;
@@ -54,16 +54,6 @@ public class DNSRBLHandler implements  R
     public static final String RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME = "org.apache.james.smtpserver.rbl.blocklisted";
     
     public static final String RBL_DETAIL_MAIL_ATTRIBUTE_NAME = "org.apache.james.smtpserver.rbl.detail";
-
-
-    /**
-     * Sets the DNS service.
-     * @param dnsService the dnsService to set
-     */
-    public final void setDNSService(DNSService dnsService) {
-        this.dnsService = dnsService;
-    }
-
    
     
     /**
@@ -137,52 +127,84 @@ public class DNSRBLHandler implements  R
 
             if (whitelist != null) {
                 String[] rblList = whitelist;
-                for (int i = 0 ; i < rblList.length ; i++) try {
-                    dnsService.getByName(reversedOctets + rblList[i]);
-                    if (session.getLogger().isInfoEnabled()) {
-                        session.getLogger().info("Connection from " + ipAddress + " whitelisted
by " + rblList[i]);
-                    }
+                for (int i = 0 ; i < rblList.length ; i++) {
+                    if (resolve(reversedOctets + rblList[i])) {
+                        if (session.getLogger().isInfoEnabled()) {
+                            session.getLogger().info("Connection from " + ipAddress + " whitelisted
by " + rblList[i]);
+                        }
                     
-                    return;
-                } catch (java.net.UnknownHostException uhe) {
-                    if (session.getLogger().isDebugEnabled()) {
-                        session.getLogger().debug("IpAddress " + session.getRemoteAddress().getAddress()
 + " not listed on " + rblList[i]);
+                        return;
+                    } else {
+                        if (session.getLogger().isDebugEnabled()) {
+                            session.getLogger().debug("IpAddress " + session.getRemoteAddress().getAddress()
 + " not listed on " + rblList[i]);
+                        }
                     }
                 }
             }
 
             if (blacklist != null) {
                 String[] rblList = blacklist;
-                for (int i = 0 ; i < rblList.length ; i++) try {
-                    dnsService.getByName(reversedOctets + rblList[i]);
-                    if (session.getLogger().isInfoEnabled()) {
-                        session.getLogger().info("Connection from " + ipAddress + " restricted
by " + rblList[i] + " to SMTP AUTH/postmaster/abuse.");
-                    }
-                    
-                    // we should try to retrieve details
-                    if (getDetail) {
-                        Collection<String> txt = dnsService.findTXTRecords(reversedOctets
+ rblList[i]);
+                for (int i = 0 ; i < rblList.length ; i++) {
+                    if (resolve(reversedOctets + rblList[i])) {
+                        if (session.getLogger().isInfoEnabled()) {
+                            session.getLogger().info("Connection from " + ipAddress + " restricted
by " + rblList[i] + " to SMTP AUTH/postmaster/abuse.");
+                        }
                         
-                        // Check if we found a txt record
-                        if (!txt.isEmpty()) {
-                            // Set the detail
-                            String blocklistedDetail = txt.iterator().next().toString();
+                        // we should try to retrieve details
+                        if (getDetail) {
+                            Collection<String> txt = resolveTXTRecords(reversedOctets
+ rblList[i]);
                             
-                            session.setAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, blocklistedDetail,
State.Connection);
+                            // Check if we found a txt record
+                            if (!txt.isEmpty()) {
+                                // Set the detail
+                                String blocklistedDetail = txt.iterator().next().toString();
+                                
+                                session.setAttachment(RBL_DETAIL_MAIL_ATTRIBUTE_NAME, blocklistedDetail,
State.Connection);
+                            }
+                        }
+                        
+                        session.setAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, "true",
State.Connection);
+                        return;
+                    } else {
+                        // if it is unknown, it isn't blocked
+                        if (session.getLogger().isDebugEnabled()) {
+                            session.getLogger().debug("unknown host exception thrown:" +
rblList[i]);
                         }
                     }
-                    
-                    session.setAttachment(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME, "true", State.Connection);
-                    return;
-                } catch (java.net.UnknownHostException uhe) {
-                    // if it is unknown, it isn't blocked
-                    if (session.getLogger().isDebugEnabled()) {
-                        session.getLogger().debug("unknown host exception thrown:" + rblList[i]);
-                    }
+                   
                 }
             }
         }
     }
+    
+    /**
+     * Check if the given ipaddress is resolvable. 
+     * 
+     * This implementation use {@link InetAddress#getByName(String)}. Sub-classes may override
this with a more performant solution
+     * 
+     * @param ip
+     * @return canResolve
+     */
+    protected boolean resolve(String ip) {
+        try {
+            InetAddress.getByName(ip);
+            return true;
+        } catch (UnknownHostException e) {
+            return false;
+        }
+    }
+    
+    /**
+     * Return a {@link Collection} which holds all TXT records for the ip. This is most times
used to add details for a RBL entry.
+     * 
+     * This implementation always returns an empty {@link Collection}. Sub-classes may override
this.
+     * 
+     * @param ip
+     * @return txtRecords
+     */
+    protected Collection<String> resolveTXTRecords(String ip) {
+        return Collections.<String>emptyList();
+    }
 
     /**
      * @see org.apache.james.protocols.smtp.hook.RcptHook#doRcpt(org.apache.james.protocols.smtp.SMTPSession,
org.apache.mailet.MailAddress, org.apache.mailet.MailAddress)

Modified: james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ResolvableEhloHeloHandler.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ResolvableEhloHeloHandler.java?rev=1226220&r1=1226219&r2=1226220&view=diff
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ResolvableEhloHeloHandler.java
(original)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ResolvableEhloHeloHandler.java
Sun Jan  1 09:16:19 2012
@@ -19,11 +19,11 @@
 
 package org.apache.james.protocols.smtp.core.fastfail;
 
+import java.net.InetAddress;
 import java.net.UnknownHostException;
 
 
 import org.apache.james.protocols.api.ProtocolSession.State;
-import org.apache.james.protocols.smtp.DNSService;
 import org.apache.james.protocols.smtp.MailAddress;
 import org.apache.james.protocols.smtp.SMTPRetCode;
 import org.apache.james.protocols.smtp.SMTPSession;
@@ -41,25 +41,6 @@ public class ResolvableEhloHeloHandler i
 
     public final static String BAD_EHLO_HELO = "BAD_EHLO_HELO";
 
-    protected DNSService dnsService = null;
-
-    /**
-     * Gets the DNS service.
-     * @return the dnsService
-     */
-    public final DNSService getDNSService() {
-        return dnsService;
-    }
-
-    /**
-     * Sets the DNS service.
-     * @param dnsService the dnsService to set
-     */
-    public final void setDNSService(DNSService dnsService) {
-        this.dnsService = dnsService;
-    }
-
-
     /**
      * Check if EHLO/HELO is resolvable
      * 
@@ -75,6 +56,9 @@ public class ResolvableEhloHeloHandler i
         }
     }
     
+    protected String resolve(String host) throws UnknownHostException {
+        return InetAddress.getByName(host).getHostName();
+    }
     /**
      * @param session the SMTPSession
      * @param argument the argument
@@ -84,7 +68,7 @@ public class ResolvableEhloHeloHandler i
         // try to resolv the provided helo. If it can not resolved do not
         // accept it.
         try {
-            dnsService.getByName(argument);
+            resolve(argument);
         } catch (UnknownHostException e) {
             return true;
         }

Modified: james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ReverseEqualsEhloHeloHandler.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ReverseEqualsEhloHeloHandler.java?rev=1226220&r1=1226219&r2=1226220&view=diff
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ReverseEqualsEhloHeloHandler.java
(original)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ReverseEqualsEhloHeloHandler.java
Sun Jan  1 09:16:19 2012
@@ -19,11 +19,9 @@
 
 package org.apache.james.protocols.smtp.core.fastfail;
 
-import org.apache.james.protocols.smtp.SMTPSession;
-
-
 import java.net.UnknownHostException;
 
+import org.apache.james.protocols.smtp.SMTPSession;
 
 public class ReverseEqualsEhloHeloHandler extends ResolvableEhloHeloHandler {
 
@@ -33,16 +31,16 @@ public class ReverseEqualsEhloHeloHandle
     @Override
     protected boolean isBadHelo(SMTPSession session, String argument) {
         try {
-            // get reverse entry
-            String reverse = dnsService.getHostName(dnsService.getByName(
-                    session.getRemoteAddress().getAddress().getHostAddress()));
+            String reverse = resolve(session.getRemoteAddress().getAddress().getHostAddress());
             if (!argument.equals(reverse)) {
                 return true;
             }
         } catch (UnknownHostException e) {
-            return true;
+            // ignore
         }
+
         return false;
     }
     
+    
 }

Modified: james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ValidSenderDomainHandler.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ValidSenderDomainHandler.java?rev=1226220&r1=1226219&r2=1226220&view=diff
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ValidSenderDomainHandler.java
(original)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/ValidSenderDomainHandler.java
Sun Jan  1 09:16:19 2012
@@ -18,13 +18,9 @@
  ****************************************************************/
 package org.apache.james.protocols.smtp.core.fastfail;
 
-import java.util.Collection;
-
-import org.apache.james.protocols.smtp.DNSService;
 import org.apache.james.protocols.smtp.MailAddress;
 import org.apache.james.protocols.smtp.SMTPRetCode;
 import org.apache.james.protocols.smtp.SMTPSession;
-import org.apache.james.protocols.smtp.TemporaryResolutionException;
 import org.apache.james.protocols.smtp.dsn.DSNStatus;
 import org.apache.james.protocols.smtp.hook.HookResult;
 import org.apache.james.protocols.smtp.hook.HookReturnCode;
@@ -34,50 +30,19 @@ import org.apache.james.protocols.smtp.h
  * Add MFDNSCheck feature to SMTPServer. This handler reject mail from domains which have
not an an valid MX record.  
  * 
  */
-public class ValidSenderDomainHandler implements MailHook {
-    
-    private DNSService dnsService = null;
-
-    /**
-     * Sets the DNS service.
-     * @param dnsService the dnsService to set
-     */
-    public final void setDNSService(DNSService dnsService) {
-        this.dnsService = dnsService;
-    }
-    
-        
-
+public abstract class ValidSenderDomainHandler implements MailHook { 
 
-    
-    protected boolean check(SMTPSession session, MailAddress senderAddress) {
-        // null sender so return
-        if (senderAddress == null) return false;
-
-        Collection<String> records = null;
-            
-        // try to resolv the provided domain in the senderaddress. If it can not resolved
do not accept it.
-        try {
-            records = dnsService.findMXRecords(senderAddress.getDomain());
-        } catch (TemporaryResolutionException e) {
-            // TODO: Should we reject temporary ?
-        }
-    
-        if (records == null || records.size() == 0) {
-            return true;
-        }
 
-        return false;
-    }
-    
     /**
      * @see org.apache.james.protocols.smtp.hook.MailHook#doMail(org.apache.james.protocols.smtp.SMTPSession,
org.apache.mailet.MailAddress)
      */
     public HookResult doMail(SMTPSession session, MailAddress sender) {
-        if (check(session,sender)) {
+        if (sender != null  && !hasMXRecord(session,sender.getDomain())) {
             return new HookResult(HookReturnCode.DENY,SMTPRetCode.SYNTAX_ERROR_ARGUMENTS,DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+
" sender " + sender + " contains a domain with no valid MX records");
         } else {
             return HookResult.declined();
         }
     }
+    
+    protected abstract boolean hasMXRecord(SMTPSession session, String domain);
 }

Modified: james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/DNSRBLHandlerTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/DNSRBLHandlerTest.java?rev=1226220&r1=1226219&r2=1226220&view=diff
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/DNSRBLHandlerTest.java
(original)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/DNSRBLHandlerTest.java
Sun Jan  1 09:16:19 2012
@@ -20,33 +20,27 @@
 
 package org.apache.james.protocols.smtp.core.fastfail;
 
-import java.net.InetAddress;
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNull;
+
 import java.net.InetSocketAddress;
-import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-
-import static junit.framework.Assert.*;
-
 import org.apache.james.protocols.api.ProtocolSession.State;
-import org.apache.james.protocols.smtp.BaseFakeDNSService;
 import org.apache.james.protocols.smtp.BaseFakeSMTPSession;
-import org.apache.james.protocols.smtp.DNSService;
 import org.apache.james.protocols.smtp.MailAddress;
 import org.apache.james.protocols.smtp.MailAddressException;
 import org.apache.james.protocols.smtp.SMTPSession;
-import org.apache.james.protocols.smtp.core.fastfail.DNSRBLHandler;
 import org.junit.Before;
 import org.junit.Test;
 
 public class DNSRBLHandlerTest {
 
-    private DNSService mockedDnsServer;
-
     private SMTPSession mockedSMTPSession;
 
     private String remoteIp = "127.0.0.2";
@@ -59,7 +53,6 @@ public class DNSRBLHandlerTest {
 
     @Before
     public void setUp() throws Exception {
-        setupMockedDnsServer();
         setRelayingAllowed(false);
     }
 
@@ -85,10 +78,23 @@ public class DNSRBLHandlerTest {
      * Setup the mocked dnsserver
      *
      */
-    private void setupMockedDnsServer() {
-        mockedDnsServer  = new BaseFakeDNSService() {
+    private DNSRBLHandler createHandler() {
+        return new DNSRBLHandler() {
 
-            public Collection<String> findTXTRecords(String hostname) {
+            @Override
+            protected boolean resolve(String host) {
+                if ("2.0.0.127.bl.spamcop.net.".equals(host)) {
+                    return true;
+                } else if ("3.0.0.127.bl.spamcop.net.".equals(host)) {
+                    return true;
+                } else if ("1.0.168.192.bl.spamcop.net.".equals(host)) {
+                    return false;
+                }
+                throw new UnsupportedOperationException("getByName("+host+") not implemented
in DNSRBLHandlerTest mock");
+            }
+
+            @Override
+            protected Collection<String> resolveTXTRecords(String hostname) {
                 List<String> res = new ArrayList<String>();
                 if (hostname == null) {
                     return res;
@@ -99,19 +105,9 @@ public class DNSRBLHandlerTest {
                 }
                 return res;
             }
-
-            public InetAddress getByName(String host)
-                    throws UnknownHostException {
-                if ("2.0.0.127.bl.spamcop.net.".equals(host)) {
-                    return InetAddress.getByName("127.0.0.1");
-                } else if ("3.0.0.127.bl.spamcop.net.".equals(host)) {
-                    return InetAddress.getByName("127.0.0.1");
-                } else if ("1.0.168.192.bl.spamcop.net.".equals(host)) {
-                    throw new UnknownHostException(host);
-                }
-                throw new UnsupportedOperationException("getByName("+host+") not implemented
in DNSRBLHandlerTest mock");
-            }
+            
         };
+       
         
     }
 
@@ -178,10 +174,9 @@ public class DNSRBLHandlerTest {
     // ip is blacklisted and has txt details
     @Test
     public void testBlackListedTextPresent() throws MailAddressException {
-        DNSRBLHandler rbl = new DNSRBLHandler();
+        DNSRBLHandler rbl = createHandler();
        
         setupMockedSMTPSession(new MailAddress("any@domain"));
-        rbl.setDNSService(mockedDnsServer);
 
         rbl.setBlacklist(new String[] { "bl.spamcop.net." });
         rbl.setGetDetail(true);
@@ -194,9 +189,8 @@ public class DNSRBLHandlerTest {
     // ip is blacklisted and has txt details but we don'T want to retrieve the txt record
     @Test
     public void testGetNoDetail() throws MailAddressException {
-        DNSRBLHandler rbl = new DNSRBLHandler();
+        DNSRBLHandler rbl = createHandler();
         setupMockedSMTPSession(new MailAddress("any@domain"));
-        rbl.setDNSService(mockedDnsServer);
 
         rbl.setBlacklist(new String[] { "bl.spamcop.net." });
         rbl.setGetDetail(false);
@@ -208,12 +202,10 @@ public class DNSRBLHandlerTest {
     // ip is allowed to relay
     @Test
     public void testRelayAllowed() throws MailAddressException {
-        DNSRBLHandler rbl = new DNSRBLHandler();
+        DNSRBLHandler rbl = createHandler();
         setRelayingAllowed(true);
         setupMockedSMTPSession(new MailAddress("any@domain"));
 
-        rbl.setDNSService(mockedDnsServer);
-
         rbl.setBlacklist(new String[] { "bl.spamcop.net." });
         rbl.setGetDetail(true);
         rbl.doRcpt(mockedSMTPSession, null, new MailAddress("test@localhost"));
@@ -224,13 +216,11 @@ public class DNSRBLHandlerTest {
     // ip not on blacklist
     @Test
     public void testNotBlackListed() throws MailAddressException {
-        DNSRBLHandler rbl = new DNSRBLHandler();
+        DNSRBLHandler rbl = createHandler();
 
         setRemoteIp("192.168.0.1");
         setupMockedSMTPSession(new MailAddress("any@domain"));
 
-        rbl.setDNSService(mockedDnsServer);
-
         rbl.setBlacklist(new String[] { "bl.spamcop.net." });
         rbl.setGetDetail(true);
         rbl.doRcpt(mockedSMTPSession, null, new MailAddress("test@localhost"));
@@ -241,13 +231,11 @@ public class DNSRBLHandlerTest {
     // ip on blacklist without txt details
     @Test
     public void testBlackListedNoTxt() throws MailAddressException {
-        DNSRBLHandler rbl = new DNSRBLHandler();
+        DNSRBLHandler rbl = createHandler();
 
         setRemoteIp("127.0.0.3");
         setupMockedSMTPSession(new MailAddress("any@domain"));
 
-        rbl.setDNSService(mockedDnsServer);
-
         rbl.setBlacklist(new String[] { "bl.spamcop.net." });
         rbl.setGetDetail(true);
         rbl.doRcpt(mockedSMTPSession, null, new MailAddress("test@localhost"));
@@ -258,13 +246,11 @@ public class DNSRBLHandlerTest {
     // ip on whitelist
     @Test
     public void testWhiteListed() throws MailAddressException {
-        DNSRBLHandler rbl = new DNSRBLHandler();
+        DNSRBLHandler rbl = createHandler();
 
         setRemoteIp("127.0.0.2");
         setupMockedSMTPSession(new MailAddress("any@domain"));
 
-        rbl.setDNSService(mockedDnsServer);
-
         rbl.setWhitelist(new String[] { "bl.spamcop.net." });
         rbl.setGetDetail(true);
         rbl.doRcpt(mockedSMTPSession, null, new MailAddress("test@localhost"));

Modified: james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/ResolvableEhloHeloHandlerTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/ResolvableEhloHeloHandlerTest.java?rev=1226220&r1=1226219&r2=1226220&view=diff
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/ResolvableEhloHeloHandlerTest.java
(original)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/ResolvableEhloHeloHandlerTest.java
Sun Jan  1 09:16:19 2012
@@ -30,9 +30,7 @@ import java.util.Map;
 import static junit.framework.Assert.*;
 
 import org.apache.james.protocols.api.ProtocolSession.State;
-import org.apache.james.protocols.smtp.BaseFakeDNSService;
 import org.apache.james.protocols.smtp.BaseFakeSMTPSession;
-import org.apache.james.protocols.smtp.DNSService;
 import org.apache.james.protocols.smtp.MailAddress;
 import org.apache.james.protocols.smtp.MailAddressException;
 import org.apache.james.protocols.smtp.SMTPSession;
@@ -112,24 +110,24 @@ public class ResolvableEhloHeloHandlerTe
         return session;
     }
     
-    private DNSService setupMockDNSServer() {
-    	DNSService dns = new BaseFakeDNSService(){
-            public InetAddress getByName(String host) throws UnknownHostException {
+    private ResolvableEhloHeloHandler createHandler() {
+        return new ResolvableEhloHeloHandler() {
+
+            @Override
+            protected String resolve(String host) throws UnknownHostException {
                 if (host.equals(INVALID_HOST)) 
                     throw new UnknownHostException();
-                return InetAddress.getLocalHost();
+                return InetAddress.getLocalHost().getHostName();
             }
+            
         };
-        
-        return dns;
     }
     
     @Test
     public void testRejectInvalidHelo() throws MailAddressException {
         MailAddress mailAddress = new MailAddress("test@localhost");
         SMTPSession session = setupMockSession(INVALID_HOST,false,false,null,mailAddress);
-        ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
-        handler.setDNSService(setupMockDNSServer());
+        ResolvableEhloHeloHandler handler = createHandler();
         
         handler.doHelo(session, INVALID_HOST);
         assertNotNull("Invalid HELO",session.getAttachment(ResolvableEhloHeloHandler.BAD_EHLO_HELO,
State.Transaction));
@@ -142,9 +140,8 @@ public class ResolvableEhloHeloHandlerTe
     public void testNotRejectValidHelo() throws MailAddressException {
         MailAddress mailAddress = new MailAddress("test@localhost");
         SMTPSession session = setupMockSession(VALID_HOST,false,false,null,mailAddress);
-        ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
-                
-        handler.setDNSService(setupMockDNSServer());
+        ResolvableEhloHeloHandler handler = createHandler();
+
   
         handler.doHelo(session, VALID_HOST);
         assertNull("Valid HELO",session.getAttachment(ResolvableEhloHeloHandler.BAD_EHLO_HELO,
State.Transaction));
@@ -157,9 +154,8 @@ public class ResolvableEhloHeloHandlerTe
     public void testRejectInvalidHeloAuthUser() throws MailAddressException {
         MailAddress mailAddress = new MailAddress("test@localhost");
         SMTPSession session = setupMockSession(INVALID_HOST,false,true,"valid@user",mailAddress);
-        ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
-                
-        handler.setDNSService(setupMockDNSServer());
+        ResolvableEhloHeloHandler handler = createHandler();
+
 
         handler.doHelo(session, INVALID_HOST);
         assertNotNull("Value stored",session.getAttachment(ResolvableEhloHeloHandler.BAD_EHLO_HELO,
State.Transaction));
@@ -174,10 +170,8 @@ public class ResolvableEhloHeloHandlerTe
     public void testRejectRelay() throws MailAddressException {
         MailAddress mailAddress = new MailAddress("test@localhost");
         SMTPSession session = setupMockSession(INVALID_HOST,true,false,null,mailAddress);
-        ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
-        
-        
-        handler.setDNSService(setupMockDNSServer());
+        ResolvableEhloHeloHandler handler = createHandler();
+
 
         handler.doHelo(session, INVALID_HOST);
         assertNotNull("Value stored",session.getAttachment(ResolvableEhloHeloHandler.BAD_EHLO_HELO,
State.Transaction));

Modified: james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/ValidSenderDomainHandlerTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/ValidSenderDomainHandlerTest.java?rev=1226220&r1=1226219&r2=1226220&view=diff
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/ValidSenderDomainHandlerTest.java
(original)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/protocols/smtp/core/fastfail/ValidSenderDomainHandlerTest.java
Sun Jan  1 09:16:19 2012
@@ -19,40 +19,32 @@
 
 package org.apache.james.protocols.smtp.core.fastfail;
 
-import java.util.ArrayList;
-import java.util.Collection;
+import static junit.framework.Assert.assertEquals;
+
 import java.util.HashMap;
 import java.util.Map;
 
-
-import static junit.framework.Assert.*;
-
 import org.apache.james.protocols.api.ProtocolSession.State;
-import org.apache.james.protocols.smtp.BaseFakeDNSService;
 import org.apache.james.protocols.smtp.BaseFakeSMTPSession;
-import org.apache.james.protocols.smtp.DNSService;
 import org.apache.james.protocols.smtp.MailAddress;
 import org.apache.james.protocols.smtp.MailAddressException;
 import org.apache.james.protocols.smtp.SMTPSession;
-import org.apache.james.protocols.smtp.core.fastfail.ValidSenderDomainHandler;
 import org.apache.james.protocols.smtp.hook.HookReturnCode;
 import org.junit.Test;
 
 public class ValidSenderDomainHandlerTest {
     
-    private DNSService setupDNSServer() {
-    	DNSService dns = new BaseFakeDNSService(){
-
-            public Collection<String> findMXRecords(String hostname) {
-                Collection<String> mx = new ArrayList<String>();
-                if (hostname.equals("test.james.apache.org")) {
-                    mx.add("mail.james.apache.org");
+    private ValidSenderDomainHandler createHandler() {
+        return new ValidSenderDomainHandler() {
+            
+            @Override
+            protected boolean hasMXRecord(SMTPSession session, String domain) {
+                if (domain.equals("test.james.apache.org")) {
+                    return true;
                 }
-                return mx;
+                return false;
             }
-            
         };
-        return dns;
     }
     
     private SMTPSession setupMockedSession(final MailAddress sender) {
@@ -106,8 +98,7 @@ public class ValidSenderDomainHandlerTes
     // Test for JAMES-580
     @Test
     public void testNullSenderNotReject() {
-        ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
-        handler.setDNSService(setupDNSServer());
+        ValidSenderDomainHandler handler = createHandler();
         int response = handler.doMail(setupMockedSession(null),null).getResult();
         
         assertEquals("Not blocked cause its a nullsender",response,HookReturnCode.DECLINED);
@@ -115,9 +106,8 @@ public class ValidSenderDomainHandlerTes
 
     @Test
     public void testInvalidSenderDomainReject() throws MailAddressException {
-        ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
+        ValidSenderDomainHandler handler = createHandler();
         SMTPSession session = setupMockedSession(new MailAddress("invalid@invalid"));
-        handler.setDNSService(setupDNSServer());
         int response = handler.doMail(session,(MailAddress) session.getAttachment(SMTPSession.SENDER,
State.Transaction)).getResult();
         
         assertEquals("Blocked cause we use reject action", response,HookReturnCode.DENY);



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


Mime
View raw message