Skip to content

Commit 3c3e31c

Browse files
promagjnewbery
authored andcommitted
[tests] Add wallet-tool test
Original tests by João Barbosa <[email protected]> Additional contribution by John Newbery <[email protected]>
1 parent 49d2374 commit 3c3e31c

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

test/functional/test_framework/test_framework.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def main(self):
139139

140140
config = configparser.ConfigParser()
141141
config.read_file(open(self.options.configfile))
142+
self.config = config
142143
self.options.bitcoind = os.getenv("BITCOIND", default=config["environment"]["BUILDDIR"] + '/src/bitcoind' + config["environment"]["EXEEXT"])
143144
self.options.bitcoincli = os.getenv("BITCOINCLI", default=config["environment"]["BUILDDIR"] + '/src/bitcoin-cli' + config["environment"]["EXEEXT"])
144145

test/functional/test_runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
'interface_bitcoin_cli.py',
109109
'mempool_resurrect.py',
110110
'wallet_txn_doublespend.py --mineblock',
111+
'tool_wallet.py',
111112
'wallet_txn_clone.py',
112113
'wallet_txn_clone.py --segwit',
113114
'rpc_getchaintips.py',
@@ -562,7 +563,7 @@ def was_successful(self):
562563
def check_script_prefixes():
563564
"""Check that test scripts start with one of the allowed name prefixes."""
564565

565-
good_prefixes_re = re.compile("(example|feature|interface|mempool|mining|p2p|rpc|wallet)_")
566+
good_prefixes_re = re.compile("(example|feature|interface|mempool|mining|p2p|rpc|wallet|tool)_")
566567
bad_script_names = [script for script in ALL_SCRIPTS if good_prefixes_re.match(script) is None]
567568

568569
if bad_script_names:

test/functional/tool_wallet.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)