Return-Path: Delivered-To: apmail-jakarta-httpclient-commits-archive@www.apache.org Received: (qmail 64607 invoked from network); 1 Apr 2005 21:53:55 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 1 Apr 2005 21:53:55 -0000 Received: (qmail 28542 invoked by uid 500); 1 Apr 2005 21:53:55 -0000 Mailing-List: contact httpclient-commits-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: httpclient-dev@jakarta.apache.org Delivered-To: mailing list httpclient-commits@jakarta.apache.org Received: (qmail 28529 invoked by uid 500); 1 Apr 2005 21:53:54 -0000 Delivered-To: apmail-jakarta-httpclient-cvs@jakarta.apache.org Received: (qmail 28525 invoked by uid 99); 1 Apr 2005 21:53:54 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Fri, 01 Apr 2005 13:53:54 -0800 Received: (qmail 64604 invoked by uid 65534); 1 Apr 2005 21:53:53 -0000 Message-ID: <20050401215353.64603.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Mailer: svnmailer-1.0.0-dev Date: Fri, 01 Apr 2005 21:53:53 -0000 Subject: svn commit: r159739 - in jakarta/httpclient/trunk/http-common/src: java/org/apache/http/io/ChunkedInputStream.java test/org/apache/http/io/TestChunkCoding.java To: httpclient-cvs@jakarta.apache.org From: olegk@apache.org X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: olegk Date: Fri Apr 1 13:53:51 2005 New Revision: 159739 URL: http://svn.apache.org/viewcvs?view=3Drev&rev=3D159739 Log: Extra test coverage Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/Chunke= dInputStream.java jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestCh= unkCoding.java Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/= ChunkedInputStream.java URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src= /java/org/apache/http/io/ChunkedInputStream.java?view=3Ddiff&r1=3D159738&r2= =3D159739 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/Chunke= dInputStream.java (original) +++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/Chunke= dInputStream.java Fri Apr 1 13:53:51 2005 @@ -227,7 +227,7 @@ while (state !=3D -1) { int b =3D in.read(); if (b =3D=3D -1) {=20 - throw new IOException("chunked stream ended unexpectedly"); + throw new IOException("Chunked stream ended unexpectedly"); } switch (state) { case 0:=20 @@ -248,7 +248,7 @@ state =3D -1; } else { // this was not CRLF - throw new IOException("Protocol violation: Unexpec= ted" + throw new IOException("Unexpected" + " single newline character in chunk size"); } break; @@ -266,7 +266,7 @@ baos.write(b); } break; - default: throw new RuntimeException("assertion failed"); + default: throw new IllegalStateException("Invalid state co= ndition"); } } =20 @@ -294,7 +294,7 @@ try { this.footers =3D HeadersParser.processHeaders(in); } catch (HttpException e) { - IOException ioe =3D new IOException(e.getMessage()); + IOException ioe =3D new IOException("Invalid footer: " + e.get= Message()); ExceptionUtil.initCause(ioe, e);=20 throw ioe; } Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/= TestChunkCoding.java URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src= /test/org/apache/http/io/TestChunkCoding.java?view=3Ddiff&r1=3D159738&r2=3D= 159739 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestCh= unkCoding.java (original) +++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestCh= unkCoding.java Fri Apr 1 13:53:51 2005 @@ -35,6 +35,7 @@ import java.io.OutputStream; =20 import org.apache.http.Header; +import org.apache.http.impl.DefaultHttpParams; import org.apache.http.util.EncodingUtil; =20 import junit.framework.Test; @@ -61,22 +62,45 @@ junit.textui.TestRunner.main(testCaseName); } =20 - public void testChunkedInputStream() throws IOException { - String correctInput =3D "10;key=3D\"value\r\nnewline\"\r\n12345678= 90123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n"; - String correctResult =3D "123456789012345612345"; + public void testConstructors() throws Exception { + try { + new ChunkedInputStream((HttpDataReceiver)null); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + new ChunkedInputStream((InputStream)null); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } =20 - //Test for when buffer is larger than chunk size + private final static String CHUNKED_INPUT=20 + =3D "10;key=3D\"value\\\r\nnewline\"\r\n1234567890123456\r\n5\r\n1= 2345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n"; + =20 + private final static String CHUNKED_RESULT=20 + =3D "123456789012345612345"; + =20 + // Test for when buffer is larger than chunk size + public void testChunkedInputStreamLargeBuffer() throws IOException { ChunkedInputStream in =3D new ChunkedInputStream( new ByteArrayInputStream( - EncodingUtil.getBytes(correctInput, CONTENT_CHARSE= T))); + EncodingUtil.getBytes(CHUNKED_INPUT, CONTENT_CHARS= ET))); byte[] buffer =3D new byte[300]; ByteArrayOutputStream out =3D new ByteArrayOutputStream(); int len; while ((len =3D in.read(buffer)) > 0) { out.write(buffer, 0, len); } + assertEquals(-1, in.read(buffer)); + assertEquals(-1, in.read(buffer)); + =20 + in.close(); + =20 String result =3D EncodingUtil.getString(out.toByteArray(), CONTEN= T_CHARSET); - assertEquals(result, correctResult); + assertEquals(result, CHUNKED_RESULT); =20 Header[] footers =3D in.getFooters(); assertNotNull(footers); @@ -85,18 +109,27 @@ assertEquals("abcde", footers[0].getValue()); assertEquals("Footer2", footers[1].getName()); assertEquals("fghij", footers[1].getValue()); - =20 - //Test for when buffer is smaller than chunk size. - in =3D new ChunkedInputStream( + } =20 + + //Test for when buffer is smaller than chunk size. + public void testChunkedInputStreamSmallBuffer() throws IOException { + ChunkedInputStream in =3D new ChunkedInputStream( new ByteArrayInputStream( - EncodingUtil.getBytes(correctInput, CONTENT_CH= ARSET))); - buffer =3D new byte[7]; - out =3D new ByteArrayOutputStream(); + EncodingUtil.getBytes(CHUNKED_INPUT, CONTENT_C= HARSET))); + + byte[] buffer =3D new byte[7]; + ByteArrayOutputStream out =3D new ByteArrayOutputStream(); + int len; while ((len =3D in.read(buffer)) > 0) { out.write(buffer, 0, len); } - result =3D EncodingUtil.getString(out.toByteArray(), CONTENT_CHARS= ET); - footers =3D in.getFooters(); + assertEquals(-1, in.read(buffer)); + assertEquals(-1, in.read(buffer)); + + in.close(); + =20 + String result =3D EncodingUtil.getString(out.toByteArray(), CONTEN= T_CHARSET); + Header[] footers =3D in.getFooters(); assertNotNull(footers); assertEquals(2, footers.length); assertEquals("Footer1", footers[0].getName()); @@ -104,14 +137,59 @@ assertEquals("Footer2", footers[1].getName()); assertEquals("fghij", footers[1].getValue()); } + =20 + // One byte read + public void testChunkedInputStreamOneByteRead() throws IOException { + String s =3D "5\r\n01234\r\n5\r\n56789\r\n0\r\n"; + ChunkedInputStream in =3D new ChunkedInputStream( + new ByteArrayInputStream( + EncodingUtil.getBytes(s, CONTENT_CHARSET))); + int ch; + int i =3D '0'; + while ((ch =3D in.read()) !=3D -1) { + assertEquals(i, ch); + i++; + } + assertEquals(-1, in.read()); + assertEquals(-1, in.read()); =20 =20 - public void testCorruptChunkedInputStream1() throws IOException { - //missing \r\n at the end of the first chunk - String corrupInput =3D "10;key=3D\"value\"\r\n123456789012345\r\n5= \r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n"; + in.close(); =20 + } =20 + public void testChunkedInputStreamClose() throws IOException { + String s =3D "5\r\n01234\r\n5\r\n56789\r\n0\r\n"; + ChunkedInputStream in =3D new ChunkedInputStream( + new ByteArrayInputStream( + EncodingUtil.getBytes(s, CONTENT_CHARSET))); + in.close(); + in.close(); + try { + in.read(); + fail("IOException should have been thrown"); + } catch (IOException ex) { + // expected + } + byte[] tmp =3D new byte[10];=20 + try { + in.read(tmp); + fail("IOException should have been thrown"); + } catch (IOException ex) { + // expected + } + try { + in.read(tmp, 0, tmp.length); + fail("IOException should have been thrown"); + } catch (IOException ex) { + // expected + } + } + + // Missing \r\n at the end of the first chunk + public void testCorruptChunkedInputStreamMissingCRLF() throws IOExcept= ion { + String s =3D "5\r\n012345\r\n56789\r\n0\r\n"; InputStream in =3D new ChunkedInputStream( new ByteArrayInputStream( - EncodingUtil.getBytes(corrupInput, CONTENT_CHARSET= ))); + EncodingUtil.getBytes(s, CONTENT_CHARSET))); byte[] buffer =3D new byte[300]; ByteArrayOutputStream out =3D new ByteArrayOutputStream(); int len; @@ -119,7 +197,62 @@ while ((len =3D in.read(buffer)) > 0) { out.write(buffer, 0, len); } - fail("Should have thrown exception"); + fail("IOException should have been thrown"); + } catch(IOException e) { + /* expected exception */ + } + } + + // Missing LF + public void testCorruptChunkedInputStreamMissingLF() throws IOExceptio= n { + String s =3D "5\r01234\r\n5\r\n56789\r\n0\r\n"; + InputStream in =3D new ChunkedInputStream( + new ByteArrayInputStream( + EncodingUtil.getBytes(s, CONTENT_CHARSET))); + try { + in.read(); + fail("IOException should have been thrown"); + } catch(IOException e) { + /* expected exception */ + } + } + + // Missing closing chunk + public void testCorruptChunkedInputStreamNoClosingChunk() throws IOExc= eption { + InputStream in =3D new ChunkedInputStream( + new ByteArrayInputStream(new byte[] {})); + try { + in.read(); + fail("IOException should have been thrown"); + } catch(IOException e) { + /* expected exception */ + } + } + + // Invalid chunk size + public void testCorruptChunkedInputStreamInvalidSize() throws IOExcept= ion { + String s =3D "whatever\r\n01234\r\n5\r\n56789\r\n0\r\n"; + InputStream in =3D new ChunkedInputStream( + new ByteArrayInputStream( + EncodingUtil.getBytes(s, CONTENT_CHARSET))); + try { + in.read(); + fail("IOException should have been thrown"); + } catch(IOException e) { + /* expected exception */ + } + } + + // Invalid footer + public void testCorruptChunkedInputStreamInvalidFooter() throws IOExce= ption { + String s =3D "1\r\n0\r\n0\r\nstuff\r\n"; + InputStream in =3D new ChunkedInputStream( + new ByteArrayInputStream( + EncodingUtil.getBytes(s, CONTENT_CHARSET))); + try { + in.read(); + in.read(); + fail("IOException should have been thrown"); } catch(IOException e) { /* expected exception */ } @@ -139,6 +272,27 @@ assertEquals(0, out.size()); } =20 + public void testInputStreamHttpDataReceiver() throws IOException { + String s =3D "aaaaa"; + InputStream in =3D new ByteArrayInputStream( + EncodingUtil.getBytes(s, CONTENT_CHARSET)); + ChunkedInputStream.InputStreamHttpDataReceiver datareceiver =3D + new ChunkedInputStream.InputStreamHttpDataReceiver(in); + assertTrue(datareceiver.isDataAvailable(1)); + assertEquals('a', datareceiver.read()); + byte[] tmp =3D new byte[2]; + datareceiver.read(tmp); + assertEquals('a', tmp[0]); + assertEquals('a', tmp[1]); + datareceiver.read(tmp, 0, tmp.length); + assertEquals('a', tmp[0]); + assertEquals('a', tmp[1]); + assertEquals(-1, datareceiver.read()); + datareceiver.reset(new DefaultHttpParams(null)); + } + + =20 + =20 public void testContentLengthInputStream() throws IOException { String correct =3D "1234567890123456"; InputStream in =3D new ContentLengthInputStream(new ByteArrayInput= Stream( @@ -173,6 +327,8 @@ ByteArrayOutputStream buffer =3D new ByteArrayOutputStream(); OutputStream out =3D new ChunkedOutputStream(buffer); out.write(EncodingUtil.getBytes(input, CONTENT_CHARSET)); + out.flush(); + out.close(); out.close(); buffer.close(); InputStream in =3D new ChunkedInputStream(