2018-11-29 02:51:50 +08:00
|
|
|
import pytest
|
|
|
|
|
2019-09-20 16:17:13 +08:00
|
|
|
from libp2p.host.exceptions import StreamFailure
|
2019-12-01 17:43:14 +08:00
|
|
|
from libp2p.tools.factories import HostFactory
|
|
|
|
from libp2p.tools.utils import create_echo_stream_handler
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-12-01 17:43:14 +08:00
|
|
|
PROTOCOL_ECHO = "/echo/1.0.0"
|
|
|
|
PROTOCOL_POTATO = "/potato/1.0.0"
|
|
|
|
PROTOCOL_FOO = "/foo/1.0.0"
|
|
|
|
PROTOCOL_ROCK = "/rock/1.0.0"
|
|
|
|
|
|
|
|
ACK_PREFIX = "ack:"
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
async def perform_simple_test(
|
2019-12-01 17:43:14 +08:00
|
|
|
expected_selected_protocol,
|
|
|
|
protocols_for_client,
|
|
|
|
protocols_with_handlers,
|
2020-02-19 23:15:03 +08:00
|
|
|
security_protocol,
|
2019-08-01 06:00:12 +08:00
|
|
|
):
|
2020-02-19 23:15:03 +08:00
|
|
|
async with HostFactory.create_batch_and_listen(
|
|
|
|
2, security_protocol=security_protocol
|
|
|
|
) as hosts:
|
2019-12-01 17:43:14 +08:00
|
|
|
for protocol in protocols_with_handlers:
|
|
|
|
hosts[1].set_stream_handler(
|
|
|
|
protocol, create_echo_stream_handler(ACK_PREFIX)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Associate the peer with local ip address (see default parameters of Libp2p())
|
|
|
|
hosts[0].get_peerstore().add_addrs(hosts[1].get_id(), hosts[1].get_addrs(), 10)
|
|
|
|
stream = await hosts[0].new_stream(hosts[1].get_id(), protocols_for_client)
|
|
|
|
messages = ["hello" + str(x) for x in range(10)]
|
|
|
|
for message in messages:
|
|
|
|
expected_resp = "ack:" + message
|
|
|
|
await stream.write(message.encode())
|
|
|
|
response = (await stream.read(len(expected_resp))).decode()
|
|
|
|
assert response == expected_resp
|
|
|
|
|
|
|
|
assert expected_selected_protocol == stream.get_protocol()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.trio
|
2020-02-19 23:15:03 +08:00
|
|
|
async def test_single_protocol_succeeds(security_protocol):
|
2019-12-01 17:43:14 +08:00
|
|
|
expected_selected_protocol = PROTOCOL_ECHO
|
2019-08-14 05:36:42 +08:00
|
|
|
await perform_simple_test(
|
2019-12-01 17:43:14 +08:00
|
|
|
expected_selected_protocol,
|
|
|
|
[expected_selected_protocol],
|
|
|
|
[expected_selected_protocol],
|
2020-02-19 23:15:03 +08:00
|
|
|
security_protocol,
|
2019-08-14 05:36:42 +08:00
|
|
|
)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-12-01 17:43:14 +08:00
|
|
|
@pytest.mark.trio
|
2020-02-19 23:15:03 +08:00
|
|
|
async def test_single_protocol_fails(security_protocol):
|
2019-09-20 16:17:13 +08:00
|
|
|
with pytest.raises(StreamFailure):
|
2019-12-01 17:43:14 +08:00
|
|
|
await perform_simple_test(
|
2020-02-19 23:15:03 +08:00
|
|
|
"", [PROTOCOL_ECHO], [PROTOCOL_POTATO], security_protocol
|
2019-12-01 17:43:14 +08:00
|
|
|
)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-02-25 09:58:23 +08:00
|
|
|
# Cleanup not reached on error
|
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-12-01 17:43:14 +08:00
|
|
|
@pytest.mark.trio
|
2020-02-19 23:15:03 +08:00
|
|
|
async def test_multiple_protocol_first_is_valid_succeeds(security_protocol):
|
2019-12-01 17:43:14 +08:00
|
|
|
expected_selected_protocol = PROTOCOL_ECHO
|
|
|
|
protocols_for_client = [PROTOCOL_ECHO, PROTOCOL_POTATO]
|
|
|
|
protocols_for_listener = [PROTOCOL_FOO, PROTOCOL_ECHO]
|
2019-08-01 06:00:12 +08:00
|
|
|
await perform_simple_test(
|
2019-12-01 17:43:14 +08:00
|
|
|
expected_selected_protocol,
|
|
|
|
protocols_for_client,
|
|
|
|
protocols_for_listener,
|
2020-02-19 23:15:03 +08:00
|
|
|
security_protocol,
|
2019-08-01 06:00:12 +08:00
|
|
|
)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-12-01 17:43:14 +08:00
|
|
|
@pytest.mark.trio
|
2020-02-19 23:15:03 +08:00
|
|
|
async def test_multiple_protocol_second_is_valid_succeeds(security_protocol):
|
2019-12-01 17:43:14 +08:00
|
|
|
expected_selected_protocol = PROTOCOL_FOO
|
|
|
|
protocols_for_client = [PROTOCOL_ROCK, PROTOCOL_FOO]
|
|
|
|
protocols_for_listener = [PROTOCOL_FOO, PROTOCOL_ECHO]
|
2019-08-01 06:00:12 +08:00
|
|
|
await perform_simple_test(
|
2019-12-01 17:43:14 +08:00
|
|
|
expected_selected_protocol,
|
|
|
|
protocols_for_client,
|
|
|
|
protocols_for_listener,
|
2020-02-19 23:15:03 +08:00
|
|
|
security_protocol,
|
2019-08-01 06:00:12 +08:00
|
|
|
)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-12-01 17:43:14 +08:00
|
|
|
@pytest.mark.trio
|
2020-02-19 23:15:03 +08:00
|
|
|
async def test_multiple_protocol_fails(security_protocol):
|
2019-12-01 17:43:14 +08:00
|
|
|
protocols_for_client = [PROTOCOL_ROCK, PROTOCOL_FOO, "/bar/1.0.0"]
|
2018-11-29 02:51:50 +08:00
|
|
|
protocols_for_listener = ["/aspyn/1.0.0", "/rob/1.0.0", "/zx/1.0.0", "/alex/1.0.0"]
|
2019-09-20 16:17:13 +08:00
|
|
|
with pytest.raises(StreamFailure):
|
2019-12-01 17:43:14 +08:00
|
|
|
await perform_simple_test(
|
2020-02-19 23:15:03 +08:00
|
|
|
"", protocols_for_client, protocols_for_listener, security_protocol
|
2019-12-01 17:43:14 +08:00
|
|
|
)
|