diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4302ed4..db88ec5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,3 +16,11 @@ repos: pass_filenames: false language: system 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] diff --git a/examples/test_client.py b/examples/test_client.py index 0e1c351..bb7404d 100644 --- a/examples/test_client.py +++ b/examples/test_client.py @@ -17,76 +17,79 @@ import base64 api = MuxRPCAPI() -@api.define('createHistoryStream') +@api.define("createHistoryStream") 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) # connection.write(msg) -@api.define('blobs.createWants') +@api.define("blobs.createWants") def create_wants(connection, msg): - print('create_wants', msg) + print("create_wants", msg) async def test_client(): - async for msg in api.call('createHistoryStream', [{ - 'id': "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", - 'seq': 1, - 'live': False, - 'keys': False - }], 'source'): - print('> RESPONSE:', msg) + async for msg in api.call( + "createHistoryStream", + [{"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False}], + "source", + ): + print("> RESPONSE:", msg) try: - print('> RESPONSE:', await api.call('whoami', [], 'sync')) + print("> RESPONSE:", await api.call("whoami", [], "sync")) except MuxRPCAPIException as e: print(e) - handler = api.call('gossip.ping', [], 'duplex') - handler.send(struct.pack('l', int(time.time() * 1000)), msg_type=PSMessageType.BUFFER) + handler = api.call("gossip.ping", [], "duplex") + handler.send(struct.pack("l", int(time.time() * 1000)), msg_type=PSMessageType.BUFFER) + async for msg in handler: - print('> RESPONSE:', msg) + print("> RESPONSE:", msg) handler.send(True, end=True) break - img_data = b'' - async for msg in api.call('blobs.get', ['&kqZ52sDcJSHOx7m4Ww80kK1KIZ65gpGnqwZlfaIVWWM=.sha256'], 'source'): - if msg.type.name == 'BUFFER': + img_data = b"" + async for msg in api.call("blobs.get", ["&kqZ52sDcJSHOx7m4Ww80kK1KIZ65gpGnqwZlfaIVWWM=.sha256"], "source"): + if msg.type.name == "BUFFER": img_data += msg.data - if msg.type.name == 'JSON' and msg.data == b'true': - assert (base64.b64encode(hashlib.sha256(img_data).digest()) == - b'kqZ52sDcJSHOx7m4Ww80kK1KIZ65gpGnqwZlfaIVWWM=') - with open('./ub1k.jpg', 'wb') as f: + if msg.type.name == "JSON" and msg.data == b"true": + assert ( + base64.b64encode(hashlib.sha256(img_data).digest()) == b"kqZ52sDcJSHOx7m4Ww80kK1KIZ65gpGnqwZlfaIVWWM=" + ) + + with open("./ub1k.jpg", "wb") as f: f.write(img_data) 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) await client.open() api.add_connection(packet_stream) await gather(ensure_future(api), test_client()) -if __name__ == '__main__': +if __name__ == "__main__": # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.INFO) # create formatter - formatter = ColoredFormatter('%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - ' - '%(cyan)s%(message)s%(reset)s') + formatter = ColoredFormatter( + "%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - %(cyan)s%(message)s%(reset)s" + ) # add formatter to ch ch.setFormatter(formatter) # add ch to logger - logger = logging.getLogger('packet_stream') + logger = logging.getLogger("packet_stream") logger.setLevel(logging.INFO) logger.addHandler(ch) - keypair = load_ssb_secret()['keypair'] + keypair = load_ssb_secret()["keypair"] loop = get_event_loop() loop.run_until_complete(main()) diff --git a/examples/test_server.py b/examples/test_server.py index fe89c75..4527791 100644 --- a/examples/test_server.py +++ b/examples/test_server.py @@ -15,31 +15,33 @@ async def on_connect(conn): packet_stream = PacketStream(conn) api.add_connection(packet_stream) - print('connect', conn) + print("connect", conn) + async for msg in packet_stream: print(msg) 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) await server.listen() -if __name__ == '__main__': +if __name__ == "__main__": # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # create formatter - formatter = ColoredFormatter('%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - ' - '%(cyan)s%(message)s%(reset)s') + formatter = ColoredFormatter( + "%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - %(cyan)s%(message)s%(reset)s" + ) # add formatter to ch ch.setFormatter(formatter) # add ch to logger - logger = logging.getLogger('packet_stream') + logger = logging.getLogger("packet_stream") logger.setLevel(logging.DEBUG) logger.addHandler(ch) diff --git a/poetry.lock b/poetry.lock index a55c051..72529fd 100644 --- a/poetry.lock +++ b/poetry.lock @@ -42,6 +42,48 @@ setuptools = {version = "*", markers = "python_version >= \"3.12\""} [package.extras] dev = ["freezegun (>=1.0,<2.0)", "pytest (>=6.0)", "pytest-cov"] +[[package]] +name = "black" +version = "23.10.1" +description = "The uncompromising code formatter." +optional = false +python-versions = ">=3.8" +files = [ + {file = "black-23.10.1-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:ec3f8e6234c4e46ff9e16d9ae96f4ef69fa328bb4ad08198c8cee45bb1f08c69"}, + {file = "black-23.10.1-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:1b917a2aa020ca600483a7b340c165970b26e9029067f019e3755b56e8dd5916"}, + {file = "black-23.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9c74de4c77b849e6359c6f01987e94873c707098322b91490d24296f66d067dc"}, + {file = "black-23.10.1-cp310-cp310-win_amd64.whl", hash = "sha256:7b4d10b0f016616a0d93d24a448100adf1699712fb7a4efd0e2c32bbb219b173"}, + {file = "black-23.10.1-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:b15b75fc53a2fbcac8a87d3e20f69874d161beef13954747e053bca7a1ce53a0"}, + {file = "black-23.10.1-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:e293e4c2f4a992b980032bbd62df07c1bcff82d6964d6c9496f2cd726e246ace"}, + {file = "black-23.10.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7d56124b7a61d092cb52cce34182a5280e160e6aff3137172a68c2c2c4b76bcb"}, + {file = "black-23.10.1-cp311-cp311-win_amd64.whl", hash = "sha256:3f157a8945a7b2d424da3335f7ace89c14a3b0625e6593d21139c2d8214d55ce"}, + {file = "black-23.10.1-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:cfcce6f0a384d0da692119f2d72d79ed07c7159879d0bb1bb32d2e443382bf3a"}, + {file = "black-23.10.1-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:33d40f5b06be80c1bbce17b173cda17994fbad096ce60eb22054da021bf933d1"}, + {file = "black-23.10.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:840015166dbdfbc47992871325799fd2dc0dcf9395e401ada6d88fe11498abad"}, + {file = "black-23.10.1-cp38-cp38-win_amd64.whl", hash = "sha256:037e9b4664cafda5f025a1728c50a9e9aedb99a759c89f760bd83730e76ba884"}, + {file = "black-23.10.1-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:7cb5936e686e782fddb1c73f8aa6f459e1ad38a6a7b0e54b403f1f05a1507ee9"}, + {file = "black-23.10.1-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:7670242e90dc129c539e9ca17665e39a146a761e681805c54fbd86015c7c84f7"}, + {file = "black-23.10.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5ed45ac9a613fb52dad3b61c8dea2ec9510bf3108d4db88422bacc7d1ba1243d"}, + {file = "black-23.10.1-cp39-cp39-win_amd64.whl", hash = "sha256:6d23d7822140e3fef190734216cefb262521789367fbdc0b3f22af6744058982"}, + {file = "black-23.10.1-py3-none-any.whl", hash = "sha256:d431e6739f727bb2e0495df64a6c7a5310758e87505f5f8cde9ff6c0f2d7e4fe"}, + {file = "black-23.10.1.tar.gz", hash = "sha256:1f8ce316753428ff68749c65a5f7844631aa18c8679dfd3ca9dc1a289979c258"}, +] + +[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]] name = "certifi" version = "2023.7.22" @@ -233,6 +275,20 @@ toml = "*" [package.extras] 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]] name = "colorama" version = "0.4.6" @@ -543,6 +599,17 @@ files = [ {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]] name = "packaging" version = "23.2" @@ -554,6 +621,17 @@ files = [ {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]] name = "pep257" version = "0.7.0" @@ -565,6 +643,21 @@ files = [ {file = "pep257-0.7.0.tar.gz", hash = "sha256:f3d67547f5617a9cfeb4b8097ed94a954888315defaf6e9b518ff1719363bf03"}, ] +[[package]] +name = "platformdirs" +version = "3.11.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-3.11.0-py3-none-any.whl", hash = "sha256:e9d171d00af68be50e9202731309c4e658fd8bc76f55c11c7dd760d023bda68e"}, + {file = "platformdirs-3.11.0.tar.gz", hash = "sha256:cf8ee52a3afdb965072dcc652433e0c7e3e40cf5ea1477cd4b3b1d2eb75495b3"}, +] + +[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]] name = "pluggy" version = "1.3.0" @@ -1050,6 +1143,17 @@ files = [ {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]] name = "urllib3" version = "2.0.7" @@ -1096,4 +1200,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "f36d397c63377df66056beb600b8db2e6fe32f8f8224c4cafb3d472723069854" +content-hash = "a69623d229f05becfdd7a18072ae96970994ceebdc193d7840aa704ba0d86169" diff --git a/pyproject.toml b/pyproject.toml index 2652ab9..a96d327 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,10 +24,14 @@ pytest-asyncio = "^0.21.1" pytest-cov = "^4.1.0" pytest-mock = "^3.12.0" commitizen = "^3.12.0" +black = "^23.10.1" [tool.poetry.group.docs.dependencies] Sphinx = "^2.1.1" +[tool.black] +line-length = 120 + [tool.coverage.run] branch = true diff --git a/ssb/feed/__init__.py b/ssb/feed/__init__.py index c3080c0..b8c7cb6 100644 --- a/ssb/feed/__init__.py +++ b/ssb/feed/__init__.py @@ -1,3 +1,3 @@ from .models import Feed, LocalFeed, Message, LocalMessage, NoPrivateKeyException -__all__ = ('Feed', 'LocalFeed', 'Message', 'LocalMessage', 'NoPrivateKeyException') +__all__ = ("Feed", "LocalFeed", "Message", "LocalMessage", "NoPrivateKeyException") diff --git a/ssb/feed/models.py b/ssb/feed/models.py index 8d28432..816e86f 100644 --- a/ssb/feed/models.py +++ b/ssb/feed/models.py @@ -8,7 +8,7 @@ from simplejson import dumps, loads 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): @@ -30,10 +30,10 @@ class Feed(object): @property def id(self): - return tag(self.public_key).decode('ascii') + return tag(self.public_key).decode("ascii") 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): @@ -55,6 +55,7 @@ class Message(object): if signature is None: raise ValueError("signature can't be None") + self.signature = signature self.previous = previous @@ -68,24 +69,26 @@ class Message(object): @classmethod def parse(cls, data, feed): 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 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): - obj = to_ordered({ - 'previous': self.previous.key if self.previous else None, - 'author': self.feed.id, - 'sequence': self.sequence, - 'timestamp': self.timestamp, - 'hash': 'sha256', - 'content': self.content - }) + obj = to_ordered( + { + "previous": self.previous.key if self.previous else None, + "author": self.feed.id, + "sequence": self.sequence, + "timestamp": self.timestamp, + "hash": "sha256", + "content": self.content, + } + ) if add_signature: - obj['signature'] = self.signature + obj["signature"] = self.signature return obj def verify(self, signature): @@ -94,11 +97,11 @@ class Message(object): @property def hash(self): hash = sha256(self.serialize()).digest() - return b64encode(hash).decode('ascii') + '.sha256' + return b64encode(hash).decode("ascii") + ".sha256" @property def key(self): - return '%' + self.hash + return "%" + self.hash class LocalMessage(Message): @@ -122,4 +125,4 @@ class LocalMessage(Message): def _sign(self): # ensure ordering of keys and indentation of 2 characters, like ssb-keys 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") diff --git a/ssb/muxrpc.py b/ssb/muxrpc.py index 97fb025..a86894a 100644 --- a/ssb/muxrpc.py +++ b/ssb/muxrpc.py @@ -10,8 +10,9 @@ class MuxRPCAPIException(Exception): class MuxRPCHandler: def check_message(self, msg): body = msg.body - if isinstance(body, dict) and 'name' in body and body['name'] == 'Error': - raise MuxRPCAPIException(body['message']) + + if isinstance(body, dict) and "name" in body and body["name"] == "Error": + raise MuxRPCAPIException(body["message"]) class MuxRPCRequestHandler(MuxRPCHandler): @@ -19,7 +20,7 @@ class MuxRPCRequestHandler(MuxRPCHandler): self.ps_handler = ps_handler def __await__(self): - msg = (yield from self.ps_handler.__await__()) + msg = yield from self.ps_handler.__await__() self.check_message(msg) return msg @@ -39,7 +40,6 @@ class MuxRPCSourceHandler(MuxRPCHandler): class MuxRPCSinkHandlerMixin(object): - 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) @@ -58,13 +58,13 @@ class MuxRPCSinkHandler(MuxRPCHandler, MuxRPCSinkHandlerMixin): def _get_appropriate_api_handler(type_, connection, ps_handler, req): - if type_ in {'sync', 'async'}: + if type_ in {"sync", "async"}: return MuxRPCRequestHandler(ps_handler) - elif type_ == 'source': + elif type_ == "source": return MuxRPCSourceHandler(ps_handler) - elif type_ == 'sink': + elif type_ == "sink": return MuxRPCSinkHandler(connection, req) - elif type_ == 'duplex': + elif type_ == "duplex": return MuxRPCDuplexHandler(ps_handler, connection, req) @@ -72,14 +72,14 @@ class MuxRPCRequest(object): @classmethod def from_message(cls, message): body = message.body - return cls('.'.join(body['name']), body['args']) + return cls(".".join(body["name"]), body["args"]) def __init__(self, name, args): self.name = name self.args = args def __repr__(self): - return ''.format(self) + return "".format(self) class MuxRPCMessage(object): @@ -91,7 +91,7 @@ class MuxRPCMessage(object): self.body = body def __repr__(self): - return ''.format(self) + return "".format(self) class MuxRPCAPI(object): @@ -104,7 +104,8 @@ class MuxRPCAPI(object): body = req_message.body if req_message is None: 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)) def add_connection(self, connection): @@ -117,22 +118,25 @@ class MuxRPCAPI(object): @wraps(f) def _f(*args, **kwargs): return f(*args, **kwargs) + return f + return _handle def process(self, connection, request): handler = self.handlers.get(request.name) if not handler: - raise MuxRPCAPIException('Method {} not found!'.format(request.name)) + raise MuxRPCAPIException("Method {} not found!".format(request.name)) handler(connection, request) - def call(self, name, args, type_='sync'): + def call(self, name, args, type_="sync"): if not self.connection.is_connected: - raise Exception('not connected') + raise Exception("not connected") + old_counter = self.connection.req_counter - ps_handler = self.connection.send({ - 'name': name.split('.'), - 'args': args, - 'type': type_ - }, stream=type_ in {'sink', 'source', 'duplex'}) + ps_handler = self.connection.send( + {"name": name.split("."), "args": args, "type": type_}, + stream=type_ in {"sink", "source", "duplex"}, + ) + return _get_appropriate_api_handler(type_, self.connection, ps_handler, old_counter) diff --git a/ssb/packet_stream.py b/ssb/packet_stream.py index 169775d..59b4a7b 100644 --- a/ssb/packet_stream.py +++ b/ssb/packet_stream.py @@ -10,7 +10,7 @@ import simplejson from secret_handshake import SHSClient, SHSServer -logger = logging.getLogger('packet_stream') +logger = logging.getLogger("packet_stream") class PSMessageType(Enum): @@ -65,13 +65,12 @@ class PSRequestHandler(object): class PSMessage(object): - @classmethod def from_header_body(cls, flags, req, body): type_ = PSMessageType(flags & 0x03) if type_ == PSMessageType.TEXT: - body = body.decode('utf-8') + body = body.decode("utf-8") elif type_ == PSMessageType.JSON: body = simplejson.loads(body) @@ -80,9 +79,9 @@ class PSMessage(object): @property def data(self): if self.type == PSMessageType.TEXT: - return self.body.encode('utf-8') + return self.body.encode("utf-8") elif self.type == PSMessageType.JSON: - return simplejson.dumps(self.body).encode('utf-8') + return simplejson.dumps(self.body).encode("utf-8") return self.body def __init__(self, type_, body, stream, end_err, req=None): @@ -94,12 +93,16 @@ class PSMessage(object): def __repr__(self): if self.type == PSMessageType.BUFFER: - body = '{} bytes'.format(len(self.body)) + body = "{} bytes".format(len(self.body)) else: body = self.body - return ''.format(self.type.name, body, - '' if self.req is None else ' [{}]'.format(self.req), - '~' if self.stream else '', '!' if self.end_err else '') + return "".format( + self.type.name, + body, + "" if self.req is None else " [{}]".format(self.req), + "~" if self.stream else "", + "!" if self.end_err else "", + ) class PacketStream(object): @@ -131,27 +134,27 @@ class PacketStream(object): async def __await__(self): async for data in self: - logger.info('RECV: %r', data) + logger.info("RECV: %r", data) if data is None: return async def _read(self): try: header = await self.connection.read() - if not header or header == b'\x00' * 9: + if not header or header == b"\x00" * 9: return - flags, length, req = struct.unpack('>BIi', header) + flags, length, req = struct.unpack(">BIi", header) n_packets = ceil(length / 4096) - body = b'' + body = b"" for n in range(n_packets): 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) except StopAsyncIteration: - logger.debug('DISCONNECT') + logger.debug("DISCONNECT") self.connection.disconnect() return None @@ -163,21 +166,22 @@ class PacketStream(object): if msg.req < 0: t, handler = self._event_map[-msg.req] await handler.process(msg) - logger.info('RESPONSE [%d]: %r', -msg.req, msg) + logger.info("RESPONSE [%d]: %r", -msg.req, msg) if msg.end_err: await handler.stop() del self._event_map[-msg.req] - logger.info('RESPONSE [%d]: EOS', -msg.req) + logger.info("RESPONSE [%d]: EOS", -msg.req) return msg def _write(self, 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), - msg.req) + 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), msg.req + ) self.connection.write(header) self.connection.write(msg.data) - logger.debug('WRITE HDR: %s', header) - logger.debug('WRITE DATA: %s', msg.data) + logger.debug("WRITE HDR: %s", header) + logger.debug("WRITE DATA: %s", msg.data) def send(self, data, msg_type=PSMessageType.JSON, stream=False, end_err=False, req=None): update_counter = False diff --git a/ssb/util.py b/ssb/util.py index 39e51e5..9efab92 100644 --- a/ssb/util.py +++ b/ssb/util.py @@ -11,19 +11,18 @@ class ConfigException(Exception): def tag(key): """Create tag from publick key.""" - return b'@' + b64encode(bytes(key)) + b'.ed25519' + + return b"@" + b64encode(bytes(key)) + b".ed25519" def load_ssb_secret(): """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) - if config['curve'] != 'ed25519': - raise ConfigException('Algorithm not known: ' + config['curve']) + if config["curve"] != "ed25519": + raise ConfigException("Algorithm not known: " + config["curve"]) - server_prv_key = b64decode(config['private'][:-8]) - return { - 'keypair': SigningKey(server_prv_key[:32]), - 'id': config['id'] - } + server_prv_key = b64decode(config["private"][:-8]) + return {"keypair": SigningKey(server_prv_key[:32]), "id": config["id"]} diff --git a/tests/test_feed.py b/tests/test_feed.py index 78f499a..dab0cd9 100644 --- a/tests/test_feed.py +++ b/tests/test_feed.py @@ -23,127 +23,129 @@ SERIALIZED_M1 = b"""{ }""" -@pytest.fixture() +@pytest.fixture def local_feed(): - secret = b64decode('Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=') + secret = b64decode("Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=") return LocalFeed(SigningKey(secret)) -@pytest.fixture() +@pytest.fixture def remote_feed(): - public = b64decode('I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=') + public = b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=") return Feed(VerifyKey(public)) def test_local_feed(): - secret = b64decode('Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=') + secret = b64decode("Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=") feed = LocalFeed(SigningKey(secret)) assert bytes(feed.private_key) == secret - assert bytes(feed.public_key) == b64decode('I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=') - assert feed.id == '@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519' + assert bytes(feed.public_key) == b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=") + assert feed.id == "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519" def test_remote_feed(): - public = b64decode('I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=') + public = b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=") feed = Feed(VerifyKey(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([ - ('type', 'about'), - ('about', feed.id), - ('name', 'neo'), - ('description', 'The Chosen One') - ]), 'foo', timestamp=1495706260190) + m1 = Message( + feed, + OrderedDict([("type", "about"), ("about", feed.id), ("name", "neo"), ("description", "The Chosen One")]), + "foo", + timestamp=1495706260190, + ) with pytest.raises(NoPrivateKeyException): feed.sign(m1) def test_local_message(local_feed): - m1 = LocalMessage(local_feed, OrderedDict([ - ('type', 'about'), - ('about', local_feed.id), - ('name', 'neo'), - ('description', 'The Chosen One') - ]), timestamp=1495706260190) + m1 = LocalMessage( + local_feed, + OrderedDict([("type", "about"), ("about", local_feed.id), ("name", "neo"), ("description", "The Chosen One")]), + timestamp=1495706260190, + ) assert m1.timestamp == 1495706260190 assert m1.previous is None assert m1.sequence == 1 - assert m1.signature == \ - 'lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519' - assert m1.key == '%xRDqws/TrQmOd4aEwZ32jdLhP873ZKjIgHlggPR0eoo=.sha256' + assert m1.signature == ( + "lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519" + ) + assert m1.key == "%xRDqws/TrQmOd4aEwZ32jdLhP873ZKjIgHlggPR0eoo=.sha256" - m2 = LocalMessage(local_feed, OrderedDict([ - ('type', 'about'), - ('about', local_feed.id), - ('name', 'morpheus'), - ('description', 'Dude with big jaw') - ]), previous=m1, timestamp=1495706447426) + m2 = LocalMessage( + local_feed, + OrderedDict( + [("type", "about"), ("about", local_feed.id), ("name", "morpheus"), ("description", "Dude with big jaw")] + ), + previous=m1, + timestamp=1495706447426, + ) assert m2.timestamp == 1495706447426 assert m2.previous is m1 assert m2.sequence == 2 - assert m2.signature == \ - '3SY85LX6/ppOfP4SbfwZbKfd6DccbLRiB13pwpzbSK0nU52OEJxOqcJ2Uensr6RkrWztWLIq90sNOn1zRAoOAw==.sig.ed25519' - assert m2.key == '%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256' + assert m2.signature == ( + "3SY85LX6/ppOfP4SbfwZbKfd6DccbLRiB13pwpzbSK0nU52OEJxOqcJ2Uensr6RkrWztWLIq90sNOn1zRAoOAw==.sig.ed25519" + ) + assert m2.key == "%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256" def test_remote_message(remote_feed): - signature = 'lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519' - m1 = Message(remote_feed, OrderedDict([ - ('type', 'about'), - ('about', remote_feed.id), - ('name', 'neo'), - ('description', 'The Chosen One') - ]), signature, timestamp=1495706260190) + signature = "lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519" + m1 = Message( + remote_feed, + OrderedDict([("type", "about"), ("about", remote_feed.id), ("name", "neo"), ("description", "The Chosen One")]), + signature, + timestamp=1495706260190, + ) assert m1.timestamp == 1495706260190 assert m1.previous is None assert m1.sequence == 1 assert m1.signature == signature - assert m1.key == '%xRDqws/TrQmOd4aEwZ32jdLhP873ZKjIgHlggPR0eoo=.sha256' + assert m1.key == "%xRDqws/TrQmOd4aEwZ32jdLhP873ZKjIgHlggPR0eoo=.sha256" - signature = '3SY85LX6/ppOfP4SbfwZbKfd6DccbLRiB13pwpzbSK0nU52OEJxOqcJ2Uensr6RkrWztWLIq90sNOn1zRAoOAw==.sig.ed25519' - m2 = Message(remote_feed, OrderedDict([ - ('type', 'about'), - ('about', remote_feed.id), - ('name', 'morpheus'), - ('description', 'Dude with big jaw') - ]), signature, previous=m1, timestamp=1495706447426) + signature = "3SY85LX6/ppOfP4SbfwZbKfd6DccbLRiB13pwpzbSK0nU52OEJxOqcJ2Uensr6RkrWztWLIq90sNOn1zRAoOAw==.sig.ed25519" + m2 = Message( + remote_feed, + OrderedDict( + [("type", "about"), ("about", remote_feed.id), ("name", "morpheus"), ("description", "Dude with big jaw")] + ), + signature, + previous=m1, + timestamp=1495706447426, + ) assert m2.timestamp == 1495706447426 assert m2.previous is m1 assert m2.sequence == 2 assert m2.signature == signature m2.verify(signature) - assert m2.key == '%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256' + assert m2.key == "%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256" def test_remote_no_signature(remote_feed): with pytest.raises(ValueError): - Message(remote_feed, OrderedDict([ - ('type', 'about'), - ('about', remote_feed.id), - ('name', 'neo'), - ('description', 'The Chosen One') - ]), None, timestamp=1495706260190) + Message( + remote_feed, + OrderedDict( + [("type", "about"), ("about", remote_feed.id), ("name", "neo"), ("description", "The Chosen One")] + ), + None, + timestamp=1495706260190, + ) def test_serialize(local_feed): - m1 = LocalMessage(local_feed, OrderedDict([ - ('type', 'about'), - ('about', local_feed.id), - ('name', 'neo'), - ('description', 'The Chosen One') - ]), timestamp=1495706260190) + m1 = LocalMessage( + local_feed, + OrderedDict([("type", "about"), ("about", local_feed.id), ("name", "neo"), ("description", "The Chosen One")]), + timestamp=1495706260190, + ) assert m1.serialize() == SERIALIZED_M1 def test_parse(local_feed): m1 = LocalMessage.parse(SERIALIZED_M1, local_feed) - assert m1.content == { - 'type': 'about', - 'about': local_feed.id, - 'name': 'neo', - 'description': 'The Chosen One' - } + assert m1.content == {"type": "about", "about": local_feed.id, "name": "neo", "description": "The Chosen One"} assert m1.timestamp == 1495706260190 diff --git a/tests/test_packet_stream.py b/tests/test_packet_stream.py index c4a61d7..6db5395 100644 --- a/tests/test_packet_stream.py +++ b/tests/test_packet_stream.py @@ -14,20 +14,25 @@ async def _collect_messages(generator): results.append(msg) return results -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"}') +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"}' +) class MockSHSSocket(SHSDuplexStream): @@ -107,26 +112,23 @@ async def test_message_decoding(ps_client): assert ps.is_connected - 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"}' - ]) + 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"}', + ] + ) - messages = (await _collect_messages(ps)) + messages = await _collect_messages(ps) assert len(messages) == 1 assert messages[0].type == PSMessageType.JSON assert messages[0].body == { - 'name': ['createHistoryStream'], - 'args': [ - { - 'id': '@omgyp7Pnrw+Qm0I6T6Fh5VvnKmodMXwnxTIesW2DgMg=.ed25519', - 'seq': 10, - 'live': True, - 'keys': False - } + "name": ["createHistoryStream"], + "args": [ + {"id": "@omgyp7Pnrw+Qm0I6T6Fh5VvnKmodMXwnxTIesW2DgMg=.ed25519", "seq": 10, "live": True, "keys": False} ], - 'type': 'source' + "type": "source", } @@ -138,26 +140,26 @@ async def test_message_encoding(ps_client): assert ps.is_connected - ps.send({ - 'name': ['createHistoryStream'], - 'args': [{ - 'id': "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", - 'seq': 1, - 'live': False, - 'keys': False - }], - 'type': 'source' - }, stream=True) + ps.send( + { + "name": ["createHistoryStream"], + "args": [ + {"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False} + ], + "type": "source", + }, + stream=True, + ) header, body = list(ps_client.get_output()) - assert header == b'\x0a\x00\x00\x00\xa6\x00\x00\x00\x01' - assert json.loads(body.decode('utf-8')) == { + assert header == b"\x0a\x00\x00\x00\xa6\x00\x00\x00\x01" + assert json.loads(body.decode("utf-8")) == { "name": ["createHistoryStream"], "args": [ {"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False} ], - "type": "source" + "type": "source", } @@ -166,56 +168,57 @@ async def test_message_stream(ps_client, mocker): await ps_client.connect() 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 - ps.send({ - 'name': ['createHistoryStream'], - 'args': [{ - 'id': "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", - 'seq': 1, - 'live': False, - 'keys': False - }], - 'type': 'source' - }, stream=True) + ps.send( + { + "name": ["createHistoryStream"], + "args": [ + {"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False} + ], + "type": "source", + }, + stream=True, + ) assert ps.req_counter == 2 assert ps.register_handler.call_count == 1 handler = list(ps._event_map.values())[0][1] - mock_process = mocker.AsyncMock() - mocker.patch.object(handler, 'process', mock_process) + mock_process = mocker.patch.object(handler, "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() assert mock_process.await_count == 1 # responses have negative req assert msg.req == -1 - assert msg.body['previous'] == '%KTGP6W8vF80McRAZHYDWuKOD0KlNyKSq6Gb42iuV7Iw=.sha256' + assert msg.body["previous"] == "%KTGP6W8vF80McRAZHYDWuKOD0KlNyKSq6Gb42iuV7Iw=.sha256" assert ps.req_counter == 2 - stream_handler = ps.send({ - 'name': ['createHistoryStream'], - 'args': [{ - 'id': "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", - 'seq': 1, - 'live': False, - 'keys': False - }], - 'type': 'source' - }, stream=True) + stream_handler = ps.send( + { + "name": ["createHistoryStream"], + "args": [ + {"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False} + ], + "type": "source", + }, + stream=True, + ) assert ps.req_counter == 3 assert ps.register_handler.call_count == 2 handler = list(ps._event_map.values())[1][1] - 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]) + 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] + ) # execute both message polling and response handling loops collected, handled = await gather(_collect_messages(ps), _collect_messages(stream_handler)) @@ -236,29 +239,30 @@ async def test_message_request(ps_server, mocker): 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({ - 'name': ['whoami'], - 'args': [] - }) + ps.send({"name": ["whoami"], "args": []}) header, body = list(ps_server.get_output()) - assert header == b'\x02\x00\x00\x00 \x00\x00\x00\x01' - assert json.loads(body.decode('utf-8')) == {"name": ["whoami"], "args": []} + assert header == b"\x02\x00\x00\x00 \x00\x00\x00\x01" + assert json.loads(body.decode("utf-8")) == {"name": ["whoami"], "args": []} assert ps.req_counter == 2 assert ps.register_handler.call_count == 1 handler = list(ps._event_map.values())[0][1] - mock_process = mocker.AsyncMock() - mocker.patch.object(handler, 'process', mock_process) - ps_server.feed([b'\x02\x00\x00\x00>\xff\xff\xff\xff', - b'{"id":"@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519"}']) + mock_process = mocker.patch.object(handler, "process") + + ps_server.feed( + [ + b"\x02\x00\x00\x00>\xff\xff\xff\xff", + b'{"id":"@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519"}', + ] + ) msg = await ps.read() assert mock_process.await_count == 1 # responses have negative req assert msg.req == -1 - assert msg.body['id'] == '@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519' + assert msg.body["id"] == "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519" assert ps.req_counter == 2 diff --git a/tests/test_util.py b/tests/test_util.py index 0d171fd..5b40ca5 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -16,21 +16,21 @@ CONFIG_FILE = """ } """ -CONFIG_FILE_INVALID = CONFIG_FILE.replace('ed25519', 'foo') +CONFIG_FILE_INVALID = CONFIG_FILE.replace("ed25519", "foo") 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() 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 bytes(secret['keypair']) == priv_key - assert bytes(secret['keypair'].verify_key) == b64decode('rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=') + assert secret["id"] == "@rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=.ed25519" + assert bytes(secret["keypair"]) == priv_key + assert bytes(secret["keypair"].verify_key) == b64decode("rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=") def test_load_exception(): 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()