Skip to content

Commit 5812273

Browse files
jnewberyMarcoFalke
authored andcommitted
[Tests] Require exact match in assert_start_raises_init_eror()
1 parent 0ec08a6 commit 5812273

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

test/functional/feature_config_args.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""Test various command line arguments and configuration file parameters."""
66

77
import os
8+
import re
89

910
from test_framework.test_framework import BitcoinTestFramework
1011
from test_framework.util import get_datadir_path
@@ -25,13 +26,13 @@ def run_test(self):
2526

2627
# Check that using -datadir argument on non-existent directory fails
2728
self.nodes[0].datadir = new_data_dir
28-
self.nodes[0].assert_start_raises_init_error(['-datadir='+new_data_dir], 'Error: Specified data directory "' + new_data_dir + '" does not exist.')
29+
self.nodes[0].assert_start_raises_init_error(['-datadir=' + new_data_dir], 'Error: Specified data directory "' + re.escape(new_data_dir) + '" does not exist.')
2930

3031
# Check that using non-existent datadir in conf file fails
3132
conf_file = os.path.join(default_data_dir, "bitcoin.conf")
3233
with open(conf_file, 'a', encoding='utf8') as f:
3334
f.write("datadir=" + new_data_dir + "\n")
34-
self.nodes[0].assert_start_raises_init_error(['-conf='+conf_file], 'Error reading configuration file: specified data directory "' + new_data_dir + '" does not exist.')
35+
self.nodes[0].assert_start_raises_init_error(['-conf=' + conf_file], 'Error reading configuration file: specified data directory "' + re.escape(new_data_dir) + '" does not exist.')
3536

3637
# Create the directory and ensure the config file now works
3738
os.mkdir(new_data_dir)

test/functional/feature_logging.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def run_test(self):
3030
invdir = os.path.join(self.nodes[0].datadir, "regtest", "foo")
3131
invalidname = os.path.join("foo", "foo.log")
3232
self.stop_node(0)
33-
self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % (invalidname)],
34-
"Error: Could not open debug log file")
33+
exp_stderr = "Error: Could not open debug log file \S+$"
34+
self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % (invalidname)], exp_stderr)
3535
assert not os.path.isfile(os.path.join(invdir, "foo.log"))
3636

3737
# check that invalid log (relative) works after path exists
@@ -44,8 +44,7 @@ def run_test(self):
4444
self.stop_node(0)
4545
invdir = os.path.join(self.options.tmpdir, "foo")
4646
invalidname = os.path.join(invdir, "foo.log")
47-
self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % invalidname],
48-
"Error: Could not open debug log file")
47+
self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % invalidname], exp_stderr)
4948
assert not os.path.isfile(os.path.join(invdir, "foo.log"))
5049

5150
# check that invalid log (absolute) works after path exists

test/functional/feature_uacomment.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the -uacomment option."""
66

7+
import re
8+
79
from test_framework.test_framework import BitcoinTestFramework
810
from test_framework.util import assert_equal
911

12+
1013
class UacommentTest(BitcoinTestFramework):
1114
def set_test_params(self):
1215
self.num_nodes = 1
@@ -23,13 +26,14 @@ def run_test(self):
2326

2427
self.log.info("test -uacomment max length")
2528
self.stop_node(0)
26-
expected = "exceeds maximum length (256). Reduce the number or size of uacomments."
29+
expected = "Error: Total length of network version string \([0-9]+\) exceeds maximum length \(256\). Reduce the number or size of uacomments."
2730
self.nodes[0].assert_start_raises_init_error(["-uacomment=" + 'a' * 256], expected)
2831

2932
self.log.info("test -uacomment unsafe characters")
3033
for unsafe_char in ['/', ':', '(', ')']:
31-
expected = "User Agent comment (" + unsafe_char + ") contains unsafe characters"
34+
expected = "Error: User Agent comment \(" + re.escape(unsafe_char) + "\) contains unsafe characters."
3235
self.nodes[0].assert_start_raises_init_error(["-uacomment=" + unsafe_char], expected)
3336

37+
3438
if __name__ == '__main__':
3539
UacommentTest().main()

