MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_hsm_wrong_passphrase_crash

Function test_hsm_wrong_passphrase_crash

tests/test_wallet.py:2601–2641  ·  view source on GitHub ↗

Test that hsmd handles wrong passphrase gracefully without crashing. This test reproduces a bug where hsmd would crash with "HSM sent unknown message type" when a wrong passphrase was provided. The issue was that hsmd_send_init_reply_failure was using write_all() instead of wire_sync_wr

(node_factory)

Source from the content-addressed store, hash-verified

2599
2600@unittest.skipIf(VALGRIND, "It does not play well with prompt and key derivation.")
2601def test_hsm_wrong_passphrase_crash(node_factory):
2602 """Test that hsmd handles wrong passphrase gracefully without crashing.
2603
2604 This test reproduces a bug where hsmd would crash with "HSM sent unknown message type"
2605 when a wrong passphrase was provided. The issue was that hsmd_send_init_reply_failure
2606 was using write_all() instead of wire_sync_write(), missing the length prefix.
2607 """
2608 l1 = node_factory.get_node(start=False, expect_fail=True)
2609 hsm_path = os.path.join(l1.daemon.lightning_dir, TEST_NETWORK, "hsm_secret")
2610 os.remove(hsm_path)
2611
2612 # Create hsm_secret with a passphrase
2613 passphrase = "correct_passphrase"
2614 mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
2615
2616 hsmtool = HsmTool(node_factory.directory, "generatehsm", hsm_path)
2617 master_fd, slave_fd = os.openpty()
2618 hsmtool.start(stdin=slave_fd)
2619 hsmtool.wait_for_log(r"Introduce your BIP39 word list")
2620 write_all(master_fd, f"{mnemonic}\n".encode("utf-8"))
2621 hsmtool.wait_for_log(r"Enter your passphrase:")
2622 write_all(master_fd, f"{passphrase}\n".encode("utf-8"))
2623 assert hsmtool.proc.wait(WAIT_TIMEOUT) == 0
2624 os.close(master_fd)
2625 os.close(slave_fd)
2626
2627 # Try to start with wrong passphrase
2628 l1.daemon.opts["hsm-passphrase"] = None
2629 master_fd2, slave_fd2 = os.openpty()
2630 l1.daemon.start(stdin=slave_fd2, wait_for_initialized=False, stderr_redir=True)
2631 l1.daemon.wait_for_log("Enter hsm_secret passphrase:")
2632 write_all(master_fd2, "wrong_passphrase\n".encode("utf-8"))
2633
2634 # Should fail gracefully with proper error message, not "unknown message type"
2635 l1.daemon.wait()
2636 assert l1.daemon.is_in_stderr("Failed to load hsm_secret: Wrong passphrase")
2637 assert not l1.daemon.is_in_stderr("HSM sent unknown message type")
2638 assert not l1.daemon.is_in_stderr("send_backtrace") # No backtrace for user error
2639
2640 os.close(master_fd2)
2641 os.close(slave_fd2)
2642
2643
2644def test_unspend_during_reorg(node_factory, bitcoind):

Callers

nothing calls this directly

Calls 11

HsmToolClass · 0.85
joinMethod · 0.80
wait_for_logMethod · 0.80
is_in_stderrMethod · 0.80
write_allFunction · 0.70
get_nodeMethod · 0.45
removeMethod · 0.45
startMethod · 0.45
encodeMethod · 0.45
waitMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected