From 2b53bcf50306cce61dfaa7d95d619fb7f85fd4dd Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Tue, 15 Oct 2024 14:08:59 +0200 Subject: [PATCH] Add golden test for failed hash check in `governance vote create` --- .../Test/Golden/Governance/Vote.hs | 69 +++++++++++++++++-- .../governance_vote_create_hash_fails.out | 3 + 2 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 cardano-cli/test/cardano-cli-golden/files/golden/governance/vote/governance_vote_create_hash_fails.out diff --git a/cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs b/cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs index e6125a5216..e4120e9cfc 100644 --- a/cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs +++ b/cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs @@ -3,12 +3,18 @@ module Test.Golden.Governance.Vote where import Control.Monad (void) - -import Test.Cardano.CLI.Util (FileSem, bracketSem, execCardanoCLI, newFileSem, - noteInputFile, propertyOnce) - -import Hedgehog -import qualified Hedgehog.Extras.Test.Base as H +import Data.Monoid (Last (..)) +import GHC.IO.Exception (ExitCode (..)) +import qualified System.Environment as IO + +import Test.Cardano.CLI.Hash (exampleAnchorDataHash, exampleAnchorDataIpfsHash, + exampleAnchorDataPathGolden, serveFilesWhile, tamperBase16Hash) +import Test.Cardano.CLI.Util (FileSem, bracketSem, execCardanoCLI, + execDetailConfigCardanoCLI, newFileSem, noteInputFile, propertyOnce) + +import Hedgehog (Property, (===)) +import qualified Hedgehog as H +import qualified Hedgehog.Extras as H import qualified Hedgehog.Extras.Test.Golden as H hprop_golden_governance_governance_vote_create :: Property @@ -179,3 +185,54 @@ hprop_golden_governance_governance_vote_create_abstain_cc_hot_key = ] H.diffFileVsGoldenFile voteFile voteGold + +-- Execute me with: +-- @cabal test cardano-cli-test --test-options '-p "/golden governance vote create wrong hash fails/"'@ +hprop_golden_governance_vote_create_hash_fails :: Property +hprop_golden_governance_vote_create_hash_fails = + propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do + -- We modify the hash slightly so that the hash check fails + alteredHash <- H.evalMaybe $ tamperBase16Hash exampleAnchorDataHash + let relativeUrl = ["ipfs", exampleAnchorDataIpfsHash] + + vkeyFile <- noteInputFile "test/cardano-cli-golden/files/input/drep.vkey" + voteFile <- H.noteTempFile tempDir "vote" + voteGold <- + H.note "test/cardano-cli-golden/files/golden/governance/vote/governance_vote_create_hash_fails.out" + + -- Create temporary HTTP server with files required by the call to `cardano-cli` + env <- H.evalIO IO.getEnvironment + (exitCode, _, result) <- + serveFilesWhile + [ (relativeUrl, exampleAnchorDataPathGolden) + ] + ( \port -> do + execDetailConfigCardanoCLI + ( H.defaultExecConfig + { H.execConfigEnv = Last $ Just (("IPFS_GATEWAY_URI", "http://localhost:" ++ show port ++ "/") : env) + } + ) + [ "conway" + , "governance" + , "vote" + , "create" + , "--yes" + , "--governance-action-tx-id" + , "b1015258a99351c143a7a40b7b58f033ace10e3cc09c67780ed5b2b0992aa60a" + , "--governance-action-index" + , "5" + , "--drep-verification-key-file" + , vkeyFile + , "--out-file" + , voteFile + , "--anchor-url" + , "ipfs://" ++ exampleAnchorDataIpfsHash + , "--anchor-data-hash" + , alteredHash + , "--check-anchor-data-hash" + ] + ) + + exitCode === ExitFailure 1 + + H.diffVsGoldenFile result voteGold diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/governance/vote/governance_vote_create_hash_fails.out b/cardano-cli/test/cardano-cli-golden/files/golden/governance/vote/governance_vote_create_hash_fails.out new file mode 100644 index 0000000000..c8ce1e8809 --- /dev/null +++ b/cardano-cli/test/cardano-cli-golden/files/golden/governance/vote/governance_vote_create_hash_fails.out @@ -0,0 +1,3 @@ +Command failed: governance vote create Error: Error while checking resignation certificate metadata hash: Hashes do not match! +Expected: "ee38a4f5b8b9d8372386cc923bad19d1a0662298cf355bbe947e5eedf127fa9c" + Actual: "de38a4f5b8b9d8372386cc923bad19d1a0662298cf355bbe947e5eedf127fa9c"