View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.impl.io;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.nio.charset.StandardCharsets;
35  
36  import org.apache.hc.core5.http.ConnectionClosedException;
37  import org.apache.hc.core5.http.StreamClosedException;
38  import org.apache.hc.core5.http.io.SessionInputBuffer;
39  import org.junit.Assert;
40  import org.junit.Test;
41  
42  public class TestContentLengthInputStream {
43  
44      @Test
45      public void testBasics() throws IOException {
46          final String s = "1234567890123456";
47          final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
48          final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16);
49          final InputStream in = new ContentLengthInputStream(inbuffer, inputStream, 10L);
50          final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
51  
52          final byte[] buffer = new byte[50];
53          int len = in.read(buffer, 0, 2);
54          outputStream.write(buffer, 0, len);
55          len = in.read(buffer);
56          outputStream.write(buffer, 0, len);
57  
58          final String result = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1);
59          Assert.assertEquals(result, "1234567890");
60          in.close();
61      }
62  
63      @Test
64      public void testSkip() throws IOException {
65          final ByteArrayInputStream inputStream1 = new ByteArrayInputStream(new byte[20]);
66          final SessionInputBuffer inbuffer1 = new SessionInputBufferImpl(16);
67          final InputStream in1 = new ContentLengthInputStream(inbuffer1, inputStream1, 10L);
68          Assert.assertEquals(10, in1.skip(10));
69          Assert.assertTrue(in1.read() == -1);
70          in1.close();
71  
72          final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(new byte[20]);
73          final SessionInputBuffer inbuffer2 = new SessionInputBufferImpl(16);
74          final InputStream in2 = new ContentLengthInputStream(inbuffer2, inputStream2, 10L);
75          in2.read();
76          Assert.assertEquals(9, in2.skip(10));
77          Assert.assertTrue(in2.read() == -1);
78          in2.close();
79  
80          final ByteArrayInputStream inputStream3 = new ByteArrayInputStream(new byte[20]);
81          final SessionInputBuffer inbuffer3 = new SessionInputBufferImpl(16);
82          final InputStream in3 = new ContentLengthInputStream(inbuffer3, inputStream3, 2L);
83          in3.read();
84          in3.read();
85          Assert.assertTrue(in3.skip(10) <= 0);
86          Assert.assertTrue(in3.skip(-1) == 0);
87          Assert.assertTrue(in3.read() == -1);
88          in3.close();
89  
90          final ByteArrayInputStream inputStream4 = new ByteArrayInputStream(new byte[20]);
91          final SessionInputBuffer inbuffer4 = new SessionInputBufferImpl(16);
92          final InputStream in4 = new ContentLengthInputStream(inbuffer4, inputStream4, 10L);
93          Assert.assertEquals(5,in4.skip(5));
94          Assert.assertEquals(5, in4.read(new byte[20]));
95          in4.close();
96      }
97  
98      @Test
99      public void testAvailable() throws IOException {
100         final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {1, 2, 3});
101         final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16);
102         final InputStream in = new ContentLengthInputStream(inbuffer, inputStream, 3L);
103         Assert.assertEquals(0, in.available());
104         in.read();
105         Assert.assertEquals(2, in.available());
106         in.close();
107     }
108 
109     @Test
110     public void testClose() throws IOException {
111         final String s = "1234567890123456-";
112         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
113         final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16);
114         final InputStream in = new ContentLengthInputStream(inbuffer, inputStream, 16L);
115 
116         in.close();
117         in.close();
118         try {
119             in.read();
120             Assert.fail("StreamClosedException expected");
121         } catch (final StreamClosedException expected) {
122         }
123         final byte[] tmp = new byte[10];
124         try {
125             in.read(tmp);
126             Assert.fail("StreamClosedException expected");
127         } catch (final StreamClosedException expected) {
128         }
129         try {
130             in.read(tmp, 0, tmp.length);
131             Assert.fail("StreamClosedException expected");
132         } catch (final StreamClosedException expected) {
133         }
134         Assert.assertEquals('-', inbuffer.read(inputStream));
135     }
136 
137     @Test
138     public void testTruncatedContent() throws IOException {
139         final String s = "1234567890123456";
140         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
141         final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16);
142         final InputStream in = new ContentLengthInputStream(inbuffer, inputStream, 32L);
143 
144         final byte[] tmp = new byte[32];
145         final int byteRead = in.read(tmp);
146         Assert.assertEquals(16, byteRead);
147         try {
148             in.read(tmp);
149             Assert.fail("ConnectionClosedException should have been closed");
150         } catch (final ConnectionClosedException ex) {
151         }
152         try {
153             in.read();
154             Assert.fail("ConnectionClosedException should have been closed");
155         } catch (final ConnectionClosedException ex) {
156         }
157         try {
158             in.close();
159             Assert.fail("ConnectionClosedException should have been closed");
160         } catch (final ConnectionClosedException ex) {
161         }
162     }
163 
164 }
165