Skip to content

Commit

Permalink
Organise examples by grouping them in a datatype (refactoring)
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Dec 20, 2024
1 parent 8303f59 commit 92bd7ee
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 289 deletions.
4 changes: 2 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
cardano-cli/test/cardano-cli-golden/files/input/example_anchor_data.txt -text
cardano-cli/test/cardano-cli-golden/files/input/example_anchor_data1.txt -text
cardano-cli/test/cardano-cli-golden/files/input/example_anchor_data2.txt -text
cardano-cli/test/cardano-cli-golden/files/input/example_drep_reg_anchor_data.json -text
cardano-cli/test/cardano-cli-golden/files/input/example_gov_action_anchor1.json -text
cardano-cli/test/cardano-cli-golden/files/input/example_gov_action_anchor2.json -text
cardano-cli/test/cardano-cli-test/files/input/example_anchor_data.txt -text
cardano-cli/test/cardano-cli-test/files/input/example_anchor_data1.txt -text
cardano-cli/test/cardano-cli-test/files/input/example_anchor_data2.txt -text
cardano-cli/test/cardano-cli-test/files/input/example_drep_reg_anchor_data.json -text
cardano-cli/test/cardano-cli-test/files/input/example_gov_action_anchor1.json -text
Expand Down
125 changes: 64 additions & 61 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Action.hs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import System.Exit (ExitCode (..))
import System.FilePath ((</>))

import Test.Cardano.CLI.Aeson (assertHasMappings)
import Test.Cardano.CLI.Hash (exampleAnchorDataHash, exampleAnchorDataIpfsHash,
exampleAnchorDataPathGolden, serveFilesWhile, tamperBase16Hash)
import Test.Cardano.CLI.Hash (AnchorDataExample (..), dummyAnchorDataExample1,
serveFilesWhile, tamperAnchorDataExampleHash)
import qualified Test.Cardano.CLI.Util as H hiding (noteTempFile)
import Test.Cardano.CLI.Util

Expand Down Expand Up @@ -335,8 +335,8 @@ hprop_golden_governance_committee_checks_wrong_hash_fails :: Property
hprop_golden_governance_committee_checks_wrong_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]
alteredHashAnchorData <- H.evalMaybe $ tamperAnchorDataExampleHash dummyAnchorDataExample1
let relativeUrl = ["ipfs", anchorDataIpfsHash alteredHashAnchorData]

