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