Skip to content

Commit c95b112

Browse files
furszyachow101
authored andcommitted
test: add coverage for unnamed wallet migration failure
Verifies that a failed migration of the unnamed (default) wallet does not erase the main /wallets/ directory, and also that the backup file exists. Github-Pull: #34156 Rebased-From: 36093bd
1 parent a074d36 commit c95b112

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/functional/wallet_migration.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test Migrating a wallet from legacy to descriptor."""
66

7+
import os
78
import random
89
import shutil
910
import struct
@@ -548,6 +549,39 @@ def test_default_wallet(self):
548549

549550
self.master_node.setmocktime(0)
550551

552+
def test_default_wallet_failure(self):
553+
self.log.info("Test failure during unnamed (default) wallet migration")
554+
master_wallet = self.master_node.get_wallet_rpc(self.default_wallet_name)
555+
wallet = self.create_legacy_wallet("", blank=True)
556+
wallet.importaddress(master_wallet.getnewaddress(address_type="legacy"))
557+
558+
# Create wallet directory with the watch-only name and a wallet file.
559+
# Because the wallet dir exists, this will cause migration to fail.
560+
watch_only_dir = self.master_node.wallets_path / "_watchonly"
561+
os.mkdir(watch_only_dir)
562+
shutil.copyfile(self.old_node.wallets_path / "wallet.dat", watch_only_dir / "wallet.dat")
563+
564+
mocked_time = int(time.time())
565+
self.master_node.setmocktime(mocked_time)
566+
assert_raises_rpc_error(-1, "filesystem error: cannot copy file: File exists", self.migrate_and_get_rpc, "")
567+
self.master_node.setmocktime(0)
568+
569+
# Verify the /wallets/ path exists
570+
assert self.master_node.wallets_path.exists()
571+
# Check backup file exists. Because the wallet has no name, the backup is prefixed with 'default_wallet'
572+
backup_path = self.master_node.wallets_path / f"default_wallet_{mocked_time}.legacy.bak"
573+
assert backup_path.exists()
574+
# Migration will fail to restore the original unnamed wallet
575+
# Verify that there is a wallet file
576+
assert (self.master_node.wallets_path / "wallet.dat").exists()
577+
# And verify it is now a SQLite wallet
578+
with open(self.master_node.wallets_path / "wallet.dat", 'rb') as f:
579+
file_magic = f.read(16)
580+
assert_equal(file_magic, b'SQLite format 3\x00')
581+
582+
# Test cleanup: clear default wallet for next test
583+
os.remove(self.old_node.wallets_path / "wallet.dat")
584+
551585
def test_direct_file(self):
552586
self.log.info("Test migration of a wallet that is not in a wallet directory")
553587
wallet = self.create_legacy_wallet("plainfile")
@@ -1372,6 +1406,7 @@ def run_test(self):
13721406
self.test_encrypted()
13731407
self.test_nonexistent()
13741408
self.test_unloaded_by_path()
1409+
self.test_default_wallet_failure()
13751410
self.test_default_wallet()
13761411
self.test_direct_file()
13771412
self.test_addressbook()

0 commit comments

Comments
 (0)