diff options
-rw-r--r-- | src/Crypto/Macaroon/Verifier.hs | 6 | ||||
-rw-r--r-- | test/Crypto/Macaroon/Serializer/Base64/Tests.hs | 2 | ||||
-rw-r--r-- | test/Crypto/Macaroon/Verifier/Tests.hs | 30 |
3 files changed, 23 insertions, 15 deletions
diff --git a/src/Crypto/Macaroon/Verifier.hs b/src/Crypto/Macaroon/Verifier.hs index 0d1636c..e257f5f 100644 --- a/src/Crypto/Macaroon/Verifier.hs +++ b/src/Crypto/Macaroon/Verifier.hs | |||
@@ -24,10 +24,10 @@ import Crypto.Macaroon.Internal | |||
24 | 24 | ||
25 | 25 | ||
26 | -- | Opaque datatype for now. Might need more explicit errors | 26 | -- | Opaque datatype for now. Might need more explicit errors |
27 | data Result = Success | Failure deriving (Show,Eq) | 27 | data VResult = VSuccess | VFailure deriving (Show,Eq) |
28 | 28 | ||
29 | verifySig :: Key -> Macaroon -> Result | 29 | verifySig :: Key -> Macaroon -> VResult |
30 | verifySig k m = bool Failure Success $ | 30 | verifySig k m = bool VFailure VSuccess $ |
31 | signature m == foldl' hash (toBytes (hmac derivedKey (identifier m) :: HMAC SHA256)) (caveats m) | 31 | signature m == foldl' hash (toBytes (hmac derivedKey (identifier m) :: HMAC SHA256)) (caveats m) |
32 | where | 32 | where |
33 | hash s c = toBytes (hmac s (vid c `BS.append` cid c) :: HMAC SHA256) | 33 | hash s c = toBytes (hmac s (vid c `BS.append` cid c) :: HMAC SHA256) |
diff --git a/test/Crypto/Macaroon/Serializer/Base64/Tests.hs b/test/Crypto/Macaroon/Serializer/Base64/Tests.hs index 19084af..ea3bed9 100644 --- a/test/Crypto/Macaroon/Serializer/Base64/Tests.hs +++ b/test/Crypto/Macaroon/Serializer/Base64/Tests.hs | |||
@@ -30,7 +30,7 @@ tests = testGroup "Crypto.Macaroon.Serializer.Base64" [ basic | |||
30 | ] | 30 | ] |
31 | 31 | ||
32 | basicQC = testProperty "Reversibility" $ | 32 | basicQC = testProperty "Reversibility" $ |
33 | forAll (macaroon <$> arbitrary) (\m -> deserialize (serialize m) == Right m) | 33 | \sm -> deserialize (serialize (macaroon sm)) == Right (macaroon sm) |
34 | 34 | ||
35 | m :: Macaroon | 35 | m :: Macaroon |
36 | m = create secret key loc | 36 | m = create secret key loc |
diff --git a/test/Crypto/Macaroon/Verifier/Tests.hs b/test/Crypto/Macaroon/Verifier/Tests.hs index 92a8a21..f87f192 100644 --- a/test/Crypto/Macaroon/Verifier/Tests.hs +++ b/test/Crypto/Macaroon/Verifier/Tests.hs | |||
@@ -15,6 +15,7 @@ module Crypto.Macaroon.Verifier.Tests where | |||
15 | import qualified Data.ByteString.Char8 as B8 | 15 | import qualified Data.ByteString.Char8 as B8 |
16 | import Test.Tasty | 16 | import Test.Tasty |
17 | import Test.Tasty.HUnit | 17 | import Test.Tasty.HUnit |
18 | import Test.Tasty.QuickCheck | ||
18 | 19 | ||
19 | import Crypto.Macaroon | 20 | import Crypto.Macaroon |
20 | import Crypto.Macaroon.Verifier | 21 | import Crypto.Macaroon.Verifier |
@@ -25,6 +26,9 @@ tests :: TestTree | |||
25 | tests = testGroup "Crypto.Macaroon.Verifier" [ sigs | 26 | tests = testGroup "Crypto.Macaroon.Verifier" [ sigs |
26 | ] | 27 | ] |
27 | 28 | ||
29 | {- | ||
30 | - Test fixtures | ||
31 | -} | ||
28 | sec = B8.pack "this is our super secret key; only we should know it" | 32 | sec = B8.pack "this is our super secret key; only we should know it" |
29 | 33 | ||
30 | m :: Macaroon | 34 | m :: Macaroon |
@@ -39,21 +43,25 @@ m2 = addFirstPartyCaveat "test = caveat" m | |||
39 | m3 :: Macaroon | 43 | m3 :: Macaroon |
40 | m3 = addFirstPartyCaveat "test = acaveat" m | 44 | m3 = addFirstPartyCaveat "test = acaveat" m |
41 | 45 | ||
46 | {- | ||
47 | - Tests | ||
48 | -} | ||
42 | sigs = testGroup "Signatures" [ basic | 49 | sigs = testGroup "Signatures" [ basic |
43 | , minted | 50 | , one |
51 | , two | ||
44 | ] | 52 | ] |
45 | 53 | ||
46 | basic = testCase "Basic Macaroon Signature" $ | 54 | basic = testGroup "Basic Macaroon" [ none , sigQC ] |
47 | Success @=? verifySig sec m | ||
48 | 55 | ||
56 | none = testCase "No caveat" $ | ||
57 | VSuccess @=? verifySig sec m | ||
49 | 58 | ||
50 | minted :: TestTree | 59 | sigQC = testProperty "Random" $ |
51 | minted = testGroup "Macaroon with first party caveats" [ one | 60 | \sm -> verifySig (secret sm) (macaroon sm) == VSuccess |
52 | , two | ||
53 | ] | ||
54 | one = testCase "One caveat" $ | ||
55 | Success @=? verifySig sec m2 | ||
56 | 61 | ||
57 | two = testCase "Two caveats" $ | 62 | one = testCase "Macaroon with one caveat" $ |
58 | Success @=? verifySig sec m3 | 63 | VSuccess @=? verifySig sec m2 |
64 | |||
65 | two = testCase "Macaroon with two caveats" $ | ||
66 | VSuccess @=? verifySig sec m3 | ||
59 | 67 | ||