]> git.immae.eu Git - github/fretlink/hmacaroons.git/blame - test/Crypto/Macaroon/Instances.hs
Nixify package & documentation
[github/fretlink/hmacaroons.git] / test / Crypto / Macaroon / Instances.hs
CommitLineData
a22bcdb6
JT
1{-# LANGUAGE OverloadedStrings #-}
2{-|
3Copyright : (c) 2015 Julien Tanguy
4License : BSD3
5
6Maintainer : julien.tanguy@jhome.fr
7
8
9This test suite is based on the pymacaroons test suite:
10<https://github.com/ecordell/pymacaroons>
11-}
12module Crypto.Macaroon.Instances where
13
14import Control.Monad
15import Data.Byteable
16import qualified Data.ByteString as BS
17import qualified Data.ByteString.Char8 as B8
18import Data.Hex
19import Data.List
20import Test.Tasty
21import Test.Tasty.HUnit
22import Test.Tasty.QuickCheck
23
24import Crypto.Macaroon
25
7001a61e
JT
26
27-- | Adjust the size parameter, by transforming it with the given
28-- function.
29scale :: (Int -> Int) -> Gen a -> Gen a
30scale f g = sized (\n -> resize (f n) g)
31
a22bcdb6
JT
32newtype Url = Url { unUrl :: BS.ByteString } deriving (Show)
33
34instance Arbitrary Url where
35 arbitrary = do
36 protocol <- elements ["http://"]
37 name <- fmap (intercalate ".") <$> listOf1 . listOf1 $ elements ['a'..'z']
38 domain <- elements [".com",".net"]
39 return . Url . B8.pack $ (protocol ++ name ++ domain)
40
41newtype Secret = Secret { unSecret :: BS.ByteString } deriving (Show)
42
43instance Arbitrary Secret where
44 arbitrary = Secret . B8.pack <$> scale (*3) arbitrary
45
46newtype Identifier = Identifier { unIdent :: BS.ByteString } deriving (Show)
47
48instance Arbitrary Identifier where
49 arbitrary = Identifier . B8.pack <$>(scale (*3) . listOf1 . elements $ ['a'..'z'])
50
51data SimpleMac = SimpleMac { secret :: BS.ByteString, macaroon :: Macaroon } deriving Show
52
53instance Arbitrary SimpleMac where
54 arbitrary = do
55 secret <- unSecret <$> arbitrary
56 location <- unUrl <$> arbitrary
57 ident <- unIdent <$> arbitrary
58 return $ SimpleMac secret (create secret ident location)
59
60