You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2006/12/25 19:39:59 UTC
svn commit: r490160 [2/2] - in
/james/server/sandbox/handlerapi-experiment/src:
java/org/apache/james/smtpserver/
java/org/apache/james/smtpserver/core/filter/fastfail/
java/org/apache/james/util/ test/org/apache/james/smtpserver/
Modified: james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java?view=diff&rev=490160&r1=490159&r2=490160
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java (original)
+++ james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidRcptMX.java Mon Dec 25 10:39:58 2006
@@ -1,186 +1,186 @@
-/****************************************************************
- * 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.core.filter.fastfail;
-
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-
-
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.service.ServiceException;
-import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.Serviceable;
-import org.apache.james.dnsserver.TemporaryResolutionException;
-import org.apache.james.services.DNSServer;
-import org.apache.james.smtpserver.CommandHandler;
-import org.apache.james.smtpserver.SMTPResponse;
-import org.apache.james.smtpserver.SMTPSession;
-import org.apache.james.util.NetMatcher;
-import org.apache.james.util.mail.dsn.DSNStatus;
-import org.apache.mailet.MailAddress;
-
-/**
- * This class can be used to reject email with bogus MX which is send from a authorized user or an authorized
- * network.
- */
-public class ValidRcptMX extends AbstractJunkHandler implements CommandHandler,
- Serviceable {
-
- private DNSServer dnsServer = null;
-
- private static final String LOCALHOST = "localhost";
-
- private NetMatcher bNetwork = null;
-
- /**
- * @see org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#configure(org.apache.avalon.framework.configuration.Configuration)
- */
- public void configure(Configuration arg0) throws ConfigurationException {
-
- Configuration[] badMX = arg0.getChildren("invalidMXNetworks");
-
- if (badMX.length != 0) {
-
- Collection bannedNetworks = new ArrayList();
-
- for (int i = 0; i < badMX.length; i++) {
- String network = badMX[i].getValue(null);
-
- if (network != null) {
- bannedNetworks.add(network);
- }
- }
-
- setBannedNetworks(bannedNetworks, dnsServer);
-
- getLogger().info("Invalid MX Networks: " + bNetwork.toString());
-
- } else {
- throw new ConfigurationException(
- "Please configure at least on invalid MX network");
- }
-
- super.configure(arg0);
- }
-
- /**
- * Set the banned networks
- *
- * @param networks Collection of networks
- * @param dnsServer The DNSServer
- */
- public void setBannedNetworks(Collection networks, DNSServer dnsServer) {
- bNetwork = new NetMatcher(networks, dnsServer) {
- protected void log(String s) {
- getLogger().debug(s);
- }
-
- };
- }
-
- /**
- * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
- */
- public void service(ServiceManager arg0) throws ServiceException {
- setDNSServer((DNSServer) arg0.lookup(DNSServer.ROLE));
- }
-
- /**
- * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
- */
- public Collection getImplCommands() {
- Collection c = new ArrayList();
- c.add("RCPT");
- return c;
- }
-
- /**
- * @see org.apache.james.smtpserver.CommandHandler#onCommand(org.apache.james.smtpserver.SMTPSession, java.lang.String, java.lang.String)
- */
- public SMTPResponse onCommand(SMTPSession session, String command, String parameters) {
- return doProcessing(session);
- }
-
- /**
- * Set the DNSServer
- *
- * @param dnsServer
- * The dnsServer
- */
- public void setDNSServer(DNSServer dnsServer) {
- this.dnsServer = dnsServer;
- }
-
- /**
- * @see org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#check(org.apache.james.smtpserver.SMTPSession)
- */
- protected boolean check(SMTPSession session) {
- MailAddress rcpt = (MailAddress) session.getState().get(SMTPSession.CURRENT_RECIPIENT);
-
- String domain = rcpt.getHost();
-
- // Email should be deliver local
- if (domain.equals(LOCALHOST)) return false;
-
- Iterator mx = null;
- try {
- mx = dnsServer.findMXRecords(domain).iterator();
- } catch (TemporaryResolutionException e1) {
- // TODO: Should we reject temporary ?
- }
-
- if (mx != null && mx.hasNext()) {
- while (mx.hasNext()) {
- String mxRec = mx.next().toString();
-
- try {
- String ip = dnsServer.getByName(mxRec).getHostAddress();
-
- // Check for invalid MX
- if (bNetwork.matchInetNetwork(ip)) {
- return true;
- }
- } catch (UnknownHostException e) {
- // Ignore this
- }
- }
- }
- return false;
- }
-
- /**
- * @see org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getJunkHandlerData(org.apache.james.smtpserver.SMTPSession)
- */
- public JunkHandlerData getJunkHandlerData(SMTPSession session) {
- MailAddress rcpt = (MailAddress) session.getState().get(SMTPSession.CURRENT_RECIPIENT);
- JunkHandlerData data = new JunkHandlerData();
-
- data.setRejectResponseString("530" + DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.SECURITY_AUTH) + " Invalid MX " + session.getRemoteIPAddress()
- + " for domain " + rcpt.getHost() + ". Reject email");
-
- data.setJunkScoreLogString("Invalid MX " + session.getRemoteIPAddress() + " for domain " + rcpt.getHost() + ". Add JunkScore: " + getScore());
- data.setRejectLogString("Invalid MX " + session.getRemoteIPAddress() + " for domain " + rcpt.getHost() + ". Reject email");
- data.setScoreName("ValidRcptMXCheck");
- return data;
- }
-}
+/****************************************************************
+ * 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.core.filter.fastfail;
+
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.james.dnsserver.TemporaryResolutionException;
+import org.apache.james.services.DNSServer;
+import org.apache.james.smtpserver.CommandHandler;
+import org.apache.james.smtpserver.SMTPResponse;
+import org.apache.james.smtpserver.SMTPSession;
+import org.apache.james.util.NetMatcher;
+import org.apache.james.util.mail.dsn.DSNStatus;
+import org.apache.mailet.MailAddress;
+
+/**
+ * This class can be used to reject email with bogus MX which is send from a authorized user or an authorized
+ * network.
+ */
+public class ValidRcptMX extends AbstractJunkHandler implements CommandHandler,
+ Serviceable {
+
+ private DNSServer dnsServer = null;
+
+ private static final String LOCALHOST = "localhost";
+
+ private NetMatcher bNetwork = null;
+
+ /**
+ * @see org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#configure(org.apache.avalon.framework.configuration.Configuration)
+ */
+ public void configure(Configuration arg0) throws ConfigurationException {
+
+ Configuration[] badMX = arg0.getChildren("invalidMXNetworks");
+
+ if (badMX.length != 0) {
+
+ Collection bannedNetworks = new ArrayList();
+
+ for (int i = 0; i < badMX.length; i++) {
+ String network = badMX[i].getValue(null);
+
+ if (network != null) {
+ bannedNetworks.add(network);
+ }
+ }
+
+ setBannedNetworks(bannedNetworks, dnsServer);
+
+ getLogger().info("Invalid MX Networks: " + bNetwork.toString());
+
+ } else {
+ throw new ConfigurationException(
+ "Please configure at least on invalid MX network");
+ }
+
+ super.configure(arg0);
+ }
+
+ /**
+ * Set the banned networks
+ *
+ * @param networks Collection of networks
+ * @param dnsServer The DNSServer
+ */
+ public void setBannedNetworks(Collection networks, DNSServer dnsServer) {
+ bNetwork = new NetMatcher(networks, dnsServer) {
+ protected void log(String s) {
+ getLogger().debug(s);
+ }
+
+ };
+ }
+
+ /**
+ * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
+ */
+ public void service(ServiceManager arg0) throws ServiceException {
+ setDNSServer((DNSServer) arg0.lookup(DNSServer.ROLE));
+ }
+
+ /**
+ * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
+ */
+ public Collection getImplCommands() {
+ Collection c = new ArrayList();
+ c.add("RCPT");
+ return c;
+ }
+
+ /**
+ * @see org.apache.james.smtpserver.CommandHandler#onCommand(org.apache.james.smtpserver.SMTPSession, java.lang.String, java.lang.String)
+ */
+ public SMTPResponse onCommand(SMTPSession session, String command, String parameters) {
+ return doProcessing(session);
+ }
+
+ /**
+ * Set the DNSServer
+ *
+ * @param dnsServer
+ * The dnsServer
+ */
+ public void setDNSServer(DNSServer dnsServer) {
+ this.dnsServer = dnsServer;
+ }
+
+ /**
+ * @see org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#check(org.apache.james.smtpserver.SMTPSession)
+ */
+ protected boolean check(SMTPSession session) {
+ MailAddress rcpt = (MailAddress) session.getState().get(SMTPSession.CURRENT_RECIPIENT);
+
+ String domain = rcpt.getHost();
+
+ // Email should be deliver local
+ if (domain.equals(LOCALHOST)) return false;
+
+ Iterator mx = null;
+ try {
+ mx = dnsServer.findMXRecords(domain).iterator();
+ } catch (TemporaryResolutionException e1) {
+ // TODO: Should we reject temporary ?
+ }
+
+ if (mx != null && mx.hasNext()) {
+ while (mx.hasNext()) {
+ String mxRec = mx.next().toString();
+
+ try {
+ String ip = dnsServer.getByName(mxRec).getHostAddress();
+
+ // Check for invalid MX
+ if (bNetwork.matchInetNetwork(ip)) {
+ return true;
+ }
+ } catch (UnknownHostException e) {
+ // Ignore this
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * @see org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getJunkHandlerData(org.apache.james.smtpserver.SMTPSession)
+ */
+ public JunkHandlerData getJunkHandlerData(SMTPSession session) {
+ MailAddress rcpt = (MailAddress) session.getState().get(SMTPSession.CURRENT_RECIPIENT);
+ JunkHandlerData data = new JunkHandlerData();
+
+ data.setRejectResponseString("530" + DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.SECURITY_AUTH) + " Invalid MX " + session.getRemoteIPAddress()
+ + " for domain " + rcpt.getHost() + ". Reject email");
+
+ data.setJunkScoreLogString("Invalid MX " + session.getRemoteIPAddress() + " for domain " + rcpt.getHost() + ". Add JunkScore: " + getScore());
+ data.setRejectLogString("Invalid MX " + session.getRemoteIPAddress() + " for domain " + rcpt.getHost() + ". Reject email");
+ data.setScoreName("ValidRcptMXCheck");
+ return data;
+ }
+}
Modified: james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/util/CRLFDelimitedByteBuffer.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/util/CRLFDelimitedByteBuffer.java?view=diff&rev=490160&r1=490159&r2=490160
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/util/CRLFDelimitedByteBuffer.java (original)
+++ james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/util/CRLFDelimitedByteBuffer.java Mon Dec 25 10:39:58 2006
@@ -58,258 +58,258 @@
*/
public class CRLFDelimitedByteBuffer {
- static public class TerminationException extends java.io.IOException {
- private int where;
- public TerminationException(int where) {
- super();
- this.where = where;
- }
-
- public TerminationException(String s, int where) {
- super(s);
- this.where = where;
- }
-
- public int position() {
- return where;
- }
- }
-
- static public class LineLengthExceededException extends java.io.IOException {
- public LineLengthExceededException(String s) {
- super(s);
- }
- }
-
- public CRLFDelimitedByteBuffer() {
- this(2048);
- }
-
- public CRLFDelimitedByteBuffer(int maxLineLength) {
- lines = new java.util.ArrayList();
- workLine = new byte[maxLineLength];
- }
-
- synchronized public boolean isEmpty() {
- return lines.isEmpty();
- }
-
- synchronized public byte[] read() throws LineLengthExceededException, TerminationException {
- return lines.isEmpty() ? null : ((Line) lines.remove(0)).getBytes();
- }
-
- synchronized public String readString() throws LineLengthExceededException, TerminationException {
- if (lines.isEmpty()) return null;
- else {
- byte[] bytes = ((Line) lines.remove(0)).getBytes();
- try {
- return new String(bytes, "US-ASCII");
- } catch (java.io.UnsupportedEncodingException uee) {
- return new String(bytes);
- }
- }
- }
-
- synchronized public void write(byte[] data, int length) {
- if (canFit(length)) {
- System.arraycopy(data, 0, workLine, writeindex, length);
- writeindex += length;
- buildlines();
- }
- }
-
- synchronized public void write(byte data) {
- if (canFit(1)) {
- workLine[writeindex++] = data;
- buildlines();
- }
- }
-
- synchronized public void write(String s) {
- write(s.getBytes(), s.getBytes().length);
- }
-
- private boolean canFit(int length) {
- if (writeindex + length > workLine.length) {
- reset();
- lines.add(new Line(new LineLengthExceededException("Exceeded maximum line length")));
- return false;
- } else return true;
- }
-
- static private class Line {
- java.io.IOException e;
- byte[] bytes;
-
- public Line(byte[] data) {
- bytes = data;
- }
-
- public Line(String data) {
- bytes = data.getBytes();
- }
-
- public Line(java.io.IOException e) {
- this.e = e;
- }
-
- public Line(byte[] data, int offset, int length) {
- bytes = new byte[length];
- System.arraycopy(data, offset, bytes, 0, length);
- }
-
- public byte[] getBytes() throws LineLengthExceededException, TerminationException {
- if (e != null) {
- if (e instanceof LineLengthExceededException) throw (LineLengthExceededException) e;
- else throw (TerminationException) e;
- }
- return bytes;
- }
- }
-
- private java.util.ArrayList lines;
-
- private byte[] workLine;
- private int writeindex = 0;
- private int readindex = 0;
- private int scanindex = 0; // current index for matching within the working buffer
-
- private void reset() {
- writeindex = 0;
- readindex = 0;
- scanindex = 0;
- }
-
- private void shift() {
- System.arraycopy(workLine, readindex, workLine, 0, writeindex - readindex);
- writeindex -= readindex;
- scanindex -= readindex;
- readindex = 0;
- }
-
- private void buildlines() {
- for (; scanindex < writeindex; scanindex++) {
- if (workLine[scanindex] == '\n') {
- final int pos = scanindex;
- reset();
- lines.add(new Line(new TerminationException("\"bare\" LF in data stream.", pos)));
- break;
- } else if (workLine[scanindex] == '\r') {
- if (scanindex+1 == writeindex) break;
- else if (workLine[++scanindex] == '\n') {
- lines.add(new Line(workLine, readindex, scanindex - readindex + 1));
- readindex = scanindex + 1;
- } else {
- final int pos = scanindex - 1;
- reset();
- lines.add(new Line(new TerminationException("\"bare\" CR in data stream.", pos)));
- break;
- }
- }
- }
-
- if (readindex != 0) shift();
- }
-
- /*** THE CODE BELOW IS PURELY FOR TESTING ***/
- /*
- synchronized private void status() {
- System.out.println("\n--------------------------------------------------\n");
- if (lines.isEmpty()) System.out.println("Lines: None");
- else {
- System.out.println("Lines:");
- java.util.Iterator i = lines.iterator();
- while (i.hasNext()) {
- Line line = (Line) i.next();
- try {
- System.out.println("\tData[" + line.getBytes().length + "]: " + new String(line.getBytes(), 0, line.getBytes().length, "US-ASCII"));
- } catch (java.io.UnsupportedEncodingException uee) {
- } catch (TerminationException te) {
- System.out.println("\tSyntax error at character position " + te.position() + ". CR and LF must be CRLF paired. See RFC 2821 #2.7.1.");
- } catch (LineLengthExceededException llee) {
- System.out.println("\tLine length exceeded. See RFC 2821 #4.5.3.1.");
- }
- }
- }
-
- System.out.println("Buffer Status:");
- System.out.println("\tworkline length: " + workLine.length);
- System.out.println("\tnext read index: " + readindex);
- System.out.println("\tnext scan index: " + scanindex);
- System.out.println("\tnext write index: " + writeindex);
-
- try {
- System.out.println("\tOld data: " + new String(workLine, 0, readindex, "US-ASCII"));
- System.out.println("\tData: " + new String(workLine, readindex, writeindex - readindex, "US-ASCII"));
- } catch (java.io.UnsupportedEncodingException uee) {
- System.err.println(uee);
- }
- System.out.println("\n--------------------------------------------------\n");
- }
-
-
- static public void main(String[] args) throws java.io.IOException {
- CRLFDelimitedByteBuffer dbb = new CRLFDelimitedByteBuffer();
- dbb.status();
- dbb.write("Hello"); dbb.status();
- dbb.write(" "); dbb.status();
- dbb.write("World."); dbb.status();
- dbb.write("\r"); dbb.status();
- dbb.write("\n"); dbb.status();
- dbb.write("\r\n"); dbb.status();
- dbb.write("\n"); dbb.status();
- dbb.write("\r\r"); dbb.status();
- dbb.write("stuff\n"); dbb.status();
- dbb.write("morestuff\r\r"); dbb.status();
- for (int i = 0; i < 2500; i++) dbb.write("\0"); dbb.status();
-
- while (!dbb.isEmpty()) {
- try {
- byte[] line = dbb.read();
- System.out.println("Read line[" + line.length + "]: " + new String(line, 0, line.length, "US-ASCII"));
- } catch (java.io.UnsupportedEncodingException uee) {
- } catch (TerminationException te) {
- System.out.println("Syntax error at character position " + te.position() + ". CR and LF must be CRLF paired. See RFC 2821 #2.7.1.");
- } catch (LineLengthExceededException llee) {
- System.out.println("Line length exceeded. See RFC 2821 #4.5.3.1.");
- }
- }
- dbb.status();
-
- dbb.write("This is a test.\015\012.... Three dots\015\012.\015\012");
- dbb.status();
-
- while (!dbb.isEmpty()) {
- try {
- byte[] line = dbb.read();
- System.out.println("Read line[" + line.length + "]: " + new String(line, 0, line.length, "US-ASCII"));
- } catch (java.io.UnsupportedEncodingException uee) {
- } catch (TerminationException te) {
- System.out.println("Syntax error at character position " + te.position() + ". CR and LF must be CRLF paired. See RFC 2821 #2.7.1.");
- } catch (LineLengthExceededException llee) {
- System.out.println("Line length exceeded. See RFC 2821 #4.5.3.1.");
- }
- }
- dbb.status();
-
- dbb.write("This is"); dbb.status();
- dbb.write(" a tes"); dbb.status();
- dbb.write("t.\015"); dbb.status();
- dbb.write("\012..."); dbb.status();
- dbb.write(". Three dot"); dbb.status();
- dbb.write("s\015\012.\015\012"); dbb.status();
-
- while (!dbb.isEmpty()) {
- try {
- String text = dbb.readString();
- System.out.println ("read : " + text);
- dbb.status();
- } catch (TerminationException te) {
- System.out.println("Syntax error at character position " + te.position() + ". CR and LF must be CRLF paired. See RFC 2821 #2.7.1.");
- } catch (LineLengthExceededException llee) {
- System.out.println("Line length exceeded. See RFC 2821 #4.5.3.1.");
- }
- }
- }
- */
+ static public class TerminationException extends java.io.IOException {
+ private int where;
+ public TerminationException(int where) {
+ super();
+ this.where = where;
+ }
+
+ public TerminationException(String s, int where) {
+ super(s);
+ this.where = where;
+ }
+
+ public int position() {
+ return where;
+ }
+ }
+
+ static public class LineLengthExceededException extends java.io.IOException {
+ public LineLengthExceededException(String s) {
+ super(s);
+ }
+ }
+
+ public CRLFDelimitedByteBuffer() {
+ this(2048);
+ }
+
+ public CRLFDelimitedByteBuffer(int maxLineLength) {
+ lines = new java.util.ArrayList();
+ workLine = new byte[maxLineLength];
+ }
+
+ synchronized public boolean isEmpty() {
+ return lines.isEmpty();
+ }
+
+ synchronized public byte[] read() throws LineLengthExceededException, TerminationException {
+ return lines.isEmpty() ? null : ((Line) lines.remove(0)).getBytes();
+ }
+
+ synchronized public String readString() throws LineLengthExceededException, TerminationException {
+ if (lines.isEmpty()) return null;
+ else {
+ byte[] bytes = ((Line) lines.remove(0)).getBytes();
+ try {
+ return new String(bytes, "US-ASCII");
+ } catch (java.io.UnsupportedEncodingException uee) {
+ return new String(bytes);
+ }
+ }
+ }
+
+ synchronized public void write(byte[] data, int length) {
+ if (canFit(length)) {
+ System.arraycopy(data, 0, workLine, writeindex, length);
+ writeindex += length;
+ buildlines();
+ }
+ }
+
+ synchronized public void write(byte data) {
+ if (canFit(1)) {
+ workLine[writeindex++] = data;
+ buildlines();
+ }
+ }
+
+ synchronized public void write(String s) {
+ write(s.getBytes(), s.getBytes().length);
+ }
+
+ private boolean canFit(int length) {
+ if (writeindex + length > workLine.length) {
+ reset();
+ lines.add(new Line(new LineLengthExceededException("Exceeded maximum line length")));
+ return false;
+ } else return true;
+ }
+
+ static private class Line {
+ java.io.IOException e;
+ byte[] bytes;
+
+ public Line(byte[] data) {
+ bytes = data;
+ }
+
+ public Line(String data) {
+ bytes = data.getBytes();
+ }
+
+ public Line(java.io.IOException e) {
+ this.e = e;
+ }
+
+ public Line(byte[] data, int offset, int length) {
+ bytes = new byte[length];
+ System.arraycopy(data, offset, bytes, 0, length);
+ }
+
+ public byte[] getBytes() throws LineLengthExceededException, TerminationException {
+ if (e != null) {
+ if (e instanceof LineLengthExceededException) throw (LineLengthExceededException) e;
+ else throw (TerminationException) e;
+ }
+ return bytes;
+ }
+ }
+
+ private java.util.ArrayList lines;
+
+ private byte[] workLine;
+ private int writeindex = 0;
+ private int readindex = 0;
+ private int scanindex = 0; // current index for matching within the working buffer
+
+ private void reset() {
+ writeindex = 0;
+ readindex = 0;
+ scanindex = 0;
+ }
+
+ private void shift() {
+ System.arraycopy(workLine, readindex, workLine, 0, writeindex - readindex);
+ writeindex -= readindex;
+ scanindex -= readindex;
+ readindex = 0;
+ }
+
+ private void buildlines() {
+ for (; scanindex < writeindex; scanindex++) {
+ if (workLine[scanindex] == '\n') {
+ final int pos = scanindex;
+ reset();
+ lines.add(new Line(new TerminationException("\"bare\" LF in data stream.", pos)));
+ break;
+ } else if (workLine[scanindex] == '\r') {
+ if (scanindex+1 == writeindex) break;
+ else if (workLine[++scanindex] == '\n') {
+ lines.add(new Line(workLine, readindex, scanindex - readindex + 1));
+ readindex = scanindex + 1;
+ } else {
+ final int pos = scanindex - 1;
+ reset();
+ lines.add(new Line(new TerminationException("\"bare\" CR in data stream.", pos)));
+ break;
+ }
+ }
+ }
+
+ if (readindex != 0) shift();
+ }
+
+ /*** THE CODE BELOW IS PURELY FOR TESTING ***/
+ /*
+ synchronized private void status() {
+ System.out.println("\n--------------------------------------------------\n");
+ if (lines.isEmpty()) System.out.println("Lines: None");
+ else {
+ System.out.println("Lines:");
+ java.util.Iterator i = lines.iterator();
+ while (i.hasNext()) {
+ Line line = (Line) i.next();
+ try {
+ System.out.println("\tData[" + line.getBytes().length + "]: " + new String(line.getBytes(), 0, line.getBytes().length, "US-ASCII"));
+ } catch (java.io.UnsupportedEncodingException uee) {
+ } catch (TerminationException te) {
+ System.out.println("\tSyntax error at character position " + te.position() + ". CR and LF must be CRLF paired. See RFC 2821 #2.7.1.");
+ } catch (LineLengthExceededException llee) {
+ System.out.println("\tLine length exceeded. See RFC 2821 #4.5.3.1.");
+ }
+ }
+ }
+
+ System.out.println("Buffer Status:");
+ System.out.println("\tworkline length: " + workLine.length);
+ System.out.println("\tnext read index: " + readindex);
+ System.out.println("\tnext scan index: " + scanindex);
+ System.out.println("\tnext write index: " + writeindex);
+
+ try {
+ System.out.println("\tOld data: " + new String(workLine, 0, readindex, "US-ASCII"));
+ System.out.println("\tData: " + new String(workLine, readindex, writeindex - readindex, "US-ASCII"));
+ } catch (java.io.UnsupportedEncodingException uee) {
+ System.err.println(uee);
+ }
+ System.out.println("\n--------------------------------------------------\n");
+ }
+
+
+ static public void main(String[] args) throws java.io.IOException {
+ CRLFDelimitedByteBuffer dbb = new CRLFDelimitedByteBuffer();
+ dbb.status();
+ dbb.write("Hello"); dbb.status();
+ dbb.write(" "); dbb.status();
+ dbb.write("World."); dbb.status();
+ dbb.write("\r"); dbb.status();
+ dbb.write("\n"); dbb.status();
+ dbb.write("\r\n"); dbb.status();
+ dbb.write("\n"); dbb.status();
+ dbb.write("\r\r"); dbb.status();
+ dbb.write("stuff\n"); dbb.status();
+ dbb.write("morestuff\r\r"); dbb.status();
+ for (int i = 0; i < 2500; i++) dbb.write("\0"); dbb.status();
+
+ while (!dbb.isEmpty()) {
+ try {
+ byte[] line = dbb.read();
+ System.out.println("Read line[" + line.length + "]: " + new String(line, 0, line.length, "US-ASCII"));
+ } catch (java.io.UnsupportedEncodingException uee) {
+ } catch (TerminationException te) {
+ System.out.println("Syntax error at character position " + te.position() + ". CR and LF must be CRLF paired. See RFC 2821 #2.7.1.");
+ } catch (LineLengthExceededException llee) {
+ System.out.println("Line length exceeded. See RFC 2821 #4.5.3.1.");
+ }
+ }
+ dbb.status();
+
+ dbb.write("This is a test.\015\012.... Three dots\015\012.\015\012");
+ dbb.status();
+
+ while (!dbb.isEmpty()) {
+ try {
+ byte[] line = dbb.read();
+ System.out.println("Read line[" + line.length + "]: " + new String(line, 0, line.length, "US-ASCII"));
+ } catch (java.io.UnsupportedEncodingException uee) {
+ } catch (TerminationException te) {
+ System.out.println("Syntax error at character position " + te.position() + ". CR and LF must be CRLF paired. See RFC 2821 #2.7.1.");
+ } catch (LineLengthExceededException llee) {
+ System.out.println("Line length exceeded. See RFC 2821 #4.5.3.1.");
+ }
+ }
+ dbb.status();
+
+ dbb.write("This is"); dbb.status();
+ dbb.write(" a tes"); dbb.status();
+ dbb.write("t.\015"); dbb.status();
+ dbb.write("\012..."); dbb.status();
+ dbb.write(". Three dot"); dbb.status();
+ dbb.write("s\015\012.\015\012"); dbb.status();
+
+ while (!dbb.isEmpty()) {
+ try {
+ String text = dbb.readString();
+ System.out.println ("read : " + text);
+ dbb.status();
+ } catch (TerminationException te) {
+ System.out.println("Syntax error at character position " + te.position() + ". CR and LF must be CRLF paired. See RFC 2821 #2.7.1.");
+ } catch (LineLengthExceededException llee) {
+ System.out.println("Line length exceeded. See RFC 2821 #4.5.3.1.");
+ }
+ }
+ }
+ */
}
Modified: james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ResolvableEhloHeloHandlerTest.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ResolvableEhloHeloHandlerTest.java?view=diff&rev=490160&r1=490159&r2=490160
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ResolvableEhloHeloHandlerTest.java (original)
+++ james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ResolvableEhloHeloHandlerTest.java Mon Dec 25 10:39:58 2006
@@ -44,28 +44,8 @@
public final static String INVALID_HOST = "foo.bar";
public final static String VALID_HOST = "james.apache.org";
-
- public final static String HELO = "HELO";
-
- public final static String RCPT = "RCPT";
-
- private String response = null;
-
- private String command = null;
-
- public void setUp() {
- response = null;
- command = null;
- }
-
- private String getResponse() {
- return response;
- }
-
- private void setCommand(String command) {
- this.command = command;
- }
-
+
+
private SMTPSession setupMockSession(final String argument,
final boolean relaying, final boolean authRequired, final String user, final MailAddress recipient) {
@@ -79,10 +59,6 @@
return argument;
}
- public String getCommandName() {
- return command;
- }
-
public boolean isAuthRequired() {
return authRequired;
}
@@ -95,10 +71,6 @@
return connectionMap;
}
- public void writeResponse(String resp) {
- response = resp;
- }
-
public boolean isRelayingAllowed() {
return relaying;
}
@@ -142,16 +114,11 @@
handler.setDnsServer(setupMockDNSServer());
- // helo
- setCommand(HELO);
- handler.onCommand(session);
+ handler.onCommand(session, "HELO", INVALID_HOST);
assertNotNull("Invalid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
-
- // rcpt
- setCommand(RCPT);
- handler.onCommand(session);
- assertNotNull("Reject", getResponse());
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+ assertNotNull("Reject", response);
assertTrue("Stop handler processing",session.getStopHandlerProcessing());
}
@@ -164,17 +131,12 @@
ContainerUtil.enableLogging(handler,new MockLogger());
handler.setDnsServer(setupMockDNSServer());
-
- // helo
- setCommand(HELO);
- handler.onCommand(session);
+
+ handler.onCommand(session, "HELO", VALID_HOST);
assertNull("Valid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
-
-
- // rcpt
- setCommand(RCPT);
- handler.onCommand(session);
- assertNull("Not reject", getResponse());
+
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+ assertNull("Not reject", response);
assertFalse("Not stop handler processing",session.getStopHandlerProcessing());
}
@@ -187,16 +149,12 @@
handler.setDnsServer(setupMockDNSServer());
- // helo
- setCommand(HELO);
- handler.onCommand(session);
+ handler.onCommand(session,"HELO", INVALID_HOST);
assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
-
-
- // rcpt
- setCommand(RCPT);
- handler.onCommand(session);
- assertNull("Not reject", getResponse());
+
+
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+ assertNull("Not reject", response);
assertFalse("Not stop handler processing",session.getStopHandlerProcessing());
}
@@ -209,17 +167,13 @@
handler.setDnsServer(setupMockDNSServer());
handler.setCheckAuthUsers(true);
-
- // helo
- setCommand(HELO);
- handler.onCommand(session);
+
+ handler.onCommand(session,"HELO", INVALID_HOST);
assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
- // rcpt
- setCommand(RCPT);
- handler.onCommand(session);
- assertNotNull("reject", getResponse());
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+ assertNotNull("reject", response);
assertTrue("stop handler processing",session.getStopHandlerProcessing());
}
@@ -232,16 +186,12 @@
handler.setDnsServer(setupMockDNSServer());
- // helo
- setCommand(HELO);
- handler.onCommand(session);
+
+ handler.onCommand(session, "HELO", INVALID_HOST);
assertNull("Value not stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
-
-
- // rcpt
- setCommand(RCPT);
- handler.onCommand(session);
- assertNull("Not reject", getResponse());
+
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+ assertNull("Not reject", response);
assertFalse("Not stop handler processing",session.getStopHandlerProcessing());
}
@@ -254,17 +204,13 @@
handler.setDnsServer(setupMockDNSServer());
handler.setCheckAuthNetworks(true);
-
- // helo
- setCommand(HELO);
- handler.onCommand(session);
+
+ handler.onCommand(session,"HELO", INVALID_HOST);
assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
- // rcpt
- setCommand(RCPT);
- handler.onCommand(session);
- assertNotNull("Reject", getResponse());
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+ assertNotNull("Reject", response);
assertTrue("Stop handler processing",session.getStopHandlerProcessing());
}
@@ -277,16 +223,12 @@
handler.setDnsServer(setupMockDNSServer());
- // helo
- setCommand(HELO);
- handler.onCommand(session);
+ handler.onCommand(session, "HELO", INVALID_HOST);
assertNotNull("stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
- // rcpt
- setCommand(RCPT);
- handler.onCommand(session);
- assertNull("Not Reject", getResponse());
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+ assertNull("Not Reject", response);
assertFalse("Not stop handler processing",session.getStopHandlerProcessing());
}
@@ -299,16 +241,12 @@
handler.setDnsServer(setupMockDNSServer());
- // helo
- setCommand(HELO);
- handler.onCommand(session);
+ handler.onCommand(session, "HELO", INVALID_HOST);
assertNotNull("stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
- // rcpt
- setCommand(RCPT);
- handler.onCommand(session);
- assertNull("Not Reject", getResponse());
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+ assertNull("Not Reject", response);
assertFalse("Not stop handler processing",session.getStopHandlerProcessing());
}
@@ -324,16 +262,13 @@
handler.setAction("junkScore");
handler.setScore(20);
- // helo
- setCommand(HELO);
- handler.onCommand(session);
+
+ handler.onCommand(session, "HELO", INVALID_HOST);
assertNotNull("Invalid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
- // rcpt
- setCommand(RCPT);
- handler.onCommand(session);
- assertNull("Not Reject", getResponse());
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+ assertNull("Not Reject", response);
assertFalse("Don'T stop handler processing",session.getStopHandlerProcessing());
assertEquals("JunkScore added", ((JunkScore) session.getConnectionState().get(JunkScore.JUNK_SCORE_SESSION)).getStoredScore("ResolvableEhloHeloCheck"), 20.0, 0d);
Modified: james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/SPFHandlerTest.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/SPFHandlerTest.java?view=diff&rev=490160&r1=490159&r2=490160
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/SPFHandlerTest.java (original)
+++ james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/SPFHandlerTest.java Mon Dec 25 10:39:58 2006
@@ -189,20 +189,16 @@
return connectionState;
}
- public void resetConnectionState() {
- connectionState.clear();
- }
-
};
}
private void runHandlers(SPFHandler spf, SMTPSession mockedSMTPSession) {
+ MailAddress sender = (MailAddress) mockedSMTPSession.getState().get(SMTPSession.SENDER);
+ MailAddress rcpt = (MailAddress) mockedSMTPSession.getState().get(SMTPSession.CURRENT_RECIPIENT);
- setCommand("MAIL");
- spf.onCommand(mockedSMTPSession);
+ spf.onCommand(mockedSMTPSession, "MAIL", "<"+ sender + ">");
- setCommand("RCPT");
- spf.onCommand(mockedSMTPSession);
+ spf.onCommand(mockedSMTPSession,"RCPT","<" + rcpt + ">");
spf.onMessage(mockedSMTPSession);
}
@@ -303,11 +299,7 @@
spf.setBlockSoftFail(true);
- setCommand("MAIL");
- spf.onCommand(mockedSMTPSession);
-
- setCommand("RCPT");
- spf.onCommand(mockedSMTPSession);
+ runHandlers(spf, mockedSMTPSession);
assertNotNull("reject", mockedSMTPSession.getState().get(
SPFHandler.SPF_BLOCKLISTED));
Modified: james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/TarpitHandlerTest.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/TarpitHandlerTest.java?view=diff&rev=490160&r1=490159&r2=490160
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/TarpitHandlerTest.java (original)
+++ james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/TarpitHandlerTest.java Mon Dec 25 10:39:58 2006
@@ -53,13 +53,13 @@
// no tarpit used
startTime = System.currentTimeMillis();
- handler.onCommand(setupMockedSession(0));
+ handler.onCommand(setupMockedSession(0),"RCPT","<te...@test>");
assertTrue("No tarpit",
(System.currentTimeMillis() - startTime) < tarpitTime - tarpitTolerance);
// tarpit used
startTime = System.currentTimeMillis();
- handler.onCommand(setupMockedSession(3));
+ handler.onCommand(setupMockedSession(3),"RCPT","<te...@test>");
assertTrue("tarpit",
(System.currentTimeMillis() - startTime) >= tarpitTime - tarpitTolerance);
}
Modified: james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java?view=diff&rev=490160&r1=490159&r2=490160
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java (original)
+++ james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java Mon Dec 25 10:39:58 2006
@@ -56,7 +56,7 @@
private static final String URISERVER = "multi.surbl.org.";
private SMTPSession mockedSMTPSession;
- private String response = null;
+ private SMTPResponse response = null;
public void setUp() {
// reset reponse
@@ -104,8 +104,8 @@
this.relayingAllowed = relayingAllowed;
}
- public void writeResponse(String respString) {
- response = respString;
+ public void writeSMTPResponse(SMTPResponse resp) {
+ response = resp;
}
public void setStopHandlerProcessing(boolean processing) {
@@ -121,7 +121,7 @@
}
- private String getResponse() {
+ private SMTPResponse getResponse() {
return response;
}
Modified: james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidRcptHandlerTest.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidRcptHandlerTest.java?view=diff&rev=490160&r1=490159&r2=490160
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidRcptHandlerTest.java (original)
+++ james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidRcptHandlerTest.java Mon Dec 25 10:39:58 2006
@@ -47,13 +47,8 @@
private final static String INVALID_USER = "invalid";
private final static String USER1 = "user1";
private final static String USER2 = "user2";
- private String response = null;
private MockServiceManager serviceMan;
- public void setUp() {
- response = null;
- }
-
private SMTPSession setupMockedSMTPSession(final SMTPHandlerConfigurationData conf, final MailAddress rcpt, final boolean relayingAllowed, final boolean authRequired, final String username) {
SMTPSession session = new AbstractSMTPSession() {
HashMap state = new HashMap();
@@ -81,10 +76,6 @@
return state;
}
- public void writeResponse(String resp) {
- response = resp;
- }
-
public void setStopHandlerProcessing(boolean stop) {
this.stop = stop;
}
@@ -183,7 +174,7 @@
SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(INVALID_USER + "@localhost"),false,false,null);
ContainerUtil.enableLogging(handler,new MockLogger());
- handler.onCommand(session);
+ SMTPResponse response = handler.onCommand(session,"RCPT","<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
assertTrue("Rejected",session.getStopHandlerProcessing());
assertNotNull("Rejected",response);
@@ -195,7 +186,7 @@
SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(INVALID_USER + "@localhost"),false,true,"authedUser");
ContainerUtil.enableLogging(handler,new MockLogger());
- handler.onCommand(session);
+ SMTPResponse response = handler.onCommand(session,"RCPT","<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
assertFalse("Not rejected",session.getStopHandlerProcessing());
assertNull("Not rejected",response);
@@ -206,8 +197,9 @@
ContainerUtil.service(handler, setUpServiceManager());
SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(INVALID_USER + "@localhost"),true,false,null);
ContainerUtil.enableLogging(handler,new MockLogger());
+
+ SMTPResponse response = handler.onCommand(session,"RCPT","<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
- handler.onCommand(session);
assertFalse("Not rejected",session.getStopHandlerProcessing());
assertNull("Not rejected",response);
@@ -219,7 +211,9 @@
SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(VALID_USER + "@localhost"),false,false,null);
ContainerUtil.enableLogging(handler,new MockLogger());
- handler.onCommand(session);
+
+ SMTPResponse response = handler.onCommand(session,"RCPT","<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+
assertFalse("Not rejected",session.getStopHandlerProcessing());
assertNull("Not rejected",response);
@@ -233,8 +227,9 @@
ContainerUtil.enableLogging(handler,new MockLogger());
handler.setValidRecipients(recipient);
- handler.onCommand(session);
-
+
+ SMTPResponse response = handler.onCommand(session,"RCPT","<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+
assertFalse("Not rejected",session.getStopHandlerProcessing());
assertNull("Not rejected",response);
@@ -250,8 +245,9 @@
ContainerUtil.enableLogging(handler,new MockLogger());
handler.setValidDomains(domain);
- handler.onCommand(session);
-
+
+ SMTPResponse response = handler.onCommand(session,"RCPT","<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+
assertFalse("Not rejected",session.getStopHandlerProcessing());
assertNull("Not rejected",response);
@@ -267,8 +263,9 @@
ContainerUtil.enableLogging(handler,new MockLogger());
handler.setValidRegex("reci.*");
- handler.onCommand(session);
-
+
+ SMTPResponse response = handler.onCommand(session,"RCPT","<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+
assertFalse("Not rejected",session.getStopHandlerProcessing());
assertNull("Not rejected",response);
@@ -295,7 +292,9 @@
ValidRcptHandler handler = new ValidRcptHandler();
ContainerUtil.service(handler, setUpServiceManager());
ContainerUtil.enableLogging(handler,new MockLogger());
- handler.onCommand(session);
+
+ SMTPResponse response = handler.onCommand(session,"RCPT","<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+
assertNull("No reject",response);
assertFalse("Not stop processing",session.getStopHandlerProcessing());
@@ -307,10 +306,11 @@
ValidRcptHandler handler = new ValidRcptHandler();
ContainerUtil.service(handler, setUpServiceManager());
ContainerUtil.enableLogging(handler,new MockLogger());
- handler.onCommand(session);
-
- assertNull("Valid Error mapping",session.getState().get("VALID_USER"));
- assertNotNull("Error mapping",response);
- assertTrue("Stop processing",session.getStopHandlerProcessing());
+
+ SMTPResponse response = handler.onCommand(session,"RCPT","<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT) + ">");
+
+ assertNull("Valid Error mapping",session.getState().get("VALID_USER"));
+ assertNotNull("Error mapping",response);
+ assertTrue("Stop processing",session.getStopHandlerProcessing());
}
}
Modified: james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidRcptMXTest.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidRcptMXTest.java?view=diff&rev=490160&r1=490159&r2=490160
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidRcptMXTest.java (original)
+++ james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidRcptMXTest.java Mon Dec 25 10:39:58 2006
@@ -40,7 +40,6 @@
import junit.framework.TestCase;
public class ValidRcptMXTest extends TestCase {
- private String response = null;
private final static String INVALID_HOST = "invalid.host.de";
@@ -48,12 +47,6 @@
private final static String LOOPBACK = "127.0.0.1";
- protected void setUp() throws Exception {
- response = null;
-
- super.setUp();
- }
-
private SMTPSession setupMockedSMTPSession(final MailAddress rcpt) {
SMTPSession session = new AbstractSMTPSession() {
HashMap state = new HashMap();
@@ -63,10 +56,6 @@
state.put(SMTPSession.CURRENT_RECIPIENT, rcpt);
return state;
}
-
- public void writeResponse(String resp) {
- response = resp;
- }
public String getRemoteIPAddress() {
return "127.0.0.1";
@@ -111,13 +100,14 @@
bNetworks.add("127.0.0.1");
DNSServer dns = setupMockedDNSServer();
+ SMTPSession session = setupMockedSMTPSession(new MailAddress("test@" + INVALID_HOST));
ValidRcptMX handler = new ValidRcptMX();
ContainerUtil.enableLogging(handler, new MockLogger());
handler.setDNSServer(dns);
handler.setBannedNetworks(bNetworks, dns);
- handler.onCommand(setupMockedSMTPSession(new MailAddress("test@" + INVALID_HOST)));
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT + ">"));
assertNotNull("Reject", response);
}
@@ -139,7 +129,8 @@
handler.setDNSServer(dns);
handler.setBannedNetworks(bNetworks, dns);
- handler.onCommand(session);
+ SMTPResponse response = handler.onCommand(session, "RCPT", "<" + session.getState().get(SMTPSession.CURRENT_RECIPIENT + ">"));
+
assertNull("Not Reject", response);
assertEquals("JunkScore added",((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("ValidRcptMXCheck"),20.0, 0d);
Modified: james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java
URL: http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java?view=diff&rev=490160&r1=490159&r2=490160
==============================================================================
--- james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java (original)
+++ james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java Mon Dec 25 10:39:58 2006
@@ -39,12 +39,6 @@
public class ValidSenderDomainHandlerTest extends TestCase {
- private String response = null;
-
- public void setUp() {
- response = null;
- }
-
private DNSServer setupDNSServer() {
DNSServer dns = new AbstractDNSServer(){
@@ -76,10 +70,6 @@
return false;
}
- public void writeResponse(String resp) {
- response = resp;
- }
-
public void setStopHandlerProcessing(boolean processing) {
this.processing = processing;
}
@@ -92,9 +82,6 @@
return session;
}
- private String getResponse() {
- return response;
- }
// Test for JAMES-580
public void testNullSenderNotReject() {
@@ -102,9 +89,9 @@
ContainerUtil.enableLogging(handler, new MockLogger());
handler.setDnsServer(setupDNSServer());
- handler.onCommand(setupMockedSession(null));
+ SMTPResponse response = handler.onCommand(setupMockedSession(null),"MAIL","<>");
- assertNull("Not blocked cause its a nullsender",getResponse());
+ assertNull("Not blocked cause its a nullsender",response);
}
public void testInvalidSenderDomainAddJunkScore() throws ParseException {
@@ -115,9 +102,9 @@
handler.setAction("JunkScore");
handler.setScore(20);
handler.setDnsServer(setupDNSServer());
- handler.onCommand(session);
+ SMTPResponse response = handler.onCommand(session,"MAIL","<" + session.getState().get(SMTPSession.SENDER) + ">");
- assertNull("Not blocked cause we use JunkScore",getResponse());
+ assertNull("Not blocked cause we use JunkScore", response);
assertEquals("JunkScore is stored",((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("ValidSenderDomainCheck"),20.0,0d);
}
@@ -126,8 +113,8 @@
SMTPSession session = setupMockedSession(new MailAddress("invalid@invalid"));
ContainerUtil.enableLogging(handler, new MockLogger());
handler.setDnsServer(setupDNSServer());
- handler.onCommand(session);
+ SMTPResponse response = handler.onCommand(session,"MAIL","<" + session.getState().get(SMTPSession.SENDER) + ">");
- assertNotNull("Blocked cause we use reject action",getResponse());
+ assertNotNull("Blocked cause we use reject action", response);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org