View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.utils;
20  
21  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.IOException;
27  import java.util.zip.Adler32;
28  import java.util.zip.CRC32;
29  
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests for class {@link ChecksumVerifyingInputStream org.apache.commons.compress.utils.ChecksumVerifyingInputStream}.
34   *
35   * @see ChecksumVerifyingInputStream
36   */
37  class ChecksumVerifyingInputStreamTest {
38  
39      @Test
40      void testReadTakingByteArrayThrowsIOException() throws IOException {
41          final Adler32 adler32 = new Adler32();
42          final byte[] byteArray = new byte[3];
43          final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
44          try (ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(adler32, byteArrayInputStream, -1859L, (byte) -68)) {
45              assertThrows(IOException.class, () -> checksumVerifyingInputStream.read(byteArray));
46          }
47      }
48  
49      @Test
50      void testReadTakingNoArgumentsThrowsIOException() throws IOException {
51          final CRC32 crc32 = new CRC32();
52          final byte[] byteArray = new byte[9];
53          final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
54          try (ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(crc32, byteArrayInputStream, (byte) 1, (byte) 1)) {
55              assertThrows(IOException.class, () -> checksumVerifyingInputStream.read());
56          }
57      }
58  
59      @Test
60      void testSkip() throws IOException {
61          final CRC32 crc32 = new CRC32();
62          final byte[] byteArray = new byte[4];
63          final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
64          try (ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(crc32, byteArrayInputStream, (byte) 33, 2303L)) {
65              @SuppressWarnings("unused")
66              final int intOne = checksumVerifyingInputStream.read(byteArray);
67              final long skipReturnValue = checksumVerifyingInputStream.skip((byte) 1);
68              assertEquals(558161692L, crc32.getValue());
69              assertEquals(0, byteArrayInputStream.available());
70              assertArrayEquals(new byte[4], byteArray);
71              assertEquals(0L, skipReturnValue);
72          }
73      }
74  
75  }