Skip to content

Commit 332e2ee

Browse files
ekzyiscdecker
authored andcommitted
tests: add regression test for hsmtool commands with passphrase
1 parent 6ab5680 commit 332e2ee

1 file changed

Lines changed: 47 additions & 20 deletions

File tree

tests/test_wallet.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,38 +1656,65 @@ def test_hsmtool_generatehsm_file_exists_error(node_factory):
16561656
hsmtool.is_in_log(r"hsm_secret file.*already exists")
16571657

16581658

1659-
def test_hsmtool_all_commands_work_with_mnemonic_formats(node_factory):
1659+
@pytest.mark.parametrize("test_case", [
1660+
pytest.param({
1661+
"mnemonic": "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
1662+
"passphrase": "",
1663+
"test_commands": [
1664+
("getnodeid", "03653e90c1ce4660fd8505dd6d643356e93cfe202af109d382787639dd5890e87d"),
1665+
("getsecret", "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"),
1666+
("makerune", "6VkrWMI2hm2a2UTkg-EyUrrBJN0RcuPB80I1pCVkTD89MA=="),
1667+
("dumponchaindescriptors",
1668+
"wpkh(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)#hjszq0wk\n"
1669+
"sh(wpkh(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*))#u0t3u3xz\n"
1670+
"tr(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)#8e7pq23w"),
1671+
]
1672+
}, id="no_passphrase"),
1673+
pytest.param({
1674+
"mnemonic": "ritual idle hat sunny universe pluck key alpha wing cake have wedding",
1675+
"passphrase": "test_passphrase",
1676+
"test_commands": [
1677+
("getnodeid", "039020371fb803cd4ce1e9a909b502d7b0a9e0f10cccc35c3e9be959c52d3ba6bd"),
1678+
("getsecret", "ritual idle hat sunny universe pluck key alpha wing cake have wedding"),
1679+
("makerune", "2JtNfGMM_U0b3_R2DU9L5K6dxxRjHPgDGf7daoVwKEc9MA=="),
1680+
("dumponchaindescriptors",
1681+
"wpkh(xpub6DQ23AMoZvy6gVSMi5EvBawiyXMVWW9txHh1msjH1b8W1HsC3VbcxSz71qjMeDqc5EFFADz7DuiSHsZtLs9hxYaH1WrRoWECiD2YbpyiFbd/0/*)#9n6j954n\n"
1682+
"sh(wpkh(xpub6DQ23AMoZvy6gVSMi5EvBawiyXMVWW9txHh1msjH1b8W1HsC3VbcxSz71qjMeDqc5EFFADz7DuiSHsZtLs9hxYaH1WrRoWECiD2YbpyiFbd/0/*))#ynr7hayd\n"
1683+
"tr(xpub6DQ23AMoZvy6gVSMi5EvBawiyXMVWW9txHh1msjH1b8W1HsC3VbcxSz71qjMeDqc5EFFADz7DuiSHsZtLs9hxYaH1WrRoWECiD2YbpyiFbd/0/*)#3njcgkrj"),
1684+
]
1685+
}, id="with_passphrase")
1686+
])
1687+
def test_hsmtool_all_commands_work_with_mnemonic_formats(node_factory, test_case):
16601688
"""Test that all hsmtool commands work with mnemonic formats"""
16611689
l1 = node_factory.get_node(start=False)
16621690
hsm_path = os.path.join(l1.daemon.lightning_dir, TEST_NETWORK, "hsm_secret")
16631691
os.remove(hsm_path)
16641692

1665-
# Create a mnemonic-based hsm_secret (no passphrase for simplicity)
1693+
mnemonic = test_case['mnemonic']
1694+
passphrase = test_case['passphrase']
1695+
test_commands = test_case['test_commands']
1696+
1697+
# Create a mnemonic-based hsm_secret
16661698
hsmtool = HsmTool(node_factory.directory, "generatehsm", hsm_path)
16671699
master_fd, slave_fd = os.openpty()
16681700
hsmtool.start(stdin=slave_fd)
16691701
hsmtool.wait_for_log(r"Introduce your BIP39 word list")
1670-
write_all(master_fd, "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about\n".encode("utf-8"))
1702+
write_all(master_fd, f"{mnemonic}\n".encode("utf-8"))
16711703
hsmtool.wait_for_log(r"Enter your passphrase:")
1672-
write_all(master_fd, "\n".encode("utf-8"))
1704+
write_all(master_fd, f"{passphrase}\n".encode("utf-8"))
16731705
assert hsmtool.proc.wait(WAIT_TIMEOUT) == 0
16741706

1675-
# Test various commands work with mnemonic format
1676-
test_commands = [
1677-
(["getnodeid", hsm_path], "03653e90c1ce4660fd8505dd6d643356e93cfe202af109d382787639dd5890e87d"),
1678-
(["getsecret", hsm_path], "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"),
1679-
(["makerune", hsm_path], "6VkrWMI2hm2a2UTkg-EyUrrBJN0RcuPB80I1pCVkTD89MA=="),
1680-
(["dumponchaindescriptors", hsm_path],
1681-
"wpkh(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)#hjszq0wk\n"
1682-
"sh(wpkh(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*))#u0t3u3xz\n"
1683-
"tr(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)#8e7pq23w"),
1684-
]
1685-
1686-
for cmd_args, expected_output in test_commands:
1687-
cmd_line = ["tools/lightning-hsmtool"] + cmd_args
1688-
out = subprocess.check_output(cmd_line).decode("utf8")
1689-
actual_output = out.strip()
1690-
assert actual_output == expected_output, f"Command {cmd_args[0]} output mismatch"
1707+
for cmd, expected_output in test_commands:
1708+
result = subprocess.run(
1709+
["tools/lightning-hsmtool", cmd, hsm_path],
1710+
input=f"{passphrase}\n" if passphrase else None,
1711+
capture_output=True, text=True
1712+
)
1713+
actual_output = result.stdout.strip()
1714+
if passphrase:
1715+
# remove 'Enter hsm_secret password:'
1716+
actual_output = "\n".join(actual_output.split("\n")[1:])
1717+
assert actual_output == expected_output, f"Command {cmd} output mismatch"
16911718

16921719

16931720
def test_hsmtool_deterministic_node_ids(node_factory):

0 commit comments

Comments
 (0)