james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r905220 [4/4] - in /james/protocols/trunk: ./ smtp/ smtp/src/ smtp/src/main/ smtp/src/main/java/ smtp/src/main/java/org/ smtp/src/main/java/org/apache/ smtp/src/main/java/org/apache/james/ smtp/src/main/java/org/apache/james/dsn/ smtp/src/m...
Date Mon, 01 Feb 2010 09:29:58 GMT
Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/SpamTrapHandler.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/SpamTrapHandler.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/SpamTrapHandler.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/SpamTrapHandler.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,119 @@
+/****************************************************************
+ * 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.smtpserver.protocol.core.fastfail;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.james.smtpserver.protocol.hook.HookResult;
+import org.apache.james.smtpserver.protocol.hook.HookReturnCode;
+import org.apache.james.smtpserver.protocol.hook.RcptHook;
+import org.apache.mailet.MailAddress;
+
+/**
+ * This handler can be used for providing a spam trap. IPAddresses which send emails to the configured
+ * recipients will get blacklisted for the configured time.
+ */
+public class SpamTrapHandler implements RcptHook {
+
+    /** Map which hold blockedIps and blockTime in memory */
+    private Map<String,Long> blockedIps = new HashMap<String,Long>();
+    
+    private Collection<String> spamTrapRecips = new ArrayList<String>();
+    
+    /** Default blocktime 12 hours */
+    protected long blockTime = 4320000; 
+    
+   
+    public void setSpamTrapRecipients(Collection<String> spamTrapRecips) {
+        this.spamTrapRecips = spamTrapRecips;
+    }
+    
+    public void setBlockTime(long blockTime) {
+        this.blockTime = blockTime;
+    }
+    
+    /**
+     * @see org.apache.james.smtpserver.protocol.hook.RcptHook#doRcpt(org.apache.james.smtpserver.protocol.SMTPSession, org.apache.mailet.MailAddress, org.apache.mailet.MailAddress)
+     */
+    public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt) {
+        if (isBlocked(session.getRemoteIPAddress(), session)) {
+            return new HookResult(HookReturnCode.DENY);
+        } else {
+         
+            if (spamTrapRecips.contains(rcpt.toString().toLowerCase())){
+        
+                addIp(session.getRemoteIPAddress(), session);
+            
+                return new HookResult(HookReturnCode.DENY);
+            }
+        }
+        return new HookResult(HookReturnCode.DECLINED);
+    }
+    
+    
+    /**
+     * Check if ipAddress is in the blockList.
+     * 
+     * @param ip ipAddress to check
+     * @param session not null
+     * @return true or false
+     */
+    private boolean isBlocked(String ip, SMTPSession session) {
+        Long rawTime = blockedIps.get(ip);
+    
+        if (rawTime != null) {
+            long blockTime = rawTime.longValue();
+           
+            if (blockTime > System.currentTimeMillis()) {
+                session.getLogger().debug("BlockList contain Ip " + ip);
+                return true;
+            } else {
+                session.getLogger().debug("Remove ip " + ip + " from blockList");
+               
+                synchronized(blockedIps) {
+                    blockedIps.remove(ip);
+                }
+            }
+        }
+        return false;
+    }
+    
+    /**
+     * Add ipaddress to blockList
+     * 
+     * @param ip IpAddress to add
+     * @param session not null
+     */
+    private void addIp(String ip, SMTPSession session) {
+        long bTime = System.currentTimeMillis() + blockTime;
+        
+        session.getLogger().debug("Add ip " + ip + " for " + bTime + " to blockList");
+    
+        synchronized(blockedIps) {
+            blockedIps.put(ip, new Long(bTime));
+        }
+    
+    }
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/SupressDuplicateRcptHandler.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/SupressDuplicateRcptHandler.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/SupressDuplicateRcptHandler.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/SupressDuplicateRcptHandler.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,61 @@
+/****************************************************************
+ * 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.smtpserver.protocol.core.fastfail;
+
+import java.util.Collection;
+
+import org.apache.james.dsn.DSNStatus;
+import org.apache.james.smtpserver.protocol.SMTPRetCode;
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.james.smtpserver.protocol.hook.HookResult;
+import org.apache.james.smtpserver.protocol.hook.HookReturnCode;
+import org.apache.james.smtpserver.protocol.hook.RcptHook;
+import org.apache.mailet.MailAddress;
+
+/**
+ * 
+ * This handler can be used to just ignore duplicated recipients. 
+ */
+public class SupressDuplicateRcptHandler implements RcptHook {
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.hook.RcptHook#doRcpt(org.apache.james.smtpserver.protocol.SMTPSession, org.apache.mailet.MailAddress, org.apache.mailet.MailAddress)
+     */
+    @SuppressWarnings("unchecked")
+    public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt) {
+        Collection rcptList = (Collection) session.getState().get(SMTPSession.RCPT_LIST);
+    
+        // Check if the recipient is already in the rcpt list
+        if(rcptList != null && rcptList.contains(rcpt)) {
+            StringBuilder responseBuffer = new StringBuilder();
+        
+            responseBuffer.append(DSNStatus.getStatus(DSNStatus.SUCCESS, DSNStatus.ADDRESS_VALID))
+                          .append(" Recipient <")
+                          .append(rcpt.toString())
+                          .append("> OK");
+            session.getLogger().debug("Duplicate recipient not add to recipient list: " + rcpt.toString());
+            return new HookResult(HookReturnCode.OK,SMTPRetCode.MAIL_OK, responseBuffer.toString());
+        }
+        return new HookResult(HookReturnCode.DECLINED);
+    }
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/TarpitHandler.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/TarpitHandler.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/TarpitHandler.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/TarpitHandler.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,78 @@
+/****************************************************************
+ * 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.smtpserver.protocol.core.fastfail;
+
+
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.james.smtpserver.protocol.hook.HookResult;
+import org.apache.james.smtpserver.protocol.hook.HookReturnCode;
+import org.apache.james.smtpserver.protocol.hook.RcptHook;
+import org.apache.mailet.MailAddress;
+
+/**
+ * Add tarpit support to SMTPServer. See http://www.palomine.net/qmail/tarpit.html for more information
+ *
+ */
+public class TarpitHandler implements RcptHook {
+
+    private int tarpitRcptCount = 0;
+
+    private long tarpitSleepTime = 5000;
+
+
+
+
+    /**
+     * Set the tarpit count after which the tarpit sleep time will be activated
+     * 
+     * @param tarpitRcptCount
+     */
+    public void setTarpitRcptCount(int tarpitRcptCount) {
+        this.tarpitRcptCount = tarpitRcptCount;
+    }
+
+    /**
+     * Set the tarpit sleep time
+     * 
+     * @param tarpitSleepTime
+     *            Time in milliseconds
+     */
+    public void setTarpitSleepTime(long tarpitSleepTime) {
+        this.tarpitSleepTime = tarpitSleepTime;
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.hook.RcptHook#doRcpt(org.apache.james.smtpserver.protocol.SMTPSession, org.apache.mailet.MailAddress, org.apache.mailet.MailAddress)
+     */
+    public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt) {
+
+        int rcptCount = 0;
+        rcptCount = session.getRcptCount();
+        rcptCount++;
+
+        if (rcptCount > tarpitRcptCount) {
+            session.sleep(tarpitSleepTime);
+        }
+        
+        return new HookResult(HookReturnCode.DECLINED);
+    }
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/ValidSenderDomainHandler.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/ValidSenderDomainHandler.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/ValidSenderDomainHandler.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/core/fastfail/ValidSenderDomainHandler.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,83 @@
+/****************************************************************
+ * 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.smtpserver.protocol.core.fastfail;
+
+import java.util.Collection;
+
+import org.apache.james.dsn.DSNStatus;
+import org.apache.james.smtpserver.protocol.DNSService;
+import org.apache.james.smtpserver.protocol.SMTPRetCode;
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.james.smtpserver.protocol.TemporaryResolutionException;
+import org.apache.james.smtpserver.protocol.hook.HookResult;
+import org.apache.james.smtpserver.protocol.hook.HookReturnCode;
+import org.apache.james.smtpserver.protocol.hook.MailHook;
+import org.apache.mailet.MailAddress;
+
+/**
+ * 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;
+    }
+    
+        
+
+
+    
+    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.smtpserver.protocol.hook.MailHook#doMail(org.apache.james.smtpserver.protocol.SMTPSession, org.apache.mailet.MailAddress)
+     */
+    public HookResult doMail(SMTPSession session, MailAddress sender) {
+        if (check(session,sender)) {
+            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 new HookResult(HookReturnCode.DECLINED);
+        }
+    }
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/AuthHook.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/AuthHook.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/AuthHook.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/AuthHook.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,37 @@
+/****************************************************************
+ * 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.smtpserver.protocol.hook;
+
+import org.apache.james.smtpserver.protocol.SMTPSession;
+
+/**
+ * Implement this interfaces to hook in the AUTH Command
+ */
+public interface AuthHook {
+
+    /**
+     * Return the HookResult after run the hook
+     * 
+     * @param session the SMTPSession
+     * @param username the username
+     * @param password the password
+     * @return HockResult
+     */
+    public HookResult doAuth(SMTPSession session, String username, String password);
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HeloHook.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HeloHook.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HeloHook.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HeloHook.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,41 @@
+/****************************************************************
+ * 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.smtpserver.protocol.hook;
+
+import org.apache.james.smtpserver.protocol.SMTPSession;
+
+/**
+ * Implement this interfaces to hook in the HELO Command
+ * 
+ */
+public interface HeloHook {
+
+    /**
+     * Return the HookResult after run the hook
+     * 
+     * @param session the SMTPSession
+     * @param helo the helo name
+     * @return HockResult
+     */
+    public HookResult doHelo(SMTPSession session, String helo);
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookResult.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookResult.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookResult.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookResult.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,90 @@
+/****************************************************************
+ * 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.smtpserver.protocol.hook;
+
+/**
+ * Result which get used for hooks
+ * 
+ */
+public class HookResult {
+
+    private int result;
+    private String smtpRetCode;
+    private String smtpDescription;
+    
+    /**
+     * Construct new HookResult
+     * 
+     * @param result
+     * @param smtpRetCode 
+     * @param smtpDescription
+     */
+    public HookResult(int result, String smtpRetCode, CharSequence smtpDescription) {
+        this.result = result;
+        this.smtpRetCode = smtpRetCode;
+        this.smtpDescription = (smtpDescription == null) ? null : smtpDescription.toString();
+    }
+    
+    /**
+     * Construct new HookResult
+     * 
+     * @param result
+     * @param smtpDescription
+     */
+    public HookResult(int result, String smtpDescription) {
+        this(result,null,smtpDescription);
+    }
+    
+    /**
+     * Construct new HookResult
+     * 
+     * @param result
+     */
+    public HookResult(int result) {
+        this(result,null,null);
+    }
+    
+    /**
+     * Return the result
+     * 
+     * @return result
+     */
+    public int getResult() {
+        return result;
+    }
+    
+    /**
+     * Return the SMTPRetCode which should used. If not set return null. 
+     * 
+     * @return smtpRetCode
+     */
+    public String getSmtpRetCode() {
+        return smtpRetCode;
+    }
+    
+    /**
+     * Return the SMTPDescription which should used. If not set return null
+     *  
+     * @return smtpDescription
+     */
+    public String getSmtpDescription() {
+        return smtpDescription;
+    }
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookResultHook.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookResultHook.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookResultHook.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookResultHook.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,28 @@
+/****************************************************************
+ * 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.smtpserver.protocol.hook;
+
+import org.apache.james.smtpserver.protocol.SMTPSession;
+
+public interface HookResultHook {
+    
+    public HookResult onHookResult(SMTPSession session,HookResult result, Object object);
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookReturnCode.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookReturnCode.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookReturnCode.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/HookReturnCode.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,30 @@
+/****************************************************************
+ * 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.smtpserver.protocol.hook;
+
+public class HookReturnCode {
+    public final static int OK = 0;
+    public final static int DENY = 1;
+    public final static int DENYSOFT = 2;
+    public final static int DECLINED = 3;
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MailHook.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MailHook.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MailHook.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MailHook.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,43 @@
+/****************************************************************
+ * 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.smtpserver.protocol.hook;
+
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.mailet.MailAddress;
+
+/**
+ * Implement this interfaces to hook in the MAIL Command
+ * 
+ */
+public interface MailHook {
+
+    /**
+     * Return the HookResult after run the hook
+     * 
+     * @param session the SMTPSession
+     * @param sender the sender MailAddress
+     * @return HockResult
+     */
+    public HookResult doMail(SMTPSession session, MailAddress sender);
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MailParametersHook.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MailParametersHook.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MailParametersHook.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MailParametersHook.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,43 @@
+/****************************************************************
+ * 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.smtpserver.protocol.hook;
+
+import org.apache.james.smtpserver.protocol.SMTPSession;
+
+/**
+ * Implement this interfaces to hook in the MAIL Command
+ * 
+ */
+public interface MailParametersHook {
+
+    /**
+     * Return the HookResult after run the hook
+     * 
+     * @param session the SMTPSession
+     * @param paramName parameter name
+     * @param paramValue parameter value
+     * @return HockResult
+     */
+    public HookResult doMailParameter(SMTPSession session, String paramName, String paramValue);
+    
+    /**
+     * @return an array of supported parameters
+     */
+    public String[] getMailParamNames();
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MessageHook.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MessageHook.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MessageHook.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/MessageHook.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,35 @@
+/****************************************************************
+ * 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.smtpserver.protocol.hook;
+
+import org.apache.james.smtpserver.protocol.MailEnvelope;
+import org.apache.james.smtpserver.protocol.SMTPSession;
+
+/**
+ * Custom message handlers must implement this interface The message hooks will
+ * be server-wide common to all the SMTPHandlers, therefore the handlers must
+ * store all the state information in the SMTPSession object
+ */
+public interface MessageHook {
+	/**
+	 * Handle Message
+	 */
+	HookResult onMessage(SMTPSession session, MailEnvelope mail);
+
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/QuitHook.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/QuitHook.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/QuitHook.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/QuitHook.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,40 @@
+/****************************************************************
+ * 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.smtpserver.protocol.hook;
+
+import org.apache.james.smtpserver.protocol.SMTPSession;
+
+/**
+ * Implement this interfaces to hook in the MAIL Command
+ * 
+ */
+public interface QuitHook {
+
+    /**
+     * Return the HookResult after run the hook
+     * 
+     * @param session the SMTPSession
+     * @return HockResult
+     */
+    public HookResult doQuit(SMTPSession session);
+}

Added: james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/RcptHook.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/RcptHook.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/RcptHook.java (added)
+++ james/protocols/trunk/smtp/src/main/java/org/apache/james/smtpserver/protocol/hook/RcptHook.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,39 @@
+/****************************************************************
+ * 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.smtpserver.protocol.hook;
+
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.mailet.MailAddress;
+
+/**
+ * Implement this interfaces to hook in the MAIL Command
+ */
+public interface RcptHook {
+    
+    /**
+     * Return the HookResult after run the hook
+     * 
+     * @param session the SMTPSession
+     * @param sender the sender MailAddress
+     * @param rcpt the recipient MailAddress
+     * @return HookResult
+     */
+    public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt);
+
+}

Added: james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/BaseFakeDNSService.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/BaseFakeDNSService.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/BaseFakeDNSService.java (added)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/BaseFakeDNSService.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,65 @@
+/****************************************************************
+ * 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.smtpserver.protocol;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.mailet.HostAddress;
+
+public class BaseFakeDNSService implements DNSService{
+
+    public Collection<String> findMXRecords(String hostname) throws TemporaryResolutionException {
+        throw new UnsupportedOperationException("Unimplemented in mock");
+
+    }
+
+    public Collection<String> findTXTRecords(String hostname) {
+        throw new UnsupportedOperationException("Unimplemented in mock");
+
+    }
+
+    public InetAddress[] getAllByName(String host) throws UnknownHostException {
+        throw new UnsupportedOperationException("Unimplemented in mock");
+
+    }
+
+    public InetAddress getByName(String host) throws UnknownHostException {
+        throw new UnsupportedOperationException("Unimplemented in mock");
+
+    }
+
+    public String getHostName(InetAddress addr) {
+        throw new UnsupportedOperationException("Unimplemented in mock");
+
+    }
+
+    public InetAddress getLocalHost() throws UnknownHostException {
+        throw new UnsupportedOperationException("Unimplemented in mock");
+
+    }
+
+    public Iterator<HostAddress> getSMTPHostAddresses(String domainName) throws TemporaryResolutionException {
+        throw new UnsupportedOperationException("Unimplemented in mock");
+
+    }
+
+}

Added: james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/BaseFakeSMTPSession.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/BaseFakeSMTPSession.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/BaseFakeSMTPSession.java (added)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/BaseFakeSMTPSession.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,201 @@
+/****************************************************************
+ * 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.smtpserver.protocol;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.james.protocols.api.LineHandler;
+import org.apache.james.protocols.api.Response;
+import org.apache.james.smtpserver.protocol.SMTPConfiguration;
+import org.apache.james.smtpserver.protocol.SMTPSession;
+
+/**
+ * Abstract class to simplify the mocks
+ */
+public class BaseFakeSMTPSession implements SMTPSession {
+
+    private static final Log log = LogFactory.getLog(BaseFakeSMTPSession.class);
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#getConfigurationData()
+     */
+    public SMTPConfiguration getConfigurationData() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#getConnectionState()
+     */
+    public Map<String, Object> getConnectionState() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#getRcptCount()
+     */
+    public int getRcptCount() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#getRemoteHost()
+     */
+    public String getRemoteHost() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#getRemoteIPAddress()
+     */
+    public String getRemoteIPAddress() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#getSessionID()
+     */
+    public String getSessionID() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#getState()
+     */
+    public Map<String, Object> getState() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#getUser()
+     */
+    public String getUser() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#isAuthSupported()
+     */
+    public boolean isAuthSupported() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#isRelayingAllowed()
+     */
+    public boolean isRelayingAllowed() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#resetState()
+     */
+    public void resetState() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#setRelayingAllowed(boolean)
+     */
+    public void setRelayingAllowed(boolean relayingAllowed) {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#setUser(java.lang.String)
+     */
+    public void setUser(String user) {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#popLineHandler()
+     */
+    public void popLineHandler() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#pushLineHandler(org.apache.james.api.protocol.LineHandler)
+     */
+    public void pushLineHandler(LineHandler<SMTPSession> overrideCommandHandler) {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see org.apache.james.api.protocol.LogEnabledSession#writeResponse(org.apache.james.api.protocol.Response)
+     */
+    public void writeResponse(Response response) {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#sleep(long)
+     */
+    public void sleep(long ms) {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+    }
+
+    public String getHelloName() {
+        return getConfigurationData().getHelloName();
+    }
+
+    public long getMaxMessageSize() {
+        return getConfigurationData().getMaxMessageSize();
+    }
+
+    public String getSMTPGreeting() {
+        return getConfigurationData().getSMTPGreeting();
+    }
+
+    public boolean useAddressBracketsEnforcement() {
+        return getConfigurationData().useAddressBracketsEnforcement();
+    }
+
+    public boolean useHeloEhloEnforcement() {
+        return getConfigurationData().useAddressBracketsEnforcement();
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.protocol.SMTPSession#getLogger()
+     */
+    public Log getLogger() {
+        return log;
+    }
+
+	public boolean isStartTLSSupported() {
+		return getConfigurationData().isStartTLSSupported();
+	}
+
+	public boolean isTLSStarted() {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+	}
+
+	public void startTLS() throws IOException {
+        throw new UnsupportedOperationException("Unimplemented Stub Method");
+
+	}
+}

Added: james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/DNSRBLHandlerTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/DNSRBLHandlerTest.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/DNSRBLHandlerTest.java (added)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/DNSRBLHandlerTest.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,240 @@
+/****************************************************************
+ * 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.smtpserver.protocol.core.fastfail;
+
+import java.net.InetAddress;
+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 javax.mail.internet.ParseException;
+
+import junit.framework.TestCase;
+
+import org.apache.james.smtpserver.protocol.BaseFakeDNSService;
+import org.apache.james.smtpserver.protocol.BaseFakeSMTPSession;
+import org.apache.james.smtpserver.protocol.DNSService;
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.mailet.MailAddress;
+
+public class DNSRBLHandlerTest extends TestCase {
+
+    private DNSService mockedDnsServer;
+
+    private SMTPSession mockedSMTPSession;
+
+    private String remoteIp = "127.0.0.2";
+
+    private boolean relaying = false;   
+    
+    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";
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        setupMockedDnsServer();
+        setRelayingAllowed(false);
+    }
+
+    /**
+     * Set the remoteIp
+     * 
+     * @param remoteIp The remoteIP to set
+     */
+    private void setRemoteIp(String remoteIp) {
+        this.remoteIp = remoteIp;
+    }
+
+    /**
+     * Set relayingAllowed
+     * 
+     * @param relaying true or false
+     */
+    private void setRelayingAllowed(boolean relaying) {
+        this.relaying = relaying;
+    }
+
+    /**
+     * Setup the mocked dnsserver
+     *
+     */
+    private void setupMockedDnsServer() {
+        mockedDnsServer  = new BaseFakeDNSService() {
+
+            public Collection<String> findTXTRecords(String hostname) {
+                List<String> res = new ArrayList<String>();
+                if (hostname == null) {
+                    return res;
+                }
+                ;
+                if ("2.0.0.127.bl.spamcop.net.".equals(hostname)) {
+                    res.add("Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2");
+                }
+                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");
+            }
+        };
+        
+    }
+
+    /**
+     * Setup mocked smtpsession
+     */
+    private void setupMockedSMTPSession(final MailAddress rcpt) {
+        mockedSMTPSession = new BaseFakeSMTPSession() {
+            HashMap<String,Object> state = new HashMap<String,Object>();
+            HashMap<String,Object> connectionState = new HashMap<String,Object>();
+            
+            public String getRemoteIPAddress() {
+                return remoteIp;
+            }
+
+            public Map<String,Object> getState() {
+                return state;
+            }
+
+            public boolean isRelayingAllowed() {
+                return relaying;
+            }
+
+            public boolean isAuthSupported() {
+                return false;
+            }
+
+            public int getRcptCount() {
+                return 0;
+            }
+
+            public Map<String,Object> getConnectionState() {       
+                return connectionState;
+            }
+
+        };
+    }
+
+    // ip is blacklisted and has txt details
+    public void testBlackListedTextPresent() throws ParseException {
+        DNSRBLHandler rbl = new DNSRBLHandler();
+       
+        setupMockedSMTPSession(new MailAddress("any@domain"));
+        rbl.setDNSService(mockedDnsServer);
+
+        rbl.setBlacklist(new String[] { "bl.spamcop.net." });
+        rbl.setGetDetail(true);
+        rbl.onConnect(mockedSMTPSession);
+        assertEquals("Details","Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2",
+               mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
+        assertNotNull("Blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
+    }
+
+    // ip is blacklisted and has txt details but we don'T want to retrieve the txt record
+    public void testGetNoDetail() throws ParseException {
+        DNSRBLHandler rbl = new DNSRBLHandler();
+        setupMockedSMTPSession(new MailAddress("any@domain"));
+        rbl.setDNSService(mockedDnsServer);
+
+        rbl.setBlacklist(new String[] { "bl.spamcop.net." });
+        rbl.setGetDetail(false);
+        rbl.onConnect(mockedSMTPSession);
+        assertNull("No details",mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
+        assertNotNull("Blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
+    }
+
+    // ip is allowed to relay
+    public void testRelayAllowed() throws ParseException {
+        DNSRBLHandler rbl = new DNSRBLHandler();
+        setRelayingAllowed(true);
+        setupMockedSMTPSession(new MailAddress("any@domain"));
+
+        rbl.setDNSService(mockedDnsServer);
+
+        rbl.setBlacklist(new String[] { "bl.spamcop.net." });
+        rbl.setGetDetail(true);
+        rbl.onConnect(mockedSMTPSession);
+        assertNull("No details",mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
+        assertNull("Not blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
+    }
+
+    // ip not on blacklist
+    public void testNotBlackListed() throws ParseException {
+        DNSRBLHandler rbl = new DNSRBLHandler();
+
+        setRemoteIp("192.168.0.1");
+        setupMockedSMTPSession(new MailAddress("any@domain"));
+
+        rbl.setDNSService(mockedDnsServer);
+
+        rbl.setBlacklist(new String[] { "bl.spamcop.net." });
+        rbl.setGetDetail(true);
+        rbl.onConnect(mockedSMTPSession);
+        assertNull("No details",mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
+        assertNull("Not blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
+    }
+
+    // ip on blacklist without txt details
+    public void testBlackListedNoTxt() throws ParseException {
+        DNSRBLHandler rbl = new DNSRBLHandler();
+
+        setRemoteIp("127.0.0.3");
+        setupMockedSMTPSession(new MailAddress("any@domain"));
+
+        rbl.setDNSService(mockedDnsServer);
+
+        rbl.setBlacklist(new String[] { "bl.spamcop.net." });
+        rbl.setGetDetail(true);
+        rbl.onConnect(mockedSMTPSession);
+        assertNull(mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
+        assertNotNull("Blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
+    }
+
+    // ip on whitelist
+    public void testWhiteListed() throws ParseException {
+        DNSRBLHandler rbl = new DNSRBLHandler();
+
+        setRemoteIp("127.0.0.2");
+        setupMockedSMTPSession(new MailAddress("any@domain"));
+
+        rbl.setDNSService(mockedDnsServer);
+
+        rbl.setWhitelist(new String[] { "bl.spamcop.net." });
+        rbl.setGetDetail(true);
+        rbl.onConnect(mockedSMTPSession);
+        assertNull(mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
+        assertNull("Not blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
+    }
+   
+
+}

Added: james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/MaxRcptHandlerTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/MaxRcptHandlerTest.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/MaxRcptHandlerTest.java (added)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/MaxRcptHandlerTest.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,83 @@
+/****************************************************************
+ * 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.smtpserver.protocol.core.fastfail;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.mail.internet.ParseException;
+
+import junit.framework.TestCase;
+
+import org.apache.james.smtpserver.protocol.BaseFakeSMTPSession;
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.james.smtpserver.protocol.core.fastfail.MaxRcptHandler;
+import org.apache.james.smtpserver.protocol.hook.HookReturnCode;
+import org.apache.mailet.MailAddress;
+
+
+
+public class MaxRcptHandlerTest extends TestCase{
+    
+    private SMTPSession setupMockedSession(final int rcptCount) {
+        SMTPSession session = new BaseFakeSMTPSession() {
+            HashMap<String,Object> state = new HashMap<String,Object>();
+
+            public Map<String,Object> getState() {
+                return state;
+            }
+            
+            public boolean isRelayingAllowed() {
+                return false;
+            }
+            
+            public int getRcptCount() {
+                return rcptCount;
+            }
+            
+        };
+        return session;
+    }
+    
+    public void testRejectMaxRcpt() throws ParseException {
+        SMTPSession session = setupMockedSession(3);
+        MaxRcptHandler handler = new MaxRcptHandler();
+        
+        handler.setMaxRcpt(2);
+        int resp = handler.doRcpt(session,null,new MailAddress("test@test")).getResult();
+    
+        assertEquals("Rejected.. To many recipients", resp, HookReturnCode.DENY);
+    }
+  
+    
+    public void testNotRejectMaxRcpt() throws ParseException {
+        SMTPSession session = setupMockedSession(3);
+        MaxRcptHandler handler = new MaxRcptHandler();    
+
+        handler.setMaxRcpt(4);
+        int resp = handler.doRcpt(session,null,new MailAddress("test@test")).getResult();
+        
+        assertEquals("Not Rejected..", resp, HookReturnCode.DECLINED);
+    }
+
+}

Added: james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/MockLog.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/MockLog.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/MockLog.java (added)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/MockLog.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,118 @@
+/****************************************************************
+ * 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.smtpserver.protocol.core.fastfail;
+
+import org.apache.commons.logging.Log;
+
+public class MockLog implements Log{
+
+    public void debug(Object arg0) {
+        System.out.println(arg0);
+    }
+
+    public void debug(Object arg0, Throwable arg1) {
+        System.out.println(arg0);
+        arg1.printStackTrace();
+
+
+    }
+
+    public void error(Object arg0) {
+        System.out.println(arg0);
+        
+    }
+
+    public void error(Object arg0, Throwable arg1) {
+        System.out.println(arg0);
+        arg1.printStackTrace();
+
+        
+    }
+
+    public void fatal(Object arg0) {
+        System.out.println(arg0);
+        
+    }
+
+    public void fatal(Object arg0, Throwable arg1) {
+        System.out.println(arg0);
+        arg1.printStackTrace();
+
+        
+    }
+
+    public void info(Object arg0) {
+        System.out.println(arg0);
+        
+    }
+
+    public void info(Object arg0, Throwable arg1) {
+        System.out.println(arg0);
+        arg1.printStackTrace();
+
+        
+    }
+
+    public boolean isDebugEnabled() {
+        return true;
+    }
+
+    public boolean isErrorEnabled() {
+        return true;
+    }
+
+    public boolean isFatalEnabled() {
+        return true;
+    }
+
+    public boolean isInfoEnabled() {
+        return true;
+    }
+
+    public boolean isTraceEnabled() {
+        return true;
+    }
+
+    public boolean isWarnEnabled() {
+        return true;
+    }
+
+    public void trace(Object arg0) {
+        System.out.println(arg0);        
+    }
+
+    public void trace(Object arg0, Throwable arg1) {
+        System.out.println(arg0);  
+        arg1.printStackTrace();
+
+    }
+
+    public void warn(Object arg0) {
+        System.out.println(arg0);
+        
+    }
+
+    public void warn(Object arg0, Throwable arg1) {
+        System.out.println(arg0);
+        arg1.printStackTrace();
+        
+    }
+
+}

Added: james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/ResolvableEhloHeloHandlerTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/ResolvableEhloHeloHandlerTest.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/ResolvableEhloHeloHandlerTest.java (added)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/ResolvableEhloHeloHandlerTest.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,156 @@
+/****************************************************************
+ * 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.smtpserver.protocol.core.fastfail;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.mail.internet.ParseException;
+
+import junit.framework.TestCase;
+
+import org.apache.james.smtpserver.protocol.BaseFakeDNSService;
+import org.apache.james.smtpserver.protocol.BaseFakeSMTPSession;
+import org.apache.james.smtpserver.protocol.DNSService;
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.james.smtpserver.protocol.core.fastfail.ResolvableEhloHeloHandler;
+import org.apache.james.smtpserver.protocol.hook.HookReturnCode;
+import org.apache.mailet.MailAddress;
+
+public class ResolvableEhloHeloHandlerTest extends TestCase {
+
+    public final static String INVALID_HOST = "foo.bar";
+    
+    public final static String VALID_HOST = "james.apache.org";
+
+
+    private SMTPSession setupMockSession(final String argument,
+             final boolean relaying, final boolean authRequired, final String user, final MailAddress recipient) {
+        
+        SMTPSession session = new BaseFakeSMTPSession() {
+
+            HashMap<String,Object> connectionMap = new HashMap<String,Object>();
+            HashMap<String,Object> map = new HashMap<String,Object>();
+            
+            public boolean isAuthSupported() {
+                return authRequired;
+            }
+            
+            public String getUser() {
+                return user;
+            }
+            
+            public Map<String,Object> getConnectionState() {
+                return connectionMap;
+            }
+            
+            public boolean isRelayingAllowed() {
+                return relaying;
+            }
+            
+            public Map<String,Object> getState() {
+                return map;
+            }
+            
+            
+        };
+
+        return session;
+    }
+    
+    private DNSService setupMockDNSServer() {
+    	DNSService dns = new BaseFakeDNSService(){
+            public InetAddress getByName(String host) throws UnknownHostException {
+                if (host.equals(INVALID_HOST)) 
+                    throw new UnknownHostException();
+                return InetAddress.getLocalHost();
+            }
+        };
+        
+        return dns;
+    }
+    
+    public void testRejectInvalidHelo() throws ParseException {
+        MailAddress mailAddress = new MailAddress("test@localhost");
+        SMTPSession session = setupMockSession(INVALID_HOST,false,false,null,mailAddress);
+        ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
+        handler.setDNSService(setupMockDNSServer());
+        
+        handler.doHelo(session, INVALID_HOST);
+        assertNotNull("Invalid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
+        
+        int result = handler.doRcpt(session,null, mailAddress).getResult();
+        assertEquals("Reject", result,HookReturnCode.DENY);
+    }
+    
+    
+    public void testNotRejectValidHelo() throws ParseException {
+        MailAddress mailAddress = new MailAddress("test@localhost");
+        SMTPSession session = setupMockSession(VALID_HOST,false,false,null,mailAddress);
+        ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
+                
+        handler.setDNSService(setupMockDNSServer());
+  
+        handler.doHelo(session, VALID_HOST);
+        assertNull("Valid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
+
+        int result = handler.doRcpt(session,null, mailAddress).getResult();
+        assertEquals("Not reject", result,HookReturnCode.DECLINED);
+    }
+   
+    
+    public void testRejectInvalidHeloAuthUser() throws ParseException {
+        MailAddress mailAddress = new MailAddress("test@localhost");
+        SMTPSession session = setupMockSession(INVALID_HOST,false,true,"valid@user",mailAddress);
+        ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
+                
+        handler.setDNSService(setupMockDNSServer());
+
+        handler.doHelo(session, INVALID_HOST);
+        assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
+        
+        
+        int result = handler.doRcpt(session,null, mailAddress).getResult();
+        assertEquals("Reject", result,HookReturnCode.DENY);
+    }
+    
+   
+    
+    public void testRejectRelay() throws ParseException {
+        MailAddress mailAddress = new MailAddress("test@localhost");
+        SMTPSession session = setupMockSession(INVALID_HOST,true,false,null,mailAddress);
+        ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
+        
+        
+        handler.setDNSService(setupMockDNSServer());
+
+        handler.doHelo(session, INVALID_HOST);
+        assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
+        
+        
+        int result = handler.doRcpt(session,null, mailAddress).getResult();
+        assertEquals("Reject", result,HookReturnCode.DENY);
+    }
+}
+    

Added: james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/SpamTrapHandlerTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/SpamTrapHandlerTest.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/SpamTrapHandlerTest.java (added)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/SpamTrapHandlerTest.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,86 @@
+/****************************************************************
+ * 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.smtpserver.protocol.core.fastfail;
+
+import java.util.ArrayList;
+
+import javax.mail.internet.ParseException;
+
+import org.apache.james.smtpserver.protocol.BaseFakeSMTPSession;
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.james.smtpserver.protocol.core.fastfail.SpamTrapHandler;
+import org.apache.james.smtpserver.protocol.hook.HookReturnCode;
+import org.apache.mailet.MailAddress;
+
+import junit.framework.TestCase;
+
+public class SpamTrapHandlerTest extends TestCase {
+    private static final String SPAM_TRAP_RECIP1 = "spamtrap1@localhost";
+    private static final String RECIP1 = "recip@localhost";
+    
+    private SMTPSession setUpSMTPSession(final String ip) {
+        return new BaseFakeSMTPSession() {
+            public String getRemoteIPAddress() {
+                return ip;
+            }
+        
+        };
+    }
+    
+    public void testSpamTrap() throws ParseException {
+        String ip = "192.168.100.1";
+        String ip2 = "192.168.100.2";
+        long blockTime = 2000;
+    
+        ArrayList<String> rcpts = new ArrayList<String>();
+        rcpts.add(SPAM_TRAP_RECIP1);
+    
+        SpamTrapHandler handler = new SpamTrapHandler();
+    
+        handler.setBlockTime(blockTime);
+        handler.setSpamTrapRecipients(rcpts);
+    
+        int result = handler.doRcpt(setUpSMTPSession(ip),null,new MailAddress(SPAM_TRAP_RECIP1)).getResult();
+    
+        assertEquals("Blocked on first connect",HookReturnCode.DENY,result);
+    
+
+        result = handler.doRcpt(setUpSMTPSession(ip),null,new MailAddress(RECIP1)).getResult();
+    
+        assertEquals("Blocked on second connect", HookReturnCode.DENY,result);
+    
+        
+        result = handler.doRcpt(setUpSMTPSession(ip2),null,new MailAddress(RECIP1)).getResult();
+    
+        assertEquals("Not Blocked", HookReturnCode.DECLINED,result);
+    
+        try {
+            // Wait for the blockTime to exceed
+            Thread.sleep(blockTime);
+        } catch (InterruptedException e) {
+            fail("Failed to sleep for " + blockTime +" ms");
+        }
+    
+        result = handler.doRcpt(setUpSMTPSession(ip),null,new MailAddress(RECIP1)).getResult();
+    
+        assertEquals("Not blocked. BlockTime exceeded", HookReturnCode.DECLINED,result); 
+    }
+}

Added: james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/TarpitHandlerTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/TarpitHandlerTest.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/TarpitHandlerTest.java (added)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/TarpitHandlerTest.java Mon Feb  1 09:29:55 2010
@@ -0,0 +1,75 @@
+/****************************************************************
+ * 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.smtpserver.protocol.core.fastfail;
+
+import javax.mail.internet.ParseException;
+
+import junit.framework.TestCase;
+
+import org.apache.james.smtpserver.protocol.BaseFakeSMTPSession;
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.mailet.MailAddress;
+
+public class TarpitHandlerTest extends TestCase {
+
+    private SMTPSession session;
+
+    private SMTPSession setupMockedSession(final int rcptCount) {
+        session = new BaseFakeSMTPSession() {
+
+            public int getRcptCount() {
+                return rcptCount;
+            }
+            
+            public void sleep(long ms) {
+                try {
+                    Thread.sleep(1000);
+                } catch (InterruptedException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+            }
+
+        };
+
+        return session;
+    }
+
+    public void testTarpit() throws ParseException {
+        long tarpitTime = 1000;
+        long tarpitTolerance = 100;
+        long startTime;
+        TarpitHandler handler = new TarpitHandler();
+        
+        handler.setTarpitRcptCount(2);
+        handler.setTarpitSleepTime(tarpitTime);
+
+        // no tarpit used
+        startTime = System.currentTimeMillis();
+        handler.doRcpt(setupMockedSession(0),null,new MailAddress("test@test"));
+        assertTrue("No tarpit",
+                (System.currentTimeMillis() - startTime) < tarpitTime - tarpitTolerance);
+
+        // tarpit used
+        startTime = System.currentTimeMillis();
+        handler.doRcpt(setupMockedSession(3),null,new MailAddress("test@test"));
+        assertTrue("tarpit",
+                (System.currentTimeMillis() - startTime) >= tarpitTime - tarpitTolerance);
+    }
+}

Added: james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/ValidSenderDomainHandlerTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/ValidSenderDomainHandlerTest.java?rev=905220&view=auto
==============================================================================
--- james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/ValidSenderDomainHandlerTest.java (added)
+++ james/protocols/trunk/smtp/src/test/java/org/apache/james/smtpserver/protocol/core/fastfail/ValidSenderDomainHandlerTest.java Mon Feb  1 09:29:55 2010
@@ -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.smtpserver.protocol.core.fastfail;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.mail.internet.ParseException;
+
+import junit.framework.TestCase;
+
+import org.apache.james.smtpserver.protocol.BaseFakeDNSService;
+import org.apache.james.smtpserver.protocol.BaseFakeSMTPSession;
+import org.apache.james.smtpserver.protocol.DNSService;
+import org.apache.james.smtpserver.protocol.SMTPSession;
+import org.apache.james.smtpserver.protocol.core.fastfail.ValidSenderDomainHandler;
+import org.apache.james.smtpserver.protocol.hook.HookReturnCode;
+import org.apache.mailet.MailAddress;
+
+public class ValidSenderDomainHandlerTest extends TestCase {
+    
+    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");
+                }
+                return mx;
+            }
+            
+        };
+        return dns;
+    }
+    
+    private SMTPSession setupMockedSession(final MailAddress sender) {
+        SMTPSession session = new BaseFakeSMTPSession() {
+            HashMap<String,Object> state = new HashMap<String,Object>();
+
+            public Map<String,Object> getState() {
+
+                state.put(SMTPSession.SENDER, sender);
+
+                return state;
+            }
+            
+            public boolean isRelayingAllowed() {
+                return false;
+            }
+
+            
+        };
+        return session;
+    }
+    
+    
+    // Test for JAMES-580
+    public void testNullSenderNotReject() {
+        ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
+        handler.setDNSService(setupDNSServer());
+        int response = handler.doMail(setupMockedSession(null),null).getResult();
+        
+        assertEquals("Not blocked cause its a nullsender",response,HookReturnCode.DECLINED);
+    }
+
+    public void testInvalidSenderDomainReject() throws ParseException {
+        ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
+        SMTPSession session = setupMockedSession(new MailAddress("invalid@invalid"));
+        handler.setDNSService(setupDNSServer());
+        int response = handler.doMail(session,(MailAddress) session.getState().get(SMTPSession.SENDER)).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