]> git.immae.eu Git - github/fretlink/hmacaroons.git/blob - test/Crypto/Macaroon/Verifier/Tests.hs
54c8db11c0dbd193d474067331d2d87db8f49624
[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 Data.List
16 import qualified Data.ByteString.Char8 as B8
17 import Test.Tasty
18 import Test.Tasty.HUnit
19 import Test.Tasty.QuickCheck
20
21 import Crypto.Macaroon
22 import Crypto.Macaroon.Verifier
23
24 import Crypto.Macaroon.Instances
25
26 tests :: TestTree
27 tests = testGroup "Crypto.Macaroon.Verifier" [ sigs
28 , exactCavs
29 ]
30
31 {-
32 - Test fixtures
33 -}
34 sec = B8.pack "this is our super secret key; only we should know it"
35
36 m :: Macaroon
37 m = create sec key loc
38 where
39 key = B8.pack "we used our sec key"
40 loc = B8.pack "http://mybank/"
41
42 m2 :: Macaroon
43 m2 = addFirstPartyCaveat "test = caveat" m
44
45 m3 :: Macaroon
46 m3 = addFirstPartyCaveat "value = 42" m2
47
48 exVerifiers = [ verifyExact "test" "caveat" (many' letter_ascii)
49 , verifyExact "value" 42 decimal
50 ]
51 exVerifiers' = [ verifyExact "test" "caveat" (many' letter_ascii)
52 , verifyExact "value" 43 decimal
53 ]
54 funVerifiers = [ verifyFun "test" ("cav" `isPrefixOf`) (many' letter_ascii)
55 , verifyFun "value" (<= 43) decimal
56 ]
57
58 {-
59 - Tests
60 -}
61 sigs = testGroup "Signatures" [ basic
62 , one
63 , two
64 ]
65
66 basic = testGroup "Basic Macaroon" [ none , sigQC ]
67
68 none = testCase "No caveat" $
69 Ok @=? verifySig sec m
70
71 sigQC = testProperty "Random" $
72 \sm -> verifySig (secret sm) (macaroon sm) == Ok
73
74 one = testCase "Macaroon with one caveat" $
75 Ok @=? verifySig sec m2
76
77 two = testCase "Macaroon with two caveats" $
78 Ok @=? verifySig sec m3
79
80 exactCavs = testGroup "Exact Caveats" [
81 testGroup "Ignoring non-relevant" [
82 testCase "Zero caveat" $ Ok @=? verifyCavs exVerifiers m
83 , testCase "One caveat" $ Ok @=? verifyCavs exVerifiers' m2
84 ]
85 , testCase "One caveat win" $ Ok @=? verifyCavs exVerifiers m2
86 , testCase "Two caveat win" $ Ok @=? verifyCavs exVerifiers m3
87 , testCase "Two caveat fail" $ Failed @=? verifyCavs exVerifiers' m3
88 ]
89
90 funCavs = testGroup "Function Caveats" [
91 testCase "One caveat win" $ Ok @=? verifyCavs funVerifiers m2
92 , testCase "Two caveat win" $ Ok @=? verifyCavs funVerifiers m3
93 ]