2023-11-01 04:57:08 +00:00
|
|
|
|
"""Tests for the packet stream"""
|
|
|
|
|
|
2023-11-01 06:16:17 +00:00
|
|
|
|
from asyncio import Event, ensure_future, gather
|
2017-08-01 20:34:06 +00:00
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
import pytest
|
2018-02-04 21:18:36 +00:00
|
|
|
|
from secret_handshake.network import SHSDuplexStream
|
2023-11-01 06:16:17 +00:00
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
from ssb.packet_stream import PacketStream, PSMessageType
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _collect_messages(generator):
|
|
|
|
|
results = []
|
|
|
|
|
async for msg in generator:
|
|
|
|
|
results.append(msg)
|
|
|
|
|
return results
|
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
|
|
|
|
|
MSG_BODY_1 = (
|
|
|
|
|
b'{"previous":"%KTGP6W8vF80McRAZHYDWuKOD0KlNyKSq6Gb42iuV7Iw=.sha256","author":"@1+Iwm79DKvVBqYKFkhT6fWRbA'
|
|
|
|
|
b'VvNNVH4F2BSxwhYmx8=.ed25519","sequence":116,"timestamp":1496696699331,"hash":"sha256","content":{"type"'
|
|
|
|
|
b':"post","channel":"crypto","text":"Does anybody know any good resources (e.g. books) to learn cryptogra'
|
|
|
|
|
b"phy? I'm not speaking of basic concepts (e.g. what's a private key) but the actual mathematics behind"
|
|
|
|
|
b' the whole thing.\\nI have a copy of the \\"Handbook of Applied Cryptography\\" on my bookshelf but I f'
|
|
|
|
|
b'ound it too long/hard to follow. Are there any better alternatives?","mentions":[]},"signature":"hqKePb'
|
|
|
|
|
b'bTXWxEi1njDnOWFsL0M0AoNoWyBFgNE6KXj//DThepaZSy9vRbygDHX5uNmCdyOrsQrwZsZhmUYKwtDQ==.sig.ed25519"}'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
MSG_BODY_2 = (
|
|
|
|
|
b'{"previous":"%iQRhPyqmNLpGaO1Tpm1I22jqnUEwRwkCTDbwAGtM+lY=.sha256","author":"@1+Iwm79DKvVBqYKFkhT6fWRbA'
|
|
|
|
|
b'VvNNVH4F2BSxwhYmx8=.ed25519","sequence":103,"timestamp":1496674211806,"hash":"sha256","content":{"type"'
|
|
|
|
|
b':"post","channel":"git-ssb","text":"Is it only me or `git.scuttlebot.io` is timing out?\\n\\nE.g. try a'
|
|
|
|
|
b'ccessing %vZCTqraoqKBKNZeATErXEtnoEr+wnT3p8tT+vL+29I4=.sha256","mentions":[{"link":"%vZCTqraoqKBKNZeATE'
|
|
|
|
|
b'rXEtnoEr+wnT3p8tT+vL+29I4=.sha256"}]},"signature":"+i4U0HUGDDEyNoNr2NIROPnT3WQj3RuTaIhY5koWW8f0vwr4tZsY'
|
|
|
|
|
b'mAkqqMwFWfP+eBIbc7DZ835er6r6h9CwAg==.sig.ed25519"}'
|
|
|
|
|
)
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
class MockSHSSocket(SHSDuplexStream):
|
2023-11-01 04:57:08 +00:00
|
|
|
|
"""A mocked SHS socket"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
2017-07-30 20:29:43 +00:00
|
|
|
|
self.input = []
|
|
|
|
|
self.output = []
|
2017-07-31 21:17:32 +00:00
|
|
|
|
self.is_connected = False
|
|
|
|
|
self._on_connect = []
|
|
|
|
|
|
|
|
|
|
def on_connect(self, cb):
|
2023-11-01 04:57:08 +00:00
|
|
|
|
"""Set the on_connect callback"""
|
|
|
|
|
|
2017-07-31 21:17:32 +00:00
|
|
|
|
self._on_connect.append(cb)
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
|
|
|
|
async def read(self):
|
2023-11-01 04:57:08 +00:00
|
|
|
|
"""Read data from the socket"""
|
|
|
|
|
|
2017-07-30 20:29:43 +00:00
|
|
|
|
if not self.input:
|
|
|
|
|
raise StopAsyncIteration
|
|
|
|
|
return self.input.pop(0)
|
|
|
|
|
|
|
|
|
|
def write(self, data):
|
2023-11-01 04:57:08 +00:00
|
|
|
|
"""Write data to the socket"""
|
|
|
|
|
|
2017-07-30 20:29:43 +00:00
|
|
|
|
self.output.append(data)
|
|
|
|
|
|
2023-11-01 04:57:08 +00:00
|
|
|
|
def feed(self, input_):
|
|
|
|
|
"""Get the connection’s feed"""
|
|
|
|
|
|
|
|
|
|
self.input += input_
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
|
|
|
|
def get_output(self):
|
2023-11-01 04:57:08 +00:00
|
|
|
|
"""Get the output of a call"""
|
|
|
|
|
|
2017-07-30 20:29:43 +00:00
|
|
|
|
while True:
|
|
|
|
|
if not self.output:
|
|
|
|
|
break
|
|
|
|
|
yield self.output.pop(0)
|
|
|
|
|
|
2017-07-31 21:17:32 +00:00
|
|
|
|
def disconnect(self):
|
2023-11-01 04:57:08 +00:00
|
|
|
|
"""Disconnect from the remote party"""
|
|
|
|
|
|
2017-07-31 21:17:32 +00:00
|
|
|
|
self.is_connected = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MockSHSClient(MockSHSSocket):
|
2023-11-01 04:57:08 +00:00
|
|
|
|
"""A mocked SHS client"""
|
|
|
|
|
|
2017-08-01 20:32:23 +00:00
|
|
|
|
async def connect(self):
|
2023-11-01 04:57:08 +00:00
|
|
|
|
"""Connect to a SHS server"""
|
|
|
|
|
|
2017-08-01 20:32:23 +00:00
|
|
|
|
self.is_connected = True
|
2023-11-01 04:57:08 +00:00
|
|
|
|
|
2017-08-01 20:32:23 +00:00
|
|
|
|
for cb in self._on_connect:
|
|
|
|
|
await cb()
|
2017-07-31 21:17:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MockSHSServer(MockSHSSocket):
|
2023-11-01 04:57:08 +00:00
|
|
|
|
"""A mocked SHS server"""
|
|
|
|
|
|
2017-08-01 20:32:23 +00:00
|
|
|
|
def listen(self):
|
2023-11-01 04:57:08 +00:00
|
|
|
|
"""Listen for new connections"""
|
|
|
|
|
|
2017-08-01 20:32:23 +00:00
|
|
|
|
self.is_connected = True
|
2023-11-01 04:57:08 +00:00
|
|
|
|
|
2017-08-01 20:32:23 +00:00
|
|
|
|
for cb in self._on_connect:
|
2018-02-04 21:18:36 +00:00
|
|
|
|
ensure_future(cb())
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-11-01 04:57:08 +00:00
|
|
|
|
def ps_client(event_loop): # pylint: disable=unused-argument
|
|
|
|
|
"""Fixture to provide a mocked SHS client"""
|
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
return MockSHSClient()
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2017-07-31 21:17:32 +00:00
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-11-01 04:57:08 +00:00
|
|
|
|
def ps_server(event_loop): # pylint: disable=unused-argument
|
|
|
|
|
"""Fixture to provide a mocked SHS server"""
|
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
return MockSHSServer()
|
2017-07-31 21:53:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-11-01 04:57:08 +00:00
|
|
|
|
async def test_on_connect(ps_server): # pylint: disable=redefined-outer-name
|
|
|
|
|
"""Test the on_connect callback functionality"""
|
|
|
|
|
|
2017-07-31 21:53:08 +00:00
|
|
|
|
called = Event()
|
|
|
|
|
|
|
|
|
|
async def _on_connect():
|
|
|
|
|
called.set()
|
|
|
|
|
|
|
|
|
|
ps_server.on_connect(_on_connect)
|
|
|
|
|
ps_server.listen()
|
|
|
|
|
await called.wait()
|
|
|
|
|
assert ps_server.is_connected
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-11-01 04:57:08 +00:00
|
|
|
|
async def test_message_decoding(ps_client): # pylint: disable=redefined-outer-name
|
|
|
|
|
"""Test message decoding"""
|
|
|
|
|
|
2017-08-01 20:32:23 +00:00
|
|
|
|
await ps_client.connect()
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
ps = PacketStream(ps_client)
|
|
|
|
|
|
|
|
|
|
assert ps.is_connected
|
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
ps_client.feed(
|
|
|
|
|
[
|
|
|
|
|
b"\n\x00\x00\x00\x9a\x00\x00\x04\xfb",
|
|
|
|
|
b'{"name":["createHistoryStream"],"args":[{"id":"@omgyp7Pnrw+Qm0I6T6Fh5VvnKmodMXwnxTIesW2DgMg=.ed25519",'
|
|
|
|
|
b'"seq":10,"live":true,"keys":false}],"type":"source"}',
|
|
|
|
|
]
|
|
|
|
|
)
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
messages = await _collect_messages(ps)
|
2017-07-30 20:29:43 +00:00
|
|
|
|
assert len(messages) == 1
|
|
|
|
|
assert messages[0].type == PSMessageType.JSON
|
|
|
|
|
assert messages[0].body == {
|
2023-11-01 04:04:43 +00:00
|
|
|
|
"name": ["createHistoryStream"],
|
|
|
|
|
"args": [
|
|
|
|
|
{"id": "@omgyp7Pnrw+Qm0I6T6Fh5VvnKmodMXwnxTIesW2DgMg=.ed25519", "seq": 10, "live": True, "keys": False}
|
2017-07-30 20:29:43 +00:00
|
|
|
|
],
|
2023-11-01 04:04:43 +00:00
|
|
|
|
"type": "source",
|
2017-07-30 20:29:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-11-01 04:57:08 +00:00
|
|
|
|
async def test_message_encoding(ps_client): # pylint: disable=redefined-outer-name
|
|
|
|
|
"""Test message encoding"""
|
|
|
|
|
|
2017-08-01 20:32:23 +00:00
|
|
|
|
await ps_client.connect()
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
ps = PacketStream(ps_client)
|
|
|
|
|
|
|
|
|
|
assert ps.is_connected
|
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
ps.send(
|
|
|
|
|
{
|
|
|
|
|
"name": ["createHistoryStream"],
|
|
|
|
|
"args": [
|
|
|
|
|
{"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False}
|
|
|
|
|
],
|
|
|
|
|
"type": "source",
|
|
|
|
|
},
|
|
|
|
|
stream=True,
|
|
|
|
|
)
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
header, body = list(ps_client.get_output())
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
assert header == b"\x0a\x00\x00\x00\xa6\x00\x00\x00\x01"
|
|
|
|
|
assert json.loads(body.decode("utf-8")) == {
|
2017-08-01 20:32:23 +00:00
|
|
|
|
"name": ["createHistoryStream"],
|
|
|
|
|
"args": [
|
|
|
|
|
{"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False}
|
|
|
|
|
],
|
2023-11-01 04:04:43 +00:00
|
|
|
|
"type": "source",
|
2017-08-01 20:32:23 +00:00
|
|
|
|
}
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-11-01 04:57:08 +00:00
|
|
|
|
async def test_message_stream(ps_client, mocker): # pylint: disable=redefined-outer-name
|
|
|
|
|
"""Test requesting a history stream"""
|
|
|
|
|
|
2017-08-01 20:32:23 +00:00
|
|
|
|
await ps_client.connect()
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
ps = PacketStream(ps_client)
|
2023-11-01 04:04:43 +00:00
|
|
|
|
mocker.patch.object(ps, "register_handler", wraps=ps.register_handler)
|
2018-02-04 21:18:36 +00:00
|
|
|
|
|
|
|
|
|
assert ps.is_connected
|
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
ps.send(
|
|
|
|
|
{
|
|
|
|
|
"name": ["createHistoryStream"],
|
|
|
|
|
"args": [
|
|
|
|
|
{"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False}
|
|
|
|
|
],
|
|
|
|
|
"type": "source",
|
|
|
|
|
},
|
|
|
|
|
stream=True,
|
|
|
|
|
)
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
assert ps.req_counter == 2
|
2023-11-01 04:57:08 +00:00
|
|
|
|
assert ps.register_handler.call_count == 1 # pylint: disable=no-member
|
|
|
|
|
handler = list(ps._event_map.values())[0][1] # pylint: disable=protected-access
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
mock_process = mocker.patch.object(handler, "process")
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
ps_client.feed([b"\n\x00\x00\x02\xc5\xff\xff\xff\xff", MSG_BODY_1])
|
2023-11-01 05:22:15 +00:00
|
|
|
|
msg = await ps.read()
|
|
|
|
|
assert mock_process.await_count == 1
|
|
|
|
|
|
|
|
|
|
# responses have negative req
|
|
|
|
|
assert msg.req == -1
|
2023-11-01 04:04:43 +00:00
|
|
|
|
assert msg.body["previous"] == "%KTGP6W8vF80McRAZHYDWuKOD0KlNyKSq6Gb42iuV7Iw=.sha256"
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 05:22:15 +00:00
|
|
|
|
assert ps.req_counter == 2
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
stream_handler = ps.send(
|
|
|
|
|
{
|
|
|
|
|
"name": ["createHistoryStream"],
|
|
|
|
|
"args": [
|
|
|
|
|
{"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False}
|
|
|
|
|
],
|
|
|
|
|
"type": "source",
|
|
|
|
|
},
|
|
|
|
|
stream=True,
|
|
|
|
|
)
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
assert ps.req_counter == 3
|
2023-11-01 04:57:08 +00:00
|
|
|
|
assert ps.register_handler.call_count == 2 # pylint: disable=no-member
|
|
|
|
|
handler = list(ps._event_map.values())[1][1] # pylint: disable=protected-access
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
mock_process = mocker.patch.object(handler, "process", wraps=handler.process)
|
|
|
|
|
|
|
|
|
|
ps_client.feed(
|
|
|
|
|
[b"\n\x00\x00\x02\xc5\xff\xff\xff\xfe", MSG_BODY_1, b"\x0e\x00\x00\x023\xff\xff\xff\xfe", MSG_BODY_2]
|
|
|
|
|
)
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 05:22:15 +00:00
|
|
|
|
# execute both message polling and response handling loops
|
|
|
|
|
collected, handled = await gather(_collect_messages(ps), _collect_messages(stream_handler))
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 05:22:15 +00:00
|
|
|
|
# No messages collected, since they're all responses
|
2023-11-01 05:03:06 +00:00
|
|
|
|
assert collected == [None, None]
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 05:22:15 +00:00
|
|
|
|
assert mock_process.call_count == 2
|
2017-07-30 20:29:43 +00:00
|
|
|
|
|
2023-11-01 05:22:15 +00:00
|
|
|
|
for msg in handled:
|
|
|
|
|
# responses have negative req
|
|
|
|
|
assert msg.req == -2
|
2017-07-31 21:17:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-11-01 04:57:08 +00:00
|
|
|
|
async def test_message_request(ps_server, mocker): # pylint: disable=redefined-outer-name
|
|
|
|
|
"""Test message sending"""
|
|
|
|
|
|
2017-07-31 21:53:08 +00:00
|
|
|
|
ps_server.listen()
|
2017-07-31 21:17:32 +00:00
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
ps = PacketStream(ps_server)
|
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
mocker.patch.object(ps, "register_handler", wraps=ps.register_handler)
|
2017-07-31 21:17:32 +00:00
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
ps.send({"name": ["whoami"], "args": []})
|
2017-07-31 21:17:32 +00:00
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
header, body = list(ps_server.get_output())
|
2023-11-01 04:04:43 +00:00
|
|
|
|
assert header == b"\x02\x00\x00\x00 \x00\x00\x00\x01"
|
|
|
|
|
assert json.loads(body.decode("utf-8")) == {"name": ["whoami"], "args": []}
|
2017-07-31 21:17:32 +00:00
|
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
|
assert ps.req_counter == 2
|
2023-11-01 04:57:08 +00:00
|
|
|
|
assert ps.register_handler.call_count == 1 # pylint: disable=no-member
|
|
|
|
|
handler = list(ps._event_map.values())[0][1] # pylint: disable=protected-access
|
2017-07-31 21:17:32 +00:00
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
|
mock_process = mocker.patch.object(handler, "process")
|
|
|
|
|
|
|
|
|
|
ps_server.feed(
|
|
|
|
|
[
|
|
|
|
|
b"\x02\x00\x00\x00>\xff\xff\xff\xff",
|
|
|
|
|
b'{"id":"@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519"}',
|
|
|
|
|
]
|
|
|
|
|
)
|
2023-11-01 05:22:15 +00:00
|
|
|
|
msg = await ps.read()
|
|
|
|
|
assert mock_process.await_count == 1
|
2017-07-31 21:17:32 +00:00
|
|
|
|
|
2023-11-01 05:22:15 +00:00
|
|
|
|
# responses have negative req
|
|
|
|
|
assert msg.req == -1
|
2023-11-01 04:04:43 +00:00
|
|
|
|
assert msg.body["id"] == "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519"
|
2023-11-01 05:22:15 +00:00
|
|
|
|
assert ps.req_counter == 2
|