View Javadoc
1   package civitas.crypto.keyshare.tests;
2   
3   import static org.junit.jupiter.api.Assertions.*;
4   import static org.mockito.Mockito.verify;
5   
6   import org.junit.jupiter.api.DisplayName;
7   import org.junit.jupiter.api.Test;
8   import org.mockito.InjectMocks;
9   import org.mockito.Mock;
10  
11  import civitas.crypto.CryptoException;
12  import civitas.crypto.keys.tests.ElGamalKeyShareTestData;
13  import civitas.crypto.keyshare.CombineKeyShares;
14  import civitas.crypto.keyshare.ElGamalKeyShare;
15  import civitas.crypto.keyshare.VerifyElGamalKeyShare;
16  import civitas.crypto.publickey.ElGamalPublicKey;
17  import io.github.magwas.konveyor.testing.TestBase;
18  
19  class CombineKeySharesTest extends TestBase implements ElGamalKeyShareTestData {
20  
21  	@InjectMocks
22  	CombineKeyShares combineKeyShares;
23  
24  	@Mock
25  	VerifyElGamalKeyShare verifyElGamalKeyShare;
26  
27  	@Test
28  	@DisplayName("combines an array of key shares to one " + "by multiplying the public keys")
29  	void test() throws CryptoException {
30  		ElGamalPublicKey actual = combineKeyShares.apply(KEY_SHARES);
31  		assertEquals(EL_GAMAL_PUBLIC_KEY_E.y.multiply(EL_GAMAL_PUBLIC_KEY_EPRIME.y), actual.y);
32  	}
33  
34  	@Test
35  	@DisplayName("verifies the proofs")
36  	void test_1() throws CryptoException {
37  		combineKeyShares.apply(KEY_SHARES);
38  		verify(verifyElGamalKeyShare).apply(EL_GAMAL_KEY_SHARE_E);
39  		verify(verifyElGamalKeyShare).apply(EL_GAMAL_KEY_SHARE_EPRIME);
40  	}
41  
42  	@Test
43  	@DisplayName("returns null if shares is null")
44  	void test1() throws CryptoException {
45  		assertNull(combineKeyShares.apply((ElGamalKeyShare[]) null));
46  	}
47  
48  	@Test
49  	@DisplayName("if a key share does not verify, throws CryptoException")
50  	void test2() {
51  		assertThrows(CryptoException.class, () -> combineKeyShares.apply(EL_GAMAL_KEY_SHARE_NOT_GOOD_PUBKEY_TYPE));
52  	}
53  
54  	@Test
55  	@DisplayName("if a key share is invalid throws CryptoException")
56  	void test3() {
57  		assertThrows(CryptoException.class, () -> combineKeyShares.apply(new ElGamalKeyShare[] {null}));
58  	}
59  
60  	@Test
61  	@DisplayName("returns null if shares is empty")
62  	void test4() throws CryptoException {
63  		assertNull(combineKeyShares.apply());
64  	}
65  }