|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | 5 | """Test Migrating a wallet from legacy to descriptor.""" |
6 | 6 |
|
| 7 | +import os |
7 | 8 | import random |
8 | 9 | import shutil |
9 | 10 | import struct |
@@ -548,6 +549,39 @@ def test_default_wallet(self): |
548 | 549 |
|
549 | 550 | self.master_node.setmocktime(0) |
550 | 551 |
|
| 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 | + |
551 | 585 | def test_direct_file(self): |
552 | 586 | self.log.info("Test migration of a wallet that is not in a wallet directory") |
553 | 587 | wallet = self.create_legacy_wallet("plainfile") |
@@ -1372,6 +1406,7 @@ def run_test(self): |
1372 | 1406 | self.test_encrypted() |
1373 | 1407 | self.test_nonexistent() |
1374 | 1408 | self.test_unloaded_by_path() |
| 1409 | + self.test_default_wallet_failure() |
1375 | 1410 | self.test_default_wallet() |
1376 | 1411 | self.test_direct_file() |
1377 | 1412 | self.test_addressbook() |
|
0 commit comments