ci: Use black instead of flake8

It results in mostly the same style, plus it’s configurable via pyproject.toml.
This commit is contained in:
Gergely Polonkai 2023-11-01 05:04:43 +01:00
parent 50442c56fe
commit d1a0510734
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
15 changed files with 473 additions and 256 deletions

View File

@ -16,6 +16,14 @@ repos:
pass_filenames: false pass_filenames: false
language: system language: system
stages: [push] stages: [push]
- id: black
name: black
description: "Black: The uncompromising Python code formatter"
entry: poetry run black
args: ["--diff"]
language: system
require_serial: true
types_or: [python, pyi]
- id: reuse - id: reuse
name: reuse name: reuse
entry: poetry run reuse entry: poetry run reuse

View File

@ -3,6 +3,6 @@ Upstream-Name: PySSB
Upstream-Contact: Pedro Ferreira <pedro@dete.st> Upstream-Contact: Pedro Ferreira <pedro@dete.st>
Source: https://github.com/pferreir/pyssb Source: https://github.com/pferreir/pyssb
Files: AUTHORS README.rst poetry.lock .gitignore .pre-commit-config.yaml setup.cfg Files: AUTHORS README.rst poetry.lock .gitignore .pre-commit-config.yaml
Copyright: 2023 Gergely Polonkai <python-ssb@gergely.polonkai.eu> Copyright: 2023 Gergely Polonkai <python-ssb@gergely.polonkai.eu>
License: CC0-1.0 License: CC0-1.0

View File

@ -39,76 +39,86 @@ import base64
api = MuxRPCAPI() api = MuxRPCAPI()
@api.define('createHistoryStream') @api.define("createHistoryStream")
def create_history_stream(connection, msg): def create_history_stream(connection, msg):
print('create_history_stream', msg) print("create_history_stream", msg)
# msg = PSMessage(PSMessageType.JSON, True, stream=True, end_err=True, req=-req) # msg = PSMessage(PSMessageType.JSON, True, stream=True, end_err=True, req=-req)
# connection.write(msg) # connection.write(msg)
@api.define('blobs.createWants') @api.define("blobs.createWants")
def create_wants(connection, msg): def create_wants(connection, msg):
print('create_wants', msg) print("create_wants", msg)
async def test_client(): async def test_client():
async for msg in api.call('createHistoryStream', [{ async for msg in api.call(
'id': "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "createHistoryStream",
'seq': 1, [
'live': False, {
'keys': False "id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519",
}], 'source'): "seq": 1,
print('> RESPONSE:', msg) "live": False,
"keys": False,
}
],
"source",
):
print("> RESPONSE:", msg)
try: try:
print('> RESPONSE:', await api.call('whoami', [], 'sync')) print("> RESPONSE:", await api.call("whoami", [], "sync"))
except MuxRPCAPIException as e: except MuxRPCAPIException as e:
print(e) print(e)
handler = api.call('gossip.ping', [], 'duplex') handler = api.call("gossip.ping", [], "duplex")
handler.send(struct.pack('l', int(time.time() * 1000)), msg_type=PSMessageType.BUFFER) handler.send(struct.pack("l", int(time.time() * 1000)), msg_type=PSMessageType.BUFFER)
async for msg in handler: async for msg in handler:
print('> RESPONSE:', msg) print("> RESPONSE:", msg)
handler.send(True, end=True) handler.send(True, end=True)
break break
img_data = b'' img_data = b""
async for msg in api.call('blobs.get', ['&kqZ52sDcJSHOx7m4Ww80kK1KIZ65gpGnqwZlfaIVWWM=.sha256'], 'source'): async for msg in api.call("blobs.get", ["&kqZ52sDcJSHOx7m4Ww80kK1KIZ65gpGnqwZlfaIVWWM=.sha256"], "source"):
if msg.type.name == 'BUFFER': if msg.type.name == "BUFFER":
img_data += msg.data img_data += msg.data
if msg.type.name == 'JSON' and msg.data == b'true': if msg.type.name == "JSON" and msg.data == b"true":
assert (base64.b64encode(hashlib.sha256(img_data).digest()) == assert (
b'kqZ52sDcJSHOx7m4Ww80kK1KIZ65gpGnqwZlfaIVWWM=') base64.b64encode(hashlib.sha256(img_data).digest()) == b"kqZ52sDcJSHOx7m4Ww80kK1KIZ65gpGnqwZlfaIVWWM="
with open('./ub1k.jpg', 'wb') as f: )
with open("./ub1k.jpg", "wb") as f:
f.write(img_data) f.write(img_data)
async def main(): async def main():
client = SHSClient('127.0.0.1', 8008, keypair, bytes(keypair.verify_key)) client = SHSClient("127.0.0.1", 8008, keypair, bytes(keypair.verify_key))
packet_stream = PacketStream(client) packet_stream = PacketStream(client)
await client.open() await client.open()
api.add_connection(packet_stream) api.add_connection(packet_stream)
await gather(ensure_future(api), test_client()) await gather(ensure_future(api), test_client())
if __name__ == '__main__': if __name__ == "__main__":
# create console handler and set level to debug # create console handler and set level to debug
ch = logging.StreamHandler() ch = logging.StreamHandler()
ch.setLevel(logging.INFO) ch.setLevel(logging.INFO)
# create formatter # create formatter
formatter = ColoredFormatter('%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - ' formatter = ColoredFormatter(
'%(cyan)s%(message)s%(reset)s') "%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - %(cyan)s%(message)s%(reset)s"
)
# add formatter to ch # add formatter to ch
ch.setFormatter(formatter) ch.setFormatter(formatter)
# add ch to logger # add ch to logger
logger = logging.getLogger('packet_stream') logger = logging.getLogger("packet_stream")
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
logger.addHandler(ch) logger.addHandler(ch)
keypair = load_ssb_secret()['keypair'] keypair = load_ssb_secret()["keypair"]
loop = get_event_loop() loop = get_event_loop()
loop.run_until_complete(main()) loop.run_until_complete(main())

View File

@ -37,31 +37,32 @@ async def on_connect(conn):
packet_stream = PacketStream(conn) packet_stream = PacketStream(conn)
api.add_connection(packet_stream) api.add_connection(packet_stream)
print('connect', conn) print("connect", conn)
async for msg in packet_stream: async for msg in packet_stream:
print(msg) print(msg)
async def main(): async def main():
server = SHSServer('127.0.0.1', 8008, load_ssb_secret()['keypair']) server = SHSServer("127.0.0.1", 8008, load_ssb_secret()["keypair"])
server.on_connect(on_connect) server.on_connect(on_connect)
await server.listen() await server.listen()
if __name__ == '__main__': if __name__ == "__main__":
# create console handler and set level to debug # create console handler and set level to debug
ch = logging.StreamHandler() ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG) ch.setLevel(logging.DEBUG)
# create formatter # create formatter
formatter = ColoredFormatter('%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - ' formatter = ColoredFormatter(
'%(cyan)s%(message)s%(reset)s') "%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - " "%(cyan)s%(message)s%(reset)s"
)
# add formatter to ch # add formatter to ch
ch.setFormatter(formatter) ch.setFormatter(formatter)
# add ch to logger # add ch to logger
logger = logging.getLogger('packet_stream') logger = logging.getLogger("packet_stream")
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
logger.addHandler(ch) logger.addHandler(ch)

