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.charset.StandardCharsets;
40
41 import org.apache.maven.buildcache.hash.HashAlgorithm;
42 import org.apache.maven.buildcache.hash.HashChecksum;
43 import org.junit.jupiter.api.Test;
44
45 import static org.apache.maven.buildcache.hash.HashFactory.SHA256;
46 import static org.junit.jupiter.api.Assertions.assertEquals;
47
48 public class SHAHashTest {
49
50 private static final byte[] HELLO_ARRAY = "hello".getBytes(StandardCharsets.UTF_8);
51 private static final byte[] WORLD_ARRAY = "world".getBytes(StandardCharsets.UTF_8);
52 private static final String EMPTY_HASH = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
53 private static final String HELLO_HASH = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";
54 private static final String WORLD_HASH = "486ea46224d1bb4fb680f34f7c9ad96a8f24ec88be73ea8e5a6c65260e9cb8a7";
55 private static final String HELLO_CHECKSUM = "9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50";
56 private static final String WORLD_CHECKSUM = "63e5c163c81ee9a3ed99d365ff963ecea340cc455deeac7c4b63ac75b9cf4706";
57 private static final String FULL_CHECKSUM = "7305db9b2abccd706c256db3d97e5ff48d677cfe4d3a5904afb7da0e3950e1e2";
58
59 private static final HashAlgorithm ALGORITHM = SHA256.createAlgorithm();
60 private static final HashChecksum CHECKSUM = SHA256.createChecksum(0);
61
62 @Test
63 public void testEmptyArray() {
64 byte[] emptyArray = new byte[0];
65 String hash = ALGORITHM.hash(emptyArray);
66 assertEquals(EMPTY_HASH, hash);
67 }
68
69 @Test
70 public void testSimpleHash() {
71 String helloHash = ALGORITHM.hash(HELLO_ARRAY);
72 assertEquals(HELLO_HASH, helloHash);
73
74 String worldHash = ALGORITHM.hash(WORLD_ARRAY);
75 assertEquals(WORLD_HASH, worldHash);
76 }
77
78 @Test
79 public void testSimpleChecksum() {
80 assertEquals(HELLO_HASH, CHECKSUM.update(HELLO_ARRAY));
81 assertEquals(HELLO_CHECKSUM, CHECKSUM.digest());
82
83 assertEquals(WORLD_HASH, CHECKSUM.update(WORLD_ARRAY));
84 assertEquals(WORLD_CHECKSUM, CHECKSUM.digest());
85
86 assertEquals(HELLO_HASH, CHECKSUM.update(HELLO_ARRAY));
87 assertEquals(WORLD_HASH, CHECKSUM.update(WORLD_ARRAY));
88 assertEquals(FULL_CHECKSUM, CHECKSUM.digest());
89 }
90 }