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.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  }