108
poetry.lock generated
View File

@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. # This file is automatically @generated by Poetry 1.7.0 and should not be changed by hand.
[[package]] [[package]]
name = "alabaster" name = "alabaster"
@ -53,6 +53,48 @@ setuptools = {version = "*", markers = "python_version >= \"3.12\""}
[package.extras] [package.extras]
dev = ["freezegun (>=1.0,<2.0)", "pytest (>=6.0)", "pytest-cov"] dev = ["freezegun (>=1.0,<2.0)", "pytest (>=6.0)", "pytest-cov"]
[[package]]
name = "black"
version = "23.11.0"
description = "The uncompromising code formatter."
optional = false
python-versions = ">=3.8"
files = [
{file = "black-23.11.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:dbea0bb8575c6b6303cc65017b46351dc5953eea5c0a59d7b7e3a2d2f433a911"},
{file = "black-23.11.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:412f56bab20ac85927f3a959230331de5614aecda1ede14b373083f62ec24e6f"},
{file = "black-23.11.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d136ef5b418c81660ad847efe0e55c58c8208b77a57a28a503a5f345ccf01394"},
{file = "black-23.11.0-cp310-cp310-win_amd64.whl", hash = "sha256:6c1cac07e64433f646a9a838cdc00c9768b3c362805afc3fce341af0e6a9ae9f"},
{file = "black-23.11.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:cf57719e581cfd48c4efe28543fea3d139c6b6f1238b3f0102a9c73992cbb479"},
{file = "black-23.11.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:698c1e0d5c43354ec5d6f4d914d0d553a9ada56c85415700b81dc90125aac244"},
{file = "black-23.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:760415ccc20f9e8747084169110ef75d545f3b0932ee21368f63ac0fee86b221"},
{file = "black-23.11.0-cp311-cp311-win_amd64.whl", hash = "sha256:58e5f4d08a205b11800332920e285bd25e1a75c54953e05502052738fe16b3b5"},
{file = "black-23.11.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:45aa1d4675964946e53ab81aeec7a37613c1cb71647b5394779e6efb79d6d187"},
{file = "black-23.11.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:4c44b7211a3a0570cc097e81135faa5f261264f4dfaa22bd5ee2875a4e773bd6"},
{file = "black-23.11.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2a9acad1451632021ee0d146c8765782a0c3846e0e0ea46659d7c4f89d9b212b"},
{file = "black-23.11.0-cp38-cp38-win_amd64.whl", hash = "sha256:fc7f6a44d52747e65a02558e1d807c82df1d66ffa80a601862040a43ec2e3142"},
{file = "black-23.11.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:7f622b6822f02bfaf2a5cd31fdb7cd86fcf33dab6ced5185c35f5db98260b055"},
{file = "black-23.11.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:250d7e60f323fcfc8ea6c800d5eba12f7967400eb6c2d21ae85ad31c204fb1f4"},
{file = "black-23.11.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5133f5507007ba08d8b7b263c7aa0f931af5ba88a29beacc4b2dc23fcefe9c06"},
{file = "black-23.11.0-cp39-cp39-win_amd64.whl", hash = "sha256:421f3e44aa67138ab1b9bfbc22ee3780b22fa5b291e4db8ab7eee95200726b07"},
{file = "black-23.11.0-py3-none-any.whl", hash = "sha256:54caaa703227c6e0c87b76326d0862184729a69b73d3b7305b6288e1d830067e"},
{file = "black-23.11.0.tar.gz", hash = "sha256:4c68855825ff432d197229846f971bc4d6666ce90492e5b02013bcaca4d9ab05"},
]
[package.dependencies]
click = ">=8.0.0"
mypy-extensions = ">=0.4.3"
packaging = ">=22.0"
pathspec = ">=0.9.0"
platformdirs = ">=2"
tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""}
typing-extensions = {version = ">=4.0.1", markers = "python_version < \"3.11\""}
[package.extras]
colorama = ["colorama (>=0.4.3)"]
d = ["aiohttp (>=3.7.4)"]
jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"]
uvloop = ["uvloop (>=0.15.2)"]
[[package]] [[package]]
name = "certifi" name = "certifi"
version = "2023.7.22" version = "2023.7.22"
@ -244,6 +286,20 @@ toml = "*"
[package.extras] [package.extras]
test = ["mock"] test = ["mock"]
[[package]]
name = "click"
version = "8.1.7"
description = "Composable command line interface toolkit"
optional = false
python-versions = ">=3.7"
files = [
{file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"},
{file = "click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de"},
]
[package.dependencies]
colorama = {version = "*", markers = "platform_system == \"Windows\""}
[[package]] [[package]]
name = "colorama" name = "colorama"
version = "0.4.6" version = "0.4.6"
@ -554,6 +610,17 @@ files = [
{file = "MarkupSafe-2.1.3.tar.gz", hash = "sha256:af598ed32d6ae86f1b747b82783958b1a4ab8f617b06fe68795c7f026abbdcad"}, {file = "MarkupSafe-2.1.3.tar.gz", hash = "sha256:af598ed32d6ae86f1b747b82783958b1a4ab8f617b06fe68795c7f026abbdcad"},
] ]
[[package]]
name = "mypy-extensions"
version = "1.0.0"
description = "Type system extensions for programs checked with the mypy type checker."
optional = false
python-versions = ">=3.5"
files = [
{file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"},
{file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"},
]
[[package]] [[package]]
name = "packaging" name = "packaging"
version = "23.2" version = "23.2"
@ -565,6 +632,17 @@ files = [
{file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"}, {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"},
] ]
[[package]]
name = "pathspec"
version = "0.11.2"
description = "Utility library for gitignore style pattern matching of file paths."
optional = false
python-versions = ">=3.7"
files = [
{file = "pathspec-0.11.2-py3-none-any.whl", hash = "sha256:1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20"},
{file = "pathspec-0.11.2.tar.gz", hash = "sha256:e0d8d0ac2f12da61956eb2306b69f9469b42f4deb0f3cb6ed47b9cce9996ced3"},
]
[[package]] [[package]]
name = "pep257" name = "pep257"
version = "0.7.0" version = "0.7.0"
@ -576,6 +654,21 @@ files = [
{file = "pep257-0.7.0.tar.gz", hash = "sha256:f3d67547f5617a9cfeb4b8097ed94a954888315defaf6e9b518ff1719363bf03"}, {file = "pep257-0.7.0.tar.gz", hash = "sha256:f3d67547f5617a9cfeb4b8097ed94a954888315defaf6e9b518ff1719363bf03"},
] ]
[[package]]
name = "platformdirs"
version = "4.0.0"
description = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"."
optional = false
python-versions = ">=3.7"
files = [
{file = "platformdirs-4.0.0-py3-none-any.whl", hash = "sha256:118c954d7e949b35437270383a3f2531e99dd93cf7ce4dc8340d3356d30f173b"},
{file = "platformdirs-4.0.0.tar.gz", hash = "sha256:cb633b2bcf10c51af60beb0ab06d2f1d69064b43abf4c185ca6b28865f3f9731"},
]
[package.extras]
docs = ["furo (>=2023.7.26)", "proselint (>=0.13)", "sphinx (>=7.1.1)", "sphinx-autodoc-typehints (>=1.24)"]
test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.4)", "pytest-cov (>=4.1)", "pytest-mock (>=3.11.1)"]
[[package]] [[package]]
name = "pluggy" name = "pluggy"
version = "1.3.0" version = "1.3.0"
@ -1091,6 +1184,17 @@ files = [
{file = "tomlkit-0.12.1.tar.gz", hash = "sha256:38e1ff8edb991273ec9f6181244a6a391ac30e9f5098e7535640ea6be97a7c86"}, {file = "tomlkit-0.12.1.tar.gz", hash = "sha256:38e1ff8edb991273ec9f6181244a6a391ac30e9f5098e7535640ea6be97a7c86"},
] ]
[[package]]
name = "typing-extensions"
version = "4.8.0"
description = "Backported and Experimental Type Hints for Python 3.8+"
optional = false
python-versions = ">=3.8"
files = [
{file = "typing_extensions-4.8.0-py3-none-any.whl", hash = "sha256:8f92fc8806f9a6b641eaa5318da32b44d401efaac0f6678c9bc448ba3605faa0"},
{file = "typing_extensions-4.8.0.tar.gz", hash = "sha256:df8e4339e9cb77357558cbdbceca33c303714cf861d1eef15e1070055ae8b7ef"},
]
[[package]] [[package]]
name = "urllib3" name = "urllib3"
version = "2.0.7" version = "2.0.7"
@ -1137,4 +1241,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.9" python-versions = "^3.9"
content-hash = "d61fa17ada60d932d45c104bf4d45b79411c743d3eb129f65751c8d57c4bea15" content-hash = "68f8505b0bc199dbcf9d3ea4ae37ee43f36128d05427ea99bd3a09fb201bcd72"

