forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: only add all addresses if listening on any
If `-bind=` is provided then we would bind only to a particular address and should not add all the other addresses of the machine to the list of local addresses. Fixes bitcoin#20184 (case 4.)
- Loading branch information
Showing
4 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2020 The Bitcoin Core developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
""" | ||
Test that -discover does not add all intefaces' addresses if we listen just on some | ||
""" | ||
|
||
from test_framework.test_framework import BitcoinTestFramework, SkipTest | ||
from test_framework.util import assert_equal | ||
|
||
# We need to bind to a routable address for this test to exercise the relevant code | ||
# and also must have another routable address on another interface which must not | ||
# be named "lo" or "lo0". | ||
# To set a routable address on the machine use: | ||
# Linux: | ||
# ifconfig lo:0 1.1.1.1/32 up && ifconfig lo:1 2.2.2.2/32 up # to set up | ||
# ifconfig lo:0 down && ifconfig lo:1 down # to remove it, after the test | ||
# FreeBSD: | ||
# ifconfig em0 1.1.1.1/32 alias && ifconfig wlan0 2.2.2.2/32 alias # to set up | ||
# ifconfig em0 1.1.1.1 -alias && ifconfig wlan0 2.2.2.2 -alias # to remove it, after the test | ||
ADDR1 = '1.1.1.1' | ||
ADDR2 = '2.2.2.2' | ||
|
||
BIND_PORT = 20001; | ||
|
||
class BindPortDiscoverTest(BitcoinTestFramework): | ||
def set_test_params(self): | ||
# Avoid any -bind= on the command line. Force the framework to avoid adding -bind=127.0.0.1. | ||
self.setup_clean_chain = True | ||
self.bind_to_localhost_only = False | ||
self.num_nodes = 2 | ||
|
||
def add_options(self, parser): | ||
parser.add_argument( | ||
"--ihave1111and2222", action='store_true', dest="ihave1111and2222", | ||
help="Run the test, assuming {} and {} are configured on the machine".format(ADDR1, ADDR2), | ||
default=False) | ||
|
||
def setup_nodes(self): | ||
if not self.options.ihave1111and2222: | ||
raise SkipTest( | ||
"To run this test make sure that {} and {} (a routable addresses) are assigned " | ||
"to the interfaces on this machine and rerun with --ihave1111and2222".format(ADDR)) | ||
self.add_nodes(self.num_nodes, extra_args=[ | ||
['-discover', '-port={}'.format(BIND_PORT)], # bind on any | ||
['-discover', '-bind={}:{}'.format(ADDR1, BIND_PORT)], | ||
]) | ||
self.start_nodes() | ||
|
||
def run_test(self): | ||
# Check that if no -bind= is given, then all addresses are added to localaddresses. | ||
found_addr1 = False | ||
found_addr2 = False | ||
for local in self.nodes[0].getnetworkinfo()['localaddresses']: | ||
if local['address'] == ADDR1: | ||
found_addr1 = True | ||
assert_equal(local['port'], BIND_PORT) | ||
if local['address'] == ADDR2: | ||
found_addr2 = True | ||
assert_equal(local['port'], BIND_PORT) | ||
assert(found_addr1) | ||
assert(found_addr2) | ||
|
||
# Check that if -bind= is given, just that address is added to localaddresses. | ||
found_addr1 = False | ||
for local in self.nodes[1].getnetworkinfo()['localaddresses']: | ||
if local['address'] == ADDR1: | ||
found_addr1 = True | ||
assert_equal(local['port'], BIND_PORT) | ||
assert(local['address'] != ADDR2) | ||
assert(found_addr1) | ||
|
||
if __name__ == '__main__': | ||
BindPortDiscoverTest().main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters