1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
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 }