james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1099013 - in /james/server/trunk/pop3server/src: main/java/org/apache/james/pop3server/core/ test/java/org/apache/james/pop3server/
Date Tue, 03 May 2011 11:28:02 GMT
Author: norman
Date: Tue May  3 11:28:01 2011
New Revision: 1099013

URL: http://svn.apache.org/viewvc?rev=1099013&view=rev
Log:
CRLFTerminatedInputStream did not respect the buffer offset in all case which could block
the RETRCmdHandler forever. See JAMES-1222

Added:
    james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/AbstractInputStreamTest.java
Modified:
    james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/CRLFTerminatedInputStream.java
    james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/CRLFTerminatedInputStreamTest.java
    james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/ExtraDotInputStreamTest.java

Modified: james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/CRLFTerminatedInputStream.java
URL: http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/CRLFTerminatedInputStream.java?rev=1099013&r1=1099012&r2=1099013&view=diff
==============================================================================
--- james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/CRLFTerminatedInputStream.java
(original)
+++ james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/CRLFTerminatedInputStream.java
Tue May  3 11:28:01 2011
@@ -53,7 +53,9 @@ public class CRLFTerminatedInputStream e
 
                 return fillArray(b, off, len);
             } else {
-                last = b[r - 1];
+                // Make sure we respect the offset. Otherwise it could let the RETRCmdHandler
+                // hang forever. See JAMES-1222
+                last = b[off + r - 1];
                 return r;
             }
         } else {

Added: james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/AbstractInputStreamTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/AbstractInputStreamTest.java?rev=1099013&view=auto
==============================================================================
--- james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/AbstractInputStreamTest.java
(added)
+++ james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/AbstractInputStreamTest.java
Tue May  3 11:28:01 2011
@@ -0,0 +1,41 @@
+package org.apache.james.pop3server;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import junit.framework.TestCase;
+
+
+public abstract class AbstractInputStreamTest extends TestCase{
+
+    protected void checkRead(InputStream in, String expected) throws IOException {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        int i = -1;
+        while ((i = in.read()) != -1) {
+            out.write(i);
+        }
+        in.close();
+        out.close();        
+        assertEquals(expected, new String(out.toByteArray()));
+
+        
+        
+    }
+    
+    protected void checkReadViaArray(InputStream in, String expected) throws IOException
{
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+        byte[] buf = new byte[3];
+        int i = 0;
+        while ((i = in.read(buf)) != -1) {
+            out.write(buf, 0, i);
+        }
+        
+       
+        in.close();
+        out.close();
+        assertEquals(expected, new String(out.toByteArray()));
+        
+        
+    }
+}

Modified: james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/CRLFTerminatedInputStreamTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/CRLFTerminatedInputStreamTest.java?rev=1099013&r1=1099012&r2=1099013&view=diff
==============================================================================
--- james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/CRLFTerminatedInputStreamTest.java
(original)
+++ james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/CRLFTerminatedInputStreamTest.java
Tue May  3 11:28:01 2011
@@ -20,68 +20,32 @@
 package org.apache.james.pop3server;
 
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 
 import org.apache.james.pop3server.core.CRLFTerminatedInputStream;
 
-import junit.framework.TestCase;
-
-public class CRLFTerminatedInputStreamTest extends TestCase {
+public class CRLFTerminatedInputStreamTest extends AbstractInputStreamTest {
 
     public void testCRLFPresent() throws IOException {
         String data = "Subject: test\r\n\r\ndata\r\n";
-        check(data, data);
-        checkWithArray(data, data);
+        checkRead(new CRLFTerminatedInputStream(new ByteArrayInputStream(data.getBytes())),
data);
+        checkReadViaArray(new CRLFTerminatedInputStream(new ByteArrayInputStream(data.getBytes())),
data);
 
     }
 
     public void testCRPresent() throws IOException {
         String data = "Subject: test\r\n\r\ndata\r";
         String expected = data + "\n";
-        check(data, expected);
-        checkWithArray(data, expected);
+        checkRead(new CRLFTerminatedInputStream(new ByteArrayInputStream(data.getBytes())),
expected);
+        checkReadViaArray(new CRLFTerminatedInputStream(new ByteArrayInputStream(data.getBytes())),
expected);
     }
 
     public void testNonPresent() throws IOException {
         String data = "Subject: test\r\n\r\ndata";
         String expected = data + "\r\n";
-        check(data, expected);
-        checkWithArray(data, expected);
-
-    }
-
-    private void check(String source, String expected) throws IOException {
-        CRLFTerminatedInputStream in = new CRLFTerminatedInputStream(new ByteArrayInputStream(source.getBytes()));
-
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-
-        int i = -1;
-        while ((i = in.read()) != -1) {
-            out.write(i);
-        }
-        in.close();
-        out.close();
+        checkRead(new CRLFTerminatedInputStream(new ByteArrayInputStream(data.getBytes())),
expected);
+        checkReadViaArray(new CRLFTerminatedInputStream(new ByteArrayInputStream(data.getBytes())),
expected);
 
-        String output = new String(out.toByteArray());
-        assertEquals(expected, output);
     }
 
-    private void checkWithArray(String source, String expected) throws IOException {
-        CRLFTerminatedInputStream in = new CRLFTerminatedInputStream(new ByteArrayInputStream(source.getBytes()));
-
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-
-        byte[] buf = new byte[1024];
-        int i = 0;
-        while ((i = in.read(buf)) != -1) {
-            out.write(buf, 0, i);
-        }
-
-        in.close();
-        out.close();
-
-        String output = new String(out.toByteArray());
-        assertEquals(expected, output);
-    }
 }

Modified: james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/ExtraDotInputStreamTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/ExtraDotInputStreamTest.java?rev=1099013&r1=1099012&r2=1099013&view=diff
==============================================================================
--- james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/ExtraDotInputStreamTest.java
(original)
+++ james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/ExtraDotInputStreamTest.java
Tue May  3 11:28:01 2011
@@ -20,79 +20,59 @@
 package org.apache.james.pop3server;
 
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 
 import org.apache.james.pop3server.core.ExtraDotInputStream;
 
-import junit.framework.TestCase;
-
-public class ExtraDotInputStreamTest extends TestCase {
+public class ExtraDotInputStreamTest extends AbstractInputStreamTest {
 
     public void testExtraDot() throws IOException {
         String data = "This\r\n.\r\nThis.\r\n";
         String expectedOutput = "This\r\n..\r\nThis.\r\n";
-        ExtraDotInputStream in = new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes()));
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        
+        checkRead(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())), expectedOutput);
+        checkReadViaArray(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())),
expectedOutput);
 
