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