Skip to content

Commit

Permalink
Add golden test for failed hash check in governance vote create
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Oct 15, 2024
1 parent 1cca3a6 commit 2b53bcf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
69 changes: 63 additions & 6 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 2b53bcf

Please sign in to comment.