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
14 import Control.Applicative
17 import qualified Data.ByteString as BS
18 import qualified Data.ByteString.Char8 as B8
22 import Test.Tasty.HUnit
23 import Test.Tasty.QuickCheck
25 import Crypto.Macaroon
27 newtype Url = Url { unUrl :: BS.ByteString } deriving (Show)
29 instance Arbitrary Url where
31 protocol <- elements ["http://"]
32 name <- fmap (intercalate ".") <$> listOf1 . listOf1 $ elements ['a'..'z']
33 domain <- elements [".com",".net"]
34 return . Url . B8.pack $ (protocol ++ name ++ domain)
36 newtype BSSecret = BSSecret { unSecret :: BS.ByteString } deriving (Show)
38 instance Arbitrary BSSecret where
39 arbitrary = BSSecret . B8.pack <$> scale (*3) arbitrary
41 newtype Identifier = Identifier { unIdent :: BS.ByteString } deriving (Show)
43 instance Arbitrary Identifier where
44 arbitrary = Identifier . B8.pack <$>(scale (*3) . listOf1 . elements $ ['a'..'z'])
46 newtype EquationLike = EquationLike { unEqlike :: BS.ByteString } deriving (Show)
48 instance Arbitrary EquationLike where
50 keylen <- choose (3,8)
51 key <- B8.pack <$> vectorOf keylen (elements ['a'..'z'])
52 val <- B8.pack <$> (scale (*3) . listOf1 . elements $ ['a'..'z'])
53 return $ EquationLike (BS.concat [ key, " = ", val])
56 data SimpleMac = SimpleMac { secret :: BS.ByteString, macaroon :: Macaroon } deriving Show
58 instance Arbitrary SimpleMac where
60 secret <- unSecret <$> arbitrary
61 location <- unUrl <$> arbitrary
62 ident <- unIdent <$> arbitrary
63 fpcavs <- listOf arbitrary
64 let mac = foldl (flip addFirstPartyCaveat) (create secret ident location) (map unEqlike fpcavs)
65 return $ SimpleMac secret mac