summaryrefslogtreecommitdiff
path: root/tests/HUnit/Credential.hs
blob: 10d8a92bb14e5c9fed7b08431d0bdc737c6280ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{-# LANGUAGE OverloadedStrings #-}
module HUnit.Credential where

import Control.Applicative (Applicative(..))
import Test.Tasty.HUnit
import qualified Control.Monad.Trans.State.Strict as S
import qualified System.Random as Random

import Voting.Protocol
import Utils

hunit :: TestTree
hunit = testGroup "Credential"
 [ testGroup "randomCredential"
	 [ testCase "0" $
		S.evalState randomCredential (Random.mkStdGen 0) @?=
			Credential "xLcs7ev6Jy6FHHE"
	 ]
 , testGroup "randomUUID"
	 [ testCase "0" $
		S.evalState randomUUID (Random.mkStdGen 0) @?=
			UUID "xLcs7ev6Jy6FHH"
	 ]
 , testGroup "readCredential" $
		let (==>) inp exp =
			testCase (show inp) $ readCredential inp @?= exp in
	 [ "" ==> Left ErrorToken_Length
	 , "xLcs7ev6Jy6FH_E"  ==> Left (ErrorToken_BadChar '_')
	 , "xLcs7ev6Jy6FHIE"  ==> Left (ErrorToken_BadChar 'I')
	 , "xLcs7ev6Jy6FH0E"  ==> Left (ErrorToken_BadChar '0')
	 , "xLcs7ev6Jy6FHOE"  ==> Left (ErrorToken_BadChar 'O')
	 , "xLcs7ev6Jy6FHlE"  ==> Left (ErrorToken_BadChar 'l')
	 , "xLcs7ev6Jy6FH6"   ==> Left ErrorToken_Length
	 , "xLcs7ev6Jy6FHHy1" ==> Left ErrorToken_Length
	 , "xLcs7ev6Jy6FHHF"  ==> Left ErrorToken_Checksum
	 , "xLcs7ev6Jy6FHHE"  ==> Right (Credential "xLcs7ev6Jy6FHHE")
	 ]
 , testGroup "credentialSecretKey" $
	 [ testSecretKey weakFFC 0 122
	 , testSecretKey weakFFC 1 35
	 , testSecretKey beleniosFFC 0 2317630607062989137269685509390
	 , testSecretKey beleniosFFC 1 1968146140481358915910346867611
	 ]
 ]

testSecretKey :: FFC -> Int -> Natural -> TestTree
testSecretKey ffc seed exp =
	reify ffc $ \(Proxy::Proxy c) ->
	let (uuid@(UUID u), cred@(Credential c)) =
		(`S.evalState` Random.mkStdGen seed) $
			(,) <$> randomUUID <*> randomCredential in
	testCase (show (u,c)) $
		credentialSecretKey @c uuid cred @?= E exp