1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
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 }