Skip to content

Commit

Permalink
Merge pull request #1728 from saratomaz/test_stake_address_key_hash
Browse files Browse the repository at this point in the history
New tests for 'stake-address key-hash'
  • Loading branch information
mkoura committed Apr 4, 2023
2 parents 8847f0c + a254cc2 commit ff6e472
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cardano_node_tests/tests/data/golden_stake.vkey
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "StakeVerificationKeyShelley_ed25519",
"description": "Stake Verification Key",
"cborHex": "58203dbff8c7b4c72f1aee3aca2baf135b3fecbfd21e1bc4445a314ce6cff510e2d0"
}
61 changes: 61 additions & 0 deletions cardano_node_tests/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,67 @@ def test_address_invalid_data(self, cluster: clusterlib.ClusterLib, filter_str:
assert "invalid address" in err_str, err_str


@pytest.mark.smoke
class TestStakeAddressKeyHash:
"""Tests for cardano-cli stake-address key-hash."""

@allure.link(helpers.get_vcs_link())
@pytest.mark.parametrize("option", ("vkey", "vkey_file"))
def test_valid_verification_key(self, cluster: clusterlib.ClusterLib, option: str):
"""Check `stake-address key-hash` with valid verification key."""
common.get_test_id(cluster)

vkey_file = DATA_DIR / "golden_stake.vkey"

expected_hash = "a8b8c13cc0b863bc077e0bc6eafdc6f32f43a4513b70378b30ceb7b9"

if option == "vkey":
with open(vkey_file, encoding="utf-8") as infile:
# Ignore the first 4 chars, just an informative keyword
vkey = helpers.encode_bech32(
prefix="stake_vk", data=json.loads(infile.read().strip()).get("cborHex", "")[4:]
)

vkey_hash = cluster.g_stake_address.get_stake_vkey_hash(
stake_vkey=vkey if option == "vkey" else None,
stake_vkey_file=vkey_file if option == "vkey_file" else None,
)

assert vkey_hash == expected_hash, f"Unexpected vkey hash: {vkey_hash} != {expected_hash}"

@allure.link(helpers.get_vcs_link())
@pytest.mark.parametrize("option", ("vkey", "vkey_file"))
@hypothesis.given(vkey=st.text(alphabet=ADDR_ALPHABET, min_size=1))
@common.hypothesis_settings(max_examples=300)
def test_invalid_verification_key(self, cluster: clusterlib.ClusterLib, option: str, vkey: str):
"""Try to use `stake-address key-hash` with invalid verification key (property-based test).
Expect failure.
"""
temp_template = f"{common.get_test_id(cluster)}_{option}_{common.unique_time_str()}"

if option == "vkey_file":
vkey_file = f"{temp_template}.redeemer"
vkey_file_content = {
"type": "StakeVerificationKeyShelley_ed25519",
"description": "Stake Verification Key",
"cborHex": vkey,
}

with open(vkey_file, "w", encoding="utf-8") as outfile:
json.dump(vkey_file_content, outfile)

with pytest.raises(clusterlib.CLIError) as excinfo:
cluster.g_stake_address.get_stake_vkey_hash(
stake_vkey=vkey if option == "vkey" else None,
stake_vkey_file=vkey_file if option == "vkey_file" else None,
)

err_str = str(excinfo.value)

assert "Invalid key" in err_str, err_str


@common.SKIPIF_WRONG_ERA
class TestAdvancedQueries:
"""Basic sanity tests for advanced cardano-cli query commands.
Expand Down

0 comments on commit ff6e472

Please sign in to comment.