Skip to content

Commit c5b404e

Browse files
committed
Add functional tests for flexible whitebind/list
1 parent d541fa3 commit c5b404e

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

test/functional/p2p_permissions.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2015-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 p2p permission message.
6+
7+
Test that permissions are correctly calculated and applied
8+
"""
9+
10+
from test_framework.test_node import ErrorMatch
11+
from test_framework.test_framework import BitcoinTestFramework
12+
from test_framework.util import (
13+
assert_equal,
14+
connect_nodes,
15+
p2p_port,
16+
)
17+
18+
class P2PPermissionsTests(BitcoinTestFramework):
19+
def set_test_params(self):
20+
self.num_nodes = 2
21+
self.setup_clean_chain = True
22+
self.extra_args = [[],[]]
23+
24+
def run_test(self):
25+
self.checkpermission(
26+
# relay permission added
27+
["-whitelist=127.0.0.1", "-whitelistrelay"],
28+
["relay", "noban", "mempool"],
29+
True)
30+
31+
self.checkpermission(
32+
# forcerelay and relay permission added
33+
# Legacy parameter interaction which set whitelistrelay to true
34+
# if whitelistforcerelay is true
35+
["-whitelist=127.0.0.1", "-whitelistforcerelay"],
36+
["forcerelay", "relay", "noban", "mempool"],
37+
True)
38+
39+
# Let's make sure permissions are merged correctly
40+
# For this, we need to use whitebind instead of bind
41+
# by modifying the configuration file.
42+
ip_port = "127.0.0.1:{}".format(p2p_port(1))
43+
self.replaceinconfig(1, "bind=127.0.0.1", "whitebind=bloomfilter,forcerelay@" + ip_port)
44+
self.checkpermission(
45+
46+
# Check parameter interaction forcerelay should activate relay
47+
["noban", "bloomfilter", "forcerelay", "relay" ],
48+
False)
49+
self.replaceinconfig(1, "whitebind=bloomfilter,forcerelay@" + ip_port, "bind=127.0.0.1")
50+
51+
self.checkpermission(
52+
# legacy whitelistrelay should be ignored
53+
["-whitelist=noban,[email protected]", "-whitelistrelay"],
54+
["noban", "mempool"],
55+
False)
56+
57+
self.checkpermission(
58+
# legacy whitelistforcerelay should be ignored
59+
["-whitelist=noban,[email protected]", "-whitelistforcerelay"],
60+
["noban", "mempool"],
61+
False)
62+
63+
self.checkpermission(
64+
# missing mempool permission to be considered legacy whitelisted
65+
66+
["noban"],
67+
False)
68+
69+
self.checkpermission(
70+
# all permission added
71+
72+
["forcerelay", "noban", "mempool", "bloomfilter", "relay"],
73+
False)
74+
75+
self.stop_node(1)
76+
self.nodes[1].assert_start_raises_init_error(["[email protected]"], "Invalid P2P permission", match=ErrorMatch.PARTIAL_REGEX)
77+
self.nodes[1].assert_start_raises_init_error(["[email protected]:230"], "Invalid netmask specified in", match=ErrorMatch.PARTIAL_REGEX)
78+
self.nodes[1].assert_start_raises_init_error(["[email protected]/10"], "Cannot resolve -whitebind address", match=ErrorMatch.PARTIAL_REGEX)
79+
80+
def checkpermission(self, args, expectedPermissions, whitelisted):
81+
self.restart_node(1, args)
82+
connect_nodes(self.nodes[0], 1)
83+
peerinfo = self.nodes[1].getpeerinfo()[0]
84+
assert_equal(peerinfo['whitelisted'], whitelisted)
85+
assert_equal(len(expectedPermissions), len(peerinfo['permissions']))
86+
for p in expectedPermissions:
87+
if not p in peerinfo['permissions']:
88+
raise AssertionError("Expected permissions %r is not granted." % p)
89+
90+
def replaceinconfig(self, nodeid, old, new):
91+
with open(self.nodes[nodeid].bitcoinconf, encoding="utf8") as f:
92+
newText=f.read().replace(old, new)
93+
with open(self.nodes[nodeid].bitcoinconf, 'w', encoding="utf8") as f:
94+
f.write(newText)
95+
96+
if __name__ == '__main__':
97+
P2PPermissionsTests().main()

test/functional/test_framework/test_node.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __init__(self, i, datadir, *, chain, rpchost, timewait, bitcoind, bitcoin_cl
6868

6969
self.index = i
7070
self.datadir = datadir
71+
self.bitcoinconf = os.path.join(self.datadir, "bitcoin.conf")
7172
self.stdout_dir = os.path.join(self.datadir, "stdout")
7273
self.stderr_dir = os.path.join(self.datadir, "stderr")
7374
self.chain = chain

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
'rpc_scantxoutset.py',
201201
'feature_logging.py',
202202
'p2p_node_network_limited.py',
203+
'p2p_permissions.py',
203204
'feature_blocksdir.py',
204205
'feature_config_args.py',
205206
'rpc_help.py',

0 commit comments

Comments
 (0)