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    *      https://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.codec.digest;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.net.URL;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  import java.nio.file.Paths;
30  import java.util.stream.Stream;
31  
32  import org.apache.commons.io.IOUtils;
33  import org.junit.jupiter.params.ParameterizedTest;
34  import org.junit.jupiter.params.provider.Arguments;
35  import org.junit.jupiter.params.provider.MethodSource;
36  
37  class XXHash32Test {
38  
39      private static long copy(final InputStream input, final OutputStream output, final int bufferSize) throws IOException {
40          return IOUtils.copyLarge(input, output, new byte[bufferSize]);
41      }
42  
43      public static Stream<Arguments> data() {
44          // @formatter:off
45          return Stream.of(
46              // reference checksums created with xxh32sum
47              // https://cyan4973.github.io/xxHash/
48              Arguments.of("org/apache/commons/codec/bla.tar", "fbb5c8d1"),
49              Arguments.of("org/apache/commons/codec/bla.tar.xz", "4106a208"),
50              Arguments.of("org/apache/commons/codec/small.bin", "f66c26f8")
51          );
52          // @formatter:on
53      }
54  
55      private static byte[] toByteArray(final InputStream input) throws IOException {
56          final ByteArrayOutputStream output = new ByteArrayOutputStream();
57          copy(input, output, 10240);
58          return output.toByteArray();
59      }
60  
61      private Path file;
62  
63      private String expectedChecksum;
64  
65      public void initData(final String path, final String c) throws Exception {
66          final URL url = XXHash32Test.class.getClassLoader().getResource(path);
67          if (url == null) {
68              throw new FileNotFoundException("couldn't find " + path);
69          }
70          file = Paths.get(url.toURI());
71          expectedChecksum = c;
72      }
73  
74      @ParameterizedTest
75      @MethodSource("data")
76      public void verifyChecksum(final String path, final String c) throws Exception {
77          initData(path, c);
78          final XXHash32 hasher = new XXHash32();
79          try (InputStream in = Files.newInputStream(file)) {
80              final byte[] bytes = toByteArray(in);
81              hasher.update(bytes, 0, bytes.length);
82          }
83          assertEquals(expectedChecksum, Long.toHexString(hasher.getValue()), "checksum for " + file);
84      }
85  
86      @ParameterizedTest
87      @MethodSource("data")
88      public void verifyIncrementalChecksum(final String path, final String c) throws Exception {
89          initData(path, c);
90          final XXHash32 hasher = new XXHash32();
91          try (InputStream in = Files.newInputStream(file)) {
92              final byte[] bytes = toByteArray(in);
93              // Hit the case where the hash should be reset
94              hasher.update(bytes[0]);
95              hasher.reset();
96              // Pass in chunks
97              hasher.update(bytes[0]);
98              hasher.update(bytes, 1, bytes.length - 2);
99              hasher.update(bytes, bytes.length - 1, 1);
100             // Check the hash ignores negative length
101             hasher.update(bytes, 0, -1);
102         }
103         assertEquals(expectedChecksum, Long.toHexString(hasher.getValue()), "checksum for " + file);
104     }
105 }