View Javadoc
1   package civitas.crypto.tests;
2   
3   import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4   import static org.junit.jupiter.api.Assertions.assertEquals;
5   
6   import java.io.ByteArrayInputStream;
7   import java.io.DataInputStream;
8   import java.io.IOException;
9   import java.util.Base64;
10  import java.util.List;
11  import java.util.stream.Stream;
12  
13  import org.junit.jupiter.api.DisplayName;
14  import org.junit.jupiter.api.Tag;
15  import org.junit.jupiter.api.Test;
16  import org.mockito.InjectMocks;
17  
18  import civitas.common.ballotdesign.tests.BallotDesignTestData;
19  import civitas.crypto.messagedigest.CryptoHash;
20  import civitas.crypto.messagedigest.tests.MessageDigestTestData;
21  import civitas.crypto.signedciphertext.tests.ElGamalSignedCiphertextTestData;
22  import civitas.util.CivitasBigIntegerFactory;
23  import io.github.magwas.konveyor.testing.TestBase;
24  
25  @Tag("functional")
26  class CryptoHashFunctionalTest extends TestBase
27  		implements MessageDigestTestData, BallotDesignTestData, ElGamalSignedCiphertextTestData {
28  
29  	@InjectMocks
30  	CryptoHash cryptoHash;
31  
32  	@Test
33  	@DisplayName("if updated with (byte[]) null, nothing happens")
34  	void test2() {
35  		assertArrayEquals(BASELINE_DIGEST.digest(), cryptoHash.apply((byte[]) null));
36  	}
37  
38  	@Test
39  	@DisplayName("hash for list of CivitasBigintegers is correct")
40  	void test_list() {
41  		BASELINE_DIGEST.update(BIGINT_A.toByteArray());
42  		BASELINE_DIGEST.update(BIGINT_B.toByteArray());
43  		BASELINE_DIGEST.update(BIGINT_C.toByteArray());
44  		BASELINE_DIGEST.update(BIGINT_D.toByteArray());
45  		assertArrayEquals(BASELINE_DIGEST.digest(), cryptoHash.apply(List.of(BIGINT_A, BIGINT_B, BIGINT_C, BIGINT_D)));
46  	}
47  
48  	@Test
49  	@DisplayName("hash for list of CivitasBigintegers is correct even if some of them is null")
50  	void test_list2() {
51  		BASELINE_DIGEST.update(BIGINT_A.toByteArray());
52  		BASELINE_DIGEST.update(BIGINT_C.toByteArray());
53  		BASELINE_DIGEST.update(BIGINT_D.toByteArray());
54  
55  		assertArrayEquals(
56  				BASELINE_DIGEST.digest(),
57  				cryptoHash.apply(Stream.of(BIGINT_A, null, BIGINT_C, BIGINT_D).toList()));
58  	}
59  
60  	@Test
61  	@Tag("testdata")
62  	@DisplayName("CURRENT_TIME")
63  	void test10() throws IOException {
64  		assertEquals(
65  				CURRENT_TIME,
66  				new DataInputStream(new ByteArrayInputStream(CURRENT_TIME_STRINGBASE.getBytes())).readLong());
67  	}
68  
69  	@Test
70  	@DisplayName("hash for three bigintegers and an environment byte array is correct")
71  	void test8() {
72  		assertEquals(
73  				EL_GAMAL_SIGNED_CIPHERTEXT_C_BASE64,
74  				Base64.getEncoder()
75  						.encodeToString(cryptoHash
76  								.apply(
77  										EL_GAMAL_SIGNED_CIPHERTEXT_HASH1,
78  										EL_GAMAL_SIGNED_CIPHERTEXT_A,
79  										EL_GAMAL_SIGNED_CIPHERTEXT_B,
80  										ADDITIONALENV_BYTES)
81  								.toByteArray()));
82  	}
83  
84  	@Test
85  	@DisplayName("hash for two bigintegers and an environment byte array is correct")
86  	void test9() {
87  		BASELINE_DIGEST.update(BIGINT_A.toByteArray());
88  		BASELINE_DIGEST.update(BIGINT_B.toByteArray());
89  		BASELINE_DIGEST.update(ADDITIONALENV_BYTES);
90  
91  		assertEquals(
92  				CivitasBigIntegerFactory.obtain(BASELINE_DIGEST.digest()),
93  				cryptoHash.apply(BIGINT_A, BIGINT_B, null, ADDITIONALENV_BYTES));
94  	}
95  }