~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/test/java/org/apache/http/impl/io/TestChunkCoding.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/test/java/org/apache/http/impl/io/TestChunkCoding.java $
 
3
 * $Revision: 705885 $
 
4
 * $Date: 2008-10-18 16:26:42 +0200 (Sat, 18 Oct 2008) $
 
5
 * ====================================================================
 
6
 * Licensed to the Apache Software Foundation (ASF) under one
 
7
 * or more contributor license agreements.  See the NOTICE file
 
8
 * distributed with this work for additional information
 
9
 * regarding copyright ownership.  The ASF licenses this file
 
10
 * to you under the Apache License, Version 2.0 (the
 
11
 * "License"); you may not use this file except in compliance
 
12
 * with the License.  You may obtain a copy of the License at
 
13
 *
 
14
 *   http://www.apache.org/licenses/LICENSE-2.0
 
15
 *
 
16
 * Unless required by applicable law or agreed to in writing,
 
17
 * software distributed under the License is distributed on an
 
18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
19
 * KIND, either express or implied.  See the License for the
 
20
 * specific language governing permissions and limitations
 
21
 * under the License.
 
22
 * ====================================================================
 
23
 *
 
24
 * This software consists of voluntary contributions made by many
 
25
 * individuals on behalf of the Apache Software Foundation.  For more
 
26
 * information on the Apache Software Foundation, please see
 
27
 * <http://www.apache.org/>.
 
28
 *
 
29
 */
 
30
 
 
31
package org.apache.http.impl.io;
 
32
 
 
33
import java.io.ByteArrayOutputStream;
 
34
import java.io.IOException;
 
35
import java.io.InputStream;
 
36
import java.io.OutputStream;
 
37
 
 
38
import org.apache.http.Header;
 
39
import org.apache.http.MalformedChunkCodingException;
 
40
import org.apache.http.impl.io.ChunkedInputStream;
 
41
import org.apache.http.impl.io.ChunkedOutputStream;
 
42
import org.apache.http.io.SessionInputBuffer;
 
43
import org.apache.http.mockup.SessionInputBufferMockup;
 
44
import org.apache.http.mockup.SessionOutputBufferMockup;
 
45
import org.apache.http.util.EncodingUtils;
 
46
 
 
47
import junit.framework.Test;
 
48
import junit.framework.TestCase;
 
49
import junit.framework.TestSuite;
 