View File

@ -19,7 +19,9 @@ simplejson = "3.16.0"
colorlog = "^6.7.0" colorlog = "^6.7.0"
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]
black = "^23.10.1"
check-manifest = "^0.39" check-manifest = "^0.39"
commitizen = "^3.12.0"
coverage = "^7.3.2" coverage = "^7.3.2"
isort = "^4.3.20" isort = "^4.3.20"
pep257 = "^0.7.0" pep257 = "^0.7.0"
@ -27,11 +29,13 @@ pytest = "^7.4.3"
pytest-asyncio = "^0.21.1" pytest-asyncio = "^0.21.1"
pytest-cov = "^4.1.0" pytest-cov = "^4.1.0"
pytest-mock = "^3.12.0" pytest-mock = "^3.12.0"
commitizen = "^3.12.0"
[tool.poetry.group.docs.dependencies] [tool.poetry.group.docs.dependencies]
Sphinx = "^2.1.1" Sphinx = "^2.1.1"
[tool.black]
line-length = 120
[tool.coverage.run] [tool.coverage.run]
branch = true branch = true

View File

@ -1,2 +0,0 @@
[flake8]
max-line-length=120

View File

@ -22,4 +22,4 @@
from .models import Feed, LocalFeed, Message, LocalMessage, NoPrivateKeyException from .models import Feed, LocalFeed, Message, LocalMessage, NoPrivateKeyException
__all__ = ('Feed', 'LocalFeed', 'Message', 'LocalMessage', 'NoPrivateKeyException') __all__ = ("Feed", "LocalFeed", "Message", "LocalMessage", "NoPrivateKeyException")

View File

@ -30,7 +30,7 @@ from simplejson import dumps, loads
from ssb.util import tag from ssb.util import tag
OrderedMsg = namedtuple('OrderedMsg', ('previous', 'author', 'sequence', 'timestamp', 'hash', 'content')) OrderedMsg = namedtuple("OrderedMsg", ("previous", "author", "sequence", "timestamp", "hash", "content"))
class NoPrivateKeyException(Exception): class NoPrivateKeyException(Exception):
@ -52,10 +52,10 @@ class Feed(object):
@property @property
def id(self): def id(self):
return tag(self.public_key).decode('ascii') return tag(self.public_key).decode("ascii")
def sign(self, msg): def sign(self, msg):
raise NoPrivateKeyException('Cannot use remote identity to sign (no private key!)') raise NoPrivateKeyException("Cannot use remote identity to sign (no private key!)")
class LocalFeed(Feed): class LocalFeed(Feed):
@ -90,24 +90,26 @@ class Message(object):
@classmethod @classmethod
def parse(cls, data, feed): def parse(cls, data, feed):
obj = loads(data, object_pairs_hook=OrderedDict) obj = loads(data, object_pairs_hook=OrderedDict)
msg = cls(feed, obj['content'], timestamp=obj['timestamp']) msg = cls(feed, obj["content"], timestamp=obj["timestamp"])
return msg return msg
def serialize(self, add_signature=True): def serialize(self, add_signature=True):
return dumps(self.to_dict(add_signature=add_signature), indent=2).encode('utf-8') return dumps(self.to_dict(add_signature=add_signature), indent=2).encode("utf-8")
def to_dict(self, add_signature=True): def to_dict(self, add_signature=True):
obj = to_ordered({ obj = to_ordered(
'previous': self.previous.key if self.previous else None, {
'author': self.feed.id, "previous": self.previous.key if self.previous else None,
'sequence': self.sequence, "author": self.feed.id,
'timestamp': self.timestamp, "sequence": self.sequence,
'hash': 'sha256', "timestamp": self.timestamp,
'content': self.content "hash": "sha256",
}) "content": self.content,
}
)
if add_signature: if add_signature:
obj['signature'] = self.signature obj["signature"] = self.signature
return obj return obj
def verify(self, signature): def verify(self, signature):
@ -116,11 +118,11 @@ class Message(object):
@property @property
def hash(self): def hash(self):
hash = sha256(self.serialize()).digest() hash = sha256(self.serialize()).digest()
return b64encode(hash).decode('ascii') + '.sha256' return b64encode(hash).decode("ascii") + ".sha256"
@property @property
def key(self): def key(self):
return '%' + self.hash return "%" + self.hash
class LocalMessage(Message): class LocalMessage(Message):
@ -144,4 +146,4 @@ class LocalMessage(Message):
def _sign(self): def _sign(self):
# ensure ordering of keys and indentation of 2 characters, like ssb-keys # ensure ordering of keys and indentation of 2 characters, like ssb-keys
data = self.serialize(add_signature=False) data = self.serialize(add_signature=False)
return (b64encode(bytes(self.feed.sign(data))) + b'.sig.ed25519').decode('ascii') return (b64encode(bytes(self.feed.sign(data))) + b".sig.ed25519").decode("ascii")

