From 5d1b7d51854d355bf5b6438c1a96ce9e743fd810 Mon Sep 17 00:00:00 2001 From: Julien Tanguy Date: Fri, 15 May 2015 16:03:30 +0200 Subject: Add quickcheck properties --- src/Crypto/Macaroon/Verifier.hs | 6 ++--- test/Crypto/Macaroon/Serializer/Base64/Tests.hs | 2 +- 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 -- | Opaque datatype for now. Might need more explicit errors -data Result = Success | Failure deriving (Show,Eq) +data VResult = VSuccess | VFailure deriving (Show,Eq) -verifySig :: Key -> Macaroon -> Result -verifySig k m = bool Failure Success $ +verifySig :: Key -> Macaroon -> VResult +verifySig k m = bool VFailure VSuccess $ signature m == foldl' hash (toBytes (hmac derivedKey (identifier m) :: HMAC SHA256)) (caveats m) where 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 ] basicQC = testProperty "Reversibility" $ - forAll (macaroon <$> arbitrary) (\m -> deserialize (serialize m) == Right m) + \sm -> deserialize (serialize (macaroon sm)) == Right (macaroon sm) m :: Macaroon 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 import qualified Data.ByteString.Char8 as B8 import Test.Tasty import Test.Tasty.HUnit +import Test.Tasty.QuickCheck import Crypto.Macaroon import Crypto.Macaroon.Verifier @@ -25,6 +26,9 @@ tests :: TestTree tests = testGroup "Crypto.Macaroon.Verifier" [ sigs ] +{- + - Test fixtures + -} sec = B8.pack "this is our super secret key; only we should know it" m :: Macaroon @@ -39,21 +43,25 @@ m2 = addFirstPartyCaveat "test = caveat" m m3 :: Macaroon m3 = addFirstPartyCaveat "test = acaveat" m +{- + - Tests + -} sigs = testGroup "Signatures" [ basic - , minted + , one + , two ] -basic = testCase "Basic Macaroon Signature" $ - Success @=? verifySig sec m +basic = testGroup "Basic Macaroon" [ none , sigQC ] +none = testCase "No caveat" $ + VSuccess @=? verifySig sec m -minted :: TestTree -minted = testGroup "Macaroon with first party caveats" [ one - , two - ] -one = testCase "One caveat" $ - Success @=? verifySig sec m2 +sigQC = testProperty "Random" $ + \sm -> verifySig (secret sm) (macaroon sm) == VSuccess -two = testCase "Two caveats" $ - Success @=? verifySig sec m3 +one = testCase "Macaroon with one caveat" $ + VSuccess @=? verifySig sec m2 + +two = testCase "Macaroon with two caveats" $ + VSuccess @=? verifySig sec m3 -- cgit v1.2.3