module Signet.Unstable.Type.AsymmetricSignatureTest where

import qualified Control.Monad.Catch as Exception
import qualified Crypto.Error as Error
import qualified Crypto.PubKey.Ed25519 as Ed25519
import qualified Data.ByteString.Char8 as Ascii
import qualified Heck as Test
import qualified Signet.Unstable.Exception.InvalidAsymmetricSignature as InvalidAsymmetricSignature
import qualified Signet.Unstable.Extra.Either as Either
import qualified Signet.Unstable.Type.AsymmetricSignature as AsymmetricSignature

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 (m :: * -> *) (n :: * -> *).
Test m n -> String -> n () -> n ()
Test.describe Test io tree
test String
"Signet.Unstable.Type.AsymmetricSignature" (tree () -> tree ()) -> tree () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
  Test io tree -> String -> tree () -> tree ()
forall (m :: * -> *) (n :: * -> *).
Test m n -> String -> n () -> n ()
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 (m :: * -> *) (n :: * -> *).
Test m n -> String -> m () -> n ()
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 result :: Either InvalidAsymmetricSignature AsymmetricSignature
result = ByteString -> Either InvalidAsymmetricSignature AsymmetricSignature
AsymmetricSignature.parse ByteString
byteString
      Test io tree
-> Either InvalidAsymmetricSignature AsymmetricSignature
-> Either InvalidAsymmetricSignature AsymmetricSignature
-> io ()
forall (m :: * -> *) a (n :: * -> *).
(HasCallStack, Applicative m, Eq a, Show a) =>
Test m n -> a -> a -> m ()
Test.assertEq Test io tree
test Either InvalidAsymmetricSignature AsymmetricSignature
result (InvalidAsymmetricSignature
-> Either InvalidAsymmetricSignature AsymmetricSignature
forall a b. a -> Either a b
Left (InvalidAsymmetricSignature
 -> Either InvalidAsymmetricSignature AsymmetricSignature)
-> InvalidAsymmetricSignature
-> Either InvalidAsymmetricSignature AsymmetricSignature
forall a b. (a -> b) -> a -> b
$ ByteString -> InvalidAsymmetricSignature
InvalidAsymmetricSignature.MkInvalidAsymmetricSignature ByteString
byteString)

    Test io tree -> String -> io () -> tree ()
forall (m :: * -> *) (n :: * -> *).
Test m n -> String -> m () -> n ()
Test.it Test io tree
test String
"succeeds with valid input" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      let result :: Either InvalidAsymmetricSignature AsymmetricSignature
result = ByteString -> Either InvalidAsymmetricSignature AsymmetricSignature
AsymmetricSignature.parse (ByteString
 -> Either InvalidAsymmetricSignature AsymmetricSignature)
-> ByteString
-> Either InvalidAsymmetricSignature AsymmetricSignature
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVotMDEyMzQ1Njc4OS1hYmNkZWZnaGlqa2xtbm9wcXJzdHV2cXh5eg=="
      signature <- Either CryptoError Signature -> io Signature
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either CryptoError Signature -> io Signature)
-> (ByteString -> Either CryptoError Signature)
-> ByteString
-> io Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CryptoFailable Signature -> Either CryptoError Signature
forall a. CryptoFailable a -> Either CryptoError a
Error.eitherCryptoError (CryptoFailable Signature -> Either CryptoError Signature)
-> (ByteString -> CryptoFailable Signature)
-> ByteString
-> Either CryptoError Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> CryptoFailable Signature
forall ba. ByteArrayAccess ba => ba -> CryptoFailable Signature
Ed25519.signature (ByteString -> io Signature) -> ByteString -> io Signature
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789-abcdefghijklmnopqrstuvqxyz"
      Test.assertEq test result (Right $ AsymmetricSignature.MkAsymmetricSignature signature)

  Test io tree -> String -> tree () -> tree ()
forall (m :: * -> *) (n :: * -> *).
Test m n -> String -> n () -> n ()
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 (m :: * -> *) (n :: * -> *).
Test m n -> String -> m () -> n ()
Test.it Test io tree
test String
"works" (io () -> tree ()) -> io () -> tree ()
forall a b. (a -> b) -> a -> b
$ do
      asymmetricSignature <- Either CryptoError AsymmetricSignature -> io AsymmetricSignature
forall e (m :: * -> *) a.
(Exception e, MonadThrow m) =>
Either e a -> m a
Either.throw (Either CryptoError AsymmetricSignature -> io AsymmetricSignature)
-> (ByteString -> Either CryptoError AsymmetricSignature)
-> ByteString
-> io AsymmetricSignature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Signature -> AsymmetricSignature)
-> Either CryptoError Signature
-> Either CryptoError AsymmetricSignature
forall a b.
(a -> b) -> Either CryptoError a -> Either CryptoError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Signature -> AsymmetricSignature
AsymmetricSignature.MkAsymmetricSignature (Either CryptoError Signature
 -> Either CryptoError AsymmetricSignature)
-> (ByteString -> Either CryptoError Signature)
-> ByteString
-> Either CryptoError AsymmetricSignature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CryptoFailable Signature -> Either CryptoError Signature
forall a. CryptoFailable a -> Either CryptoError a
Error.eitherCryptoError (CryptoFailable Signature -> Either CryptoError Signature)
-> (ByteString -> CryptoFailable Signature)
-> ByteString
-> Either CryptoError Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> CryptoFailable Signature
forall ba. ByteArrayAccess ba => ba -> CryptoFailable Signature
Ed25519.signature (ByteString -> io AsymmetricSignature)
-> ByteString -> io AsymmetricSignature
forall a b. (a -> b) -> a -> b
$ String -> ByteString
Ascii.pack String
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789-abcdefghijklmnopqrstuvqxyz"
      Test.assertEq test (AsymmetricSignature.render asymmetricSignature) (Ascii.pack "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVotMDEyMzQ1Njc4OS1hYmNkZWZnaGlqa2xtbm9wcXJzdHV2cXh5eg==")