commit cbfd7d94632389309cc89921602d6d09bb85a151 parent 711e17cfb0919cecb4534b2b5c5b026771e2a8ec Author: Jared Tobin <jared@jtobin.io> Date: Wed, 21 Feb 2024 16:11:29 +0400 Add context tests. Diffstat:
M | secp256k1-sys/test/Main.hs | | | 42 | +++++++++++++++++++++++++++++++++++++++++- |
1 file changed, 41 insertions(+), 1 deletion(-)
diff --git a/secp256k1-sys/test/Main.hs b/secp256k1-sys/test/Main.hs @@ -1,4 +1,44 @@ module Main where +import Control.Exception (bracket) +import Crypto.Secp256k1.Internal +import qualified Data.ByteString as BS +import Foreign.Ptr (Ptr) +import qualified Foreign.Ptr as F (nullPtr, castPtr) +import qualified System.Entropy as E +import Test.Tasty +import Test.Tasty.HUnit + main :: IO () -main = pure () +main = defaultMain units + +units :: TestTree +units = testGroup "unit tests" [ + context_create + , context_randomize + ] + +wcontext :: (Ptr Context -> IO a) -> IO a +wcontext = + bracket + (secp256k1_context_create _SECP256K1_CONTEXT_NONE) + secp256k1_context_destroy + +wentropy :: (Ptr Seed32 -> IO a) -> IO a +wentropy c = do + bs <- E.getEntropy 32 + BS.useAsCStringLen bs $ \(b, _) -> + c (F.castPtr b) + +-- context + +context_create :: TestTree +context_create = testCase "secp256k1_context_create (non-null)" $ + wcontext $ \tex -> assertBool "non-null" $ tex /= F.nullPtr + +context_randomize :: TestTree +context_randomize = testCase "secp256k1_context_randomize (success)" $ + wcontext $ \tex -> do + suc <- wentropy (secp256k1_context_randomize tex) + assertBool "success" (suc == 1) +