View Javadoc
1   package civitas.common.capabilityencryption.tests;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertThrows;
5   import static org.mockito.Mockito.verify;
6   
7   import org.junit.jupiter.api.DisplayName;
8   import org.junit.jupiter.api.Test;
9   import org.mockito.InjectMocks;
10  import org.mockito.Mock;
11  
12  import civitas.common.capabilityencryption.EncryptCapability;
13  import civitas.common.tests.RandomAwareTestBase;
14  import civitas.common.votersubmission.tests.VoterSubmissionTestData;
15  import civitas.crypto.ciphertext.ElGamalEncrypt;
16  import civitas.crypto.reencryptfactor.GenerateElGamalReencryptFactor;
17  
18  class EncryptCapabilityTest extends RandomAwareTestBase implements VoterSubmissionTestData {
19  
20  	@InjectMocks
21  	EncryptCapability encryptCapability;
22  
23  	@Mock
24  	GenerateElGamalReencryptFactor generateElGamalReencryptFactor;
25  
26  	@Mock
27  	ElGamalEncrypt elGamalEncrypt;
28  
29  	@Test
30  	@DisplayName(
31  			"""
32  			creates an encrypted capability
33  			- generates a reencrypt factor
34  			- encrypts the capability associated with the context using the key and the reencrypt factor
35  			""")
36  	void test() throws IllegalAccessException {
37  		assertEquals(
38  				ENCRYPT_CAPABILITY_RESULT, encryptCapability.apply(EL_GAMAL_PUBLIC_KEY_E, CAPABILITY_MAP, CONTEXT_0));
39  		verify(generateElGamalReencryptFactor).apply(EL_GAMAL_PARAMETERS);
40  		verify(elGamalEncrypt)
41  				.apply(EL_GAMAL_PUBLIC_KEY_E, VOTE_CAPABILITIES.getFirst(), ELGAMAL_REENCRYPT_FACTOR_EPRIME);
42  	}
43  
44  	@Test
45  	@DisplayName(
46  			"if the capability map misses any needed capability for the vote," + "IllegalArgumentException is thrown")
47  	void test4() {
48  
49  		assertThrows(
50  				IllegalArgumentException.class,
51  				() -> encryptCapability.apply(EL_GAMAL_PUBLIC_KEY_E, CAPABILITY_MAP, SOMESTRING));
52  	}
53  }