1 {-# LANGUAGE AllowAmbiguousTypes #-}
2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 {-# LANGUAGE PatternSynonyms #-}
5 module HUnit.Election where
7 import Test.Tasty.HUnit
8 import qualified Data.List as List
9 import qualified System.Random as Random
11 import Protocol.Arithmetic
12 import Protocol.Credential
13 import Protocol.Election
14 import Protocol.Trustees.Simple
19 hunit = testGroup "Election"
20 [ testGroup "groupGenInverses"
21 [ testCase "WeakParams" $
22 List.take 10 (groupGenInverses @WeakParams) @?=
23 [groupGen^neg (fromNatural n) | n <- [0..9]]
24 , testCase "BeleniosParams" $
25 List.take 10 (groupGenInverses @BeleniosParams) @?=
26 [groupGen^neg (fromNatural n) | n <- [0..9]]
28 , testGroup "encryptBallot" $
29 [ testsEncryptBallot @WeakParams
30 , testsEncryptBallot @BeleniosParams
32 , testGroup "trustee" $
33 [ testsTrustee @WeakParams
37 testsEncryptBallot :: forall q. Params q => TestTree
39 testGroup (paramsName @q)
40 [ testEncryptBallot @q 0
41 [Question "q1" ["a1","a2","a3"] zero one]
42 [[True, False, False]]
44 , testEncryptBallot @q 0
45 [Question "q1" ["a1","a2","a3"] zero one]
46 [[False, False, False]]
48 , testEncryptBallot @q 0
49 [Question "q1" ["a1","a2","a3"] zero one]
50 [[False, False, False]]
52 , testEncryptBallot @q 0
53 [Question "q1" [] zero one]
55 (Left (ErrorBallot_WrongNumberOfAnswers 0 1))
56 , testEncryptBallot @q 0
57 [Question "q1" ["a1","a2"] one one]
59 (Left (ErrorBallot_Answer (ErrorAnswer_WrongNumberOfOpinions 1 2)))
60 , testEncryptBallot @q 0
61 [Question "q1" ["a1","a2","a3"] zero one]
63 (Left (ErrorBallot_Answer (ErrorAnswer_WrongSumOfOpinions 2 0 1)))
64 , testEncryptBallot @q 0
65 [Question "q1" ["a1","a2","a3"] one one]
66 [[False, False, False]]
67 (Left (ErrorBallot_Answer (ErrorAnswer_WrongSumOfOpinions 0 1 1)))
68 , testEncryptBallot @q 0
69 [Question "q1" ["a1","a2"] one one]
70 [[False, False, True]]
71 (Left (ErrorBallot_Answer (ErrorAnswer_WrongNumberOfOpinions 3 2)))
72 , testEncryptBallot @q 0
73 [ Question "q1" ["a11","a12","a13"] zero (one+one)
74 , Question "q2" ["a21","a22","a23"] one one
77 , [False, True, False] ]
82 forall q. SubGroup q =>
83 Int -> [Question q] -> [[Bool]] ->
84 Either ErrorBallot Bool ->
86 testEncryptBallot seed quests opins exp =
89 (`evalStateT` Random.mkStdGen seed) $ do
91 cred <- randomCredential
92 let ballotSecKey = credentialSecretKey @q uuid cred
93 let elecPubKey = publicKey ballotSecKey -- FIXME: wrong key
95 { election_name = "election"
96 , election_description = "description"
97 , election_publicKey = elecPubKey
98 , election_questions = quests
99 , election_uuid = uuid
100 , election_hash = Hash "" -- FIXME: when implemented
103 <$> encryptBallot elec (Just ballotSecKey) opins
105 testCase (show opins) $
108 testsTrustee :: forall q. Params q => TestTree
110 testGroup (paramsName @q)
111 [ testTrustee @q 0 (Right ())
115 forall q. SubGroup q =>
116 Int -> Either ErrorTrusteePublicKey () -> TestTree
117 testTrustee seed exp =
120 (`evalStateT` Random.mkStdGen seed) $ do
121 trustSecKey <- randomSecretKey @_ @_ @q
122 trustPubKey <- proveTrusteePublicKey trustSecKey
123 lift $ verifyTrusteePublicKey trustPubKey
125 testCase (show seed) $