1 package civitas.common.votercapabilitysharesandproofs;
2
3 import static org.junit.jupiter.api.Assertions.assertFalse;
4 import static org.junit.jupiter.api.Assertions.assertThrows;
5 import static org.junit.jupiter.api.Assertions.assertTrue;
6
7 import org.junit.jupiter.api.DisplayName;
8 import org.junit.jupiter.api.Test;
9 import org.mockito.InjectMocks;
10
11 import civitas.common.RandomAwareTestBase;
12 import civitas.common.electionresults.TellerTestData;
13 import civitas.crypto.signedciphertext.ElGamalSignedCiphertext;
14
15 class VerifyVoterCapabilitySharesAndProofTest extends RandomAwareTestBase
16 implements VoterCapabilitySharesAndProofTestData, TellerTestData {
17
18 @InjectMocks
19 VerifyVoterCapabilitySharesAndProof verifyVoterCapabilitySharesAndProof;
20
21 @Test
22 @DisplayName(
23 """
24 verifies the voter capabilities and proofs
25 - check that p.e equals the posted capability
26 - check that the posted capability verifies
27 - check that p.e' equals enc(vc, ttKey r)
28 - check the proof, i.e., that p.e is a reencryption of p.e'
29 """)
30 void test() {
31 assertTrue(verifyVoterCapabilitySharesAndProof.apply(
32 VOTER_CAPABILITIES_AND_PROOFS,
33 POSTED_CAPABILITIES,
34 EL_GAMAL_PUBLIC_KEY_E,
35 EL_GAMAL_PUBLIC_KEY_E,
36 VOTER_NAME,
37 TELLER_INDEX));
38 }
39
40 @Test
41 @DisplayName("if the proof does not verify, the check fails")
42 void test1() {
43 assertFalse(verifyVoterCapabilitySharesAndProof.apply(
44 VOTER_CAPABILITIES_AND_PROOFS,
45 POSTED_CAPABILITIES,
46 EL_GAMAL_PUBLIC_KEY_EPRIME,
47 EL_GAMAL_PUBLIC_KEY_E,
48 VOTER_NAME,
49 TELLER_INDEX));
50 }
51
52 @Test
53 @DisplayName("if p.e' != enc(vc, ttKey r), the check fails")
54 void test2() {
55 assertFalse(verifyVoterCapabilitySharesAndProof.apply(
56 VOTER_CAPABILITIES_AND_PROOFS,
57 POSTED_CAPABILITIES,
58 EL_GAMAL_PUBLIC_KEY_E,
59 EL_GAMAL_PUBLIC_KEY_EPRIME,
60 VOTER_NAME,
61 TELLER_INDEX));
62 }
63
64 @Test
65 @DisplayName("if the posted capability does not verify, the check fails")
66 void test3() {
67 assertFalse(verifyVoterCapabilitySharesAndProof.apply(
68 VOTER_CAPABILITIES_AND_PROOFS_CAP_NONVERIFY,
69 POSTED_CAPABILITIES_NONVERIFY,
70 EL_GAMAL_PUBLIC_KEY_E,
71 EL_GAMAL_PUBLIC_KEY_E,
72 VOTER_NAME,
73 TELLER_INDEX));
74 }
75
76 @Test
77 @DisplayName("if p.e != the posted capability, the check fails")
78 void test4() {
79 assertFalse(verifyVoterCapabilitySharesAndProof.apply(
80 VOTER_CAPABILITIES_AND_PROOFS,
81 POSTED_CAPABILITIES_NONVERIFY,
82 EL_GAMAL_PUBLIC_KEY_E,
83 EL_GAMAL_PUBLIC_KEY_E,
84 VOTER_NAME,
85 TELLER_INDEX));
86 }
87
88 @Test
89 @DisplayName("if tabTellerSharedPublicKey is null, NullPointerException is throwns")
90 void test5() {
91 assertThrows(
92 NullPointerException.class,
93 () -> verifyVoterCapabilitySharesAndProof.apply(
94 VOTER_CAPABILITIES_AND_PROOFS,
95 POSTED_CAPABILITIES,
96 EL_GAMAL_PUBLIC_KEY_E,
97 null,
98 VOTER_NAME,
99 TELLER_INDEX));
100 }
101
102 @Test
103 @DisplayName("if posted capabilities is null, NullPointerException is throwns")
104 void test6() {
105 assertThrows(
106 NullPointerException.class,
107 () -> verifyVoterCapabilitySharesAndProof.apply(
108 VOTER_CAPABILITIES_AND_PROOFS,
109 null,
110 EL_GAMAL_PUBLIC_KEY_E,
111 EL_GAMAL_PUBLIC_KEY_E,
112 VOTER_NAME,
113 TELLER_INDEX));
114 }
115
116 @Test
117 @DisplayName(
118 "if the length of posted capabilities and the capabilities in the VoterCapabilitySharesAndProof are different, the check fails")
119 void test7() {
120 assertFalse(verifyVoterCapabilitySharesAndProof.apply(
121 VOTER_CAPABILITIES_AND_PROOFS,
122 new ElGamalSignedCiphertext[1],
123 EL_GAMAL_PUBLIC_KEY_E,
124 EL_GAMAL_PUBLIC_KEY_E,
125 VOTER_NAME,
126 TELLER_INDEX));
127 }
128
129 @Test
130 @DisplayName(
131 "if the length of capabilities and reencrypt factors in the VoterCapabilitySharesAndProof are different, the check fails")
132 void test8() {
133 assertFalse(verifyVoterCapabilitySharesAndProof.apply(
134 VOTER_CAPABILITIES_AND_PROOFS_BAD_FACTOR_COUNTS,
135 POSTED_CAPABILITIES,
136 EL_GAMAL_PUBLIC_KEY_E,
137 EL_GAMAL_PUBLIC_KEY_E,
138 VOTER_NAME,
139 TELLER_INDEX));
140 }
141
142 @Test
143 @DisplayName(
144 "if the length of capabilities and reencrypt factors in the VoterCapabilitySharesAndProof are different, the check fails")
145 void test9() {
146 assertFalse(verifyVoterCapabilitySharesAndProof.apply(
147 VOTER_CAPABILITIES_AND_PROOFS_BAD_PROOF_COUNT,
148 POSTED_CAPABILITIES,
149 EL_GAMAL_PUBLIC_KEY_E,
150 EL_GAMAL_PUBLIC_KEY_E,
151 VOTER_NAME,
152 TELLER_INDEX));
153 }
154
155 @Test
156 @DisplayName("if VoterCapabilitySharesAndProof is null, NullPointerException is throwns")
157 void test10() {
158 assertThrows(
159 NullPointerException.class,
160 () -> verifyVoterCapabilitySharesAndProof.apply(
161 null,
162 POSTED_CAPABILITIES,
163 EL_GAMAL_PUBLIC_KEY_E,
164 EL_GAMAL_PUBLIC_KEY_E,
165 VOTER_NAME,
166 TELLER_INDEX));
167 }
168
169 @Test
170 @DisplayName("if the voter public key is null, NullPointerException is throwns")
171 void test11() {
172 assertThrows(
173 NullPointerException.class,
174 () -> verifyVoterCapabilitySharesAndProof.apply(
175 VOTER_CAPABILITIES_AND_PROOFS,
176 POSTED_CAPABILITIES,
177 null,
178 EL_GAMAL_PUBLIC_KEY_E,
179 VOTER_NAME,
180 TELLER_INDEX));
181 }
182 }