View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.util;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.CharArrayReader;
25  import java.io.CharArrayWriter;
26  import java.io.Closeable;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  import java.io.Reader;
30  import java.io.Writer;
31  import java.net.Socket;
32  
33  import org.apache.commons.net.io.CopyStreamEvent;
34  import org.apache.commons.net.io.CopyStreamListener;
35  import org.apache.commons.net.io.Util;
36  import org.junit.jupiter.api.Test;
37  
38  class UtilTest {
39  
40      static class CSL implements CopyStreamListener {
41  
42          final long expectedTotal;
43          final int expectedBytes;
44          final long expectedSize;
45  
46          CSL(final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {
47              this.expectedTotal = totalBytesTransferred;
48              this.expectedBytes = bytesTransferred;
49              this.expectedSize = streamSize;
50          }
51  
52          @Override
53          public void bytesTransferred(final CopyStreamEvent event) {
54              // noop
55          }
56  
57          @Override
58          public void bytesTransferred(final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {
59              assertEquals(expectedTotal, totalBytesTransferred, "Wrong total");
60              assertEquals(expectedSize, streamSize, "Wrong streamSize");
61              assertEquals(expectedBytes, bytesTransferred, "Wrong bytes");
62          }
63  
64      }
65  
66      // Class to check overall counts as well as batch size
67      static class CSLtotal implements CopyStreamListener {
68  
69          final long expectedTotal;
70          final long expectedBytes;
71          volatile long totalBytesTransferredTotal;
72          volatile long bytesTransferredTotal;
73  
74          CSLtotal(final long totalBytesTransferred, final long bytesTransferred) {
75              this.expectedTotal = totalBytesTransferred;
76              this.expectedBytes = bytesTransferred;
77          }
78  
79          @Override
80          public void bytesTransferred(final CopyStreamEvent event) {
81              // noop
82          }
83  
84          @Override
85          public void bytesTransferred(final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {
86              assertEquals(expectedBytes, bytesTransferred, "Wrong bytes");
87              totalBytesTransferredTotal = totalBytesTransferred;
88              bytesTransferredTotal += bytesTransferred;
89          }
90  
91          void checkExpected() {
92              assertEquals(expectedTotal, totalBytesTransferredTotal, "Wrong totalBytesTransferred total");
93              assertEquals(totalBytesTransferredTotal, bytesTransferredTotal, "Total should equal sum of parts");
94          }
95  
96      }
97  
98      private final Writer dest = new CharArrayWriter();
99      private final Reader source = new CharArrayReader(new char[] { 'a' });
100 
101     private final InputStream src = new ByteArrayInputStream(new byte[] { 'z' });
102 
103     private final OutputStream dst = new ByteArrayOutputStream();
104 
105     @Test
106     void testcloseQuietly() {
107         Util.closeQuietly((Closeable) null);
108         Util.closeQuietly((Socket) null);
109     }
110 
111     @Test
112     void testNET550_Reader() throws Exception {
113         final char[] buff = { 'a', 'b', 'c', 'd' }; // must be multiple of 2
114         final int bufflen = buff.length;
115         { // Check buffer size 1 processes in chunks of 1
116             final Reader rdr = new CharArrayReader(buff);
117             final CSLtotal listener = new CSLtotal(bufflen, 1);
118             Util.copyReader(rdr, dest, 1, 0, listener); // buffer size 1
119             listener.checkExpected();
120         }
121         { // Check bufsize 2 uses chunks of 2
122             final Reader rdr = new CharArrayReader(buff);
123             final CSLtotal listener = new CSLtotal(bufflen, 2);
124             Util.copyReader(rdr, dest, 2, 0, listener); // buffer size 2
125             listener.checkExpected();
126         }
127         { // Check bigger size reads the lot
128             final Reader rdr = new CharArrayReader(buff);
129             final CSLtotal listener = new CSLtotal(bufflen, bufflen);
130             Util.copyReader(rdr, dest, 20, 0, listener); // buffer size 20
131             listener.checkExpected();
132         }
133         { // Check negative size reads reads full amount
134             final Reader rdr = new CharArrayReader(buff);
135             final CSLtotal listener = new CSLtotal(bufflen, bufflen);
136             Util.copyReader(rdr, dest, -1, 0, listener); // buffer size -1
137             listener.checkExpected();
138         }
139         { // Check zero size reads reads full amount
140             final Reader rdr = new CharArrayReader(buff);
141             final CSLtotal listener = new CSLtotal(bufflen, bufflen);
142             Util.copyReader(rdr, dest, 0, 0, listener); // buffer size -1
143             listener.checkExpected();
144         }
145     }
146 
147     @Test
148     void testNET550_Stream() throws Exception {
149         final byte[] buff = { 'a', 'b', 'c', 'd' }; // must be multiple of 2
150         final int bufflen = buff.length;
151         { // Check buffer size 1 processes in chunks of 1
152             final InputStream is = new ByteArrayInputStream(buff);
153             final CSLtotal listener = new CSLtotal(bufflen, 1);
154             Util.copyStream(is, dst, 1, 0, listener); // buffer size 1
155             listener.checkExpected();
156         }
157         { // Check bufsize 2 uses chunks of 2
158             final InputStream is = new ByteArrayInputStream(buff);
159             final CSLtotal listener = new CSLtotal(bufflen, 2);
160             Util.copyStream(is, dst, 2, 0, listener); // buffer size 2
161             listener.checkExpected();
162         }
163         { // Check bigger size reads the lot
164             final InputStream is = new ByteArrayInputStream(buff);
165             final CSLtotal listener = new CSLtotal(bufflen, bufflen);
166             Util.copyStream(is, dst, 20, 0, listener); // buffer size 20
167             listener.checkExpected();
168         }
169         { // Check negative size reads reads full amount
170             final InputStream is = new ByteArrayInputStream(buff);
171             final CSLtotal listener = new CSLtotal(bufflen, bufflen);
172             Util.copyStream(is, dst, -1, 0, listener); // buffer size -1
173             listener.checkExpected();
174         }
175         { // Check zero size reads reads full amount
176             final InputStream is = new ByteArrayInputStream(buff);
177             final CSLtotal listener = new CSLtotal(bufflen, bufflen);
178             Util.copyStream(is, dst, 0, 0, listener); // buffer size -1
179             listener.checkExpected();
180         }
181     }
182 
183     @Test
184     void testReader_1() throws Exception {
185         final long streamSize = 0;
186         final int bufferSize = -1;
187         Util.copyReader(source, dest, bufferSize, streamSize, new CSL(1, 1, streamSize));
188     }
189 
190     @Test
191     void testReader0() throws Exception {
192         final long streamSize = 0;
193         final int bufferSize = 0;
194         Util.copyReader(source, dest, bufferSize, streamSize, new CSL(1, 1, streamSize));
195     }
196 
197     @Test
198     void testReader1() throws Exception {
199         final long streamSize = 0;
200         final int bufferSize = 1;
201         Util.copyReader(source, dest, bufferSize, streamSize, new CSL(1, 1, streamSize));
202     }
203 
204     @Test
205     void testStream_1() throws Exception {
206         final long streamSize = 0;
207         final int bufferSize = -1;
208         Util.copyStream(src, dst, bufferSize, streamSize, new CSL(1, 1, streamSize));
209     }
210 
211     @Test
212     void testStream0() throws Exception {
213         final long streamSize = 0;
214         final int bufferSize = 0;
215         Util.copyStream(src, dst, bufferSize, streamSize, new CSL(1, 1, streamSize));
216     }
217 
218     @Test
219     void testStream1() throws Exception {
220         final long streamSize = 0;
221         final int bufferSize = 1;
222         Util.copyStream(src, dst, bufferSize, streamSize, new CSL(1, 1, streamSize));
223     }
224 }