mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
feat/606-enable-nat-traversal-via-hole-punching (#668)
* feat: base implementation of dcutr for hole-punching * chore: removed circuit-relay imports from __init__ * feat: implemented dcutr protocol * added test suite with mock setup * Fix pre-commit hook issues in DCUtR implementation * usages of CONNECT_TYPE and SYNC_TYPE have been replaced with HolePunch.Type.CONNECT and HolePunch.Type.SYNC * added unit tests for dcutr and nat module and * added multiaddr.get_peer_id() with proper DNS address handling and fixed method signature inconsistencies * added assertions to verify DCUtR hole punch result in integration test --------- Co-authored-by: Manu Sheel Gupta <manusheel.edu@gmail.com>
This commit is contained in:
563
tests/core/relay/test_dcutr_integration.py
Normal file
563
tests/core/relay/test_dcutr_integration.py
Normal file
@ -0,0 +1,563 @@
|
||||
"""Integration tests for DCUtR protocol with real libp2p hosts using circuit relay."""
|
||||
|
||||
import logging
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
from multiaddr import Multiaddr
|
||||
import trio
|
||||
|
||||
from libp2p.relay.circuit_v2.dcutr import (
|
||||
MAX_HOLE_PUNCH_ATTEMPTS,
|
||||
PROTOCOL_ID,
|
||||
DCUtRProtocol,
|
||||
)
|
||||
from libp2p.relay.circuit_v2.pb.dcutr_pb2 import (
|
||||
HolePunch,
|
||||
)
|
||||
from libp2p.relay.circuit_v2.protocol import (
|
||||
DEFAULT_RELAY_LIMITS,
|
||||
CircuitV2Protocol,
|
||||
)
|
||||
from libp2p.tools.async_service import (
|
||||
background_trio_service,
|
||||
)
|
||||
from tests.utils.factories import (
|
||||
HostFactory,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Test timeouts
|
||||
SLEEP_TIME = 0.5 # seconds
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_through_relay_connection():
|
||||
"""
|
||||
Test DCUtR protocol where peers are connected via relay,
|
||||
then upgrade to direct.
|
||||
"""
|
||||
# Create three hosts: two peers and one relay
|
||||
async with HostFactory.create_batch_and_listen(3) as hosts:
|
||||
peer1, peer2, relay = hosts
|
||||
|
||||
# Create circuit relay protocol for the relay
|
||||
relay_protocol = CircuitV2Protocol(relay, DEFAULT_RELAY_LIMITS, allow_hop=True)
|
||||
|
||||
# Create DCUtR protocols for both peers
|
||||
dcutr1 = DCUtRProtocol(peer1)
|
||||
dcutr2 = DCUtRProtocol(peer2)
|
||||
|
||||
# Track if DCUtR stream handlers were called
|
||||
handler1_called = False
|
||||
handler2_called = False
|
||||
|
||||
# Override stream handlers to track calls
|
||||
original_handler1 = dcutr1._handle_dcutr_stream
|
||||
original_handler2 = dcutr2._handle_dcutr_stream
|
||||
|
||||
async def tracked_handler1(stream):
|
||||
nonlocal handler1_called
|
||||
handler1_called = True
|
||||
await original_handler1(stream)
|
||||
|
||||
async def tracked_handler2(stream):
|
||||
nonlocal handler2_called
|
||||
handler2_called = True
|
||||
await original_handler2(stream)
|
||||
|
||||
dcutr1._handle_dcutr_stream = tracked_handler1
|
||||
dcutr2._handle_dcutr_stream = tracked_handler2
|
||||
|
||||
# Start all protocols
|
||||
async with background_trio_service(relay_protocol):
|
||||
async with background_trio_service(dcutr1):
|
||||
async with background_trio_service(dcutr2):
|
||||
await relay_protocol.event_started.wait()
|
||||
await dcutr1.event_started.wait()
|
||||
await dcutr2.event_started.wait()
|
||||
|
||||
# Connect both peers to the relay
|
||||
relay_addrs = relay.get_addrs()
|
||||
|
||||
# Add relay addresses to both peers' peerstores
|
||||
for addr in relay_addrs:
|
||||
peer1.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
peer2.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
|
||||
# Connect peers to relay
|
||||
await peer1.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await peer2.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await trio.sleep(0.1)
|
||||
|
||||
# Verify peers are connected to relay
|
||||
assert relay.get_id() in [
|
||||
peer_id for peer_id in peer1.get_network().connections.keys()
|
||||
]
|
||||
assert relay.get_id() in [
|
||||
peer_id for peer_id in peer2.get_network().connections.keys()
|
||||
]
|
||||
|
||||
# Verify peers are NOT directly connected to each other
|
||||
assert peer2.get_id() not in [
|
||||
peer_id for peer_id in peer1.get_network().connections.keys()
|
||||
]
|
||||
assert peer1.get_id() not in [
|
||||
peer_id for peer_id in peer2.get_network().connections.keys()
|
||||
]
|
||||
|
||||
# Now test DCUtR: peer1 opens a DCUtR stream to peer2 through the
|
||||
# relay
|
||||
# This should trigger the DCUtR protocol for hole punching
|
||||
try:
|
||||
# Create a circuit relay multiaddr for peer2 through the relay
|
||||
relay_addr = relay_addrs[0]
|
||||
circuit_addr = Multiaddr(
|
||||
f"{relay_addr}/p2p-circuit/p2p/{peer2.get_id()}"
|
||||
)
|
||||
|
||||
# Add the circuit address to peer1's peerstore
|
||||
peer1.get_peerstore().add_addrs(
|
||||
peer2.get_id(), [circuit_addr], 3600
|
||||
)
|
||||
|
||||
# Open a DCUtR stream from peer1 to peer2 through the relay
|
||||
stream = await peer1.new_stream(peer2.get_id(), [PROTOCOL_ID])
|
||||
|
||||
# Send a CONNECT message with observed addresses
|
||||
peer1_addrs = peer1.get_addrs()
|
||||
connect_msg = HolePunch(
|
||||
type=HolePunch.CONNECT,
|
||||
ObsAddrs=[addr.to_bytes() for addr in peer1_addrs[:2]],
|
||||
)
|
||||
await stream.write(connect_msg.SerializeToString())
|
||||
|
||||
# Wait for the message to be processed
|
||||
await trio.sleep(0.2)
|
||||
|
||||
# Verify that the DCUtR stream handler was called on peer2
|
||||
assert handler2_called, (
|
||||
"DCUtR stream handler should have been called on peer2"
|
||||
)
|
||||
|
||||
# Close the stream
|
||||
await stream.close()
|
||||
|
||||
except Exception as e:
|
||||
logger.info(
|
||||
"Expected error when trying to open DCUtR stream through "
|
||||
"relay: %s",
|
||||
e,
|
||||
)
|
||||
# This might fail because we need more setup, but the important
|
||||
# thing is testing the right scenario
|
||||
|
||||
# Wait a bit more
|
||||
await trio.sleep(0.1)
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_relay_to_direct_upgrade():
|
||||
"""Test the complete flow: relay connection -> DCUtR -> direct connection."""
|
||||
# Create three hosts: two peers and one relay
|
||||
async with HostFactory.create_batch_and_listen(3) as hosts:
|
||||
peer1, peer2, relay = hosts
|
||||
|
||||
# Create circuit relay protocol for the relay
|
||||
relay_protocol = CircuitV2Protocol(relay, DEFAULT_RELAY_LIMITS, allow_hop=True)
|
||||
|
||||
# Create DCUtR protocols for both peers
|
||||
dcutr1 = DCUtRProtocol(peer1)
|
||||
dcutr2 = DCUtRProtocol(peer2)
|
||||
|
||||
# Track messages received
|
||||
messages_received = []
|
||||
|
||||
# Override stream handler to capture messages
|
||||
original_handler = dcutr2._handle_dcutr_stream
|
||||
|
||||
async def message_capturing_handler(stream):
|
||||
try:
|
||||
# Read the message
|
||||
msg_data = await stream.read()
|
||||
hole_punch = HolePunch()
|
||||
hole_punch.ParseFromString(msg_data)
|
||||
messages_received.append(hole_punch)
|
||||
|
||||
# Send a SYNC response
|
||||
sync_msg = HolePunch(type=HolePunch.SYNC)
|
||||
await stream.write(sync_msg.SerializeToString())
|
||||
|
||||
await original_handler(stream)
|
||||
except Exception as e:
|
||||
logger.error(f"Error in message capturing handler: {e}")
|
||||
await stream.close()
|
||||
|
||||
dcutr2._handle_dcutr_stream = message_capturing_handler
|
||||
|
||||
# Start all protocols
|
||||
async with background_trio_service(relay_protocol):
|
||||
async with background_trio_service(dcutr1):
|
||||
async with background_trio_service(dcutr2):
|
||||
await relay_protocol.event_started.wait()
|
||||
await dcutr1.event_started.wait()
|
||||
await dcutr2.event_started.wait()
|
||||
|
||||
# Re-register the handler with the host
|
||||
dcutr2.host.set_stream_handler(
|
||||
PROTOCOL_ID, message_capturing_handler
|
||||
)
|
||||
|
||||
# Connect both peers to the relay
|
||||
relay_addrs = relay.get_addrs()
|
||||
|
||||
# Add relay addresses to both peers' peerstores
|
||||
for addr in relay_addrs:
|
||||
peer1.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
peer2.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
|
||||
# Connect peers to relay
|
||||
await peer1.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await peer2.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await trio.sleep(0.1)
|
||||
|
||||
# Verify peers are connected to relay but not to each other
|
||||
assert relay.get_id() in [
|
||||
peer_id for peer_id in peer1.get_network().connections.keys()
|
||||
]
|
||||
assert relay.get_id() in [
|
||||
peer_id for peer_id in peer2.get_network().connections.keys()
|
||||
]
|
||||
assert peer2.get_id() not in [
|
||||
peer_id for peer_id in peer1.get_network().connections.keys()
|
||||
]
|
||||
|
||||
# Try to open a DCUtR stream through the relay
|
||||
try:
|
||||
# Create a circuit relay multiaddr for peer2 through the relay
|
||||
relay_addr = relay_addrs[0]
|
||||
circuit_addr = Multiaddr(
|
||||
f"{relay_addr}/p2p-circuit/p2p/{peer2.get_id()}"
|
||||
)
|
||||
|
||||
# Add the circuit address to peer1's peerstore
|
||||
peer1.get_peerstore().add_addrs(
|
||||
peer2.get_id(), [circuit_addr], 3600
|
||||
)
|
||||
|
||||
# Open a DCUtR stream from peer1 to peer2 through the relay
|
||||
stream = await peer1.new_stream(peer2.get_id(), [PROTOCOL_ID])
|
||||
|
||||
# Send a CONNECT message with observed addresses
|
||||
peer1_addrs = peer1.get_addrs()
|
||||
connect_msg = HolePunch(
|
||||
type=HolePunch.CONNECT,
|
||||
ObsAddrs=[addr.to_bytes() for addr in peer1_addrs[:2]],
|
||||
)
|
||||
await stream.write(connect_msg.SerializeToString())
|
||||
|
||||
# Wait for the message to be processed
|
||||
await trio.sleep(0.2)
|
||||
|
||||
# Verify that the CONNECT message was received
|
||||
assert len(messages_received) == 1, (
|
||||
"Should have received one message"
|
||||
)
|
||||
assert messages_received[0].type == HolePunch.CONNECT, (
|
||||
"Should have received CONNECT message"
|
||||
)
|
||||
assert len(messages_received[0].ObsAddrs) == 2, (
|
||||
"Should have received 2 observed addresses"
|
||||
)
|
||||
|
||||
# Close the stream
|
||||
await stream.close()
|
||||
|
||||
except Exception as e:
|
||||
logger.info(
|
||||
"Expected error when trying to open DCUtR stream through "
|
||||
"relay: %s",
|
||||
e,
|
||||
)
|
||||
|
||||
# Wait a bit more
|
||||
await trio.sleep(0.1)
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_hole_punch_through_relay():
|
||||
"""Test hole punching when peers are connected through relay."""
|
||||
# Create three hosts: two peers and one relay
|
||||
async with HostFactory.create_batch_and_listen(3) as hosts:
|
||||
peer1, peer2, relay = hosts
|
||||
|
||||
# Create circuit relay protocol for the relay
|
||||
relay_protocol = CircuitV2Protocol(relay, DEFAULT_RELAY_LIMITS, allow_hop=True)
|
||||
|
||||
# Create DCUtR protocols for both peers
|
||||
dcutr1 = DCUtRProtocol(peer1)
|
||||
dcutr2 = DCUtRProtocol(peer2)
|
||||
|
||||
# Start all protocols
|
||||
async with background_trio_service(relay_protocol):
|
||||
async with background_trio_service(dcutr1):
|
||||
async with background_trio_service(dcutr2):
|
||||
await relay_protocol.event_started.wait()
|
||||
await dcutr1.event_started.wait()
|
||||
await dcutr2.event_started.wait()
|
||||
|
||||
# Connect both peers to the relay
|
||||
relay_addrs = relay.get_addrs()
|
||||
|
||||
# Add relay addresses to both peers' peerstores
|
||||
for addr in relay_addrs:
|
||||
peer1.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
peer2.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
|
||||
# Connect peers to relay
|
||||
await peer1.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await peer2.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await trio.sleep(0.1)
|
||||
|
||||
# Verify peers are connected to relay but not to each other
|
||||
assert relay.get_id() in [
|
||||
peer_id for peer_id in peer1.get_network().connections.keys()
|
||||
]
|
||||
assert relay.get_id() in [
|
||||
peer_id for peer_id in peer2.get_network().connections.keys()
|
||||
]
|
||||
assert peer2.get_id() not in [
|
||||
peer_id for peer_id in peer1.get_network().connections.keys()
|
||||
]
|
||||
|
||||
# Check if there's already a direct connection (should be False)
|
||||
has_direct = await dcutr1._have_direct_connection(peer2.get_id())
|
||||
assert not has_direct, "Peers should not have a direct connection"
|
||||
|
||||
# Try to initiate a hole punch (this should work through the relay
|
||||
# connection)
|
||||
# In a real scenario, this would be called after establishing a
|
||||
# relay connection
|
||||
result = await dcutr1.initiate_hole_punch(peer2.get_id())
|
||||
|
||||
# This should attempt hole punching but likely fail due to no public
|
||||
# addresses
|
||||
# The important thing is that the DCUtR protocol logic is executed
|
||||
logger.info(
|
||||
"Hole punch result: %s",
|
||||
result,
|
||||
)
|
||||
|
||||
assert result is not None, "Hole punch result should not be None"
|
||||
assert isinstance(result, bool), (
|
||||
"Hole punch result should be a boolean"
|
||||
)
|
||||
|
||||
# Wait a bit more
|
||||
await trio.sleep(0.1)
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_relay_connection_verification():
|
||||
"""Test that DCUtR works correctly when peers are connected via relay."""
|
||||
# Create three hosts: two peers and one relay
|
||||
async with HostFactory.create_batch_and_listen(3) as hosts:
|
||||
peer1, peer2, relay = hosts
|
||||
|
||||
# Create circuit relay protocol for the relay
|
||||
relay_protocol = CircuitV2Protocol(relay, DEFAULT_RELAY_LIMITS, allow_hop=True)
|
||||
|
||||
# Create DCUtR protocols for both peers
|
||||
dcutr1 = DCUtRProtocol(peer1)
|
||||
dcutr2 = DCUtRProtocol(peer2)
|
||||
|
||||
# Start all protocols
|
||||
async with background_trio_service(relay_protocol):
|
||||
async with background_trio_service(dcutr1):
|
||||
async with background_trio_service(dcutr2):
|
||||
await relay_protocol.event_started.wait()
|
||||
await dcutr1.event_started.wait()
|
||||
await dcutr2.event_started.wait()
|
||||
|
||||
# Connect both peers to the relay
|
||||
relay_addrs = relay.get_addrs()
|
||||
|
||||
# Add relay addresses to both peers' peerstores
|
||||
for addr in relay_addrs:
|
||||
peer1.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
peer2.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
|
||||
# Connect peers to relay
|
||||
await peer1.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await peer2.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await trio.sleep(0.1)
|
||||
|
||||
# Verify peers are connected to relay
|
||||
assert relay.get_id() in [
|
||||
peer_id for peer_id in peer1.get_network().connections.keys()
|
||||
]
|
||||
assert relay.get_id() in [
|
||||
peer_id for peer_id in peer2.get_network().connections.keys()
|
||||
]
|
||||
|
||||
# Verify peers are NOT directly connected to each other
|
||||
assert peer2.get_id() not in [
|
||||
peer_id for peer_id in peer1.get_network().connections.keys()
|
||||
]
|
||||
assert peer1.get_id() not in [
|
||||
peer_id for peer_id in peer2.get_network().connections.keys()
|
||||
]
|
||||
|
||||
# Test getting observed addresses (real implementation)
|
||||
observed_addrs1 = await dcutr1._get_observed_addrs()
|
||||
observed_addrs2 = await dcutr2._get_observed_addrs()
|
||||
|
||||
assert isinstance(observed_addrs1, list)
|
||||
assert isinstance(observed_addrs2, list)
|
||||
|
||||
# Should contain the hosts' actual addresses
|
||||
assert len(observed_addrs1) > 0, (
|
||||
"Peer1 should have observed addresses"
|
||||
)
|
||||
assert len(observed_addrs2) > 0, (
|
||||
"Peer2 should have observed addresses"
|
||||
)
|
||||
|
||||
# Test decoding observed addresses
|
||||
test_addrs = [
|
||||
Multiaddr("/ip4/127.0.0.1/tcp/1234").to_bytes(),
|
||||
Multiaddr("/ip4/192.168.1.1/tcp/5678").to_bytes(),
|
||||
b"invalid-addr", # This should be filtered out
|
||||
]
|
||||
decoded = dcutr1._decode_observed_addrs(test_addrs)
|
||||
assert len(decoded) == 2, "Should decode 2 valid addresses"
|
||||
assert all(str(addr).startswith("/ip4/") for addr in decoded)
|
||||
|
||||
# Wait a bit more
|
||||
await trio.sleep(0.1)
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_relay_error_handling():
|
||||
"""Test DCUtR error handling when working through relay connections."""
|
||||
# Create three hosts: two peers and one relay
|
||||
async with HostFactory.create_batch_and_listen(3) as hosts:
|
||||
peer1, peer2, relay = hosts
|
||||
|
||||
# Create circuit relay protocol for the relay
|
||||
relay_protocol = CircuitV2Protocol(relay, DEFAULT_RELAY_LIMITS, allow_hop=True)
|
||||
|
||||
# Create DCUtR protocols for both peers
|
||||
dcutr1 = DCUtRProtocol(peer1)
|
||||
dcutr2 = DCUtRProtocol(peer2)
|
||||
|
||||
# Start all protocols
|
||||
async with background_trio_service(relay_protocol):
|
||||
async with background_trio_service(dcutr1):
|
||||
async with background_trio_service(dcutr2):
|
||||
await relay_protocol.event_started.wait()
|
||||
await dcutr1.event_started.wait()
|
||||
await dcutr2.event_started.wait()
|
||||
|
||||
# Connect both peers to the relay
|
||||
relay_addrs = relay.get_addrs()
|
||||
|
||||
# Add relay addresses to both peers' peerstores
|
||||
for addr in relay_addrs:
|
||||
peer1.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
peer2.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
|
||||
# Connect peers to relay
|
||||
await peer1.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await peer2.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await trio.sleep(0.1)
|
||||
|
||||
# Test with a stream that times out
|
||||
timeout_stream = MagicMock()
|
||||
timeout_stream.muxed_conn.peer_id = peer2.get_id()
|
||||
timeout_stream.read = AsyncMock(side_effect=trio.TooSlowError())
|
||||
timeout_stream.write = AsyncMock()
|
||||
timeout_stream.close = AsyncMock()
|
||||
|
||||
# This should not raise an exception, just log and close
|
||||
await dcutr1._handle_dcutr_stream(timeout_stream)
|
||||
|
||||
# Verify stream was closed
|
||||
assert timeout_stream.close.called
|
||||
|
||||
# Test with malformed message
|
||||
malformed_stream = MagicMock()
|
||||
malformed_stream.muxed_conn.peer_id = peer2.get_id()
|
||||
malformed_stream.read = AsyncMock(return_value=b"not-a-protobuf")
|
||||
malformed_stream.write = AsyncMock()
|
||||
malformed_stream.close = AsyncMock()
|
||||
|
||||
# This should not raise an exception, just log and close
|
||||
await dcutr1._handle_dcutr_stream(malformed_stream)
|
||||
|
||||
# Verify stream was closed
|
||||
assert malformed_stream.close.called
|
||||
|
||||
# Wait a bit more
|
||||
await trio.sleep(0.1)
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_relay_attempt_limiting():
|
||||
"""Test DCUtR attempt limiting when working through relay connections."""
|
||||
# Create three hosts: two peers and one relay
|
||||
async with HostFactory.create_batch_and_listen(3) as hosts:
|
||||
peer1, peer2, relay = hosts
|
||||
|
||||
# Create circuit relay protocol for the relay
|
||||
relay_protocol = CircuitV2Protocol(relay, DEFAULT_RELAY_LIMITS, allow_hop=True)
|
||||
|
||||
# Create DCUtR protocols for both peers
|
||||
dcutr1 = DCUtRProtocol(peer1)
|
||||
dcutr2 = DCUtRProtocol(peer2)
|
||||
|
||||
# Start all protocols
|
||||
async with background_trio_service(relay_protocol):
|
||||
async with background_trio_service(dcutr1):
|
||||
async with background_trio_service(dcutr2):
|
||||
await relay_protocol.event_started.wait()
|
||||
await dcutr1.event_started.wait()
|
||||
await dcutr2.event_started.wait()
|
||||
|
||||
# Connect both peers to the relay
|
||||
relay_addrs = relay.get_addrs()
|
||||
|
||||
# Add relay addresses to both peers' peerstores
|
||||
for addr in relay_addrs:
|
||||
peer1.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
peer2.get_peerstore().add_addrs(relay.get_id(), [addr], 3600)
|
||||
|
||||
# Connect peers to relay
|
||||
await peer1.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await peer2.connect(relay.get_peerstore().peer_info(relay.get_id()))
|
||||
await trio.sleep(0.1)
|
||||
|
||||
# Set max attempts reached
|
||||
dcutr1._hole_punch_attempts[peer2.get_id()] = (
|
||||
MAX_HOLE_PUNCH_ATTEMPTS
|
||||
)
|
||||
|
||||
# Try to initiate hole punch - should fail due to max attempts
|
||||
result = await dcutr1.initiate_hole_punch(peer2.get_id())
|
||||
assert result is False, "Hole punch should fail due to max attempts"
|
||||
|
||||
# Reset attempts
|
||||
dcutr1._hole_punch_attempts.clear()
|
||||
|
||||
# Add to direct connections
|
||||
dcutr1._direct_connections.add(peer2.get_id())
|
||||
|
||||
# Try to initiate hole punch - should succeed immediately
|
||||
result = await dcutr1.initiate_hole_punch(peer2.get_id())
|
||||
assert result is True, (
|
||||
"Hole punch should succeed for already connected peers"
|
||||
)
|
||||
|
||||
# Wait a bit more
|
||||
await trio.sleep(0.1)
|
||||
208
tests/core/relay/test_dcutr_protocol.py
Normal file
208
tests/core/relay/test_dcutr_protocol.py
Normal file
@ -0,0 +1,208 @@
|
||||
"""Unit tests for DCUtR protocol."""
|
||||
|
||||
import logging
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
import trio
|
||||
|
||||
from libp2p.abc import INetStream
|
||||
from libp2p.peer.id import ID
|
||||
from libp2p.relay.circuit_v2.dcutr import (
|
||||
MAX_HOLE_PUNCH_ATTEMPTS,
|
||||
DCUtRProtocol,
|
||||
)
|
||||
from libp2p.relay.circuit_v2.pb.dcutr_pb2 import HolePunch
|
||||
from libp2p.tools.async_service import background_trio_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_protocol_initialization():
|
||||
"""Test DCUtR protocol initialization."""
|
||||
mock_host = MagicMock()
|
||||
dcutr = DCUtRProtocol(mock_host)
|
||||
|
||||
# Test that the protocol is initialized correctly
|
||||
assert dcutr.host == mock_host
|
||||
assert not dcutr.event_started.is_set()
|
||||
assert dcutr._hole_punch_attempts == {}
|
||||
assert dcutr._direct_connections == set()
|
||||
assert dcutr._in_progress == set()
|
||||
|
||||
# Test that the protocol can be started
|
||||
async with background_trio_service(dcutr):
|
||||
# Wait for the protocol to start
|
||||
await dcutr.event_started.wait()
|
||||
|
||||
# Verify that the stream handler was registered
|
||||
mock_host.set_stream_handler.assert_called_once()
|
||||
|
||||
# Verify that the event is set
|
||||
assert dcutr.event_started.is_set()
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_message_exchange():
|
||||
"""Test DCUtR message exchange."""
|
||||
mock_host = MagicMock()
|
||||
dcutr = DCUtRProtocol(mock_host)
|
||||
|
||||
# Test that the protocol can be started
|
||||
async with background_trio_service(dcutr):
|
||||
# Wait for the protocol to start
|
||||
await dcutr.event_started.wait()
|
||||
|
||||
# Test CONNECT message
|
||||
connect_msg = HolePunch(
|
||||
type=HolePunch.CONNECT,
|
||||
ObsAddrs=[b"/ip4/127.0.0.1/tcp/1234", b"/ip4/192.168.1.1/tcp/5678"],
|
||||
)
|
||||
|
||||
# Test SYNC message
|
||||
sync_msg = HolePunch(type=HolePunch.SYNC)
|
||||
|
||||
# Verify message types
|
||||
assert connect_msg.type == HolePunch.CONNECT
|
||||
assert sync_msg.type == HolePunch.SYNC
|
||||
assert len(connect_msg.ObsAddrs) == 2
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_error_handling(monkeypatch):
|
||||
"""Test DCUtR error handling."""
|
||||
mock_host = MagicMock()
|
||||
dcutr = DCUtRProtocol(mock_host)
|
||||
|
||||
async with background_trio_service(dcutr):
|
||||
await dcutr.event_started.wait()
|
||||
|
||||
# Simulate a stream that times out
|
||||
class TimeoutStream(INetStream):
|
||||
def __init__(self):
|
||||
self._protocol = None
|
||||
self.muxed_conn = MagicMock(peer_id=ID(b"peer"))
|
||||
|
||||
async def read(self, n: int | None = None) -> bytes:
|
||||
await trio.sleep(0.2)
|
||||
raise trio.TooSlowError()
|
||||
|
||||
async def write(self, data: bytes) -> None:
|
||||
return None
|
||||
|
||||
async def close(self, *args, **kwargs):
|
||||
return None
|
||||
|
||||
async def reset(self):
|
||||
return None
|
||||
|
||||
def get_protocol(self):
|
||||
return self._protocol
|
||||
|
||||
def set_protocol(self, protocol_id):
|
||||
self._protocol = protocol_id
|
||||
|
||||
def get_remote_address(self):
|
||||
return ("127.0.0.1", 1234)
|
||||
|
||||
# Should not raise, just log and close
|
||||
await dcutr._handle_dcutr_stream(TimeoutStream())
|
||||
|
||||
# Simulate a stream with malformed message
|
||||
class MalformedStream(INetStream):
|
||||
def __init__(self):
|
||||
self._protocol = None
|
||||
self.muxed_conn = MagicMock(peer_id=ID(b"peer"))
|
||||
|
||||
async def read(self, n: int | None = None) -> bytes:
|
||||
return b"not-a-protobuf"
|
||||
|
||||
async def write(self, data: bytes) -> None:
|
||||
return None
|
||||
|
||||
async def close(self, *args, **kwargs):
|
||||
return None
|
||||
|
||||
async def reset(self):
|
||||
return None
|
||||
|
||||
def get_protocol(self):
|
||||
return self._protocol
|
||||
|
||||
def set_protocol(self, protocol_id):
|
||||
self._protocol = protocol_id
|
||||
|
||||
def get_remote_address(self):
|
||||
return ("127.0.0.1", 1234)
|
||||
|
||||
await dcutr._handle_dcutr_stream(MalformedStream())
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_max_attempts_and_already_connected():
|
||||
"""Test max hole punch attempts and already-connected peer."""
|
||||
mock_host = MagicMock()
|
||||
dcutr = DCUtRProtocol(mock_host)
|
||||
peer_id = ID(b"peer")
|
||||
|
||||
# Simulate already having a direct connection
|
||||
dcutr._direct_connections.add(peer_id)
|
||||
result = await dcutr.initiate_hole_punch(peer_id)
|
||||
assert result is True
|
||||
|
||||
# Remove direct connection, simulate max attempts
|
||||
dcutr._direct_connections.clear()
|
||||
dcutr._hole_punch_attempts[peer_id] = MAX_HOLE_PUNCH_ATTEMPTS
|
||||
result = await dcutr.initiate_hole_punch(peer_id)
|
||||
assert result is False
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_observed_addr_encoding_decoding():
|
||||
"""Test observed address encoding/decoding."""
|
||||
from multiaddr import Multiaddr
|
||||
|
||||
mock_host = MagicMock()
|
||||
dcutr = DCUtRProtocol(mock_host)
|
||||
# Simulate valid and invalid multiaddrs as bytes
|
||||
valid = [
|
||||
Multiaddr("/ip4/127.0.0.1/tcp/1234").to_bytes(),
|
||||
Multiaddr("/ip4/192.168.1.1/tcp/5678").to_bytes(),
|
||||
]
|
||||
invalid = [b"not-a-multiaddr", b""]
|
||||
decoded = dcutr._decode_observed_addrs(valid + invalid)
|
||||
assert len(decoded) == 2
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_dcutr_real_perform_hole_punch(monkeypatch):
|
||||
"""Test initiate_hole_punch with real _perform_hole_punch logic (mock network)."""
|
||||
mock_host = MagicMock()
|
||||
dcutr = DCUtRProtocol(mock_host)
|
||||
peer_id = ID(b"peer")
|
||||
|
||||
# Patch methods to simulate a successful punch
|
||||
monkeypatch.setattr(dcutr, "_have_direct_connection", AsyncMock(return_value=False))
|
||||
monkeypatch.setattr(
|
||||
dcutr,
|
||||
"_get_observed_addrs",
|
||||
AsyncMock(return_value=[b"/ip4/127.0.0.1/tcp/1234"]),
|
||||
)
|
||||
mock_stream = MagicMock()
|
||||
mock_stream.read = AsyncMock(
|
||||
side_effect=[
|
||||
HolePunch(
|
||||
type=HolePunch.CONNECT, ObsAddrs=[b"/ip4/192.168.1.1/tcp/4321"]
|
||||
).SerializeToString(),
|
||||
HolePunch(type=HolePunch.SYNC).SerializeToString(),
|
||||
]
|
||||
)
|
||||
mock_stream.write = AsyncMock()
|
||||
mock_stream.close = AsyncMock()
|
||||
mock_stream.muxed_conn = MagicMock(peer_id=peer_id)
|
||||
mock_host.new_stream = AsyncMock(return_value=mock_stream)
|
||||
monkeypatch.setattr(dcutr, "_perform_hole_punch", AsyncMock(return_value=True))
|
||||
|
||||
result = await dcutr.initiate_hole_punch(peer_id)
|
||||
assert result is True
|
||||
297
tests/core/relay/test_nat.py
Normal file
297
tests/core/relay/test_nat.py
Normal file
@ -0,0 +1,297 @@
|
||||
"""Tests for NAT traversal utilities."""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from multiaddr import Multiaddr
|
||||
|
||||
from libp2p.peer.id import ID
|
||||
from libp2p.relay.circuit_v2.nat import (
|
||||
ReachabilityChecker,
|
||||
extract_ip_from_multiaddr,
|
||||
ip_to_int,
|
||||
is_ip_in_range,
|
||||
is_private_ip,
|
||||
)
|
||||
|
||||
|
||||
def test_ip_to_int_ipv4():
|
||||
"""Test converting IPv4 addresses to integers."""
|
||||
assert ip_to_int("192.168.1.1") == 3232235777
|
||||
assert ip_to_int("10.0.0.1") == 167772161
|
||||
assert ip_to_int("127.0.0.1") == 2130706433
|
||||
|
||||
|
||||
def test_ip_to_int_ipv6():
|
||||
"""Test converting IPv6 addresses to integers."""
|
||||
# Test with a simple IPv6 address
|
||||
ipv6_int = ip_to_int("::1")
|
||||
assert isinstance(ipv6_int, int)
|
||||
assert ipv6_int > 0
|
||||
|
||||
|
||||
def test_ip_to_int_invalid():
|
||||
"""Test handling of invalid IP addresses."""
|
||||
with pytest.raises(ValueError):
|
||||
ip_to_int("invalid-ip")
|
||||
|
||||
|
||||
def test_is_ip_in_range():
|
||||
"""Test IP range checking."""
|
||||
# Test within range
|
||||
assert is_ip_in_range("192.168.1.5", "192.168.1.1", "192.168.1.10") is True
|
||||
assert is_ip_in_range("10.0.0.5", "10.0.0.0", "10.0.0.255") is True
|
||||
|
||||
# Test outside range
|
||||
assert is_ip_in_range("192.168.2.5", "192.168.1.1", "192.168.1.10") is False
|
||||
assert is_ip_in_range("8.8.8.8", "10.0.0.0", "10.0.0.255") is False
|
||||
|
||||
|
||||
def test_is_ip_in_range_invalid():
|
||||
"""Test IP range checking with invalid inputs."""
|
||||
assert is_ip_in_range("invalid", "192.168.1.1", "192.168.1.10") is False
|
||||
assert is_ip_in_range("192.168.1.5", "invalid", "192.168.1.10") is False
|
||||
|
||||
|
||||
def test_is_private_ip():
|
||||
"""Test private IP detection."""
|
||||
# Private IPs
|
||||
assert is_private_ip("192.168.1.1") is True
|
||||
assert is_private_ip("10.0.0.1") is True
|
||||
assert is_private_ip("172.16.0.1") is True
|
||||
assert is_private_ip("127.0.0.1") is True # Loopback
|
||||
assert is_private_ip("169.254.1.1") is True # Link-local
|
||||
|
||||
# Public IPs
|
||||
assert is_private_ip("8.8.8.8") is False
|
||||
assert is_private_ip("1.1.1.1") is False
|
||||
assert is_private_ip("208.67.222.222") is False
|
||||
|
||||
|
||||
def test_extract_ip_from_multiaddr():
|
||||
"""Test IP extraction from multiaddrs."""
|
||||
# IPv4 addresses
|
||||
addr1 = Multiaddr("/ip4/192.168.1.1/tcp/1234")
|
||||
assert extract_ip_from_multiaddr(addr1) == "192.168.1.1"
|
||||
|
||||
addr2 = Multiaddr("/ip4/10.0.0.1/udp/5678")
|
||||
assert extract_ip_from_multiaddr(addr2) == "10.0.0.1"
|
||||
|
||||
# IPv6 addresses
|
||||
addr3 = Multiaddr("/ip6/::1/tcp/1234")
|
||||
assert extract_ip_from_multiaddr(addr3) == "::1"
|
||||
|
||||
addr4 = Multiaddr("/ip6/2001:db8::1/udp/5678")
|
||||
assert extract_ip_from_multiaddr(addr4) == "2001:db8::1"
|
||||
|
||||
# No IP address
|
||||
addr5 = Multiaddr("/dns4/example.com/tcp/1234")
|
||||
assert extract_ip_from_multiaddr(addr5) is None
|
||||
|
||||
# Complex multiaddr (without p2p to avoid base58 issues)
|
||||
addr6 = Multiaddr("/ip4/192.168.1.1/tcp/1234/udp/5678")
|
||||
assert extract_ip_from_multiaddr(addr6) == "192.168.1.1"
|
||||
|
||||
|
||||
def test_reachability_checker_init():
|
||||
"""Test ReachabilityChecker initialization."""
|
||||
mock_host = MagicMock()
|
||||
checker = ReachabilityChecker(mock_host)
|
||||
|
||||
assert checker.host == mock_host
|
||||
assert checker._peer_reachability == {}
|
||||
assert checker._known_public_peers == set()
|
||||
|
||||
|
||||
def test_reachability_checker_is_addr_public():
|
||||
"""Test public address detection."""
|
||||
mock_host = MagicMock()
|
||||
checker = ReachabilityChecker(mock_host)
|
||||
|
||||
# Public addresses
|
||||
public_addr1 = Multiaddr("/ip4/8.8.8.8/tcp/1234")
|
||||
assert checker.is_addr_public(public_addr1) is True
|
||||
|
||||
public_addr2 = Multiaddr("/ip4/1.1.1.1/udp/5678")
|
||||
assert checker.is_addr_public(public_addr2) is True
|
||||
|
||||
# Private addresses
|
||||
private_addr1 = Multiaddr("/ip4/192.168.1.1/tcp/1234")
|
||||
assert checker.is_addr_public(private_addr1) is False
|
||||
|
||||
private_addr2 = Multiaddr("/ip4/10.0.0.1/udp/5678")
|
||||
assert checker.is_addr_public(private_addr2) is False
|
||||
|
||||
private_addr3 = Multiaddr("/ip4/127.0.0.1/tcp/1234")
|
||||
assert checker.is_addr_public(private_addr3) is False
|
||||
|
||||
# No IP address
|
||||
dns_addr = Multiaddr("/dns4/example.com/tcp/1234")
|
||||
assert checker.is_addr_public(dns_addr) is False
|
||||
|
||||
|
||||
def test_reachability_checker_get_public_addrs():
|
||||
"""Test filtering for public addresses."""
|
||||
mock_host = MagicMock()
|
||||
checker = ReachabilityChecker(mock_host)
|
||||
|
||||
addrs = [
|
||||
Multiaddr("/ip4/8.8.8.8/tcp/1234"), # Public
|
||||
Multiaddr("/ip4/192.168.1.1/tcp/1234"), # Private
|
||||
Multiaddr("/ip4/1.1.1.1/udp/5678"), # Public
|
||||
Multiaddr("/ip4/10.0.0.1/tcp/1234"), # Private
|
||||
Multiaddr("/dns4/example.com/tcp/1234"), # DNS
|
||||
]
|
||||
|
||||
public_addrs = checker.get_public_addrs(addrs)
|
||||
assert len(public_addrs) == 2
|
||||
assert Multiaddr("/ip4/8.8.8.8/tcp/1234") in public_addrs
|
||||
assert Multiaddr("/ip4/1.1.1.1/udp/5678") in public_addrs
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_check_peer_reachability_connected_direct():
|
||||
"""Test peer reachability when directly connected."""
|
||||
mock_host = MagicMock()
|
||||
mock_network = MagicMock()
|
||||
mock_host.get_network.return_value = mock_network
|
||||
|
||||
peer_id = ID(b"test-peer-id")
|
||||
mock_conn = MagicMock()
|
||||
mock_conn.get_transport_addresses.return_value = [
|
||||
Multiaddr("/ip4/192.168.1.1/tcp/1234") # Direct connection
|
||||
]
|
||||
|
||||
mock_network.connections = {peer_id: mock_conn}
|
||||
|
||||
checker = ReachabilityChecker(mock_host)
|
||||
result = await checker.check_peer_reachability(peer_id)
|
||||
|
||||
assert result is True
|
||||
assert checker._peer_reachability[peer_id] is True
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_check_peer_reachability_connected_relay():
|
||||
"""Test peer reachability when connected through relay."""
|
||||
mock_host = MagicMock()
|
||||
mock_network = MagicMock()
|
||||
mock_host.get_network.return_value = mock_network
|
||||
|
||||
peer_id = ID(b"test-peer-id")
|
||||
mock_conn = MagicMock()
|
||||
mock_conn.get_transport_addresses.return_value = [
|
||||
Multiaddr("/p2p-circuit/ip4/192.168.1.1/tcp/1234") # Relay connection
|
||||
]
|
||||
|
||||
mock_network.connections = {peer_id: mock_conn}
|
||||
|
||||
# Mock peerstore with public addresses
|
||||
mock_peerstore = MagicMock()
|
||||
mock_peerstore.addrs.return_value = [
|
||||
Multiaddr("/ip4/8.8.8.8/tcp/1234") # Public address
|
||||
]
|
||||
mock_host.get_peerstore.return_value = mock_peerstore
|
||||
|
||||
checker = ReachabilityChecker(mock_host)
|
||||
result = await checker.check_peer_reachability(peer_id)
|
||||
|
||||
assert result is True
|
||||
assert checker._peer_reachability[peer_id] is True
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_check_peer_reachability_not_connected():
|
||||
"""Test peer reachability when not connected."""
|
||||
mock_host = MagicMock()
|
||||
mock_network = MagicMock()
|
||||
mock_host.get_network.return_value = mock_network
|
||||
|
||||
peer_id = ID(b"test-peer-id")
|
||||
mock_network.connections = {} # No connections
|
||||
|
||||
checker = ReachabilityChecker(mock_host)
|
||||
result = await checker.check_peer_reachability(peer_id)
|
||||
|
||||
assert result is False
|
||||
# When not connected, the method doesn't add to cache
|
||||
assert peer_id not in checker._peer_reachability
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_check_peer_reachability_cached():
|
||||
"""Test that peer reachability results are cached."""
|
||||
mock_host = MagicMock()
|
||||
checker = ReachabilityChecker(mock_host)
|
||||
|
||||
peer_id = ID(b"test-peer-id")
|
||||
checker._peer_reachability[peer_id] = True
|
||||
|
||||
result = await checker.check_peer_reachability(peer_id)
|
||||
assert result is True
|
||||
|
||||
# Should not call host methods when cached
|
||||
mock_host.get_network.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_check_self_reachability_with_public_addrs():
|
||||
"""Test self reachability when host has public addresses."""
|
||||
mock_host = MagicMock()
|
||||
mock_host.get_addrs.return_value = [
|
||||
Multiaddr("/ip4/8.8.8.8/tcp/1234"), # Public
|
||||
Multiaddr("/ip4/192.168.1.1/tcp/1234"), # Private
|
||||
Multiaddr("/ip4/1.1.1.1/udp/5678"), # Public
|
||||
]
|
||||
|
||||
checker = ReachabilityChecker(mock_host)
|
||||
is_reachable, public_addrs = await checker.check_self_reachability()
|
||||
|
||||
assert is_reachable is True
|
||||
assert len(public_addrs) == 2
|
||||
assert Multiaddr("/ip4/8.8.8.8/tcp/1234") in public_addrs
|
||||
assert Multiaddr("/ip4/1.1.1.1/udp/5678") in public_addrs
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_check_self_reachability_no_public_addrs():
|
||||
"""Test self reachability when host has no public addresses."""
|
||||
mock_host = MagicMock()
|
||||
mock_host.get_addrs.return_value = [
|
||||
Multiaddr("/ip4/192.168.1.1/tcp/1234"), # Private
|
||||
Multiaddr("/ip4/10.0.0.1/udp/5678"), # Private
|
||||
Multiaddr("/ip4/127.0.0.1/tcp/1234"), # Loopback
|
||||
]
|
||||
|
||||
checker = ReachabilityChecker(mock_host)
|
||||
is_reachable, public_addrs = await checker.check_self_reachability()
|
||||
|
||||
assert is_reachable is False
|
||||
assert len(public_addrs) == 0
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_check_peer_reachability_multiple_connections():
|
||||
"""Test peer reachability with multiple connections."""
|
||||
mock_host = MagicMock()
|
||||
mock_network = MagicMock()
|
||||
mock_host.get_network.return_value = mock_network
|
||||
|
||||
peer_id = ID(b"test-peer-id")
|
||||
mock_conn1 = MagicMock()
|
||||
mock_conn1.get_transport_addresses.return_value = [
|
||||
Multiaddr("/p2p-circuit/ip4/192.168.1.1/tcp/1234") # Relay
|
||||
]
|
||||
|
||||
mock_conn2 = MagicMock()
|
||||
mock_conn2.get_transport_addresses.return_value = [
|
||||
Multiaddr("/ip4/192.168.1.1/tcp/1234") # Direct
|
||||
]
|
||||
|
||||
mock_network.connections = {peer_id: [mock_conn1, mock_conn2]}
|
||||
|
||||
checker = ReachabilityChecker(mock_host)
|
||||
result = await checker.check_peer_reachability(peer_id)
|
||||
|
||||
assert result is True
|
||||
assert checker._peer_reachability[peer_id] is True
|
||||
Reference in New Issue
Block a user