Skip to content

Commit

Permalink
Add tests for local file hashing in hash anchor-data
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Sep 13, 2024
1 parent 4646017 commit 9a594e4
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cardano-cli/test/cardano-cli-test/files/input/example_anchor_data.txt -text
2 changes: 2 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ test-suite cardano-cli-test
cardano-ledger-alonzo,
cardano-slotting,
containers,
directory,
exceptions,
filepath,
hedgehog,
Expand All @@ -345,6 +346,7 @@ test-suite cardano-cli-test
Test.Cli.FilePermissions
Test.Cli.Governance.DRep
Test.Cli.Governance.Hash
Test.Cli.Hash
Test.Cli.ITN
Test.Cli.Json
Test.Cli.MonadWarning
Expand Down
95 changes: 95 additions & 0 deletions cardano-cli/test/cardano-cli-test/Test/Cli/Hash.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module Test.Cli.Hash where

import Control.Monad (void)
import Data.List (intercalate)
import GHC.IO.Exception (ExitCode (ExitFailure))
import System.Directory (getCurrentDirectory)
import System.FilePath (dropTrailingPathSeparator)
import System.FilePath.Posix (splitDirectories)

import Test.Cardano.CLI.Util

import Hedgehog as H
import qualified Hedgehog.Extras as H

exampleAnchorDataHash :: String
exampleAnchorDataHash = "de38a4f5b8b9d8372386cc923bad19d1a0662298cf355bbe947e5eedf127fa9c"

exampleAnchorDataPath :: String
exampleAnchorDataPath = "test/cardano-cli-test/files/input/example_anchor_data.txt"

-- | Execute me with:
-- @cabal test cardano-cli-test --test-options '-p "/generate anchor data hash from file/"'@
hprop_generate_anchor_data_hash_from_file :: Property
hprop_generate_anchor_data_hash_from_file =
propertyOnce $ do
result <-
execCardanoCLI
[ "hash"
, "anchor-data"
, "--file-binary"
, exampleAnchorDataPath
]
result === exampleAnchorDataHash

-- | Execute me with:
-- @cabal test cardano-cli-test --test-options '-p "/check anchor data hash from file/"'@
hprop_check_anchor_data_hash_from_file :: Property
hprop_check_anchor_data_hash_from_file =
propertyOnce $ do
void $
execCardanoCLI
[ "hash"
, "anchor-data"
, "--file-binary"
, exampleAnchorDataPath
, "--expected-hash"
, exampleAnchorDataHash
]

-- | Execute me with:
-- @cabal test cardano-cli-test --test-options '-p "/check anchor data hash from file fails/"'@
hprop_check_anchor_data_hash_from_file_fails :: Property
hprop_check_anchor_data_hash_from_file_fails =
propertyOnce $ do
(ec, _, _) <-
execDetailCardanoCLI
[ "hash"
, "anchor-data"
, "--file-binary"
, exampleAnchorDataPath
, "--expected-hash"
, 'c' : drop 1 exampleAnchorDataHash
]
ec === ExitFailure 1

-- | Execute me with:
-- @cabal test cardano-cli-test --test-options '-p "/generate anchor data hash from file uri/"'@
hprop_generate_anchor_data_hash_from_file_uri :: Property
hprop_generate_anchor_data_hash_from_file_uri =
propertyOnce $ do
cwd <- H.evalIO getCurrentDirectory
posixCwd <- toPOSIX cwd
result <-
execCardanoCLI
[ "hash"
, "anchor-data"
, "--url"
, "file://" ++ posixCwd ++ "/" ++ exampleAnchorDataPath
]
result === exampleAnchorDataHash
where
toPOSIX :: FilePath -> PropertyT IO [Char]
toPOSIX path =
case map dropTrailingPathSeparator (splitDirectories path) of
letter : restOfPath -> do
fixedLetter <- case letter of
'/' : _ -> return ""
l : ':' : _ -> return $ l : ":/"
wrongLetter -> do
H.note_ ("Unexpected letter: " ++ wrongLetter)
H.failure
return $ fixedLetter ++ intercalate "/" (fixedLetter : restOfPath)
[] -> do
H.note_ ("Path doesn't split:" ++ path)
H.failure
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is just a random file with content that is used for
testing the hashing of anchor data files.

0 comments on commit 9a594e4

Please sign in to comment.