From 3b46d034cb4e627ca9d4352888b04bf291f241c7 Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Tue, 15 Oct 2024 12:18:30 +0200 Subject: [PATCH] Add test to hash check in `governance committee create-cold-key-resignation-certificate` --- cardano-cli/cardano-cli.cabal | 1 + .../Test/Cli/Governance/Committee.hs | 83 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 cardano-cli/test/cardano-cli-test/Test/Cli/Governance/Committee.hs diff --git a/cardano-cli/cardano-cli.cabal b/cardano-cli/cardano-cli.cabal index ac8f9bb1d4..47c3ed4c40 100644 --- a/cardano-cli/cardano-cli.cabal +++ b/cardano-cli/cardano-cli.cabal @@ -337,6 +337,7 @@ test-suite cardano-cli-test Test.Cli.CreateTestnetData Test.Cli.DRepMetadata Test.Cli.FilePermissions + Test.Cli.Governance.Committee Test.Cli.Governance.DRep Test.Cli.Governance.Hash Test.Cli.Hash diff --git a/cardano-cli/test/cardano-cli-test/Test/Cli/Governance/Committee.hs b/cardano-cli/test/cardano-cli-test/Test/Cli/Governance/Committee.hs new file mode 100644 index 0000000000..791b6377bc --- /dev/null +++ b/cardano-cli/test/cardano-cli-test/Test/Cli/Governance/Committee.hs @@ -0,0 +1,83 @@ +{-# LANGUAGE FlexibleContexts #-} + +module Test.Cli.Governance.Committee 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 (execCardanoCLI, execCardanoCLIWithEnvVars, expectFailure, + noteTempFile, propertyOnce) + +import Hedgehog (MonadTest, Property) +import qualified Hedgehog as H +import qualified Hedgehog.Extras as H + +-- | Execute me with: +-- @cabal test cardano-cli-test --test-options '-p "/governance governance committee checks wrong hash fails/"'@ +hprop_governance_governance_committee_checks_wrong_hash_fails :: Property +hprop_governance_governance_committee_checks_wrong_hash_fails = + propertyOnce . expectFailure . H.moduleWorkspace "tmp" $ \tempDir -> do + -- We modify the hash slightly so that the hash check fails + alteredHash <- H.evalMaybe $ tamperBase16Hash exampleAnchorDataHash + -- We run the test with the altered + baseGovernanceGovernanceCommitteeChecksHash + alteredHash + tempDir + +-- | Execute me with: +-- @cabal test cardano-cli-test --test-options '-p "/governance governance committee checks right hash works/"'@ +hprop_governance_governance_committee_checks_right_hash_works :: Property +hprop_governance_governance_committee_checks_right_hash_works = + propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> + baseGovernanceGovernanceCommitteeChecksHash exampleAnchorDataHash tempDir + +baseGovernanceGovernanceCommitteeChecksHash + :: (MonadBaseControl IO m, MonadTest m, MonadIO m, MonadCatch m) => String -> FilePath -> m () +baseGovernanceGovernanceCommitteeChecksHash hash tempDir = do + ccColdVKey <- noteTempFile tempDir "cold.vkey" + ccColdSKey <- noteTempFile tempDir "cold.skey" + + certFile <- noteTempFile tempDir "hot-auth.cert" + + void $ + execCardanoCLI + [ "conway" + , "governance" + , "committee" + , "key-gen-cold" + , "--verification-key-file" + , ccColdVKey + , "--signing-key-file" + , ccColdSKey + ] + + 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" + , "committee" + , "create-cold-key-resignation-certificate" + , "--cold-verification-key-file" + , ccColdVKey + , "--resignation-metadata-url" + , "ipfs://" ++ exampleAnchorDataIpfsHash + , "--resignation-metadata-hash" + , hash + , "--check-resignation-metadata-hash" + , "--out-file" + , certFile + ] + )