1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.buildcache.checksum;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 }