1 {-# LANGUAGE OverloadedStrings #-}
3 Copyright : (c) 2015 Julien Tanguy
6 Maintainer : julien.tanguy@jhome.fr
9 This test suite is based on the pymacaroons test suite:
10 <https://github.com/ecordell/pymacaroons>
12 module Crypto.Macaroon.Instances where
16 import qualified Data.ByteString as BS
17 import qualified Data.ByteString.Char8 as B8
21 import Test.Tasty.HUnit
22 import Test.Tasty.QuickCheck
24 import Crypto.Macaroon
26 newtype Url = Url { unUrl :: BS.ByteString } deriving (Show)
28 instance Arbitrary Url where
30 protocol <- elements ["http://"]
31 name <- fmap (intercalate ".") <$> listOf1 . listOf1 $ elements ['a'..'z']
32 domain <- elements [".com",".net"]
33 return . Url . B8.pack $ (protocol ++ name ++ domain)
35 newtype Secret = Secret { unSecret :: BS.ByteString } deriving (Show)
37 instance Arbitrary Secret where
38 arbitrary = Secret . B8.pack <$> scale (*3) arbitrary
40 newtype Identifier = Identifier { unIdent :: BS.ByteString } deriving (Show)
42 instance Arbitrary Identifier where
43 arbitrary = Identifier . B8.pack <$>(scale (*3) . listOf1 . elements $ ['a'..'z'])
45 newtype EquationLike = EquationLike { unEqlike :: BS.ByteString } deriving (Show)
47 instance Arbitrary EquationLike where
49 keylen <- choose (3,8)
50 key <- B8.pack <$> vectorOf keylen (elements ['a'..'z'])
51 val <- B8.pack <$> (scale (*3) . listOf1 . elements $ ['a'..'z'])
52 return $ EquationLike (BS.concat [ key, " = ", val])
55 data SimpleMac = SimpleMac { secret :: BS.ByteString, macaroon :: Macaroon } deriving Show
57 instance Arbitrary SimpleMac where
59 secret <- unSecret <$> arbitrary
60 location <- unUrl <$> arbitrary
61 ident <- unIdent <$> arbitrary
62 fpcavs <- listOf arbitrary
63 let mac = foldl (flip addFirstPartyCaveat) (create secret ident location) (map unEqlike fpcavs)
64 return $ SimpleMac secret mac