mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-15 00:14:08 +02:00
test: enable feature_bind_extra on macOS and BSD
`feature_bind_extra` checks `-bind` and `-whitebind` by comparing the node's listening sockets with the expected addresses. Add `get_bind_addrs` support for macOS, FreeBSD, NetBSD, and OpenBSD using `lsof`, and switch the test to the POSIX platform guard so it runs there too. On FreeBSD, pass `-Di` to avoid device-cache warnings on stderr that the functional test runner treats as failures. Co-authored-by: willcl-ark <will8clark@gmail.com> Co-authored-by: fanquake <fanquake@gmail.com>
This commit is contained in:
@@ -32,8 +32,7 @@ class BindExtraTest(BitcoinTestFramework):
|
||||
self.num_nodes = 3
|
||||
|
||||
def skip_test_if_missing_module(self):
|
||||
# Due to OS-specific network stats queries, we only run on Linux.
|
||||
self.skip_if_platform_not_linux()
|
||||
self.skip_if_platform_not_posix()
|
||||
|
||||
def setup_network(self):
|
||||
loopback_ipv4 = addr_to_hex("127.0.0.1")
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# Copyright (c) 2014-present The Bitcoin Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
"""Linux network utilities.
|
||||
"""Linux, macOS, and BSD network utilities.
|
||||
|
||||
Roughly based on https://web.archive.org/web/20190424172231/http://voorloopnul.com/blog/a-python-netstat-in-less-than-100-lines-of-code/ by Ricardo Pascal
|
||||
"""
|
||||
@@ -88,12 +88,31 @@ def get_bind_addrs(pid):
|
||||
'''
|
||||
Get bind addresses as (host,port) tuples for process pid.
|
||||
'''
|
||||
inodes = get_socket_inodes(pid)
|
||||
bind_addrs = []
|
||||
for conn in netstat('tcp') + netstat('tcp6'):
|
||||
if conn[3] == STATE_LISTEN and conn[4] in inodes:
|
||||
bind_addrs.append(conn[1])
|
||||
return bind_addrs
|
||||
if sys.platform == 'linux':
|
||||
inodes = get_socket_inodes(pid)
|
||||
bind_addrs = []
|
||||
for conn in netstat('tcp') + netstat('tcp6'):
|
||||
if conn[3] == STATE_LISTEN and conn[4] in inodes:
|
||||
bind_addrs.append(conn[1])
|
||||
return bind_addrs
|
||||
elif sys.platform.startswith(("darwin", "freebsd", "netbsd", "openbsd")):
|
||||
import re
|
||||
import subprocess
|
||||
output = subprocess.check_output(["lsof",
|
||||
*(["-Di"] if sys.platform.startswith("freebsd") else []), # Ignore device cache to avoid stderr warnings.
|
||||
"-nP", # Keep hosts and ports numeric.
|
||||
"-a", # Require all filters to match.
|
||||
"-p", str(pid), # Limit results to the target pid.
|
||||
"-iTCP", # Only inspect TCP sockets.
|
||||
"-sTCP:LISTEN", # Only keep listening sockets.
|
||||
"-Ftn", # Emit machine-readable type and name fields.
|
||||
], text=True)
|
||||
return [
|
||||
(addr_to_hex(("::" if sock_type == "IPv6" else "0.0.0.0") if host == "*" else host.strip("[]")), int(port))
|
||||
for sock_type, host, port in re.findall(r"t(IPv[46])\nn(\*|\[.+?]|[^:]+):(\d+)", output)
|
||||
]
|
||||
else:
|
||||
raise NotImplementedError(f"get_bind_addrs is not supported on {sys.platform}")
|
||||
|
||||
# from: https://code.activestate.com/recipes/439093/
|
||||
def all_interfaces():
|
||||
|
||||
Reference in New Issue
Block a user