Return-Path: Delivered-To: apmail-james-mime4j-dev-archive@minotaur.apache.org Received: (qmail 52991 invoked from network); 14 Apr 2011 15:02:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 14 Apr 2011 15:02:24 -0000 Received: (qmail 14244 invoked by uid 500); 14 Apr 2011 15:02:24 -0000 Delivered-To: apmail-james-mime4j-dev-archive@james.apache.org Received: (qmail 14206 invoked by uid 500); 14 Apr 2011 15:02:23 -0000 Mailing-List: contact mime4j-dev-help@james.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: mime4j-dev@james.apache.org Delivered-To: mailing list mime4j-dev@james.apache.org Received: (qmail 14196 invoked by uid 99); 14 Apr 2011 15:02:23 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Apr 2011 15:02:23 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Apr 2011 15:02:21 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id B938B23889C5; Thu, 14 Apr 2011 15:02:01 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1092322 - in /james/mime4j/trunk/core/src: main/java/org/apache/james/mime4j/io/ main/java/org/apache/james/mime4j/stream/ test/java/org/apache/james/mime4j/io/ test/java/org/apache/james/mime4j/stream/ Date: Thu, 14 Apr 2011 15:02:01 -0000 To: mime4j-dev@james.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110414150201.B938B23889C5@eris.apache.org> Author: olegk Date: Thu Apr 14 15:02:01 2011 New Revision: 1092322 URL: http://svn.apache.org/viewvc?rev=1092322&view=rev Log: Added test case for MIME4J-180; minor code refactoring in MimeBoundaryInputStream and MimeEntity.java Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/StrictMimeTokenStreamTest.java Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java?rev=1092322&r1=1092321&r2=1092322&view=diff ============================================================================== --- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java (original) +++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java Thu Apr 14 15:02:01 2011 @@ -58,10 +58,11 @@ public class MimeBoundaryInputStream ext public MimeBoundaryInputStream(BufferedLineReaderInputStream inbuffer, String boundary) throws IOException { super(inbuffer); - - if (inbuffer.capacity() < boundary.length() * 2) { - throw new IllegalArgumentException("Boundary is too long"); + int bufferSize = 2 * boundary.length(); + if (bufferSize < 4096) { + bufferSize = 4096; } + inbuffer.ensureCapacity(bufferSize); this.buffer = inbuffer; this.eof = false; this.limit = -1; Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java?rev=1092322&r1=1092321&r2=1092322&view=diff ============================================================================== --- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java (original) +++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java Thu Apr 14 15:02:01 2011 @@ -212,13 +212,7 @@ class MimeEntity extends AbstractEntity private void createMimePartStream() throws MimeException, IOException { String boundary = body.getBoundary(); - // TODO move the following lines inside the MimeBoundaryInputStream constructor - int bufferSize = 2 * boundary.length(); - if (bufferSize < 4096) { - bufferSize = 4096; - } try { - inbuffer.ensureCapacity(bufferSize); currentMimePartStream = new MimeBoundaryInputStream(inbuffer, boundary); } catch (IllegalArgumentException e) { // thrown when boundary is too long Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java?rev=1092322&r1=1092321&r2=1092322&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java Thu Apr 14 15:02:01 2011 @@ -327,17 +327,4 @@ public class MimeBoundaryInputStreamTest assertEquals(-1, instream.readLine(linebuf)); } - public void testboundaryLongerThanBuffer() throws IOException { - String text = "--looooooooooooooooooooooooooong-boundary\r\n"; - - ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes()); - BufferedLineReaderInputStream buffer = new BufferedLineReaderInputStream(bis, 10); - - try { - new MimeBoundaryInputStream(buffer, "looooooooooooooooooooooooooong-boundary"); - fail("IllegalArgumentException should have been thrown"); - } catch (IllegalArgumentException expected) { - } - } - } Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/StrictMimeTokenStreamTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/StrictMimeTokenStreamTest.java?rev=1092322&r1=1092321&r2=1092322&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/StrictMimeTokenStreamTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/StrictMimeTokenStreamTest.java Thu Apr 14 15:02:01 2011 @@ -20,6 +20,10 @@ package org.apache.james.mime4j.stream; import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; import junit.framework.TestCase; @@ -28,15 +32,26 @@ public class StrictMimeTokenStreamTest e private static final String HEADER_ONLY = "From: foo@abr.com\r\nSubject: A subject\r\n"; private static final String CORRECT_HEADERS = HEADER_ONLY + "\r\n"; - public void testUnexpectedEndOfHeaders() throws Exception { - + MimeTokenStream parser; + + @Override + protected void setUp() throws Exception { + super.setUp(); MimeEntityConfig config = new MimeEntityConfig(); config.setStrictParsing(true); - MimeTokenStream parser = new MimeTokenStream(config); - parser.parse(new ByteArrayInputStream(HEADER_ONLY.getBytes())); + parser = new MimeTokenStream(config); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testUnexpectedEndOfHeaders() throws Exception { + parser.parse(new ByteArrayInputStream(HEADER_ONLY.getBytes("US-ASCII"))); - assertEquals("Headers start", EntityState.T_START_HEADER, parser.next()); - assertEquals("Field", EntityState.T_FIELD, parser.next()); + checkNextIs(EntityState.T_START_HEADER); + checkNextIs(EntityState.T_FIELD); try { parser.next(); fail("Expected exception to be thrown"); @@ -46,16 +61,41 @@ public class StrictMimeTokenStreamTest e } public void testCorrectEndOfHeaders() throws Exception { + parser.parse(new ByteArrayInputStream(CORRECT_HEADERS.getBytes("US-ASCII"))); - MimeEntityConfig config = new MimeEntityConfig(); - config.setStrictParsing(true); - MimeTokenStream parser = new MimeTokenStream(); - - parser.parse(new ByteArrayInputStream(CORRECT_HEADERS.getBytes())); - - assertEquals("Headers start", EntityState.T_START_HEADER, parser.next()); - assertEquals("From header", EntityState.T_FIELD, parser.next()); - assertEquals("Subject header", EntityState.T_FIELD, parser.next()); - assertEquals("End message", EntityState.T_END_HEADER, parser.next()); + checkNextIs(EntityState.T_START_HEADER); + checkNextIs(EntityState.T_FIELD); + checkNextIs(EntityState.T_FIELD); + checkNextIs(EntityState.T_END_HEADER); + } + + public void testMissingBoundary() throws Exception { + String message = + "Content-Type: multipart/mixed;boundary=aaaa\r\n\r\n" + + "--aaaa\r\n" + + "Content-Type:text/plain; charset=US-ASCII\r\n\r\n" + + "Oh my god! Boundary is missing!\r\n"; + parser.parse(new ByteArrayInputStream(message.getBytes("US-ASCII"))); + checkNextIs(EntityState.T_START_HEADER); + checkNextIs(EntityState.T_FIELD); + checkNextIs(EntityState.T_END_HEADER); + checkNextIs(EntityState.T_START_MULTIPART); + checkNextIs(EntityState.T_START_BODYPART); + checkNextIs(EntityState.T_START_HEADER); + checkNextIs(EntityState.T_FIELD); + checkNextIs(EntityState.T_END_HEADER); + checkNextIs(EntityState.T_BODY); + InputStream out = parser.getInputStream(); + assertEquals("Oh my god! Boundary is missing!\r\n", IOUtils.toString(out, "US-ASCII")); + checkNextIs(EntityState.T_END_BODYPART); + try { + parser.next(); + fail("MimeParseEventException should have been thrown"); + } catch (MimeParseEventException expected) { + } } + + private void checkNextIs(EntityState expected) throws Exception { + assertEquals(MimeTokenStream.stateToString(expected), MimeTokenStream.stateToString(parser.next())); + } }