module Signet.Unstable.Type.SignerTest where

import qualified Control.Monad.Catch as Exception
import qualified Data.ByteString.Char8 as Ascii
import qualified Signet.Unstable.Exception.InvalidSigner as InvalidSigner
import qualified Signet.Unstable.Extra.Either as Either
import qualified Signet.Unstable.Type.AsymmetricSignature as AsymmetricSignature
import qualified Signet.Unstable.Type.Message as Message
import qualified Signet.Unstable.Type.Secret as Secret
import qualified Signet.Unstable.Type.SecretKey as SecretKey
import qualified Signet.Unstable.Type.Signature as Signature
import qualified Signet.Unstable.Type.Signer as Signer
import qualified Signet.Unstable.Type.SymmetricSignature as SymmetricSignature
import qualified Signet.Unstable.Type.Test as Test

spec :: (Exception.MonadThrow io, Monad tree) => Test.Test io tree -> tree ()
spec :: forall (io :: * -> *) (tree :: * -> *).
(MonadThrow io, Monad tree) =>
Test io tree -> tree ()
spec Test io tree
test = Test io tree -> String -> tree () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> tree () -> tree ()
Test.describe Test io tree
test String
"Signet.Unstable.Type.Signer" (tree () -> tree ()) -> tree () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
  Test io tree -> String -> tree () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> tree () -> tree ()
Test.describe Test io tree
test String
"parse" (tree () -> tree ()) -> tree () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
    Test io tree -> String -> io () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> io () -> tree ()
Test.it Test io tree
test String
"succeeds with a valid secret key" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      let byteString :: ByteString
byteString = String -> ByteString
Ascii.pack String
"whsk_QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU="
      let actual :: Either InvalidSigner Signer
actual = ByteString -> Either InvalidSigner Signer
Signer.parse ByteString
byteString
      SecretKey
secretKey <- Either InvalidSecretKey SecretKey -> io SecretKey
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidSecretKey SecretKey -> io SecretKey)
-> Either InvalidSecretKey SecretKey -> io SecretKey
forall a b. (a -> b) -> a -> b
$ ByteString -> Either InvalidSecretKey SecretKey
SecretKey.parse ByteString
byteString
      Test io tree
-> Either InvalidSigner Signer
-> Either InvalidSigner Signer
-> io ()
forall (io :: * -> *) a (tree :: * -> *).
(HasCallStack, Applicative io, Eq a, Show a) =>
Test io tree -> a -> a -> io ()
Test.assertEq Test io tree
test Either InvalidSigner Signer
actual (Signer -> Either InvalidSigner Signer
forall a b. b -> Either a b
Right (SecretKey -> Signer
Signer.Asymmetric SecretKey
secretKey))

    Test io tree -> String -> io () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> io () -> tree ()
Test.it Test io tree
test String
"succeeds with a valid secret" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      let byteString :: ByteString
byteString = String -> ByteString
Ascii.pack String
"whsec_bXlzZWNyZXRrZXkxMjM0NQ=="
      let actual :: Either InvalidSigner Signer
actual = ByteString -> Either InvalidSigner Signer
Signer.parse ByteString
byteString
      Secret
secret <- Either InvalidSecret Secret -> io Secret
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidSecret Secret -> io Secret)
-> Either InvalidSecret Secret -> io Secret
forall a b. (a -> b) -> a -> b
$ ByteString -> Either InvalidSecret Secret
Secret.parse ByteString
byteString
      Test io tree
-> Either InvalidSigner Signer
-> Either InvalidSigner Signer
-> io ()
forall (io :: * -> *) a (tree :: * -> *).
(HasCallStack, Applicative io, Eq a, Show a) =>
Test io tree -> a -> a -> io ()
Test.assertEq Test io tree
test Either InvalidSigner Signer
actual (Signer -> Either InvalidSigner Signer
forall a b. b -> Either a b
Right (Secret -> Signer
Signer.Symmetric Secret
secret))

    Test io tree -> String -> io () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> io () -> tree ()
