Skip to content

Commit

Permalink
Test URL functionality for governance drep metadata-hash
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Oct 7, 2024
1 parent c1adca8 commit 3eca05c
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ test-suite cardano-cli-test
Test.Cli.AddCostModels
Test.Cli.CreateCardano
Test.Cli.CreateTestnetData
Test.Cli.DRepMetadata
Test.Cli.FilePermissions
Test.Cli.Governance.DRep
Test.Cli.Governance.Hash
Expand Down
40 changes: 38 additions & 2 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import Numeric (showOct)
import System.Posix.Files (fileMode, getFileStatus)
#endif

import Test.Cardano.CLI.Util (FileSem, bracketSem, execCardanoCLI, newFileSem,
noteInputFile, noteTempFile, propertyOnce)
import GHC.IO.Exception (ExitCode (ExitFailure))
import Test.Cardano.CLI.Hash (exampleAnchorDataHash, exampleAnchorDataIpfsHash,
exampleAnchorDataPathGolden, serveFilesWhile, tamperBase16Hash)
import Test.Cardano.CLI.Util (FileSem, bracketSem, execCardanoCLI, execDetailCardanoCLI,
newFileSem, noteInputFile, noteTempFile, propertyOnce)

import Hedgehog
import qualified Hedgehog as H
Expand Down Expand Up @@ -330,3 +333,36 @@ hprop_golden_verification_key_drep =
]

H.diffFileVsGoldenFile vkeyFileOut goldenFile

-- Execute me with:
-- @cabal test cardano-cli-test --test-options '-p "/drep metadata hash url wrong hash fails/"'@
hprop_golden_drep_metadata_hash_url_wrong_hash_fails :: Property
hprop_golden_drep_metadata_hash_url_wrong_hash_fails =
propertyOnce $ do
-- We modify the hash slightly so that the hash check fails
alteredHash <- H.evalMaybe $ tamperBase16Hash exampleAnchorDataHash
let relativeUrl = [exampleAnchorDataIpfsHash]

-- Create temporary HTTP server with files required by the call to `cardano-cli`
(exitCode, _, result) <-
serveFilesWhile
[ (relativeUrl, exampleAnchorDataPathGolden)
]
( \port -> do
execDetailCardanoCLI
[ "conway"
, "governance"
, "drep"
, "metadata-hash"
, "--drep-metadata-url"
, "http://127.0.0.1:" ++ show port ++ "/" ++ exampleAnchorDataIpfsHash
, "--expected-hash"
, alteredHash
]
)

exitCode === ExitFailure 1

H.diffVsGoldenFile
result
"test/cardano-cli-golden/files/golden/governance/drep/drep_metadata_hash_url_wrong_hash_fails.out"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Command failed: governance drep metadata-hash Error: Hashes do not match!
Expected: "ee38a4f5b8b9d8372386cc923bad19d1a0662298cf355bbe947e5eedf127fa9c"
Actual: "de38a4f5b8b9d8372386cc923bad19d1a0662298cf355bbe947e5eedf127fa9c"
60 changes: 60 additions & 0 deletions cardano-cli/test/cardano-cli-test/Test/Cli/DRepMetadata.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{-# LANGUAGE FlexibleContexts #-}

module Test.Cli.DRepMetadata where

import Cardano.Api (MonadIO)

import Control.Monad (void)
import Control.Monad.Catch (MonadCatch)
import Control.Monad.Trans.Control (MonadBaseControl)

import Test.Cardano.CLI.Hash (exampleAnchorDataHash, exampleAnchorDataIpfsHash,
exampleAnchorDataPathTest, serveFilesWhile, tamperBase16Hash)
import Test.Cardano.CLI.Util (execCardanoCLIWithEnvVars, expectFailure, propertyOnce)

import Hedgehog (Property)
import qualified Hedgehog as H
import Hedgehog.Internal.Property (MonadTest)

-- Execute me with:
-- @cabal test cardano-cli-test --test-options '-p "/drep metadata hash url wrong hash fails/"'@
hprop_drep_metadata_hash_url_wrong_hash_fails :: Property
hprop_drep_metadata_hash_url_wrong_hash_fails =
propertyOnce . expectFailure $ do
-- We modify the hash slightly so that the hash check fails
alteredHash <- H.evalMaybe $ tamperBase16Hash exampleAnchorDataHash
-- We run the test with the modified hash
baseDrepMetadataHashUrl alteredHash

-- Execute me with:
-- @cabal test cardano-cli-test --test-options '-p "/drep metadata hash url correct hash/"'@
hprop_drep_metadata_hash_url_correct_hash :: Property
hprop_drep_metadata_hash_url_correct_hash =
propertyOnce $ baseDrepMetadataHashUrl exampleAnchorDataHash

baseDrepMetadataHashUrl
:: (MonadBaseControl IO m, MonadTest m, MonadIO m, MonadCatch m)
=> String
-> m ()
baseDrepMetadataHashUrl hash = do
let relativeUrl = ["ipfs", exampleAnchorDataIpfsHash]

-- Create temporary HTTP server with files required by the call to `cardano-cli`
-- In this case, the server emulates an IPFS gateway
serveFilesWhile
[ (relativeUrl, exampleAnchorDataPathTest)
]
( \port -> do
void $
execCardanoCLIWithEnvVars
[("IPFS_GATEWAY_URI", "http://localhost:" ++ show port ++ "/")]
[ "conway"
, "governance"
, "drep"
, "metadata-hash"
, "--drep-metadata-url"
, "ipfs://" ++ exampleAnchorDataIpfsHash
, "--expected-hash"
, hash
]
)

0 comments on commit 3eca05c

Please sign in to comment.