mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-13 15:33:51 +02:00
test: enable rpc_bind on macOS and BSD
`rpc_bind` uses `all_interfaces` to find a non-loopback IPv4 address and `get_bind_addrs` to verify the node's listening sockets. Add `all_interfaces` support for macOS, FreeBSD, NetBSD, and OpenBSD using `ifconfig -au`, switch the test to the POSIX platform guard so it runs there too, and fail early if no IPv4 interfaces are returned. Co-authored-by: Sjors Provoost <sjors@sprovoost.nl>
This commit is contained in:
@@ -17,8 +17,7 @@ class RPCBindTest(BitcoinTestFramework):
|
||||
self.supports_cli = False
|
||||
|
||||
def skip_test_if_missing_module(self):
|
||||
# due to OS-specific network stats queries, this test works only on Linux
|
||||
self.skip_if_platform_not_linux()
|
||||
self.skip_if_platform_not_posix()
|
||||
|
||||
def setup_network(self):
|
||||
self.add_nodes(self.num_nodes, None)
|
||||
@@ -105,8 +104,11 @@ class RPCBindTest(BitcoinTestFramework):
|
||||
raise SkipTest("This test requires ipv6 support.")
|
||||
|
||||
self.log.info("Check for non-loopback interface")
|
||||
interfaces = all_interfaces()
|
||||
if not interfaces:
|
||||
raise AssertionError("all_interfaces() returned no IPv4 interfaces")
|
||||
self.non_loopback_ip = None
|
||||
for name,ip in all_interfaces():
|
||||
for name,ip in interfaces:
|
||||
if ip != '127.0.0.1':
|
||||
self.non_loopback_ip = ip
|
||||
break
|
||||
|
||||
@@ -114,33 +114,44 @@ def get_bind_addrs(pid):
|
||||
else:
|
||||
raise NotImplementedError(f"get_bind_addrs is not supported on {sys.platform}")
|
||||
|
||||
# from: https://code.activestate.com/recipes/439093/
|
||||
def all_interfaces():
|
||||
'''
|
||||
Return all interfaces that are up
|
||||
Return all IPv4 interfaces that are up.
|
||||
'''
|
||||
import fcntl # Linux only, so only import when required
|
||||
if sys.platform == 'linux':
|
||||
import fcntl # Linux only, so only import when required
|
||||
|
||||
is_64bits = sys.maxsize > 2**32
|
||||
struct_size = 40 if is_64bits else 32
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
max_possible = 8 # initial value
|
||||
while True:
|
||||
bytes = max_possible * struct_size
|
||||
names = array.array('B', b'\0' * bytes)
|
||||
outbytes = struct.unpack('iL', fcntl.ioctl(
|
||||
s.fileno(),
|
||||
0x8912, # SIOCGIFCONF
|
||||
struct.pack('iL', bytes, names.buffer_info()[0])
|
||||
))[0]
|
||||
if outbytes == bytes:
|
||||
max_possible *= 2
|
||||
else:
|
||||
break
|
||||
namestr = names.tobytes()
|
||||
return [(namestr[i:i+16].split(b'\0', 1)[0],
|
||||
socket.inet_ntoa(namestr[i+20:i+24]))
|
||||
for i in range(0, outbytes, struct_size)]
|
||||
is_64bits = sys.maxsize > 2**32
|
||||
struct_size = 40 if is_64bits else 32
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
max_possible = 8 # initial value
|
||||
while True:
|
||||
bytes = max_possible * struct_size
|
||||
names = array.array('B', b'\0' * bytes)
|
||||
outbytes = struct.unpack('iL', fcntl.ioctl(
|
||||
s.fileno(),
|
||||
0x8912, # SIOCGIFCONF
|
||||
struct.pack('iL', bytes, names.buffer_info()[0])
|
||||
))[0]
|
||||
if outbytes == bytes:
|
||||
max_possible *= 2
|
||||
else:
|
||||
break
|
||||
namestr = names.tobytes()
|
||||
return [(namestr[i:i+16].split(b'\0', 1)[0],
|
||||
socket.inet_ntoa(namestr[i+20:i+24]))
|
||||
for i in range(0, outbytes, struct_size)]
|
||||
elif sys.platform.startswith(("darwin", "freebsd", "netbsd", "openbsd")):
|
||||
import re
|
||||
import subprocess
|
||||
output = subprocess.check_output(["ifconfig", "-au"], text=True)
|
||||
return [
|
||||
(m["iface"].encode(), ip)
|
||||
for m in re.finditer(r"(?m)^(?P<iface>\S+):(?P<block>[^\n]*(?:\n[ \t]+[^\n]*)*)", output)
|
||||
for ip in re.findall(r"inet (\S+)", m["block"])
|
||||
]
|
||||
else:
|
||||
raise NotImplementedError(f"all_interfaces is not supported on {sys.platform}")
|
||||
|
||||
def addr_to_hex(addr):
|
||||
'''
|
||||
|
||||
Reference in New Issue
Block a user