1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.codec.digest;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.net.URI;
29 import java.net.URL;
30 import java.util.stream.Stream;
31
32 import org.junit.jupiter.params.ParameterizedTest;
33 import org.junit.jupiter.params.provider.Arguments;
34 import org.junit.jupiter.params.provider.MethodSource;
35
36 public class XXHash32Test {
37
38 private static long copy(final InputStream input, final OutputStream output, final int bufferSize) throws IOException {
39 final byte[] buffer = new byte[bufferSize];
40 int n = 0;
41 long count = 0;
42 while (-1 != (n = input.read(buffer))) {
43 output.write(buffer, 0, n);
44 count += n;
45 }
46 return count;
47 }
48
49 public static Stream<Arguments> data() {
50
51 return Stream.of(
52
53
54 Arguments.of("org/apache/commons/codec/bla.tar", "fbb5c8d1"),
55 Arguments.of("org/apache/commons/codec/bla.tar.xz", "4106a208"),
56 Arguments.of("org/apache/commons/codec/small.bin", "f66c26f8")
57 );
58
59 }
60
61 private static byte[] toByteArray(final InputStream input) throws IOException {
62 final ByteArrayOutputStream output = new ByteArrayOutputStream();
63 copy(input, output, 10240);
64 return output.toByteArray();
65 }
66
67 private File file;
68
69 private String expectedChecksum;
70
71 public void initData(final String path, final String c) throws IOException {
72 final URL url = XXHash32Test.class.getClassLoader().getResource(path);
73 if (url == null) {
74 throw new FileNotFoundException("couldn't find " + path);
75 }
76 URI uri = null;
77 try {
78 uri = url.toURI();
79 } catch (final java.net.URISyntaxException ex) {
80 throw new IOException(ex);
81 }
82 file = new File(uri);
83 expectedChecksum = c;
84 }
85
86 @ParameterizedTest
87 @MethodSource("data")
88 public void verifyChecksum(final String path, final String c) throws IOException {
89 initData(path, c);
90 final XXHash32 h = new XXHash32();
91 try (final FileInputStream s = new FileInputStream(file)) {
92 final byte[] b = toByteArray(s);
93 h.update(b, 0, b.length);
94 }
95 assertEquals(expectedChecksum, Long.toHexString(h.getValue()), "checksum for " + file.getName());
96 }
97
98 @ParameterizedTest
99 @MethodSource("data")
100 public void verifyIncrementalChecksum(final String path, final String c) throws IOException {
101 initData(path, c);
102 final XXHash32 h = new XXHash32();
103 try (final FileInputStream s = new FileInputStream(file)) {
104 final byte[] b = toByteArray(s);
105
106 h.update(b[0]);
107 h.reset();
108
109 h.update(b[0]);
110 h.update(b, 1, b.length - 2);
111 h.update(b, b.length - 1, 1);
112
113 h.update(b, 0, -1);
114 }
115 assertEquals(expectedChecksum, Long.toHexString(h.getValue()), "checksum for " + file.getName());
116 }
117 }