Skip to content

Commit

Permalink
Implement golden test for failed hash check in `stake-pool metadata-h…
Browse files Browse the repository at this point in the history
…ash`
  • Loading branch information
palas committed Oct 9, 2024
1 parent 664d6ff commit e867b80
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
{- HLINT ignore "Use camelCase" -}
{-# LANGUAGE FlexibleContexts #-}

module Test.Golden.Governance.StakeAddress where

import Control.Monad (void)
import System.Exit (ExitCode (..))

import Test.Cardano.CLI.Util (execCardanoCLI, noteInputFile, propertyOnce)
import Test.Cardano.CLI.Hash (serveFilesWhile, tamperBase16Hash)
import Test.Cardano.CLI.Util (execCardanoCLI, execDetailCardanoCLI, noteInputFile,
propertyOnce)

import Hedgehog
import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.Golden as H

exampleStakePoolMetadataHash :: String
exampleStakePoolMetadataHash = "8241de08075886a7d09c847c9bbd1719459dac0bd0a2f085e673611ebb9a5965"

exampleStakePoolMetadataPathGolden :: String
exampleStakePoolMetadataPathGolden = "test/cardano-cli-golden/files/input/example_stake_pool_metadata.json"

exampleStakePoolMetadataIpfsHash :: String
exampleStakePoolMetadataIpfsHash = "QmR1HAT4Hb4HjjqcgoXwupYXMF6t8h7MoSP24HMfV8t38a"

hprop_golden_conway_stakeaddress_delegate_no_confidence :: Property
hprop_golden_conway_stakeaddress_delegate_no_confidence =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
Expand Down Expand Up @@ -220,3 +234,35 @@ hprop_golden_conway_stakeaddress_register_and_delegate_stake_and_vote =
]

H.diffFileVsGoldenFile certFile certGold

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

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

exitCode === ExitFailure 1

H.diffVsGoldenFile
result
"test/cardano-cli-golden/files/golden/governance/stakeaddress/stake_pool_metadata_hash_url_wrong_hash_fails.out"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Command failed: stake-pool metadata-hash Error: Hashes do not match!
Expected: "9241de08075886a7d09c847c9bbd1719459dac0bd0a2f085e673611ebb9a5965"
Actual: "8241de08075886a7d09c847c9bbd1719459dac0bd0a2f085e673611ebb9a5965"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"homepage":"https://iohk.io","name":"Genesis Pool C","ticker":"GPC","description":"Lorem Ipsum Dolor Sit Amet."}

0 comments on commit e867b80

Please sign in to comment.