Skip to content

Commit 6af0ad8

Browse files
committed
test: add test_complete_remote_address_delegation_chain
1 parent d9c69f0 commit 6af0ad8

File tree

1 file changed

+89
-10
lines changed

1 file changed

+89
-10
lines changed

tests/core/identity/identify/test_identify.py

+89-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
PROTOCOL_VERSION,
1212
_mk_identify_protobuf,
1313
_multiaddr_to_bytes,
14+
_remote_address_to_multiaddr,
1415
)
1516
from libp2p.identity.identify.pb.identify_pb2 import (
1617
Identify,
@@ -19,7 +20,28 @@
1920
host_pair_factory,
2021
)
2122

22-
logger = logging.getLogger("libp2p.identity.identify-test")
23+
24+
def clean_multiaddr(addr: Multiaddr) -> Multiaddr:
25+
"""
26+
Clean a multiaddr by removing the '/p2p/' part if it exists.
27+
28+
Args:
29+
addr: The multiaddr to clean
30+
31+
Returns:
32+
The cleaned multiaddr
33+
"""
34+
return Multiaddr.join(
35+
*(
36+
addr.split()[:-1]
37+
if str(addr.split()[-1]).startswith("/p2p/")
38+
else addr.split()
39+
)
40+
)
41+
42+
43+
# logger = logging.getLogger("libp2p.identity.identify-test")
44+
logger = logging.getLogger(__name__)
2345

2446

2547
@pytest.mark.trio
@@ -29,8 +51,8 @@ async def test_identify_protocol(security_protocol):
2951
host_b,
3052
):
3153
# Here, host_b is the requester and host_a is the responder.
32-
# observed_addr represent host_bs address as observed by host_a
33-
# (i.e., the address from which host_bs request was received).
54+
# observed_addr represent host_b's address as observed by host_a
55+
# (i.e., the address from which host_b's request was received).
3456
stream = await host_b.new_stream(host_a.get_id(), (ID,))
3557
response = await stream.read()
3658
await stream.close()
@@ -59,13 +81,7 @@ async def test_identify_protocol(security_protocol):
5981
# TODO: use decapsulateCode(protocols('p2p').code)
6082
# when the Multiaddr class will implement it
6183
host_b_addr = host_b.get_addrs()[0]
62-
cleaned_addr = Multiaddr.join(
63-
*(
64-
host_b_addr.split()[:-1]
65-
if str(host_b_addr.split()[-1]).startswith("/p2p/")
66-
else host_b_addr.split()
67-
)
68-
)
84+
cleaned_addr = clean_multiaddr(host_b_addr)
6985

7086
logger.debug("observed_addr: %s", Multiaddr(identify_response.observed_addr))
7187
logger.debug("host_b.get_addrs()[0]: %s", host_b.get_addrs()[0])
@@ -77,3 +93,66 @@ async def test_identify_protocol(security_protocol):
7793

7894
# sanity check
7995
assert identify_response == _mk_identify_protobuf(host_a, cleaned_addr)
96+
97+
98+
@pytest.mark.trio
99+
async def test_complete_remote_address_delegation_chain(security_protocol):
100+
async with host_pair_factory(security_protocol=security_protocol) as (
101+
host_a,
102+
host_b,
103+
):
104+
logger.debug(
105+
"test_complete_remote_address_delegation_chain security_protocol: %s",
106+
security_protocol,
107+
)
108+
109+
# Create a stream from host_a to host_b
110+
stream = await host_a.new_stream(host_b.get_id(), (ID,))
111+
112+
# Get references to all components in the delegation chain
113+
mplex_stream = stream.muxed_stream
114+
swarm_conn = host_a.get_network().connections[host_b.get_id()]
115+
mplex = swarm_conn.muxed_conn
116+
secure_session = mplex.secured_conn
117+
raw_connection = secure_session.conn
118+
trio_tcp_stream = raw_connection.stream
119+
120+
# Get remote addresses at each layer
121+
stream_addr = stream.get_remote_address()
122+
muxed_stream_addr = stream.muxed_stream.get_remote_address()
123+
mplex_addr = mplex_stream.muxed_conn.get_remote_address()
124+
secure_session_addr = mplex.secured_conn.get_remote_address()
125+
raw_connection_addr = secure_session.conn.get_remote_address()
126+
trio_tcp_stream_addr = raw_connection.stream.get_remote_address()
127+
socket_addr = trio_tcp_stream.stream.socket.getpeername()
128+
129+
# Log all addresses
130+
logger.debug("NetStream address: %s", stream_addr)
131+
logger.debug("MplexStream address: %s", muxed_stream_addr)
132+
logger.debug("Mplex address: %s", mplex_addr)
133+
logger.debug("SecureSession address: %s", secure_session_addr)
134+
logger.debug("RawConnection address: %s", raw_connection_addr)
135+
logger.debug("TrioTCPStream address: %s", trio_tcp_stream_addr)
136+
logger.debug("Socket address: %s", socket_addr)
137+
138+
# Verify complete delegation chain
139+
assert (
140+
stream_addr
141+
== muxed_stream_addr
142+
== mplex_addr
143+
== secure_session_addr
144+
== raw_connection_addr
145+
== trio_tcp_stream_addr
146+
== socket_addr
147+
)
148+
149+
# Convert to multiaddr and verify it matches host_b's cleaned address
150+
remote_address_multiaddr = _remote_address_to_multiaddr(stream_addr)
151+
host_b_addr = clean_multiaddr(host_b.get_addrs()[0])
152+
153+
logger.debug("Remote address multiaddr: %s", remote_address_multiaddr)
154+
logger.debug("Host B cleaned address: %s", host_b_addr)
155+
156+
assert remote_address_multiaddr == host_b_addr
157+
158+
await stream.close()

0 commit comments

Comments
 (0)