ccColdVKey <- noteTempFile tempDir "cold.vkey"
ccColdSKey <- noteTempFile tempDir "cold.skey"
Expand All @@ -359,7 +359,7 @@ hprop_golden_governance_committee_checks_wrong_hash_fails =
env <- H.evalIO IO.getEnvironment
(exitCode, _, result) <-
serveFilesWhile
[ (relativeUrl, exampleAnchorDataPathGolden)
[ (relativeUrl, anchorDataPathGolden alteredHashAnchorData)
]
( \port -> do
execDetailConfigCardanoCLI
Expand All @@ -374,9 +374,9 @@ hprop_golden_governance_committee_checks_wrong_hash_fails =
, "--cold-verification-key-file"
, ccColdVKey
, "--resignation-metadata-url"
, "ipfs://" ++ exampleAnchorDataIpfsHash
, "ipfs://" ++ anchorDataIpfsHash alteredHashAnchorData
, "--resignation-metadata-hash"
, alteredHash
, anchorDataHash alteredHashAnchorData
, "--check-resignation-metadata-hash"
, "--out-file"
, certFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import System.Posix.Files (fileMode, getFileStatus)
#endif

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

Expand Down Expand Up @@ -470,13 +470,13 @@ 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]
alteredHashAnchorDataExample <- H.evalMaybe $ tamperAnchorDataExampleHash dummyAnchorDataExample1
let relativeUrl = [anchorDataIpfsHash alteredHashAnchorDataExample]

-- Create temporary HTTP server with files required by the call to `cardano-cli`
(exitCode, _, result) <-
serveFilesWhile
[ (relativeUrl, exampleAnchorDataPathGolden)
[ (relativeUrl, anchorDataPathGolden alteredHashAnchorDataExample)
]
( \port -> do
execDetailCardanoCLI
Expand All @@ -485,9 +485,9 @@ hprop_golden_drep_metadata_hash_url_wrong_hash_fails =
, "drep"
, "metadata-hash"
, "--drep-metadata-url"
, "http://127.0.0.1:" ++ show port ++ "/" ++ exampleAnchorDataIpfsHash
, "http://127.0.0.1:" ++ show port ++ "/" ++ anchorDataIpfsHash alteredHashAnchorDataExample
, "--expected-hash"
, alteredHash
, anchorDataHash alteredHashAnchorDataExample
]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import Data.Monoid (Last (..))
import GHC.IO.Exception (ExitCode (..))
import qualified System.Environment as IO

import Test.Cardano.CLI.Hash (exampleGovActionAnchorHash1,
exampleGovActionAnchorIpfsHash1, exampleGovActionAnchorPathGolden1,
serveFilesWhile, tamperBase16Hash)
import Test.Cardano.CLI.Hash (AnchorDataExample (..), govActionAnchorDataExample1,
serveFilesWhile, tamperAnchorDataExampleHash)
import Test.Cardano.CLI.Util (FileSem, bracketSem, execCardanoCLI,
execDetailConfigCardanoCLI, newFileSem, noteInputFile, propertyOnce)

Expand Down Expand Up @@ -193,8 +192,8 @@ 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 exampleGovActionAnchorHash1
let relativeUrl = ["ipfs", exampleGovActionAnchorIpfsHash1]
tamperedExampleAnchorData <- H.evalMaybe $ tamperAnchorDataExampleHash govActionAnchorDataExample1
let relativeUrl = ["ipfs", anchorDataIpfsHash tamperedExampleAnchorData]

vkeyFile <- noteInputFile "test/cardano-cli-golden/files/input/drep.vkey"
voteFile <- H.noteTempFile tempDir "vote"
Expand All @@ -205,7 +204,7 @@ hprop_golden_governance_vote_create_hash_fails =
env <- H.evalIO IO.getEnvironment
(exitCode, _, result) <-
serveFilesWhile
[ (relativeUrl, exampleGovActionAnchorPathGolden1)
[ (relativeUrl, anchorDataPathGolden tamperedExampleAnchorData)
]
( \port -> do
execDetailConfigCardanoCLI
Expand All @@ -227,9 +226,9 @@ hprop_golden_governance_vote_create_hash_fails =
, "--out-file"
, voteFile
, "--anchor-url"
, "ipfs://" ++ exampleGovActionAnchorIpfsHash1
, "ipfs://" ++ anchorDataIpfsHash tamperedExampleAnchorData
, "--anchor-data-hash"
, alteredHash
, anchorDataHash tamperedExampleAnchorData
, "--check-anchor-data-hash"
]
)
Expand Down
154 changes: 82 additions & 72 deletions cardano-cli/test/cardano-cli-test-lib/Test/Cardano/CLI/Hash.hs
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
{-# LANGUAGE FlexibleContexts #-}

module Test.Cardano.CLI.Hash
( exampleAnchorDataHash
, exampleAnchorDataHash2
( AnchorDataExample (..)
, serveFilesWhile
, exampleAnchorDataPathTest
, exampleAnchorDataPathTest2
, exampleAnchorDataPathGolden
, exampleAnchorDataPathGolden2
, exampleAnchorDataIpfsHash
, exampleAnchorDataIpfsHash2
, tamperBase16Hash
, exampleDRepRegAnchorHash
, exampleDRepRegAnchorIpfsHash
, exampleDRepRegAnchorPathTest
, exampleDRepRegAnchorPathGolden
, exampleGovActionAnchorHash1
, exampleGovActionAnchorHash2
, exampleGovActionAnchorPathGolden1
, exampleGovActionAnchorPathGolden2
, exampleGovActionAnchorPathTest1
, exampleGovActionAnchorPathTest2
, exampleGovActionAnchorIpfsHash1
, exampleGovActionAnchorIpfsHash2
, dummyAnchorDataExample1
, dummyAnchorDataExample2
, drepRegAnchorDataExample
, govActionAnchorDataExample1
, govActionAnchorDataExample2
, tamperAnchorDataExampleHash
, stakePoolMetadataExample
)
where

Expand All @@ -49,58 +37,74 @@ import Network.Wai.Handler.Warp (defaultSettings, openFreePort, runSet
import Hedgehog as H
import Hedgehog.Internal.Source (HasCallStack)

-- ** Anchor hashes for the example files

exampleAnchorDataHash, exampleAnchorDataHash2 :: String
exampleAnchorDataHash = "de38a4f5b8b9d8372386cc923bad19d1a0662298cf355bbe947e5eedf127fa9c"
exampleAnchorDataHash2 = "8b4fda934272320ec8d11ba5a7904ab74686a8ec97f2c1331b68d11e28bda26f"

exampleDRepRegAnchorHash, exampleGovActionAnchorHash1, exampleGovActionAnchorHash2 :: String
exampleDRepRegAnchorHash = "15a1c724bfbb8c0a0e84e2e359525017aefb8914ecc219bb9ad469368f14975d"
exampleGovActionAnchorHash1 = "d0c923d899917cd62cbf0e766cc2534a1f4739af69da0241a4992ad24d861ff0"
exampleGovActionAnchorHash2 = "21dd5a8219936e0d756f44f7f1a7179806b5afa45b6cbfb9e7d7efe3123c8e51"

-- ** Paths to the example files for golden tests

exampleAnchorDataPathGolden, exampleAnchorDataPathGolden2 :: String
exampleAnchorDataPathGolden = "test/cardano-cli-golden/files/input/example_anchor_data.txt"
exampleAnchorDataPathGolden2 = "test/cardano-cli-golden/files/input/example_anchor_data2.txt"

exampleDRepRegAnchorPathGolden
, exampleGovActionAnchorPathGolden1
, exampleGovActionAnchorPathGolden2
:: String
exampleDRepRegAnchorPathGolden = "test/cardano-cli-golden/files/input/example_drep_reg_anchor_data.json"
exampleGovActionAnchorPathGolden1 = "test/cardano-cli-golden/files/input/example_gov_action_anchor1.json"
exampleGovActionAnchorPathGolden2 = "test/cardano-cli-golden/files/input/example_gov_action_anchor2.json"

-- ** Paths to the example files for normal testes

exampleAnchorDataPathTest, exampleAnchorDataPathTest2 :: String
exampleAnchorDataPathTest = "test/cardano-cli-test/files/input/example_anchor_data.txt"
exampleAnchorDataPathTest2 = "test/cardano-cli-test/files/input/example_anchor_data2.txt"

exampleDRepRegAnchorPathTest
, exampleGovActionAnchorPathTest1
, exampleGovActionAnchorPathTest2
:: String
exampleDRepRegAnchorPathTest = "test/cardano-cli-test/files/input/example_drep_reg_anchor_data.json"
exampleGovActionAnchorPathTest1 = "test/cardano-cli-test/files/input/example_gov_action_anchor1.json"
exampleGovActionAnchorPathTest2 = "test/cardano-cli-test/files/input/example_gov_action_anchor2.json"

-- ** Ipfs hashes for the example files

exampleAnchorDataIpfsHash, exampleAnchorDataIpfsHash2 :: String
exampleAnchorDataIpfsHash = "QmbL5EBFJLf8DdPkWAskG3Euin9tHY8naqQ2JDoHnWHHXJ"
exampleAnchorDataIpfsHash2 = "QmdTJ4PabgSabg8K1Z4MNXnSVM8bjJnAikC3rVWfPVExQj"

exampleDRepRegAnchorIpfsHash
, exampleGovActionAnchorIpfsHash1
, exampleGovActionAnchorIpfsHash2
:: String
exampleDRepRegAnchorIpfsHash = "Qmb6vATFuc2o9zeFEseTvKZiqGRvuYoZRT5SNZN6L88Les"
exampleGovActionAnchorIpfsHash1 = "QmdGH8Qa1f5mJJxbjYuHUqvwqCqUQm66y7EVNFEMgzZJeA"
exampleGovActionAnchorIpfsHash2 = "QmfCgYdiMSTTJ4Uw93vqwDw2DL2xfr7QrNeRnsxZqaCcLx"
-- * Anchor data examples

-- | Groups information about a given anchor data example
data AnchorDataExample = AnchorDataExample
{ anchorDataHash :: String
, anchorDataPathGolden :: String
, anchorDataPathTest :: String
, anchorDataIpfsHash :: String
}
deriving (Eq, Show)

-- ** Examples

dummyAnchorDataExample1 :: AnchorDataExample
dummyAnchorDataExample1 =
AnchorDataExample
{ anchorDataHash = "de38a4f5b8b9d8372386cc923bad19d1a0662298cf355bbe947e5eedf127fa9c"
, anchorDataPathGolden = "test/cardano-cli-golden/files/input/example_anchor_data1.txt"
, anchorDataPathTest = "test/cardano-cli-test/files/input/example_anchor_data1.txt"
, anchorDataIpfsHash = "QmbL5EBFJLf8DdPkWAskG3Euin9tHY8naqQ2JDoHnWHHXJ"
}

dummyAnchorDataExample2 :: AnchorDataExample
dummyAnchorDataExample2 =
AnchorDataExample
{ anchorDataHash = "8b4fda934272320ec8d11ba5a7904ab74686a8ec97f2c1331b68d11e28bda26f"
, anchorDataPathGolden = "test/cardano-cli-golden/files/input/example_anchor_data2.txt"
, anchorDataPathTest = "test/cardano-cli-test/files/input/example_anchor_data2.txt"
, anchorDataIpfsHash = "QmdTJ4PabgSabg8K1Z4MNXnSVM8bjJnAikC3rVWfPVExQj"
}

drepRegAnchorDataExample :: AnchorDataExample
drepRegAnchorDataExample =
AnchorDataExample
{ anchorDataHash = "15a1c724bfbb8c0a0e84e2e359525017aefb8914ecc219bb9ad469368f14975d"
, anchorDataPathGolden = "test/cardano-cli-golden/files/input/example_drep_reg_anchor_data.json"
, anchorDataPathTest = "test/cardano-cli-test/files/input/example_drep_reg_anchor_data.json"
, anchorDataIpfsHash = "Qmb6vATFuc2o9zeFEseTvKZiqGRvuYoZRT5SNZN6L88Les"
}

govActionAnchorDataExample1 :: AnchorDataExample
govActionAnchorDataExample1 =
AnchorDataExample
{ anchorDataHash = "d0c923d899917cd62cbf0e766cc2534a1f4739af69da0241a4992ad24d861ff0"
, anchorDataPathGolden = "test/cardano-cli-golden/files/input/example_gov_action_anchor1.json"
, anchorDataPathTest = "test/cardano-cli-test/files/input/example_gov_action_anchor1.json"
, anchorDataIpfsHash = "QmdGH8Qa1f5mJJxbjYuHUqvwqCqUQm66y7EVNFEMgzZJeA"
}

govActionAnchorDataExample2 :: AnchorDataExample
govActionAnchorDataExample2 =
AnchorDataExample
{ anchorDataHash = "21dd5a8219936e0d756f44f7f1a7179806b5afa45b6cbfb9e7d7efe3123c8e51"
, anchorDataPathGolden = "test/cardano-cli-golden/files/input/example_gov_action_anchor2.json"
, anchorDataPathTest = "test/cardano-cli-test/files/input/example_gov_action_anchor2.json"
, anchorDataIpfsHash = "QmfCgYdiMSTTJ4Uw93vqwDw2DL2xfr7QrNeRnsxZqaCcLx"
}

stakePoolMetadataExample :: AnchorDataExample
stakePoolMetadataExample =
AnchorDataExample
{ anchorDataHash = "8241de08075886a7d09c847c9bbd1719459dac0bd0a2f085e673611ebb9a5965"
, anchorDataPathGolden = "test/cardano-cli-golden/files/input/example_stake_pool_metadata.json"
, anchorDataPathTest = "test/cardano-cli-test/files/input/example_stake_pool_metadata.json"
, anchorDataIpfsHash = "QmR1HAT4Hb4HjjqcgoXwupYXMF6t8h7MoSP24HMfV8t38a"
}

-- * Utility functions

-- | Tamper with the base16 hash by adding one to the first character
tamperBase16Hash :: String -> Maybe String
Expand All @@ -114,6 +118,12 @@ tamperBase16Hash (headChar : tailStr) =
lowerCaseRest = map toLower tailStr
hexChars = ['0' .. '9'] ++ ['a' .. 'f']

-- | Tamper with the base16 hash in an AnchorDataExample by adding one to the first character
tamperAnchorDataExampleHash :: AnchorDataExample -> Maybe AnchorDataExample
tamperAnchorDataExampleHash anchorDataExample@(AnchorDataExample{anchorDataHash = anchorDataHash'}) = do
tamperedAnchorDataHash <- tamperBase16Hash anchorDataHash'
return (anchorDataExample{anchorDataHash = tamperedAnchorDataHash})

-- | Takes a relative url (as a list of segments), a file path, and an action, and it serves
-- the file in the url provided in a random free port that is passed as a parameter to the
-- action. After the action returns, it shuts down the server. It returns the result of the
Expand Down
20 changes: 10 additions & 10 deletions cardano-cli/test/cardano-cli-test/Test/Cli/DRepMetadata.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ 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.Hash (AnchorDataExample (..), dummyAnchorDataExample1,
serveFilesWhile, tamperAnchorDataExampleHash)
import Test.Cardano.CLI.Util (execCardanoCLIWithEnvVars, expectFailure, propertyOnce)

import Hedgehog (Property)
Expand All @@ -22,30 +22,30 @@ 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
alteredHash <- H.evalMaybe $ tamperAnchorDataExampleHash dummyAnchorDataExample1
-- 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
propertyOnce $ baseDrepMetadataHashUrl dummyAnchorDataExample1

baseDrepMetadataHashUrl
:: (MonadBaseControl IO m, MonadTest m, MonadIO m, MonadCatch m)
=> String
=> AnchorDataExample
-- ^ The hash to check against. Changing this value allows us to test the
-- behavior of the command both when the hash is correct and when it is incorrect
-- reusing the same code.
-> m ()
baseDrepMetadataHashUrl hash = do
let relativeUrl = ["ipfs", exampleAnchorDataIpfsHash]
baseDrepMetadataHashUrl exampleAnchorData = do
let relativeUrl = ["ipfs", anchorDataIpfsHash exampleAnchorData]

-- 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)
[ (relativeUrl, anchorDataPathTest exampleAnchorData)
]
( \port -> do
void $
Expand All @@ -56,8 +56,8 @@ baseDrepMetadataHashUrl hash = do
, "drep"
, "metadata-hash"
, "--drep-metadata-url"
, "ipfs://" ++ exampleAnchorDataIpfsHash
, "ipfs://" ++ anchorDataIpfsHash exampleAnchorData
, "--expected-hash"
, hash
, anchorDataHash exampleAnchorData
]
)
Loading

0 comments on commit 92bd7ee

Please sign in to comment.