{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Tests.Auth
( authsTo
, incrementalVsFull
) where
import Implementation
import Interface
import Tests.Core
authsTo :: ByteString
-> Prim
-> Key Prim
-> Spec
authsTo :: ByteString -> Prim -> Key Prim -> Spec
authsTo ByteString
str Prim
prim Key Prim
key = String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
msg (Key Prim -> ByteString -> Prim
forall src. PureByteSource src => Key Prim -> src -> Prim
auth Key Prim
key ByteString
str Prim -> Prim -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Prim
prim)
where msg :: String
msg = [String] -> String
unwords [ String
"authenticates"
, String -> String
shortened (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ ByteString -> String
forall a. Show a => a -> String
show ByteString
str
, String
"to"
, String -> String
shortened (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Prim -> String
forall a. Show a => a -> String
show Prim
prim
]
incrDigest :: Key Prim
-> ByteString
-> IO Prim
incrDigest :: Key Prim -> ByteString -> IO Prim
incrDigest Key Prim
k ByteString
bs = (AuthCxt 1 -> IO Prim) -> IO Prim
forall mem a. Memory mem => (mem -> IO a) -> IO a
withMemory ((AuthCxt 1 -> IO Prim) -> IO Prim)
-> (AuthCxt 1 -> IO Prim) -> IO Prim
forall a b. (a -> b) -> a -> b
$ \ (AuthCxt 1
cxt :: AuthCxt 1) ->
do Key Prim -> AuthCxt 1 -> Expectation
forall (n :: Nat).
KnownNat n =>
Key Prim -> AuthCxt n -> Expectation
startAuth Key Prim
k AuthCxt 1
cxt
ByteString -> AuthCxt 1 -> Expectation
forall (n :: Nat) src.
(KnownNat n, ByteSource src) =>
src -> AuthCxt n -> Expectation
updateAuth ByteString
bs AuthCxt 1
cxt
AuthCxt 1 -> IO Prim
forall (n :: Nat). KnownNat n => AuthCxt n -> IO Prim
finaliseAuth AuthCxt 1
cxt
incrDigestList :: Key Prim
-> [ByteString]
-> IO Prim
incrDigestList :: Key Prim -> [ByteString] -> IO Prim
incrDigestList Key Prim
k [ByteString]
bsL = (AuthCxt 1 -> IO Prim) -> IO Prim
forall mem a. Memory mem => (mem -> IO a) -> IO a
withMemory ((AuthCxt 1 -> IO Prim) -> IO Prim)
-> (AuthCxt 1 -> IO Prim) -> IO Prim
forall a b. (a -> b) -> a -> b
$ \ (AuthCxt 1
cxt :: AuthCxt 1) ->
do Key Prim -> AuthCxt 1 -> Expectation
forall (n :: Nat).
KnownNat n =>
Key Prim -> AuthCxt n -> Expectation
startAuth Key Prim
k AuthCxt 1
cxt
(ByteString -> Expectation) -> [ByteString] -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (ByteString -> AuthCxt 1 -> Expectation
forall (n :: Nat) src.
(KnownNat n, ByteSource src) =>
src -> AuthCxt n -> Expectation
`updateAuth` AuthCxt 1
cxt) [ByteString]
bsL
AuthCxt 1 -> IO Prim
forall (n :: Nat). KnownNat n => AuthCxt n -> IO Prim
finaliseAuth AuthCxt 1
cxt
incrementalVsFull :: Spec
incrementalVsFull :: Spec
incrementalVsFull = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"Incremental vs Full authenticator" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
String -> (Key Prim -> ByteString -> Expectation) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"for single source" ((Key Prim -> ByteString -> Expectation) -> Spec)
-> (Key Prim -> ByteString -> Expectation) -> Spec
forall a b. (a -> b) -> a -> b
$
\ Key Prim
k ByteString
bs -> Key Prim -> ByteString -> IO Prim
incrDigest Key Prim
k ByteString
bs IO Prim -> Prim -> Expectation
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> Expectation
`shouldReturn` Key Prim -> ByteString -> Prim
forall src. PureByteSource src => Key Prim -> src -> Prim
auth Key Prim
k ByteString
bs
String -> (Key Prim -> [ByteString] -> Expectation) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"for multiple source" ((Key Prim -> [ByteString] -> Expectation) -> Spec)
-> (Key Prim -> [ByteString] -> Expectation) -> Spec
forall a b. (a -> b) -> a -> b
$
\ Key Prim
k [ByteString]
bsL -> Key Prim -> [ByteString] -> IO Prim
incrDigestList Key Prim
k [ByteString]
bsL IO Prim -> Prim -> Expectation
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> Expectation
`shouldReturn` Key Prim -> [ByteString] -> Prim
forall src. PureByteSource src => Key Prim -> src -> Prim
auth Key Prim
k [ByteString]
bsL