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.compress.utils;
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.util.zip.Adler32;
26  import java.util.zip.CRC32;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Unit tests for class {@link ChecksumVerifyingInputStream org.apache.commons.compress.utils.ChecksumVerifyingInputStream}.
32   *
33   * @see ChecksumVerifyingInputStream
34   */
35  public class ChecksumVerifyingInputStreamTest {
36  
37      @Test
38      public void testReadTakingByteArrayThrowsIOException() throws IOException {
39          final Adler32 adler32 = new Adler32();
40          final byte[] byteArray = new byte[3];
41          final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
42          try (ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(adler32, byteArrayInputStream, -1859L, (byte) -68)) {
43              assertThrows(IOException.class, () -> checksumVerifyingInputStream.read(byteArray));
44          }
45      }
46  
47      @Test
48      public void testReadTakingNoArgumentsThrowsIOException() throws IOException {
49          final CRC32 crc32 = new CRC32();
50          final byte[] byteArray = new byte[9];
51          final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
52          try (ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(crc32, byteArrayInputStream, (byte) 1, (byte) 1)) {
53              assertThrows(IOException.class, () -> checksumVerifyingInputStream.read());
54          }
55      }
56  
57      @Test
58      public void testSkip() throws IOException {
59          final CRC32 crc32 = new CRC32();
60          final byte[] byteArray = new byte[4];
61          final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
62          try (ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(crc32, byteArrayInputStream, (byte) 33, 2303L)) {
63              @SuppressWarnings("unused")
64              final int intOne = checksumVerifyingInputStream.read(byteArray);
65              final long skipReturnValue = checksumVerifyingInputStream.skip((byte) 1);
66              assertEquals(558161692L, crc32.getValue());
67              assertEquals(0, byteArrayInputStream.available());
68              assertArrayEquals(new byte[4], byteArray);
69              assertEquals(0L, skipReturnValue);
70          }
71      }
72  
73  }