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.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          // @formatter:off
51          return Stream.of(
52              // reference checksums created with xxh32sum
53              // https://cyan4973.github.io/xxHash/
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          // @formatter:on
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             // Hit the case where the hash should be reset
106             h.update(b[0]);
107             h.reset();
108             // Pass in chunks
109             h.update(b[0]);
110             h.update(b, 1, b.length - 2);
111             h.update(b, b.length - 1, 1);
112             // Check the hash ignores negative length
113             h.update(b, 0, -1);
114         }
115         assertEquals(expectedChecksum, Long.toHexString(h.getValue()), "checksum for " + file.getName());
116     }
117 }