2018-12-08 04:00:46 +08:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
from libp2p.transport.tcp.tcp import _multiaddr_from_socket
|
2018-12-08 04:00:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_multiaddr_from_socket():
|
|
|
|
def handler(r, w):
|
|
|
|
pass
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
server = await asyncio.start_server(handler, "127.0.0.1", 8000)
|
|
|
|
assert str(_multiaddr_from_socket(server.sockets[0])) == "/ip4/127.0.0.1/tcp/8000"
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
server = await asyncio.start_server(handler, "127.0.0.1", 0)
|
2018-12-08 04:00:46 +08:00
|
|
|
addr = _multiaddr_from_socket(server.sockets[0])
|
2019-08-01 06:00:12 +08:00
|
|
|
assert addr.value_for_protocol("ip4") == "127.0.0.1"
|
|
|
|
port = addr.value_for_protocol("tcp")
|
2018-12-08 04:00:46 +08:00
|
|
|
assert int(port) > 0
|