]> Git — Sourcephile - majurity.git/blob - hjugement-protocol/tests/HUnit/Election.hs
protocol: replace reifyCrypto by groupDict
[majurity.git] / hjugement-protocol / tests / HUnit / Election.hs
1 {-# LANGUAGE DataKinds #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE PatternSynonyms #-}
4 module HUnit.Election where
5
6 import Test.Tasty.HUnit
7 import qualified Data.Aeson as JSON
8 import qualified Data.List as List
9 import qualified Data.Text as Text
10 import qualified System.Random as Random
11
12 import Voting.Protocol
13
14 import Utils
15
16 hunit :: Reifies v Version => Proxy v -> TestTree
17 hunit v = testGroup "Election" $
18 [ testGroup "groupGenInverses"
19 [ testCase "WeakParams" $
20 reify weakFFC $ \(Proxy::Proxy c) ->
21 List.take 10 (groupGenInverses @_ @c) @?=
22 [groupGen^neg (fromNatural n) | n <- [0..9]]
23 , testCase "BeleniosParams" $
24 reify beleniosFFC $ \(Proxy::Proxy c) ->
25 List.take 10 (groupGenInverses @_ @c) @?=
26 [groupGen^neg (fromNatural n) | n <- [0..9]]
27 ]
28 , testGroup "encryptBallot" $
29 [ hunitsEncryptBallot v weakFFC
30 , hunitsEncryptBallot v beleniosFFC
31 ]
32 ]
33
34 hunitsEncryptBallot ::
35 Group crypto => Key crypto => JSON.ToJSON crypto =>
36 Reifies v Version => Proxy v ->
37 crypto -> TestTree
38 hunitsEncryptBallot v crypto =
39 testGroup (Text.unpack $ cryptoName crypto)
40 [ hunitEncryptBallot v crypto 0
41 [Question "q1" ["a1","a2","a3"] zero one]
42 [[True, False, False]]
43 (Right True)
44 , hunitEncryptBallot v crypto 0
45 [Question "q1" ["a1","a2","a3"] zero one]
46 [[False, False, False]]
47 (Right True)
48 , hunitEncryptBallot v crypto 0
49 [Question "q1" ["a1","a2","a3"] zero one]
50 [[False, False, False]]
51 (Right True)
52 , hunitEncryptBallot v crypto 0
53 [Question "q1" [] zero one]
54 []
55 (Left (ErrorBallot_WrongNumberOfAnswers 0 1))
56 , hunitEncryptBallot v crypto 0
57 [Question "q1" ["a1","a2"] one one]
58 [[True]]
59 (Left (ErrorBallot_Answer (ErrorAnswer_WrongNumberOfOpinions 1 2)))
60 , hunitEncryptBallot v crypto 0
61 [Question "q1" ["a1","a2","a3"] zero one]
62 [[True, True, False]]
63 (Left (ErrorBallot_Answer (ErrorAnswer_WrongSumOfOpinions 2 0 1)))
64 , hunitEncryptBallot v crypto 0
65 [Question "q1" ["a1","a2","a3"] one one]
66 [[False, False, False]]
67 (Left (ErrorBallot_Answer (ErrorAnswer_WrongSumOfOpinions 0 1 1)))
68 , hunitEncryptBallot v crypto 0
69 [Question "q1" ["a1","a2"] one one]
70 [[False, False, True]]
71 (Left (ErrorBallot_Answer (ErrorAnswer_WrongNumberOfOpinions 3 2)))
72 , hunitEncryptBallot v crypto 0
73 [ Question "q1" ["a11","a12","a13"] zero (one+one)
74 , Question "q2" ["a21","a22","a23"] one one
75 ]
76 [ [True, False, True]
77 , [False, True, False] ]
78 (Right True)
79 ]
80
81 hunitEncryptBallot ::
82 Group crypto => Key crypto => JSON.ToJSON crypto =>
83 Reifies v Version => Proxy v ->
84 crypto -> Int -> [Question v] -> [[Bool]] ->
85 Either ErrorBallot Bool ->
86 TestTree
87 hunitEncryptBallot v election_crypto seed election_questions opins exp =
88 let got =
89 reify election_crypto $ \case
90 (c::Proxy c) | Dict <- groupDict c ->
91 runExcept $
92 (`evalStateT` Random.mkStdGen seed) $ do
93 election_uuid <- randomUUID
94 cred <- randomCredential
95 let ballotSecKey = credentialSecretKey @_ @c election_uuid cred
96 election_public_key <- publicKey <$> randomSecretKey
97 let elec = Election
98 { election_name = "election"
99 , election_description = "description"
100 , election_hash = hashElection elec
101 , election_version = Just (reflect v)
102 , ..
103 }
104 verifyBallot elec
105 <$> encryptBallot elec (Just ballotSecKey) opins
106 in
107 testCase (show opins) $
108 got @?= exp