View File

@ -34,8 +34,8 @@ class MuxRPCAPIException(Exception):
class MuxRPCHandler(object): class MuxRPCHandler(object):
def check_message(self, msg): def check_message(self, msg):
body = msg.body body = msg.body
if isinstance(body, dict) and 'name' in body and body['name'] == 'Error': if isinstance(body, dict) and "name" in body and body["name"] == "Error":
raise MuxRPCAPIException(body['message']) raise MuxRPCAPIException(body["message"])
class MuxRPCRequestHandler(MuxRPCHandler): class MuxRPCRequestHandler(MuxRPCHandler):
@ -43,7 +43,7 @@ class MuxRPCRequestHandler(MuxRPCHandler):
self.ps_handler = ps_handler self.ps_handler = ps_handler
def __await__(self): def __await__(self):
msg = (yield from self.ps_handler.__await__()) msg = yield from self.ps_handler.__await__()
self.check_message(msg) self.check_message(msg)
return msg return msg
@ -63,7 +63,6 @@ class MuxRPCSourceHandler(MuxRPCHandler):
class MuxRPCSinkHandlerMixin(object): class MuxRPCSinkHandlerMixin(object):
def send(self, msg, msg_type=PSMessageType.JSON, end=False): def send(self, msg, msg_type=PSMessageType.JSON, end=False):
self.connection.send(msg, stream=True, msg_type=msg_type, req=self.req, end_err=end) self.connection.send(msg, stream=True, msg_type=msg_type, req=self.req, end_err=end)
@ -82,13 +81,13 @@ class MuxRPCSinkHandler(MuxRPCHandler, MuxRPCSinkHandlerMixin):
def _get_appropriate_api_handler(type_, connection, ps_handler, req): def _get_appropriate_api_handler(type_, connection, ps_handler, req):
if type_ in {'sync', 'async'}: if type_ in {"sync", "async"}:
return MuxRPCRequestHandler(ps_handler) return MuxRPCRequestHandler(ps_handler)
elif type_ == 'source': elif type_ == "source":
return MuxRPCSourceHandler(ps_handler) return MuxRPCSourceHandler(ps_handler)
elif type_ == 'sink': elif type_ == "sink":
return MuxRPCSinkHandler(connection, req) return MuxRPCSinkHandler(connection, req)
elif type_ == 'duplex': elif type_ == "duplex":
return MuxRPCDuplexHandler(ps_handler, connection, req) return MuxRPCDuplexHandler(ps_handler, connection, req)
@ -96,14 +95,14 @@ class MuxRPCRequest(object):
@classmethod @classmethod
def from_message(cls, message): def from_message(cls, message):
body = message.body body = message.body
return cls('.'.join(body['name']), body['args']) return cls(".".join(body["name"]), body["args"])
def __init__(self, name, args): def __init__(self, name, args):
self.name = name self.name = name
self.args = args self.args = args
def __repr__(self): def __repr__(self):
return '<MuxRPCRequest {0.name} {0.args}>'.format(self) return "<MuxRPCRequest {0.name} {0.args}>".format(self)
class MuxRPCMessage(object): class MuxRPCMessage(object):
@ -115,7 +114,7 @@ class MuxRPCMessage(object):
self.body = body self.body = body
def __repr__(self): def __repr__(self):
return '<MuxRPCMessage {0.body}}>'.format(self) return "<MuxRPCMessage {0.body}}>".format(self)
class MuxRPCAPI(object): class MuxRPCAPI(object):
@ -128,7 +127,7 @@ class MuxRPCAPI(object):
body = req_message.body body = req_message.body
if req_message is None: if req_message is None:
return return
if isinstance(body, dict) and body.get('name'): if isinstance(body, dict) and body.get("name"):
self.process(self.connection, MuxRPCRequest.from_message(req_message)) self.process(self.connection, MuxRPCRequest.from_message(req_message))
def add_connection(self, connection): def add_connection(self, connection):
@ -141,22 +140,23 @@ class MuxRPCAPI(object):
@wraps(f) @wraps(f)
def _f(*args, **kwargs): def _f(*args, **kwargs):
return f(*args, **kwargs) return f(*args, **kwargs)
return f return f
return _handle return _handle
def process(self, connection, request): def process(self, connection, request):
handler = self.handlers.get(request.name) handler = self.handlers.get(request.name)
if not handler: if not handler:
raise MuxRPCAPIException('Method {} not found!'.format(request.name)) raise MuxRPCAPIException("Method {} not found!".format(request.name))
handler(connection, request) handler(connection, request)
def call(self, name, args, type_='sync'): def call(self, name, args, type_="sync"):
if not self.connection.is_connected: if not self.connection.is_connected:
raise Exception('not connected') raise Exception("not connected")
old_counter = self.connection.req_counter old_counter = self.connection.req_counter
ps_handler = self.connection.send({ ps_handler = self.connection.send(
'name': name.split('.'), {"name": name.split("."), "args": args, "type": type_},
'args': args, stream=type_ in {"sink", "source", "duplex"},
'type': type_ )
}, stream=type_ in {'sink', 'source', 'duplex'})
return _get_appropriate_api_handler(type_, self.connection, ps_handler, old_counter) return _get_appropriate_api_handler(type_, self.connection, ps_handler, old_counter)

View File