Test.it Test io tree
test String
"fails with invalid input" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      let byteString :: ByteString
byteString = String -> ByteString
Ascii.pack String
"invalid"
      let actual :: Either InvalidSigner Signer
actual = ByteString -> Either InvalidSigner Signer
Signer.parse ByteString
byteString
      Test io tree
-> Either InvalidSigner Signer
-> Either InvalidSigner Signer
-> io ()
forall (io :: * -> *) a (tree :: * -> *).
(HasCallStack, Applicative io, Eq a, Show a) =>
Test io tree -> a -> a -> io ()
Test.assertEq Test io tree
test Either InvalidSigner Signer
actual (InvalidSigner -> Either InvalidSigner Signer
forall a b. a -> Either a b
Left (ByteString -> InvalidSigner
InvalidSigner.MkInvalidSigner ByteString
byteString))

  Test io tree -> String -> tree () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> tree () -> tree ()
Test.describe Test io tree
test String
"render" (tree () -> tree ()) -> tree () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
    Test io tree -> String -> io () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> io () -> tree ()
Test.it Test io tree
test String
"works with a secret key" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      let byteString :: ByteString
byteString = String -> ByteString
Ascii.pack String
"whsk_QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU="
      SecretKey
secretKey <- Either InvalidSecretKey SecretKey -> io SecretKey
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidSecretKey SecretKey -> io SecretKey)
-> (ByteString -> Either InvalidSecretKey SecretKey)
-> ByteString
-> io SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidSecretKey SecretKey
SecretKey.parse (ByteString -> io SecretKey) -> ByteString -> io SecretKey
forall a b. (a -> b) -> a -> b
$ ByteString
byteString
      Test io tree -> ByteString -> ByteString -> io ()
forall (io :: * -> *) a (tree :: * -> *).
(HasCallStack, Applicative io, Eq a, Show a) =>
Test io tree -> a -> a -> io ()
Test.assertEq Test io tree
test (Signer -> ByteString
Signer.render (SecretKey -> Signer
Signer.Asymmetric SecretKey
secretKey)) ByteString
byteString

    Test io tree -> String -> io () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> io () -> tree ()
Test.it Test io tree
test String
"works with a secret" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      let byteString :: ByteString
byteString = String -> ByteString
Ascii.pack String
"whsec_bXlzZWNyZXRrZXkxMjM0NQ=="
      Secret
secret <- Either InvalidSecret Secret -> io Secret
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidSecret Secret -> io Secret)
-> (ByteString -> Either InvalidSecret Secret)
-> ByteString
-> io Secret
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidSecret Secret
Secret.parse (ByteString -> io Secret) -> ByteString -> io Secret
forall a b. (a -> b) -> a -> b
$ ByteString
byteString
      Test io tree -> ByteString -> ByteString -> io ()
forall (io :: * -> *) a (tree :: * -> *).
(HasCallStack, Applicative io, Eq a, Show a) =>
Test io tree -> a -> a -> io ()
Test.assertEq Test io tree
test (Signer -> ByteString
Signer.render (Secret -> Signer
Signer.Symmetric Secret
secret)) ByteString
byteString

  Test io tree -> String -> tree () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> tree () -> tree ()
Test.describe Test io tree
test String
"sign" (tree () -> tree ()) -> tree () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
    Test io tree -> String -> io () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> io () -> tree ()
Test.it Test io tree
test String
"works with asymmetric" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      Signer
signer <- Either InvalidSigner Signer -> io Signer
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidSigner Signer -> io Signer)
-> (ByteString -> Either InvalidSigner Signer)
-> ByteString
-> io Signer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidSigner Signer
Signer.parse (ByteString -> io Signer) -> ByteString -> io Signer
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"whsk_QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU="
      Message
