]> Git — Sourcephile - majurity.git/blob - hjugement-protocol/tests/HUnit/Election.hs
protocol: bring c from the method level to the class level
[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 Reifies v Version =>
36 ReifyCrypto crypto => Key crypto => JSON.ToJSON crypto =>
37 Proxy v -> 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 Reifies v Version =>
83 ReifyCrypto crypto => Key crypto => JSON.ToJSON crypto =>
84 Proxy v -> crypto -> Int -> [Question v] -> [[Bool]] ->
85 Either ErrorBallot Bool -> TestTree
86 hunitEncryptBallot v election_crypto seed election_questions opins exp =
87 let got =
88 reifyCrypto election_crypto $ \(_c::Proxy c) ->
89 runExcept $
90 (`evalStateT` Random.mkStdGen seed) $ do
91 election_uuid <- randomUUID
92 cred <- randomCredential
93 let ballotSecKey = credentialSecretKey @_ @c election_uuid cred
94 election_public_key <- publicKey <$> randomSecretKey
95 let elec = Election
96 { election_name = "election"
97 , election_description = "description"
98 , election_hash = hashElection elec
99 , election_version = Just (reflect v)
100 , ..
101 }
102 verifyBallot elec
103 <$> encryptBallot elec (Just ballotSecKey) opins
104 in
105 testCase (show opins) $
106 got @?= exp