Skip to content

Commit a12f927

Browse files
committed
feat(test): add mnemonic key derivation tests
Add parameterized tests for generating and deriving keys from mnemonic sentences. Covers multiple mnemonic sizes, output formats, key types, and derivation paths. Ensures generated keys exist and match expected properties.
1 parent aed834b commit a12f927

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Tests for deriving keys from a mnemonic sentence."""
2+
3+
import logging
4+
import pathlib as pl
5+
import typing as tp
6+
7+
import allure
8+
import pytest
9+
from cardano_clusterlib import clusterlib
10+
11+
from cardano_node_tests.tests import common
12+
from cardano_node_tests.utils import helpers
13+
14+
LOGGER = logging.getLogger(__name__)
15+
16+
17+
@common.SKIPIF_WRONG_ERA
18+
class TestMnemonic:
19+
"""Tests for mnemonic sentence."""
20+
21+
@allure.link(helpers.get_vcs_link())
22+
@pytest.mark.parametrize("size", (12, 15, 18, 21, 24))
23+
@pytest.mark.parametrize("out", ("file", "stdout"))
24+
@pytest.mark.parametrize(
25+
"key_type",
26+
# pyrefly: ignore # no-matching-overload
27+
iter(clusterlib.KeyType),
28+
# pyrefly: ignore # no-matching-overload
29+
ids=(k.value.replace("-", "_") for k in iter(clusterlib.KeyType)),
30+
)
31+
@pytest.mark.parametrize(
32+
"out_format",
33+
# pyrefly: ignore # no-matching-overload
34+
iter(clusterlib.OutputFormat),
35+
# pyrefly: ignore # no-matching-overload
36+
ids=(k.value.replace("-", "_") for k in iter(clusterlib.OutputFormat)),
37+
)
38+
@pytest.mark.parametrize("path_num", (0, 2**31 - 1))
39+
@pytest.mark.smoke
40+
def test_gen_and_deriv(
41+
self,
42+
cluster: clusterlib.ClusterLib,
43+
size: tp.Literal[12, 15, 18, 21, 24],
44+
out: str,
45+
key_type: clusterlib.KeyType,
46+
out_format: clusterlib.OutputFormat,
47+
path_num: int,
48+
):
49+
"""Test `generate-mnemonic` and `derive-from-mnemonic`."""
50+
temp_template = common.get_test_id(cluster)
51+
mnemonic_file = pl.Path(f"{temp_template}_mnemonic")
52+
53+
if out == "stdout":
54+
words = cluster.g_key.gen_mnemonic(size=size)
55+
mnemonic_file.write_text(" ".join(words))
56+
else:
57+
words = cluster.g_key.gen_mnemonic(size=size, out_file=mnemonic_file)
58+
assert len(words) == size
59+
60+
key_number = (
61+
path_num
62+
if (key_type in (clusterlib.KeyType.PAYMENT, clusterlib.KeyType.STAKE))
63+
else None
64+
)
65+
key_file = cluster.g_key.derive_from_mnemonic(
66+
key_name=f"{temp_template}_derived",
67+
key_type=key_type,
68+
mnemonic_file=mnemonic_file,
69+
account_number=path_num,
70+
key_number=key_number,
71+
out_format=out_format,
72+
)
73+
assert key_file.exists()

0 commit comments

Comments
 (0)