def test_0_submit_audit_request(self): election, _, voting, _, audit_requests, _, messages = \ self.get_voting_context() messages.append('\nTesting audit-requst submission\n') submit_audit_request = election.submit_audit_request get_audit_request = election.get_audit_request get_vote = election.get_vote for vote in audit_requests: vote = deepcopy(vote) with self.subTest(vote=vote): vote = election.adapt_vote(vote) (_, _, voter_key, _, fingerprint, _, _, _, _, _, _) = \ extract_vote(vote) submit_audit_request(fingerprint, voter_key, vote) assert (get_audit_request(fingerprint) is voter_key and \ get_vote(fingerprint) is vote) messages.append('[+] Audit-request successfully submitted') # Test rejection of already submitted request with self.subTest(vote=audit_requests[0]): (_, _, voter_key, _, fingerprint, _, _, _, _, _, _) = \ extract_vote(vote) with self.assertRaises(VoteRejectionError): submit_audit_request(fingerprint, voter_key, vote) messages.append('[+] Audit-request successfully rejected')
def test_4_submit_genuine_vote(self): self.clear_election() election, _, voting, votes, _, _, messages = \ self.get_voting_context() messages.append('\nTesting genuine vote submission\n') submit_genuine_vote = election.submit_genuine_vote get_voter_cast_votes = election.get_voter_cast_votes get_vote = election.get_vote for vote in votes: with self.subTest(vote=vote): vote = deepcopy(vote) vote = election.adapt_vote(vote) (_, _, voter_key, _, fingerprint, _, _, _, _, _, _) = \ extract_vote(vote) submit_genuine_vote(fingerprint, voter_key, vote) assert (fingerprint in get_voter_cast_votes(voter_key) and \ get_vote(fingerprint) is vote) messages.append('[+] Vote successfully submitted') # Test rejection of already submitted vote with self.subTest(vote=votes[0]): vote = deepcopy(vote) (_, _, voter_key, _, fingerprint, _, _, _, _, _, _) = \ extract_vote(vote) with self.assertRaises(VoteRejectionError): submit_genuine_vote(fingerprint, voter_key, vote) messages.append('[+] Submitted vote: Successfully rejected') # Test rejection upon vote limit election.options['vote_limit'] = 1 with self.subTest(vote=votes[0]): vote = deepcopy(vote) vote['fingerprint'] += '0' (_, _, voter_key, _, fingerprint, _, _, _, _, _, _) = \ extract_vote(vote) with self.assertRaises(VoteRejectionError): submit_genuine_vote(fingerprint, voter_key, vote) messages.append('[+] Vote limit reached: Successfully rejected') del election.options['vote_limit'] # Test rejection upon invalidity with self.subTest(vote=votes[0]): vote = deepcopy(vote) (_, _, voter_key, _, fingerprint, _, _, _, _, _, _) = \ extract_vote(vote) vote['public'] += 1 with self.assertRaises(VoteRejectionError): submit_genuine_vote(fingerprint, voter_key, vote) messages.append('[+] Invalid vote: Successfully rejected')
def get_decrypted_value(adapted_vote): _, _, _, encrypted_ballot, _, _, voter_secret, _, _, _, _ = \ extract_vote(adapted_vote) ciphertext, _ = cryptosys.extract_ciphertext_proof( encrypted_ballot) election_key = self.get_election_key() decrypted = cryptosys.decrypt_with_randomness( ciphertext, election_key, voter_secret).value return decrypted
def test_3_submit_audit_vote_rejection(self): election, _, voting, _, _, audit_votes, messages = \ self.get_voting_context() messages.append('\nTesting audit-vote submission on failure\n') votes_and_messages = [] # No audit-code provided vote_0 = deepcopy(audit_votes[0]) del vote_0['audit_code'] votes_and_messages.append(( vote_0, '[+] Missing audit-code: Successfully rejected', )) # Invalid audit-code provided vote_1 = deepcopy(audit_votes[1]) voter_key = vote_1['voter'] voter_audit_codes = election.get_voter_audit_codes(voter_key) vote_1['audit_code'] = voter_audit_codes[0] votes_and_messages.append(( vote_1, '[+] Invalid audit-code: Successfully rejected', )) # No prior audit-requst vote_2 = deepcopy(audit_votes[2]) vote_2['fingerprint'] += '0' votes_and_messages.append(( vote_2, '[+] No prior request: Successfully rejected', )) # Missing voter's secret vote_3 = deepcopy(audit_votes[3]) del vote_3['voter_secret'] votes_and_messages.append(( vote_3, '[+] Missing secret: Successfully rejected', )) submit_audit_vote = election.submit_audit_vote for vote, success_msg in votes_and_messages: with self.subTest(vote=vote): vote = election.adapt_vote(vote) (_, _, voter_key, _, fingerprint, voter_audit_code, \ _, _, _, _, _) = extract_vote(vote) voter_audit_codes = election.get_voter_audit_codes(voter_key) with self.assertRaises(VoteRejectionError): submit_audit_vote(vote, voter_key, fingerprint, voter_audit_code, voter_audit_codes) messages.append(success_msg)
def textify_vote(signer, vote, comments, corrupt_trustees=False, corrupt_candidates=False, malformed=False): """ Emulates the Signer.textify_vote() method with failure options for testing purposes. The provided signer should be an instance of the present module's DummySigner class """ election = signer.election cryptosys = signer.cryptosys hex_zeus_public_key = election.get_hex_zeus_public_key() hex_trustee_keys = election.get_hex_trustee_keys()[:] candidates = election.get_candidates()[:] (vote_crypto, vote_election_key, _, encrypted_ballot, fingerprint, _, _, previous, index, status, _) = extract_vote(vote) hex_parameters = cryptosys.hexify_crypto_params(vote_crypto) # possibly corrupted hex_election_key = '%x' % vote_election_key # possibly corrupted alpha, beta, commitment, challenge, response = \ election.hexify_encrypted_ballot(encrypted_ballot) # Further corruptions if corrupt_trustees: del hex_trustee_keys[-1] if corrupt_candidates: del candidates[-1] t00 = status if status is not None else NONE if malformed: t00 = 'MALFORMED... ' + t00 t01 = V_FINGERPRINT + fingerprint t02 = V_INDEX + f'{index if index is not None else NONE}' t03 = V_PREVIOUS + f'{previous if previous is not None else NONE}' t04 = V_ELECTION + hex_election_key t05 = V_ZEUS_PUBLIC + hex_zeus_public_key t06 = V_TRUSTEES + ' '.join(hex_trustee_keys) t07 = V_CANDIDATES + ' % '.join(candidates) t08, t09, t10 = hex_parameters t11 = V_ALPHA + alpha t12 = V_BETA + beta t13 = V_COMMITMENT + commitment t14 = V_CHALLENGE + challenge t15 = V_RESPONSE + response t16 = V_COMMENTS + f'{comments}' textified = '\n'.join((t00, t01, t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t12, t13, t14, t15, t16)) return textified
def test_2_submit_audit_vote_success(self): election, _, voting, _, _, audit_votes, messages = \ self.get_voting_context() messages.append('\nTesting audit-vote submission on success\n') submit_audit_vote = election.submit_audit_vote get_audit_publications = election.get_audit_publications get_vote = election.get_vote for vote in audit_votes: vote = deepcopy(vote) with self.subTest(vote=vote): vote = election.adapt_vote(vote) (_, _, voter_key, _, fingerprint, voter_audit_code, \ _, _, _, _, _) = extract_vote(vote) voter_audit_codes = election.get_voter_audit_codes(voter_key) submit_audit_vote(vote, voter_key, fingerprint, voter_audit_code, voter_audit_codes) assert (fingerprint in get_audit_publications() and \ get_vote(fingerprint) is vote) messages.append('[+] Audit-vote successfully submitted')