|
| 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() |
0 commit comments