message <- Either InvalidMessage Message -> io Message
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidMessage Message -> io Message)
-> (ByteString -> Either InvalidMessage Message)
-> ByteString
-> io Message
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidMessage Message
Message.parse (ByteString -> io Message) -> ByteString -> io Message
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"i.0.Hello, world!"
      let actual :: Signature
actual = Signer -> Message -> Signature
Signer.sign Signer
signer Message
message
      Signature
expected <- Either UnknownSignature Signature -> io Signature
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either UnknownSignature Signature -> io Signature)
-> io (Either UnknownSignature Signature) -> io Signature
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Either InvalidSignature (Either UnknownSignature Signature)
-> io (Either UnknownSignature Signature)
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (ByteString
-> Either InvalidSignature (Either UnknownSignature Signature)
Signature.parse (ByteString
 -> Either InvalidSignature (Either UnknownSignature Signature))
-> ByteString
-> Either InvalidSignature (Either UnknownSignature Signature)
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"v1a,CV1O+PvrwXM42OMUX+tmm6bA3cS0tgLp0qo3YKuu0MGmBrsUhA0MHXF11HsEUJtPfTKs80WE7WUKVt9TueLDCQ==")
      Test io tree -> Signature -> Signature -> io ()
forall (io :: * -> *) a (tree :: * -> *).
(HasCallStack, Applicative io, Eq a, Show a) =>
Test io tree -> a -> a -> io ()
Test.assertEq Test io tree
test Signature
actual Signature
expected

    Test io tree -> String -> io () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> io () -> tree ()
Test.it Test io tree
test String
"works with symmetric" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      Signer
signer <- Either InvalidSigner Signer -> io Signer
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidSigner Signer -> io Signer)
-> (ByteString -> Either InvalidSigner Signer)
-> ByteString
-> io Signer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidSigner Signer
Signer.parse (ByteString -> io Signer) -> ByteString -> io Signer
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"whsec_bXlzZWNyZXRrZXkxMjM0NQ=="
      Message
message <- Either InvalidMessage Message -> io Message
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidMessage Message -> io Message)
-> (ByteString -> Either InvalidMessage Message)
-> ByteString
-> io Message
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidMessage Message
Message.parse (ByteString -> io Message) -> ByteString -> io Message
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"i.0.Hello, world!"
      let actual :: Signature
actual = Signer -> Message -> Signature
Signer.sign Signer
signer Message
message
      Signature
expected <- Either UnknownSignature Signature -> io Signature
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either UnknownSignature Signature -> io Signature)
-> io (Either UnknownSignature Signature) -> io Signature
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Either InvalidSignature (Either UnknownSignature Signature)
-> io (Either UnknownSignature Signature)
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (ByteString
-> Either InvalidSignature (Either UnknownSignature Signature)
Signature.parse (ByteString
 -> Either InvalidSignature (Either UnknownSignature Signature))
-> ByteString
-> Either InvalidSignature (Either UnknownSignature Signature)
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"v1,IywpE5NXy+JdAScgR7j5Pt59GjmazD7iJuVsQoRZFyw=")
      Test io tree -> Signature -> Signature -> io ()
forall (io :: * -> *) a (tree :: * -> *).
(HasCallStack, Applicative io, Eq a, Show a) =>
Test io tree -> a -> a -> io ()
Test.assertEq Test io tree
test Signature
actual Signature
expected

  Test io tree -> String -> tree () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> tree () -> tree ()
Test.describe Test io tree
test String
"asymmetric" (tree () -> tree ()) -> tree () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
    Test io tree -> String -> io () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> io () -> tree ()
Test.it Test io tree
test String
"creates correct asymmetric signature" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      SecretKey
secretKey <- Either InvalidSecretKey SecretKey -> io SecretKey
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidSecretKey SecretKey -> io SecretKey)
-> (ByteString -> Either InvalidSecretKey SecretKey)
-> ByteString
-> io SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidSecretKey SecretKey
SecretKey.parse (ByteString -> io SecretKey) -> ByteString -> io SecretKey
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"whsk_QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU="
      Message
