]> git.immae.eu Git - github/fretlink/hmacaroons.git/blob - test/Crypto/Macaroon/Verifier/Tests.hs
Add basic exact caveat verifiers
[github/fretlink/hmacaroons.git] / test / Crypto / Macaroon / Verifier / Tests.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 {-|
3 Copyright : (c) 2015 Julien Tanguy
4 License : BSD3
5
6 Maintainer : julien.tanguy@jhome.fr
7
8
9 This test suite is based on the pymacaroons test suite:
10 <https://github.com/ecordell/pymacaroons>
11 -}
12 module Crypto.Macaroon.Verifier.Tests where
13
14
15 import qualified Data.ByteString.Char8 as B8
16 import Test.Tasty
17 import Test.Tasty.HUnit
18 import Test.Tasty.QuickCheck
19
20 import Crypto.Macaroon
21 import Crypto.Macaroon.Verifier
22
23 import Crypto.Macaroon.Instances
24
25 tests :: TestTree
26 tests = testGroup "Crypto.Macaroon.Verifier" [ sigs
27 , exactCavs
28 ]
29
30 {-
31 - Test fixtures
32 -}
33 sec = B8.pack "this is our super secret key; only we should know it"
34
35 m :: Macaroon
36 m = create sec key loc
37 where
38 key = B8.pack "we used our sec key"
39 loc = B8.pack "http://mybank/"
40
41 m2 :: Macaroon
42 m2 = addFirstPartyCaveat "test = caveat" m
43
44 m3 :: Macaroon
45 m3 = addFirstPartyCaveat "value = 42" m2
46
47 exVerifiers = [ verifyExact "test" "caveat" (many' letter_ascii)
48 , verifyExact "value" 42 decimal
49 ]
50 exVerifiers' = [ verifyExact "test" "caveat" (many' letter_ascii)
51 , verifyExact "value" 43 decimal
52 ]
53
54 {-
55 - Tests
56 -}
57 sigs = testGroup "Signatures" [ basic
58 , one
59 , two
60 ]
61
62 basic = testGroup "Basic Macaroon" [ none , sigQC ]
63
64 none = testCase "No caveat" $
65 Ok @=? verifySig sec m
66
67 sigQC = testProperty "Random" $
68 \sm -> verifySig (secret sm) (macaroon sm) == Ok
69
70 one = testCase "Macaroon with one caveat" $
71 Ok @=? verifySig sec m2
72
73 two = testCase "Macaroon with two caveats" $
74 Ok @=? verifySig sec m3
75
76 exactCavs = testGroup "Exact Caveats" [ zero', one', two' , one'', two'']
77
78 zero' = testCase "Zero caveat win" $
79 Ok @=? verifyCavs exVerifiers m
80 one' = testCase "One caveat win" $
81 Ok @=? verifyCavs exVerifiers m2
82 one'' = testCase "Ignoring non-relevant" $
83 Ok @=? verifyCavs exVerifiers' m2
84 two' = testCase "Two caveat win" $
85 Ok @=? verifyCavs exVerifiers m3
86 two'' = testCase "Two caveat fail" $
87 Failed @=? verifyCavs exVerifiers' m3