50
 
 
51
public class TestChunkCoding extends TestCase {
 
52
 
 
53
    private static final String CONTENT_CHARSET = "ISO-8859-1";
 
54
    
 
55
    public TestChunkCoding(String testName) {
 
56
        super(testName);
 
57
    }
 
58
 
 
59
    // ------------------------------------------------------- TestCase Methods
 
60
 
 
61
    public static Test suite() {
 
62
        return new TestSuite(TestChunkCoding.class);
 
63
    }
 
64
 
 
65
    // ------------------------------------------------------------------- Main
 
66
    public static void main(String args[]) {
 
67
        String[] testCaseName = { TestChunkCoding.class.getName() };
 
68
        junit.textui.TestRunner.main(testCaseName);
 
69
    }
 
70
 
 
71
    public void testConstructors() throws Exception {
 
72
        try {
 
73
            new ChunkedInputStream((SessionInputBuffer)null);
 
74
            fail("IllegalArgumentException should have been thrown");
 
75
        } catch (IllegalArgumentException ex) {
 
76
            // expected
 
77
        }
 
78
        new MalformedChunkCodingException();
 
79
        new MalformedChunkCodingException("");
 
80
    }
 
81
 
 
82
    private final static String CHUNKED_INPUT 
 
83
        = "10;key=\"value\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
 
84
    
 
85
    private final static String CHUNKED_RESULT 
 
86
        = "123456789012345612345";
 
87
    
 
88
    // Test for when buffer is larger than chunk size
 
89
    public void testChunkedInputStreamLargeBuffer() throws IOException {
 
90
        ChunkedInputStream in = new ChunkedInputStream(
 
91
                new SessionInputBufferMockup(
 
92
                        EncodingUtils.getBytes(CHUNKED_INPUT, CONTENT_CHARSET)));
 
93
        byte[] buffer = new byte[300];
 
94
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
95
        int len;
 
96
        while ((len = in.read(buffer)) > 0) {
 
97
            out.write(buffer, 0, len);
 
98
        }
 
99
        assertEquals(-1, in.read(buffer));
 
100
        assertEquals(-1, in.read(buffer));
 
101
        
 
102
        in.close();
 
103
        
 
104
        String result = EncodingUtils.getString(out.toByteArray(), CONTENT_CHARSET);
 
105
        assertEquals(result, CHUNKED_RESULT);
 
106
        
 
107
        Header[] footers = in.getFooters();
 
108
        assertNotNull(footers);
 
109
        assertEquals(2, footers.length);
 
110
        assertEquals("Footer1", footers[0].getName());
 
111
        assertEquals("abcde", footers[0].getValue());
 
112
        assertEquals("Footer2", footers[1].getName());
 
113
        assertEquals("fghij", footers[1].getValue());
 
114
    }        
 
115
 
 
116
    //Test for when buffer is smaller than chunk size.
 
117
    public void testChunkedInputStreamSmallBuffer() throws IOException {
 
118
        ChunkedInputStream in = new ChunkedInputStream(
 
119
                new SessionInputBufferMockup(
 
120
                            EncodingUtils.getBytes(CHUNKED_INPUT, CONTENT_CHARSET)));
 
121
 
 
122
        byte[] buffer = new byte[7];
 
123
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
124
        int len;
 
125
        while ((len = in.read(buffer)) > 0) {
 
126
            out.write(buffer, 0, len);
 
127
        }
 
128
        assertEquals(-1, in.read(buffer));
 
129
        assertEquals(-1, in.read(buffer));
 
130
 
 
131
        in.close();
 
132
                
 
133
        EncodingUtils.getString(out.toByteArray(), CONTENT_CHARSET);
 
134
        Header[] footers = in.getFooters();
 
135
        assertNotNull(footers);
 
136
        assertEquals(2, footers.length);
 
137
        assertEquals("Footer1", footers[0].getName());
 
138
        assertEquals("abcde", footers[0].getValue());
 
139
        assertEquals("Footer2", footers[1].getName());
 
140
        assertEquals("fghij", footers[1].getValue());
 
141
    }
 
142
        
 
143
    // One byte read
 
144
    public void testChunkedInputStreamOneByteRead() throws IOException {
 
145
        String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
 
146
        ChunkedInputStream in = new ChunkedInputStream(
 
147
                new SessionInputBufferMockup(
 
148
                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
 
149
        int ch;
 
150
        int i = '0';
 
151
        while ((ch = in.read()) != -1) {
 
152
            assertEquals(i, ch);
 
153
            i++;
 
154
        }
 
155
        assertEquals(-1, in.read());
 
156
        assertEquals(-1, in.read());        
 
157
 
 
158
        in.close();        
 
159
    }
 
160
 
 
161
    public void testChunkedInputStreamClose() throws IOException {
 
162
        String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
 
163
        ChunkedInputStream in = new ChunkedInputStream(
 
164
                new SessionInputBufferMockup(
 
165
                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
 
166
        in.close();
 
167
        in.close();
 
168
        try {
 
169
            in.read();
 
170
            fail("IOException should have been thrown");
 
171
        } catch (IOException ex) {
 
172
            // expected
 
173
        }
 
174
        byte[] tmp = new byte[10]; 
 
175
        try {
 
176
            in.read(tmp);
 
177
            fail("IOException should have been thrown");
 
178
        } catch (IOException ex) {
 
179
            // expected
 
180
        }
 
181
        try {
 
182
            in.read(tmp, 0, tmp.length);
 
183
            fail("IOException should have been thrown");
 
184
        } catch (IOException ex) {
 
185
            // expected
 
186
        }
 
187
    }
 
188
 
 
189
    public void testChunkedOutputStreamClose() throws IOException {
 
190
        ChunkedOutputStream out = new ChunkedOutputStream(
 
191
                new SessionOutputBufferMockup());
 
192
        out.close();
 
193
        out.close();
 
194
        try {
 
195
            out.write(new byte[] {1,2,3});
 
196
            fail("IOException should have been thrown");
 
197
        } catch (IOException ex) {
 
198
            // expected
 
199
        }
 
200
        try {
 
201
            out.write(1);
 
202
            fail("IOException should have been thrown");
 
203
        } catch (IOException ex) {
 
204
            // expected
 
205
        }
 
206
    }
 
207
 
 
208
    // Missing closing chunk
 
209
    public void testChunkedInputStreamNoClosingChunk() throws IOException {
 
210
        String s = "5\r\n01234\r\n";
 
211
        ChunkedInputStream in = new ChunkedInputStream(
 
212
                new SessionInputBufferMockup(
 
213
                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
 
214
        byte[] tmp = new byte[5];
 
215
        assertEquals(5, in.read(tmp));
 
216
        assertEquals(-1, in.read());
 
217
    }
 
218
 
 
219
    // Missing \r\n at the end of the first chunk
 
220
    public void testCorruptChunkedInputStreamMissingCRLF() throws IOException {
 
221
        String s = "5\r\n012345\r\n56789\r\n0\r\n";
 
222
        InputStream in = new ChunkedInputStream(
 
223
                new SessionInputBufferMockup(
 
224
                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
 
225
        byte[] buffer = new byte[300];
 
226
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
227
        int len;
 
228
        try {
 
229
            while ((len = in.read(buffer)) > 0) {
 
230
                out.write(buffer, 0, len);
 
231
            }
 
232
            fail("MalformedChunkCodingException should have been thrown");
 
233
        } catch(MalformedChunkCodingException e) {
 
234
            /* expected exception */
 
235
        }
 
236
    }
 
237
 
 
238
    // Missing LF
 
239
    public void testCorruptChunkedInputStreamMissingLF() throws IOException {
 
240
        String s = "5\r01234\r\n5\r\n56789\r\n0\r\n";
 
241
        InputStream in = new ChunkedInputStream(
 
242
                new SessionInputBufferMockup(
 
243
                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
 
244
        try {
 
245
            in.read();
 
246
            fail("MalformedChunkCodingException should have been thrown");
 
247
        } catch(MalformedChunkCodingException e) {
 
248
            /* expected exception */
 
249
        }
 
250
    }
 
251
 
 
252
    // Invalid chunk size
 
253
    public void testCorruptChunkedInputStreamInvalidSize() throws IOException {
 
254
        String s = "whatever\r\n01234\r\n5\r\n56789\r\n0\r\n";
 
255
        InputStream in = new ChunkedInputStream(
 
256
                new SessionInputBufferMockup(
 
257
                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
 
258
        try {
 
259
            in.read();
 
260
            fail("MalformedChunkCodingException should have been thrown");
 
261
        } catch(MalformedChunkCodingException e) {
 
262
            /* expected exception */
 
263
        }
 
264
    }
 
265
 
 
266
    // Negative chunk size
 
267
    public void testCorruptChunkedInputStreamNegativeSize() throws IOException {
 
268
        String s = "-5\r\n01234\r\n5\r\n56789\r\n0\r\n";
 
269
        InputStream in = new ChunkedInputStream(
 
270
                new SessionInputBufferMockup(
 
271
                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
 
272
        try {
 
273
            in.read();
 
274
            fail("MalformedChunkCodingException should have been thrown");
 
275
        } catch(MalformedChunkCodingException e) {
 
276
            /* expected exception */
 
277
        }
 
278
    }
 
279
 
 
280
    // Truncated chunk
 
281
    public void testCorruptChunkedInputStreamTruncatedChunk() throws IOException {
 
282
        String s = "3\r\n12";
 
283
        InputStream in = new ChunkedInputStream(
 
284
                new SessionInputBufferMockup(
 
285
                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
 
286
        byte[] buffer = new byte[300];
 
287
        assertEquals(2, in.read(buffer));
 
288
        try {
 
289
            in.read(buffer);
 
290
            fail("MalformedChunkCodingException should have been thrown");
 
291
        } catch(MalformedChunkCodingException e) {
 
292
            /* expected exception */
 
293
        }
 
294
    }
 
295
 
 
296
    // Invalid footer
 
297
    public void testCorruptChunkedInputStreamInvalidFooter() throws IOException {
 
298
        String s = "1\r\n0\r\n0\r\nstuff\r\n";
 
299
        InputStream in = new ChunkedInputStream(
 
300
                new SessionInputBufferMockup(
 
301
                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
 
302
        try {
 
303
            in.read();
 
304
            in.read();
 
305
            fail("MalformedChunkCodingException should have been thrown");
 
306
        } catch(MalformedChunkCodingException e) {
 
307
            /* expected exception */
 
308
        }
 
309
    }
 
310
 
 
311
    public void testEmptyChunkedInputStream() throws IOException {
 
312
        String input = "0\r\n";
 
313
        InputStream in = new ChunkedInputStream(
 
314
                new SessionInputBufferMockup(
 
315
                        EncodingUtils.getBytes(input, CONTENT_CHARSET)));
 
316
        byte[] buffer = new byte[300];
 
317
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
318
        int len;
 
319
        while ((len = in.read(buffer)) > 0) {
 
320
            out.write(buffer, 0, len);
 
321
        }
 
322
        assertEquals(0, out.size());
 
323
    }
 
324
    
 
325
    public void testChunkedConsistence() throws IOException {
 
326
        String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
 
327
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 
328
        OutputStream out = new ChunkedOutputStream(new SessionOutputBufferMockup(buffer));
 
329
        out.write(EncodingUtils.getBytes(input, CONTENT_CHARSET));
 
330
        out.flush();
 
331
        out.close();
 
332
        out.close();
 
333
        buffer.close();
 
334
        InputStream in = new ChunkedInputStream(
 
335
                new SessionInputBufferMockup(
 
336
                        buffer.toByteArray()));
 
337
 
 
338
        byte[] d = new byte[10];
 
339
        ByteArrayOutputStream result = new ByteArrayOutputStream();
 
340
        int len = 0;
 
341
        while ((len = in.read(d)) > 0) {
 
342
            result.write(d, 0, len);
 
343
        }
 
344
 
 
345
        String output = EncodingUtils.getString(result.toByteArray(), CONTENT_CHARSET);
 
346
        assertEquals(input, output);
 
347
    }
 
348
 
 
349
    public void testChunkedOutputStream() throws IOException {
 
350
        SessionOutputBufferMockup buffer = new SessionOutputBufferMockup();
 
351
        ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
 
352
        out.write('1');  
 
353
        out.write('2');  
 
354
        out.write('3');  
 
355
        out.write('4');  
 
356
        out.finish();
 
357
        out.close();
 
358
        
 
359
        byte [] rawdata =  buffer.getData();
 
360
        
 
361
        assertEquals(19, rawdata.length);
 
362
        assertEquals('2', rawdata[0]);
 
363
        assertEquals('\r', rawdata[1]);
 
364
        assertEquals('\n', rawdata[2]);
 
365
        assertEquals('1', rawdata[3]);
 
366
        assertEquals('2', rawdata[4]);
 
367
        assertEquals('\r', rawdata[5]);
 
368
        assertEquals('\n', rawdata[6]);
 
369
        assertEquals('2', rawdata[7]);
 
370
        assertEquals('\r', rawdata[8]);
 
371
        assertEquals('\n', rawdata[9]);
 
372
        assertEquals('3', rawdata[10]);
 
373
        assertEquals('4', rawdata[11]);
 
374
        assertEquals('\r', rawdata[12]);
 
375
        assertEquals('\n', rawdata[13]);
 
376
        assertEquals('0', rawdata[14]);
 
377
        assertEquals('\r', rawdata[15]);
 
378
        assertEquals('\n', rawdata[16]);
 
379
        assertEquals('\r', rawdata[17]);
 
380
        assertEquals('\n', rawdata[18]);
 
381
    }
 
382
 
 
383
    public void testChunkedOutputStreamLargeChunk() throws IOException {
 
384
        SessionOutputBufferMockup buffer = new SessionOutputBufferMockup();
 
385
        ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
 
386
        out.write(new byte[] {'1', '2', '3', '4'});
 
387
        out.finish();
 
388
        out.close();
 
389
        
 
390
        byte [] rawdata =  buffer.getData();
 
391
        
 
392
        assertEquals(14, rawdata.length);
 
393
        assertEquals('4', rawdata[0]);
 
394
        assertEquals('\r', rawdata[1]);
 
395
        assertEquals('\n', rawdata[2]);
 
396
        assertEquals('1', rawdata[3]);
 
397
        assertEquals('2', rawdata[4]);
 
398
        assertEquals('3', rawdata[5]);
 
399
        assertEquals('4', rawdata[6]);
 
400
        assertEquals('\r', rawdata[7]);
 
401
        assertEquals('\n', rawdata[8]);
 
402
        assertEquals('0', rawdata[9]);
 
403
        assertEquals('\r', rawdata[10]);
 
404
        assertEquals('\n', rawdata[11]);
 
405
        assertEquals('\r', rawdata[12]);
 
406
        assertEquals('\n', rawdata[13]);
 
407
    }
 
408
 
 
409
    public void testChunkedOutputStreamSmallChunk() throws IOException {
 
410
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 
411
        ChunkedOutputStream out = new ChunkedOutputStream(
 
412
                new SessionOutputBufferMockup(buffer), 2);
 
413
        out.write('1');  
 
414
        out.finish();
 
415
        out.close();
 
416
        
 
417
        byte [] rawdata =  buffer.toByteArray();
 
418
        
 
419
        assertEquals(11, rawdata.length);
 
420
        assertEquals('1', rawdata[0]);
 
421
        assertEquals('\r', rawdata[1]);
 
422
        assertEquals('\n', rawdata[2]);
 
423
        assertEquals('1', rawdata[3]);
 
424
        assertEquals('\r', rawdata[4]);
 
425
        assertEquals('\n', rawdata[5]);
 
426
        assertEquals('0', rawdata[6]);
 
427
        assertEquals('\r', rawdata[7]);
 
428
        assertEquals('\n', rawdata[8]);
 
429
        assertEquals('\r', rawdata[9]);
 
430
        assertEquals('\n', rawdata[10]);
 
431
    }
 
432
 
 
433
}
 
434