aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/Crypto/Macaroon/Instances.hs
blob: 4e2f39f4d3bad32c05cedc043694a59483fd1066 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{-# LANGUAGE OverloadedStrings #-}
{-|
Copyright   : (c) 2015 Julien Tanguy
License     : BSD3

Maintainer  : julien.tanguy@jhome.fr


This test suite is based on the pymacaroons test suite:
<https://github.com/ecordell/pymacaroons>
-}
module Crypto.Macaroon.Instances where

import Control.Monad
import           Data.Byteable
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B8
import           Data.Hex
import           Data.List
import           Test.Tasty
import           Test.Tasty.HUnit
import           Test.Tasty.QuickCheck

import           Crypto.Macaroon


-- | Adjust the size parameter, by transforming it with the given
-- function.
scale :: (Int -> Int) -> Gen a -> Gen a
scale f g = sized (\n -> resize (f n) g)

newtype Url = Url { unUrl :: BS.ByteString } deriving (Show)

instance Arbitrary Url where
    arbitrary = do
        protocol <- elements ["http://"]
        name <-  fmap (intercalate ".") <$> listOf1 . listOf1 $ elements ['a'..'z']
        domain <- elements [".com",".net"]
        return . Url . B8.pack $ (protocol ++ name ++ domain)

newtype Secret = Secret { unSecret :: BS.ByteString } deriving (Show)

instance Arbitrary Secret where
    arbitrary = Secret . B8.pack <$> scale (*3) arbitrary

newtype Identifier = Identifier { unIdent :: BS.ByteString } deriving (Show)

instance Arbitrary Identifier where
    arbitrary = Identifier . B8.pack <$>(scale (*3) . listOf1 . elements $ ['a'..'z'])

data SimpleMac = SimpleMac { secret :: BS.ByteString, macaroon :: Macaroon } deriving Show

instance Arbitrary SimpleMac where
    arbitrary = do
        secret <- unSecret <$> arbitrary
        location <- unUrl <$> arbitrary
        ident <- unIdent <$> arbitrary
        return $ SimpleMac secret (create secret ident location)