@ -33,7 +33,7 @@ from async_generator import async_generator, yield_
from secret_handshake import SHSClient, SHSServer from secret_handshake import SHSClient, SHSServer
logger = logging.getLogger('packet_stream') logger = logging.getLogger("packet_stream")
class PSMessageType(Enum): class PSMessageType(Enum):
@ -85,13 +85,12 @@ class PSRequestHandler(object):
class PSMessage(object): class PSMessage(object):
@classmethod @classmethod
def from_header_body(cls, flags, req, body): def from_header_body(cls, flags, req, body):
type_ = PSMessageType(flags & 0x03) type_ = PSMessageType(flags & 0x03)
if type_ == PSMessageType.TEXT: if type_ == PSMessageType.TEXT:
body = body.decode('utf-8') body = body.decode("utf-8")
elif type_ == PSMessageType.JSON: elif type_ == PSMessageType.JSON:
body = simplejson.loads(body) body = simplejson.loads(body)
@ -100,9 +99,9 @@ class PSMessage(object):
@property @property
def data(self): def data(self):
if self.type == PSMessageType.TEXT: if self.type == PSMessageType.TEXT:
return self.body.encode('utf-8') return self.body.encode("utf-8")
elif self.type == PSMessageType.JSON: elif self.type == PSMessageType.JSON:
return simplejson.dumps(self.body).encode('utf-8') return simplejson.dumps(self.body).encode("utf-8")
return self.body return self.body
def __init__(self, type_, body, stream, end_err, req=None): def __init__(self, type_, body, stream, end_err, req=None):
@ -114,12 +113,16 @@ class PSMessage(object):
def __repr__(self): def __repr__(self):
if self.type == PSMessageType.BUFFER: if self.type == PSMessageType.BUFFER:
body = '{} bytes'.format(len(self.body)) body = "{} bytes".format(len(self.body))
else: else:
body = self.body body = self.body
return '<PSMessage ({}): {}{} {}{}>'.format(self.type.name, body, return "<PSMessage ({}): {}{} {}{}>".format(
'' if self.req is None else ' [{}]'.format(self.req), self.type.name,
'~' if self.stream else '', '!' if self.end_err else '') body,
"" if self.req is None else " [{}]".format(self.req),
"~" if self.stream else "",
"!" if self.end_err else "",
)
class PacketStream(object): class PacketStream(object):
@ -147,27 +150,27 @@ class PacketStream(object):
async def __await__(self): async def __await__(self):
async for data in self: async for data in self:
logger.info('RECV: %r', data) logger.info("RECV: %r", data)
if data is None: if data is None:
return return
async def _read(self): async def _read(self):
try: try:
header = await self.connection.read() header = await self.connection.read()
if not header or header == b'\x00' * 9: if not header or header == b"\x00" * 9:
return return
flags, length, req = struct.unpack('>BIi', header) flags, length, req = struct.unpack(">BIi", header)
n_packets = ceil(length / 4096) n_packets = ceil(length / 4096)
body = b'' body = b""
for n in range(n_packets): for n in range(n_packets):
body += await self.connection.read() body += await self.connection.read()
logger.debug('READ %s %s', header, len(body)) logger.debug("READ %s %s", header, len(body))
return PSMessage.from_header_body(flags, req, body) return PSMessage.from_header_body(flags, req, body)
except StopAsyncIteration: except StopAsyncIteration:
logger.debug('DISCONNECT') logger.debug("DISCONNECT")
self.connection.disconnect() self.connection.disconnect()
return None return None
@ -179,21 +182,25 @@ class PacketStream(object):
if msg.req < 0: if msg.req < 0:
t, handler = self._event_map[-msg.req] t, handler = self._event_map[-msg.req]
await handler.process(msg) await handler.process(msg)
logger.info('RESPONSE [%d]: %r', -msg.req, msg) logger.info("RESPONSE [%d]: %r", -msg.req, msg)
if msg.end_err: if msg.end_err:
await handler.stop() await handler.stop()
del self._event_map[-msg.req] del self._event_map[-msg.req]
logger.info('RESPONSE [%d]: EOS', -msg.req) logger.info("RESPONSE [%d]: EOS", -msg.req)
return msg return msg
def _write(self, msg): def _write(self, msg):
logger.info('SEND [%d]: %r', msg.req, msg) logger.info("SEND [%d]: %r", msg.req, msg)
header = struct.pack('>BIi', (int(msg.stream) << 3) | (int(msg.end_err) << 2) | msg.type.value, len(msg.data), header = struct.pack(
msg.req) ">BIi",
(int(msg.stream) << 3) | (int(msg.end_err) << 2) | msg.type.value,
len(msg.data),
msg.req,
)
self.connection.write(header) self.connection.write(header)
self.connection.write(msg.data) self.connection.write(msg.data)
logger.debug('WRITE HDR: %s', header) logger.debug("WRITE HDR: %s", header)
logger.debug('WRITE DATA: %s', msg.data) logger.debug("WRITE DATA: %s", msg.data)
def send(self, data, msg_type=PSMessageType.JSON, stream=False, end_err=False, req=None): def send(self, data, msg_type=PSMessageType.JSON, stream=False, end_err=False, req=None):
update_counter = False update_counter = False

View File

@ -33,19 +33,16 @@ class ConfigException(Exception):
def tag(key): def tag(key):
"""Create tag from publick key.""" """Create tag from publick key."""
return b'@' + b64encode(bytes(key)) + b'.ed25519' return b"@" + b64encode(bytes(key)) + b".ed25519"
def load_ssb_secret(): def load_ssb_secret():
"""Load SSB keys from ~/.ssb""" """Load SSB keys from ~/.ssb"""
with open(os.path.expanduser('~/.ssb/secret')) as f: with open(os.path.expanduser("~/.ssb/secret")) as f:
config = yaml.load(f, Loader=yaml.SafeLoader) config = yaml.load(f, Loader=yaml.SafeLoader)
if config['curve'] != 'ed25519': if config["curve"] != "ed25519":
raise ConfigException('Algorithm not known: ' + config['curve']) raise ConfigException("Algorithm not known: " + config["curve"])
server_prv_key = b64decode(config['private'][:-8]) server_prv_key = b64decode(config["private"][:-8])
return { return {"keypair": SigningKey(server_prv_key[:32]), "id": config["id"]}
'keypair': SigningKey(server_prv_key[:32]),
'id': config['id']
}

View File

