|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2018 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""Test bitcoin-wallet.""" |
| 6 | +import subprocess |
| 7 | +import textwrap |
| 8 | + |
| 9 | +from test_framework.test_framework import BitcoinTestFramework |
| 10 | +from test_framework.util import assert_equal |
| 11 | + |
| 12 | +class ToolWalletTest(BitcoinTestFramework): |
| 13 | + def set_test_params(self): |
| 14 | + self.num_nodes = 1 |
| 15 | + self.setup_clean_chain = True |
| 16 | + |
| 17 | + def skip_test_if_missing_module(self): |
| 18 | + self.skip_if_no_wallet() |
| 19 | + |
| 20 | + def bitcoin_wallet_process(self, *args): |
| 21 | + binary = self.config["environment"]["BUILDDIR"] + '/src/bitcoin-wallet' + self.config["environment"]["EXEEXT"] |
| 22 | + args = ['-datadir={}'.format(self.nodes[0].datadir), '-regtest'] + list(args) |
| 23 | + return subprocess.Popen([binary] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) |
| 24 | + |
| 25 | + def assert_raises_tool_error(self, error, *args): |
| 26 | + p = self.bitcoin_wallet_process(*args) |
| 27 | + stdout, stderr = p.communicate() |
| 28 | + assert_equal(p.poll(), 1) |
| 29 | + assert_equal(stdout, '') |
| 30 | + assert_equal(stderr.strip(), error) |
| 31 | + |
| 32 | + def assert_tool_output(self, output, *args): |
| 33 | + p = self.bitcoin_wallet_process(*args) |
| 34 | + stdout, stderr = p.communicate() |
| 35 | + assert_equal(p.poll(), 0) |
| 36 | + assert_equal(stderr, '') |
| 37 | + assert_equal(stdout, output) |
| 38 | + |
| 39 | + def run_test(self): |
| 40 | + |
| 41 | + self.assert_raises_tool_error('Invalid command: foo', 'foo') |
| 42 | + # `bitcoin-wallet help` is an error. Use `bitcoin-wallet -help` |
| 43 | + self.assert_raises_tool_error('Invalid command: help', 'help') |
| 44 | + self.assert_raises_tool_error('Error: two methods provided (info and create). Only one method should be provided.', 'info', 'create') |
| 45 | + self.assert_raises_tool_error('Error parsing command line arguments: Invalid parameter -foo', '-foo') |
| 46 | + self.assert_raises_tool_error('Error loading wallet.dat. Is wallet being used by other process?', '-wallet=wallet.dat', 'info') |
| 47 | + self.assert_raises_tool_error('Error: no wallet file at nonexistent.dat', '-wallet=nonexistent.dat', 'info') |
| 48 | + |
| 49 | + # stop the node to close the wallet to call info command |
| 50 | + self.stop_node(0) |
| 51 | + |
| 52 | + out = textwrap.dedent('''\ |
| 53 | + Wallet info |
| 54 | + =========== |
| 55 | + Encrypted: no |
| 56 | + HD (hd seed available): yes |
| 57 | + Keypool Size: 2 |
| 58 | + Transactions: 0 |
| 59 | + Address Book: 3 |
| 60 | + ''') |
| 61 | + self.assert_tool_output(out, '-wallet=wallet.dat', 'info') |
| 62 | + |
| 63 | + # mutate the wallet to check the info command output changes accordingly |
| 64 | + self.start_node(0) |
| 65 | + self.nodes[0].generate(1) |
| 66 | + self.stop_node(0) |
| 67 | + |
| 68 | + out = textwrap.dedent('''\ |
| 69 | + Wallet info |
| 70 | + =========== |
| 71 | + Encrypted: no |
| 72 | + HD (hd seed available): yes |
| 73 | + Keypool Size: 2 |
| 74 | + Transactions: 1 |
| 75 | + Address Book: 3 |
| 76 | + ''') |
| 77 | + self.assert_tool_output(out, '-wallet=wallet.dat', 'info') |
| 78 | + |
| 79 | + out = textwrap.dedent('''\ |
| 80 | + Topping up keypool... |
| 81 | + Wallet info |
| 82 | + =========== |
| 83 | + Encrypted: no |
| 84 | + HD (hd seed available): yes |
| 85 | + Keypool Size: 2000 |
| 86 | + Transactions: 0 |
| 87 | + Address Book: 0 |
| 88 | + ''') |
| 89 | + self.assert_tool_output(out, '-wallet=foo', 'create') |
| 90 | + |
| 91 | + self.start_node(0, ['-wallet=foo']) |
| 92 | + out = self.nodes[0].getwalletinfo() |
| 93 | + self.stop_node(0) |
| 94 | + |
| 95 | + assert_equal(0, out['txcount']) |
| 96 | + assert_equal(1000, out['keypoolsize']) |
| 97 | + assert_equal(1000, out['keypoolsize_hd_internal']) |
| 98 | + assert_equal(True, 'hdseedid' in out) |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + ToolWalletTest().main() |
0 commit comments