message <- Either InvalidMessage Message -> io Message
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidMessage Message -> io Message)
-> (ByteString -> Either InvalidMessage Message)
-> ByteString
-> io Message
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidMessage Message
Message.parse (ByteString -> io Message) -> ByteString -> io Message
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"i.0.Hello, world!"
      let actual :: AsymmetricSignature
actual = SecretKey -> Message -> AsymmetricSignature
Signer.asymmetric SecretKey
secretKey Message
message
      AsymmetricSignature
expected <- Either InvalidAsymmetricSignature AsymmetricSignature
-> io AsymmetricSignature
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidAsymmetricSignature AsymmetricSignature
 -> io AsymmetricSignature)
-> (ByteString
    -> Either InvalidAsymmetricSignature AsymmetricSignature)
-> ByteString
-> io AsymmetricSignature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidAsymmetricSignature AsymmetricSignature
AsymmetricSignature.parse (ByteString -> io AsymmetricSignature)
-> ByteString -> io AsymmetricSignature
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"CV1O+PvrwXM42OMUX+tmm6bA3cS0tgLp0qo3YKuu0MGmBrsUhA0MHXF11HsEUJtPfTKs80WE7WUKVt9TueLDCQ=="
      Test io tree -> AsymmetricSignature -> AsymmetricSignature -> io ()
forall (io :: * -> *) a (tree :: * -> *).
(HasCallStack, Applicative io, Eq a, Show a) =>
Test io tree -> a -> a -> io ()
Test.assertEq Test io tree
test AsymmetricSignature
actual AsymmetricSignature
expected

  Test io tree -> String -> tree () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> tree () -> tree ()
Test.describe Test io tree
test String
"symmetric" (tree () -> tree ()) -> tree () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
    Test io tree -> String -> io () -> tree ()
forall (io :: * -> *) (tree :: * -> *).
Test io tree -> String -> io () -> tree ()
Test.it Test io tree
test String
"creates correct symmetric signature" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      Secret
secret <- Either InvalidSecret Secret -> io Secret
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidSecret Secret -> io Secret)
-> (ByteString -> Either InvalidSecret Secret)
-> ByteString
-> io Secret
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidSecret Secret
Secret.parse (ByteString -> io Secret) -> ByteString -> io Secret
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"whsec_bXlzZWNyZXRrZXkxMjM0NQ=="
      Message
message <- Either InvalidMessage Message -> io Message
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidMessage Message -> io Message)
-> (ByteString -> Either InvalidMessage Message)
-> ByteString
-> io Message
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidMessage Message
Message.parse (ByteString -> io Message) -> ByteString -> io Message
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"i.0.Hello, world!"
      let actual :: SymmetricSignature
actual = Secret -> Message -> SymmetricSignature
Signer.symmetric Secret
secret Message
message
      SymmetricSignature
expected <- Either InvalidSymmetricSignature SymmetricSignature
-> io SymmetricSignature
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either InvalidSymmetricSignature SymmetricSignature
 -> io SymmetricSignature)
-> (ByteString
    -> Either InvalidSymmetricSignature SymmetricSignature)
-> ByteString
-> io SymmetricSignature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either InvalidSymmetricSignature SymmetricSignature
SymmetricSignature.parse (ByteString -> io SymmetricSignature)
-> ByteString -> io SymmetricSignature
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"IywpE5NXy+JdAScgR7j5Pt59GjmazD7iJuVsQoRZFyw="
      Test io tree -> SymmetricSignature -> SymmetricSignature -> io ()
forall (io :: * -> *) a (tree :: * -> *).
(HasCallStack, Applicative io, Eq a, Show a) =>
Test io tree -> a -> a -> io ()
Test.assertEq Test io tree
test SymmetricSignature
actual SymmetricSignature
expected