View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.buildcache.checksum;
20  
21  /*
22   * Licensed to the Apache Software Foundation (ASF) under one
23   * or more contributor license agreements.  See the NOTICE file
24   * distributed with this work for additional information
25   * regarding copyright ownership.  The ASF licenses this file
26   * to you under the Apache License, Version 2.0 (the
27   * "License"); you may not use this file except in compliance
28   * with the License.  You may obtain a copy of the License at
29   *
30   *   http://www.apache.org/licenses/LICENSE-2.0
31   *
32   * Unless required by applicable law or agreed to in writing,
33   * software distributed under the License is distributed on an
34   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
35   * KIND, either express or implied.  See the License for the
36   * specific language governing permissions and limitations
37   * under the License.
38   */
39  import java.nio.ByteBuffer;
40  import java.nio.charset.StandardCharsets;
41  
42  import org.apache.maven.buildcache.hash.HashAlgorithm;
43  import org.apache.maven.buildcache.hash.HashChecksum;
44  import org.junit.jupiter.api.Test;
45  
46  import static org.apache.maven.buildcache.hash.HashFactory.XX;
47  import static org.apache.maven.buildcache.hash.HashFactory.XXMM;
48  import static org.junit.jupiter.api.Assertions.assertEquals;
49  
50  public class XXHashTest {
51  
52      private static final byte[] HELLO_ARRAY = "hello".getBytes(StandardCharsets.UTF_8);
53      private static final byte[] WORLD_ARRAY = "world".getBytes(StandardCharsets.UTF_8);
54      private static final long HELLO_LONG = 2794345569481354659L;
55      private static final long WORLD_LONG = -1767385783675760145L;
56      private static final String EMPTY_HASH = "ef46db3751d8e999";
57      private static final String HELLO_HASH = "26c7827d889f6da3";
58      private static final String WORLD_HASH = "e778fbfe66ee51ef";
59      private static final String HELLO_CHECKSUM = "c07c10338a825a5d";
60      private static final String WORLD_CHECKSUM = "cb21505d7a714523";
61      private static final String FULL_CHECKSUM = "b8ca8fa824d335e9";
62  
63      private static final HashAlgorithm ALGORITHM = XX.createAlgorithm();
64  
65      @Test
66      public void testEmptyArray() {
67          byte[] emptyArray = new byte[0];
68          String hash = ALGORITHM.hash(emptyArray);
69          assertEquals(EMPTY_HASH, hash);
70      }
71  
72      @Test
73      public void testSimpleHash() {
74          String helloHash = ALGORITHM.hash(HELLO_ARRAY);
75          assertEquals(HELLO_HASH, helloHash);
76  
77          String worldHash = ALGORITHM.hash(WORLD_ARRAY);
78          assertEquals(WORLD_HASH, worldHash);
79      }
80  
81      @Test
82      public void testSimpleChecksum() {
83          String helloChecksum = ALGORITHM.hash(longToBytes(1, HELLO_LONG));
84          assertEquals(HELLO_CHECKSUM, helloChecksum);
85  
86          String worldChecksum = ALGORITHM.hash(longToBytes(1, WORLD_LONG));
87          assertEquals(WORLD_CHECKSUM, worldChecksum);
88  
89          String checksum = ALGORITHM.hash(longToBytes(2, HELLO_LONG, WORLD_LONG));
90          assertEquals(FULL_CHECKSUM, checksum);
91      }
92  
93      @Test
94      public void testEmptyBuffer() {
95          assertEmptyBuffer(XX.createChecksum(0));
96          assertEmptyBuffer(XXMM.createChecksum(0));
97      }
98  
99      @Test
100     public void testSingleHash() {
101         assertSingleHash(XX.createChecksum(1));
102         assertSingleHash(XXMM.createChecksum(1));
103     }
104 
105     @Test
106     public void testSingleChecksum() {
107         assertSingleChecksum(XX.createChecksum(1));
108         assertSingleChecksum(XXMM.createChecksum(1));
109     }
110 
111     @Test
112     public void testNotFullChecksum() {
113         assertSingleChecksum(XX.createChecksum(2));
114         assertSingleChecksum(XXMM.createChecksum(2));
115     }
116 
117     @Test
118     public void testFullChecksum() {
119         assertFullChecksum(XX.createChecksum(2));
120         assertFullChecksum(XXMM.createChecksum(2));
121     }
122 
123     private void assertEmptyBuffer(HashChecksum checksum) {
124         assertEquals(EMPTY_HASH, checksum.digest());
125     }
126 
127     private void assertSingleHash(HashChecksum checksum) {
128         assertEquals(HELLO_HASH, checksum.update(HELLO_ARRAY));
129         assertEquals(HELLO_CHECKSUM, checksum.digest());
130     }
131 
132     private void assertSingleChecksum(HashChecksum checksum) {
133         assertEquals(HELLO_HASH, checksum.update(HELLO_HASH));
134         assertEquals(HELLO_CHECKSUM, checksum.digest());
135     }
136 
137     private void assertFullChecksum(HashChecksum checksum) {
138         assertEquals(HELLO_HASH, checksum.update(HELLO_HASH));
139         assertEquals(WORLD_HASH, checksum.update(WORLD_HASH));
140         assertEquals(FULL_CHECKSUM, checksum.digest());
141     }
142 
143     private byte[] longToBytes(int size, long... values) {
144         final ByteBuffer buffer = ByteBuffer.allocate(size * Long.SIZE / Byte.SIZE);
145         for (long value : values) {
146             buffer.putLong(value);
147         }
148         return buffer.array();
149     }
150 }