-        int i = -1;
-        while ((i = in.read()) != -1) {
-            out.write(i);
-        }
-        in.close();
-        out.close();
+    }
 
-        String output = new String(out.toByteArray());
-        assertEquals(expectedOutput, output);
+    public void testNoExtraDotOnDoubleDot() throws IOException {
+        String data = "This\r\n..\r\nThis.\r\n";
+        
+        checkRead(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())), data);
+        checkReadViaArray(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())),
data);
 
     }
+    
+
+    public void testNoExtraDotOnDotWithText() throws IOException {
+        String data = "This\r\n.TestText\r\nThis.\r\n";
+        
+        checkRead(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())), data);
+        checkReadViaArray(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())),
data);
 
+    }
+    
     public void testNoDotCLRF() throws IOException {
         String data = "ABCD\r\n";
-        ExtraDotInputStream in = new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes()));
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-
-        int i = -1;
-        while ((i = in.read()) != -1) {
-            out.write(i);
-        }
-        in.close();
-        out.close();
-
-        String output = new String(out.toByteArray());
-        assertEquals(data, output);
+        checkRead(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())), data);
+        checkReadViaArray(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())),
data);
     }
 
     public void testNoDot() throws IOException {
         String data = "ABCD";
-        ExtraDotInputStream in = new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes()));
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-
-        int i = -1;
-        while ((i = in.read()) != -1) {
-            out.write(i);
-        }
-        in.close();
-        out.close();
-
-        String output = new String(out.toByteArray());
-        assertEquals(data, output);
+        checkRead(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())), data);
+        checkReadViaArray(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())),
data);
     }
 
     // Proof of BUG JAMES-1152
     public void testNoDotHeaderBody() throws IOException {
         String data = "Subject: test\r\n\r\nABCD\r\n";
-        ExtraDotInputStream in = new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes()));
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-
-        int i = -1;
-        while ((i = in.read()) != -1) {
-            out.write(i);
-        }
-        in.close();
-        out.close();
+        checkRead(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())), data);
+        checkReadViaArray(new ExtraDotInputStream(new ByteArrayInputStream(data.getBytes())),
data);
 
-        String output = new String(out.toByteArray());
-        assertEquals(data, output);
     }
+   
+
+
+    
 }



---------------------------------------------------------------------
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