@ -47,115 +47,167 @@ SERIALIZED_M1 = b"""{
@pytest.fixture() @pytest.fixture()
def local_feed(): def local_feed():
secret = b64decode('Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=') secret = b64decode("Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=")
return LocalFeed(SigningKey(secret)) return LocalFeed(SigningKey(secret))
@pytest.fixture() @pytest.fixture()
def remote_feed(): def remote_feed():
public = b64decode('I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=') public = b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=")
return Feed(VerifyKey(public)) return Feed(VerifyKey(public))
def test_local_feed(): def test_local_feed():
secret = b64decode('Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=') secret = b64decode("Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=")
feed = LocalFeed(SigningKey(secret)) feed = LocalFeed(SigningKey(secret))
assert bytes(feed.private_key) == secret assert bytes(feed.private_key) == secret
assert bytes(feed.public_key) == b64decode('I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=') assert bytes(feed.public_key) == b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=")
assert feed.id == '@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519' assert feed.id == "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519"
def test_remote_feed(): def test_remote_feed():
public = b64decode('I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=') public = b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=")
feed = Feed(VerifyKey(public)) feed = Feed(VerifyKey(public))
assert bytes(feed.public_key) == public assert bytes(feed.public_key) == public
assert feed.id == '@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519' assert feed.id == "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519"
m1 = Message(feed, OrderedDict([ m1 = Message(
('type', 'about'), feed,
('about', feed.id), OrderedDict(
('name', 'neo'), [
('description', 'The Chosen One') ("type", "about"),
]), 'foo', timestamp=1495706260190) ("about", feed.id),
("name", "neo"),
("description", "The Chosen One"),
]
),
"foo",
timestamp=1495706260190,
)
with pytest.raises(NoPrivateKeyException): with pytest.raises(NoPrivateKeyException):
feed.sign(m1) feed.sign(m1)
def test_local_message(local_feed): def test_local_message(local_feed):
m1 = LocalMessage(local_feed, OrderedDict([ m1 = LocalMessage(
('type', 'about'), local_feed,
('about', local_feed.id), OrderedDict(
('name', 'neo'), [
('description', 'The Chosen One') ("type", "about"),
]), timestamp=1495706260190) ("about", local_feed.id),
("name", "neo"),
("description", "The Chosen One"),
]
),
timestamp=1495706260190,
)
assert m1.timestamp == 1495706260190 assert m1.timestamp == 1495706260190
assert m1.previous is None assert m1.previous is None
assert m1.sequence == 1 assert m1.sequence == 1
assert m1.signature == \ assert (
'lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519' m1.signature
assert m1.key == '%xRDqws/TrQmOd4aEwZ32jdLhP873ZKjIgHlggPR0eoo=.sha256' == "lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519"
)
assert m1.key == "%xRDqws/TrQmOd4aEwZ32jdLhP873ZKjIgHlggPR0eoo=.sha256"
m2 = LocalMessage(local_feed, OrderedDict([ m2 = LocalMessage(
('type', 'about'), local_feed,
('about', local_feed.id), OrderedDict(
('name', 'morpheus'), [
('description', 'Dude with big jaw') ("type", "about"),
]), previous=m1, timestamp=1495706447426) ("about", local_feed.id),
("name", "morpheus"),
("description", "Dude with big jaw"),
]
),
previous=m1,
timestamp=1495706447426,
)
assert m2.timestamp == 1495706447426 assert m2.timestamp == 1495706447426
assert m2.previous is m1 assert m2.previous is m1
assert m2.sequence == 2 assert m2.sequence == 2
assert m2.signature == \ assert (
'3SY85LX6/ppOfP4SbfwZbKfd6DccbLRiB13pwpzbSK0nU52OEJxOqcJ2Uensr6RkrWztWLIq90sNOn1zRAoOAw==.sig.ed25519' m2.signature
assert m2.key == '%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256' == "3SY85LX6/ppOfP4SbfwZbKfd6DccbLRiB13pwpzbSK0nU52OEJxOqcJ2Uensr6RkrWztWLIq90sNOn1zRAoOAw==.sig.ed25519"
)
assert m2.key == "%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256"
def test_remote_message(remote_feed): def test_remote_message(remote_feed):
signature = 'lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519' signature = "lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519"
m1 = Message(remote_feed, OrderedDict([ m1 = Message(
('type', 'about'), remote_feed,
('about', remote_feed.id), OrderedDict(
('name', 'neo'), [
('description', 'The Chosen One') ("type", "about"),
]), signature, timestamp=1495706260190) ("about", remote_feed.id),
("name", "neo"),
("description", "The Chosen One"),
]
),
signature,
timestamp=1495706260190,
)
assert m1.timestamp == 1495706260190 assert m1.timestamp == 1495706260190
assert m1.previous is None assert m1.previous is None
assert m1.sequence == 1 assert m1.sequence == 1
assert m1.signature == signature assert m1.signature == signature
assert m1.key == '%xRDqws/TrQmOd4aEwZ32jdLhP873ZKjIgHlggPR0eoo=.sha256' assert m1.key == "%xRDqws/TrQmOd4aEwZ32jdLhP873ZKjIgHlggPR0eoo=.sha256"
signature = '3SY85LX6/ppOfP4SbfwZbKfd6DccbLRiB13pwpzbSK0nU52OEJxOqcJ2Uensr6RkrWztWLIq90sNOn1zRAoOAw==.sig.ed25519' signature = "3SY85LX6/ppOfP4SbfwZbKfd6DccbLRiB13pwpzbSK0nU52OEJxOqcJ2Uensr6RkrWztWLIq90sNOn1zRAoOAw==.sig.ed25519"
m2 = Message(remote_feed, OrderedDict([ m2 = Message(
('type', 'about'), remote_feed,
('about', remote_feed.id), OrderedDict(
('name', 'morpheus'), [
('description', 'Dude with big jaw') ("type", "about"),
]), signature, previous=m1, timestamp=1495706447426) ("about", remote_feed.id),
("name", "morpheus"),
("description", "Dude with big jaw"),
]
),
signature,
previous=m1,
timestamp=1495706447426,
)
assert m2.timestamp == 1495706447426 assert m2.timestamp == 1495706447426
assert m2.previous is m1 assert m2.previous is m1
assert m2.sequence == 2 assert m2.sequence == 2
assert m2.signature == signature assert m2.signature == signature
m2.verify(signature) m2.verify(signature)
assert m2.key == '%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256' assert m2.key == "%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256"
def test_remote_no_signature(remote_feed): def test_remote_no_signature(remote_feed):
with pytest.raises(ValueError): with pytest.raises(ValueError):
Message(remote_feed, OrderedDict([ Message(
('type', 'about'), remote_feed,
('about', remote_feed.id), OrderedDict(
('name', 'neo'), [
('description', 'The Chosen One') ("type", "about"),
]), None, timestamp=1495706260190) ("about", remote_feed.id),
("name", "neo"),
("description", "The Chosen One"),
]
),
None,
timestamp=1495706260190,
)
def test_serialize(local_feed): def test_serialize(local_feed):
m1 = LocalMessage(local_feed, OrderedDict([ m1 = LocalMessage(
('type', 'about'), local_feed,
('about', local_feed.id), OrderedDict(
('name', 'neo'), [
('description', 'The Chosen One') ("type", "about"),
]), timestamp=1495706260190) ("about", local_feed.id),
("name", "neo"),
("description", "The Chosen One"),
]
),
timestamp=1495706260190,
)
assert m1.serialize() == SERIALIZED_M1 assert m1.serialize() == SERIALIZED_M1
@ -163,9 +215,9 @@ def test_serialize(local_feed):
def test_parse(local_feed): def test_parse(local_feed):
m1 = LocalMessage.parse(SERIALIZED_M1, local_feed) m1 = LocalMessage.parse(SERIALIZED_M1, local_feed)
assert m1.content == { assert m1.content == {
'type': 'about', "type": "about",
'about': local_feed.id, "about": local_feed.id,
'name': 'neo', "name": "neo",
'description': 'The Chosen One' "description": "The Chosen One",
} }
assert m1.timestamp == 1495706260190 assert m1.timestamp == 1495706260190

View File

@ -36,20 +36,25 @@ async def _collect_messages(generator):
results.append(msg) results.append(msg)
return results return results
MSG_BODY_1 = (b'{"previous":"%KTGP6W8vF80McRAZHYDWuKOD0KlNyKSq6Gb42iuV7Iw=.sha256","author":"@1+Iwm79DKvVBqYKFkhT6fWRbA'
MSG_BODY_1 = (
b'{"previous":"%KTGP6W8vF80McRAZHYDWuKOD0KlNyKSq6Gb42iuV7Iw=.sha256","author":"@1+Iwm79DKvVBqYKFkhT6fWRbA'
b'VvNNVH4F2BSxwhYmx8=.ed25519","sequence":116,"timestamp":1496696699331,"hash":"sha256","content":{"type"' 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':"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"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' 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'ound it too long/hard to follow. Are there any better alternatives?","mentions":[]},"signature":"hqKePb'
b'bTXWxEi1njDnOWFsL0M0AoNoWyBFgNE6KXj//DThepaZSy9vRbygDHX5uNmCdyOrsQrwZsZhmUYKwtDQ==.sig.ed25519"}') b'bTXWxEi1njDnOWFsL0M0AoNoWyBFgNE6KXj//DThepaZSy9vRbygDHX5uNmCdyOrsQrwZsZhmUYKwtDQ==.sig.ed25519"}'
)
MSG_BODY_2 = (b'{"previous":"%iQRhPyqmNLpGaO1Tpm1I22jqnUEwRwkCTDbwAGtM+lY=.sha256","author":"@1+Iwm79DKvVBqYKFkhT6fWRbA' MSG_BODY_2 = (
b'{"previous":"%iQRhPyqmNLpGaO1Tpm1I22jqnUEwRwkCTDbwAGtM+lY=.sha256","author":"@1+Iwm79DKvVBqYKFkhT6fWRbA'
b'VvNNVH4F2BSxwhYmx8=.ed25519","sequence":103,"timestamp":1496674211806,"hash":"sha256","content":{"type"' 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':"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'ccessing %vZCTqraoqKBKNZeATErXEtnoEr+wnT3p8tT+vL+29I4=.sha256","mentions":[{"link":"%vZCTqraoqKBKNZeATE'
b'rXEtnoEr+wnT3p8tT+vL+29I4=.sha256"}]},"signature":"+i4U0HUGDDEyNoNr2NIROPnT3WQj3RuTaIhY5koWW8f0vwr4tZsY' b'rXEtnoEr+wnT3p8tT+vL+29I4=.sha256"}]},"signature":"+i4U0HUGDDEyNoNr2NIROPnT3WQj3RuTaIhY5koWW8f0vwr4tZsY'
b'mAkqqMwFWfP+eBIbc7DZ835er6r6h9CwAg==.sig.ed25519"}') b'mAkqqMwFWfP+eBIbc7DZ835er6r6h9CwAg==.sig.ed25519"}'
)
class MockSHSSocket(SHSDuplexStream): class MockSHSSocket(SHSDuplexStream):
@ -129,26 +134,28 @@ async def test_message_decoding(ps_client):
assert ps.is_connected assert ps.is_connected
ps_client.feed([ ps_client.feed(
b'\n\x00\x00\x00\x9a\x00\x00\x04\xfb', [
b"\n\x00\x00\x00\x9a\x00\x00\x04\xfb",
b'{"name":["createHistoryStream"],"args":[{"id":"@omgyp7Pnrw+Qm0I6T6Fh5VvnKmodMXwnxTIesW2DgMg=.ed25519",' b'{"name":["createHistoryStream"],"args":[{"id":"@omgyp7Pnrw+Qm0I6T6Fh5VvnKmodMXwnxTIesW2DgMg=.ed25519",'
b'"seq":10,"live":true,"keys":false}],"type":"source"}' b'"seq":10,"live":true,"keys":false}],"type":"source"}',
]) ]
)
messages = (await _collect_messages(ps)) messages = await _collect_messages(ps)
assert len(messages) == 1 assert len(messages) == 1
assert messages[0].type == PSMessageType.JSON assert messages[0].type == PSMessageType.JSON
assert messages[0].body == { assert messages[0].body == {
'name': ['createHistoryStream'], "name": ["createHistoryStream"],
'args': [ "args": [
{ {
'id': '@omgyp7Pnrw+Qm0I6T6Fh5VvnKmodMXwnxTIesW2DgMg=.ed25519', "id": "@omgyp7Pnrw+Qm0I6T6Fh5VvnKmodMXwnxTIesW2DgMg=.ed25519",
'seq': 10, "seq": 10,
'live': True, "live": True,
'keys': False "keys": False,
} }
], ],
'type': 'source' "type": "source",
} }
@ -160,26 +167,36 @@ async def test_message_encoding(ps_client):
assert ps.is_connected assert ps.is_connected
ps.send({ ps.send(
'name': ['createHistoryStream'], {
'args': [{ "name": ["createHistoryStream"],
'id': "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "args": [
'seq': 1, {
'live': False, "id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519",
'keys': False "seq": 1,
}], "live": False,
'type': 'source' "keys": False,
}, stream=True) }
],
"type": "source",
},
stream=True,
)
header, body = list(ps_client.get_output()) header, body = list(ps_client.get_output())
assert header == b'\x0a\x00\x00\x00\xa6\x00\x00\x00\x01' assert header == b"\x0a\x00\x00\x00\xa6\x00\x00\x00\x01"
assert json.loads(body.decode('utf-8')) == { assert json.loads(body.decode("utf-8")) == {
"name": ["createHistoryStream"], "name": ["createHistoryStream"],
"args": [ "args": [
{"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False} {
"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519",
"seq": 1,
"live": False,
"keys": False,
}
], ],
"type": "source" "type": "source",
} }
@ -188,56 +205,72 @@ async def test_message_stream(ps_client, mocker):
await ps_client.connect() await ps_client.connect()
ps = PacketStream(ps_client) ps = PacketStream(ps_client)
mocker.patch.object(ps, 'register_handler', wraps=ps.register_handler) mocker.patch.object(ps, "register_handler", wraps=ps.register_handler)
assert ps.is_connected assert ps.is_connected
ps.send({ ps.send(
'name': ['createHistoryStream'], {
'args': [{ "name": ["createHistoryStream"],
'id': "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "args": [
'seq': 1, {
'live': False, "id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519",
'keys': False "seq": 1,
}], "live": False,
'type': 'source' "keys": False,
}, stream=True) }
],
"type": "source",
},
stream=True,
)
assert ps.req_counter == 2 assert ps.req_counter == 2
assert ps.register_handler.call_count == 1 assert ps.register_handler.call_count == 1
handler = list(ps._event_map.values())[0][1] handler = list(ps._event_map.values())[0][1]
mock_process = mocker.AsyncMock() mock_process = mocker.AsyncMock()
mocker.patch.object(handler, 'process', mock_process) mocker.patch.object(handler, "process", mock_process)
ps_client.feed([b'\n\x00\x00\x02\xc5\xff\xff\xff\xff', MSG_BODY_1]) ps_client.feed([b"\n\x00\x00\x02\xc5\xff\xff\xff\xff", MSG_BODY_1])
msg = await ps.read() msg = await ps.read()
assert mock_process.await_count == 1 assert mock_process.await_count == 1
# responses have negative req # responses have negative req
assert msg.req == -1 assert msg.req == -1
assert msg.body['previous'] == '%KTGP6W8vF80McRAZHYDWuKOD0KlNyKSq6Gb42iuV7Iw=.sha256' assert msg.body["previous"] == "%KTGP6W8vF80McRAZHYDWuKOD0KlNyKSq6Gb42iuV7Iw=.sha256"
assert ps.req_counter == 2 assert ps.req_counter == 2
stream_handler = ps.send({ stream_handler = ps.send(
'name': ['createHistoryStream'], {
'args': [{ "name": ["createHistoryStream"],
'id': "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "args": [
'seq': 1, {
'live': False, "id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519",
'keys': False "seq": 1,
}], "live": False,
'type': 'source' "keys": False,
}, stream=True) }
],
"type": "source",
},
stream=True,
)
assert ps.req_counter == 3 assert ps.req_counter == 3
assert ps.register_handler.call_count == 2 assert ps.register_handler.call_count == 2
handler = list(ps._event_map.values())[1][1] handler = list(ps._event_map.values())[1][1]
mock_process = mocker.patch.object(handler, 'process', wraps=handler.process) 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, ps_client.feed(
b'\x0e\x00\x00\x023\xff\xff\xff\xfe', MSG_BODY_2]) [
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,
]
)
# execute both message polling and response handling loops # execute both message polling and response handling loops
collected, handled = await gather(_collect_messages(ps), _collect_messages(stream_handler)) collected, handled = await gather(_collect_messages(ps), _collect_messages(stream_handler))
@ -258,29 +291,30 @@ async def test_message_request(ps_server, mocker):
ps = PacketStream(ps_server) ps = PacketStream(ps_server)
mocker.patch.object(ps, 'register_handler', wraps=ps.register_handler) mocker.patch.object(ps, "register_handler", wraps=ps.register_handler)
ps.send({ ps.send({"name": ["whoami"], "args": []})
'name': ['whoami'],
'args': []
})
header, body = list(ps_server.get_output()) header, body = list(ps_server.get_output())
assert header == b'\x02\x00\x00\x00 \x00\x00\x00\x01' assert header == b"\x02\x00\x00\x00 \x00\x00\x00\x01"
assert json.loads(body.decode('utf-8')) == {"name": ["whoami"], "args": []} assert json.loads(body.decode("utf-8")) == {"name": ["whoami"], "args": []}
assert ps.req_counter == 2 assert ps.req_counter == 2
assert ps.register_handler.call_count == 1 assert ps.register_handler.call_count == 1
handler = list(ps._event_map.values())[0][1] handler = list(ps._event_map.values())[0][1]
mock_process = mocker.AsyncMock() mock_process = mocker.AsyncMock()
mocker.patch.object(handler, 'process', mock_process) mocker.patch.object(handler, "process", mock_process)
ps_server.feed([b'\x02\x00\x00\x00>\xff\xff\xff\xff', ps_server.feed(
b'{"id":"@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519"}']) [
b"\x02\x00\x00\x00>\xff\xff\xff\xff",
b'{"id":"@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519"}',
]
)
msg = await ps.read() msg = await ps.read()
assert mock_process.await_count == 1 assert mock_process.await_count == 1
# responses have negative req # responses have negative req
assert msg.req == -1 assert msg.req == -1
assert msg.body['id'] == '@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519' assert msg.body["id"] == "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519"
assert ps.req_counter == 2 assert ps.req_counter == 2

View File

@ -38,21 +38,21 @@ CONFIG_FILE = """
} }
""" """
CONFIG_FILE_INVALID = CONFIG_FILE.replace('ed25519', 'foo') CONFIG_FILE_INVALID = CONFIG_FILE.replace("ed25519", "foo")
def test_load_secret(): def test_load_secret():
with patch('ssb.util.open', mock_open(read_data=CONFIG_FILE), create=True): with patch("ssb.util.open", mock_open(read_data=CONFIG_FILE), create=True):
secret = load_ssb_secret() secret = load_ssb_secret()
priv_key = b'\xfd\xba\x83\x04\x8f\xef\x18\xb0\xf9\xab-\xc6\xc4\xcb \x1cX\x18"\xba\xd8\xd3\xc2_O5\x1a\t\x84\xfa\xc7A' priv_key = b'\xfd\xba\x83\x04\x8f\xef\x18\xb0\xf9\xab-\xc6\xc4\xcb \x1cX\x18"\xba\xd8\xd3\xc2_O5\x1a\t\x84\xfa\xc7A'
assert secret['id'] == '@rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=.ed25519' assert secret["id"] == "@rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=.ed25519"
assert bytes(secret['keypair']) == priv_key assert bytes(secret["keypair"]) == priv_key
assert bytes(secret['keypair'].verify_key) == b64decode('rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=') assert bytes(secret["keypair"].verify_key) == b64decode("rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=")
def test_load_exception(): def test_load_exception():
with pytest.raises(ConfigException): with pytest.raises(ConfigException):
with patch('ssb.util.open', mock_open(read_data=CONFIG_FILE_INVALID), create=True): with patch("ssb.util.open", mock_open(read_data=CONFIG_FILE_INVALID), create=True):
load_ssb_secret() load_ssb_secret()