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    *      http://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  package org.apache.commons.io.input;
18  
19  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.zip.Adler32;
27  import java.util.zip.CRC32;
28  
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests {@link ChecksumInputStream}.
33   */
34  public class ChecksumInputStreamTest {
35  
36      @Test
37      public void testDefaultThresholdFailure() throws IOException {
38          final byte[] byteArray = new byte[3];
39          final Adler32 adler32 = new Adler32();
40          try (ChecksumInputStream checksum = ChecksumInputStream.builder()
41          // @formatter:off
42                  .setByteArray(byteArray)
43                  .setChecksum(adler32)
44                  .setExpectedChecksumValue((byte) -68)
45                  .get()) {
46                  // @formatter:on
47              assertEquals(0, checksum.getByteCount());
48              assertEquals(-1, checksum.getRemaining());
49              // Ask to read one more byte than there is, we get the correct byte count.
50              assertEquals(byteArray.length, checksum.read(new byte[byteArray.length + 1]));
51              // Next read is at EOF
52              assertThrows(IOException.class, () -> checksum.read(new byte[1]));
53              assertEquals(byteArray.length, checksum.getByteCount());
54              assertEquals(-4, checksum.getRemaining());
55          }
56      }
57  
58      @Test
59      public void testDefaultThresholdSuccess() throws IOException {
60          // sanity-check
61          final Adler32 sanityCheck = new Adler32();
62          final byte[] byteArray = new byte[3];
63          sanityCheck.update(byteArray);
64          final long expectedChecksum = sanityCheck.getValue();
65          // actual
66          final Adler32 adler32 = new Adler32();
67          try (ChecksumInputStream checksum = ChecksumInputStream.builder()
68          // @formatter:off
69                  .setByteArray(byteArray)
70                  .setChecksum(adler32)
71                  .setExpectedChecksumValue(expectedChecksum)
72                  .get()) {
73                  // @formatter:on
74              assertEquals(0, checksum.getByteCount());
75              assertEquals(-1, checksum.getRemaining());
76              assertEquals(3, checksum.read(byteArray));
77              assertEquals(byteArray.length, checksum.getByteCount());
78              assertEquals(-4, checksum.getRemaining());
79              assertEquals(-1, checksum.read(byteArray));
80              assertEquals(byteArray.length, checksum.getByteCount());
81              assertEquals(-4, checksum.getRemaining());
82          }
83      }
84  
85      @Test
86      public void testReadTakingByteArrayThrowsException() throws IOException {
87          final Adler32 adler32 = new Adler32();
88          final byte[] byteArray = new byte[3];
89          final long sizeThreshold = -1859L;
90          try (ChecksumInputStream checksum = ChecksumInputStream.builder()
91          // @formatter:off
92                  .setByteArray(byteArray)
93                  .setChecksum(adler32)
94                  .setExpectedChecksumValue((byte) -68)
95                  .setCountThreshold(sizeThreshold)
96                  .get()) {
97                  // @formatter:on
98              assertEquals(0, checksum.getByteCount());
99              assertEquals(sizeThreshold, checksum.getRemaining());
100             // Ask to read one more byte than there is.
101             assertEquals(byteArray.length, checksum.read(new byte[byteArray.length + 1]));
102             // Next read is at EOF
103             assertThrows(IOException.class, () -> checksum.read(new byte[1]));
104             assertEquals(byteArray.length, checksum.getByteCount());
105             assertEquals(sizeThreshold - byteArray.length, checksum.getRemaining());
106         }
107     }
108 
109     @Test
110     public void testReadTakingNoArgumentsThrowsException() throws IOException {
111         final CRC32 crc32 = new CRC32();
112         final byte[] byteArray = new byte[9];
113         try (ChecksumInputStream checksum = ChecksumInputStream.builder()
114         // @formatter:off
115                 .setByteArray(byteArray)
116                 .setChecksum(crc32)
117                 .setExpectedChecksumValue((byte) 1)
118                 .setCountThreshold(1)
119                 .get()) {
120                 // @formatter:on
121             assertEquals(0, checksum.getByteCount());
122             assertEquals(1, checksum.getRemaining());
123             assertThrows(IOException.class, () -> checksum.read());
124             assertEquals(1, checksum.getByteCount());
125             assertEquals(0, checksum.getRemaining());
126         }
127     }
128 
129     @Test
130     public void testSkip() throws IOException {
131         // sanity-check
132         final CRC32 sanityCheck = new CRC32();
133         final byte[] byteArray = new byte[4];
134         sanityCheck.update(byteArray);
135         final long expectedChecksum = sanityCheck.getValue();
136         // actual
137         final CRC32 crc32 = new CRC32();
138         final InputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
139         try (ChecksumInputStream checksum = ChecksumInputStream.builder()
140         // @formatter:off
141                 .setInputStream(byteArrayInputStream)
142                 .setChecksum(crc32)
143                 .setExpectedChecksumValue(expectedChecksum)
144                 .setCountThreshold(33)
145                 .get()) {
146                 // @formatter:on
147             assertEquals(0, checksum.getByteCount());
148             assertEquals(4, checksum.read(byteArray));
149             assertEquals(byteArray.length, checksum.getByteCount());
150             assertEquals(29, checksum.getRemaining());
151             final long skipReturnValue = checksum.skip((byte) 1);
152             assertEquals(byteArray.length, checksum.getByteCount());
153             assertEquals(29, checksum.getRemaining());
154             assertEquals(558161692L, crc32.getValue());
155             assertEquals(0, byteArrayInputStream.available());
156             assertArrayEquals(new byte[4], byteArray);
157             assertEquals(0L, skipReturnValue);
158             assertEquals(29, checksum.getRemaining());
159         }
160     }
161 
162 }