test/functional/test_framework/test_node.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT):
169169
def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, *args, **kwargs):
170170
"""Attempt to start the node and expect it to raise an error.
171171
172+
extra_args: extra arguments to pass through to bitcoind
173+
expected_msg: regex that stderr should match when bitcoind fails
174+
172175
Will throw if bitcoind starts without an error.
173-
Will throw if an expected_msg is provided and it does not appear in bitcoind's stdout."""
176+
Will throw if an expected_msg is provided and it does not match bitcoind's stdout."""
174177
with tempfile.SpooledTemporaryFile(max_size=2**16) as log_stderr:
175178
try:
176179
self.start(extra_args, stderr=log_stderr, *args, **kwargs)
@@ -181,11 +184,12 @@ def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, *ar
181184
assert 'bitcoind exited' in str(e) # node must have shutdown
182185
self.running = False
183186
self.process = None
187+
# Check stderr for expected message
184188
if expected_msg is not None:
185189
log_stderr.seek(0)
186190
stderr = log_stderr.read().decode('utf-8')
187-
if expected_msg not in stderr:
188-
raise AssertionError("Expected error \"" + expected_msg + "\" not found in:\n" + stderr)
191+
if re.fullmatch(expected_msg + '\n', stderr) is None:
192+
raise AssertionError('Expected message "{}" does not match stderr:\n"{}"'.format(expected_msg, stderr))
189193
else:
190194
if expected_msg is None:
191195
assert_msg = "bitcoind should have exited with an error"

test/functional/wallet_hd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def run_test (self):
2323

2424
# Make sure can't switch off usehd after wallet creation
2525
self.stop_node(1)
26-
self.nodes[1].assert_start_raises_init_error(['-usehd=0'], 'already existing HD wallet')
26+
self.nodes[1].assert_start_raises_init_error(['-usehd=0'], "Error: Error loading : You can't disable HD on an already existing HD wallet")
2727
self.start_node(1)
2828
connect_nodes_bi(self.nodes, 0, 1)
2929

test/functional/wallet_multiwallet.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,30 @@ def run_test(self):
6060
assert_equal(os.path.isfile(wallet_dir(wallet_name)), True)
6161

6262
# should not initialize if wallet path can't be created
63-
self.assert_start_raises_init_error(0, ['-wallet=wallet.dat/bad'], 'Not a directory')
63+
exp_stderr = "\n\n\*+\n" + \
64+
"EXCEPTION: .*\n" + \
65+
"boost::filesystem::create_directory: Not a directory:.*\n" + \
66+
"bitcoin in .*\n"
67+
self.nodes[0].assert_start_raises_init_error(['-wallet=wallet.dat/bad'], exp_stderr)
6468

6569
self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist')
6670
self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', cwd=data_dir())
6771
self.nodes[0].assert_start_raises_init_error(['-walletdir=debug.log'], 'Error: Specified -walletdir "debug.log" is not a directory', cwd=data_dir())
6872

6973
# should not initialize if there are duplicate wallets
70-
self.nodes[0].assert_start_raises_init_error(['-wallet=w1', '-wallet=w1'], 'Error loading wallet w1. Duplicate -wallet filename specified.')
74+
self.nodes[0].assert_start_raises_init_error(['-wallet=w1', '-wallet=w1'], 'Error: Error loading wallet w1. Duplicate -wallet filename specified.')
7175

7276
# should not initialize if one wallet is a copy of another
7377
shutil.copyfile(wallet_dir('w8'), wallet_dir('w8_copy'))
74-
self.nodes[0].assert_start_raises_init_error(['-wallet=w8', '-wallet=w8_copy'], 'duplicates fileid')
78+
exp_stderr = "\n\n\*+\n" + \
79+
"EXCEPTION: .*\n" + \
80+
"CDB: Can't open database w8_copy \(duplicates fileid \w+ from w8\)\s*\n" + \
81+
"bitcoin in .*\n"
82+
self.nodes[0].assert_start_raises_init_error(['-wallet=w8', '-wallet=w8_copy'], exp_stderr)
7583

7684
# should not initialize if wallet file is a symlink
7785
os.symlink('w8', wallet_dir('w8_symlink'))
78-
self.nodes[0].assert_start_raises_init_error(['-wallet=w8_symlink'], 'Invalid -wallet path')
86+
self.nodes[0].assert_start_raises_init_error(['-wallet=w8_symlink'], 'Error: Invalid -wallet path \'w8_symlink\'\. .*')
7987

8088
# should not initialize if the specified walletdir does not exist
8189
self.nodes[0].assert_start_raises_init_error(['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist')
@@ -102,8 +110,9 @@ def run_test(self):
102110

103111
competing_wallet_dir = os.path.join(self.options.tmpdir, 'competing_walletdir')
104112
os.mkdir(competing_wallet_dir)
105-
self.restart_node(0, ['-walletdir='+competing_wallet_dir])
106-
self.nodes[1].assert_start_raises_init_error(['-walletdir='+competing_wallet_dir], 'Error initializing wallet database environment')
113+
self.restart_node(0, ['-walletdir=' + competing_wallet_dir])
114+
exp_stderr = "Error: Error initializing wallet database environment \"\S+competing_walletdir\"!"
115+
self.nodes[1].assert_start_raises_init_error(['-walletdir=' + competing_wallet_dir], exp_stderr)
107116

108117
self.restart_node(0, extra_args)
109118

0 commit comments

Comments
 (0)