obelisk

Electrum server using libbitcoin as its backend
git clone https://git.parazyd.org/obelisk
Log | Files | Refs | README | LICENSE

commit 9cb7c061945451facab01970e1f0bd5f6c8b48c8
parent d684e27e0f453dd35e93c8d79325a20cfd5970b8
Author: parazyd <parazyd@dyne.org>
Date:   Mon, 19 Apr 2021 14:44:32 +0200

Some more coverage test cases.

Diffstat:
Mobelisk/protocol.py | 6+++++-
Mobelisk/util.py | 6+++---
Mtests/test_electrum_protocol.py | 40++++++++++++++++++++++++++++++++++++++++
3 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/obelisk/protocol.py b/obelisk/protocol.py @@ -555,6 +555,9 @@ class ElectrumProtocol(asyncio.Protocol): # pylint: disable=R0904,R0902 tx_hash = query["params"][0] verbose = query["params"][1] if len(query["params"]) > 1 else False + if not is_hex_str(tx_hash): + return JsonRPCError.invalidparams() + # _ec, rawtx = await self.bx.fetch_blockchain_transaction(tx_hash) _ec, rawtx = await self.bx.fetch_mempool_transaction(tx_hash) if _ec and _ec != 0 and _ec != ErrorCode.not_found.value: @@ -563,7 +566,8 @@ class ElectrumProtocol(asyncio.Protocol): # pylint: disable=R0904,R0902 # Behaviour is undefined in spec if not rawtx: - return {"result": None} + return JsonRPCError.internalerror() + # return {"result": None} if verbose: # TODO: Help needed diff --git a/obelisk/util.py b/obelisk/util.py @@ -30,7 +30,7 @@ def is_non_negative_integer(val): """Check if val is of type int and non-negative""" if is_integer(val): return val >= 0 - return False # pragma: no cover + return False def is_boolean(val): @@ -38,7 +38,7 @@ def is_boolean(val): return isinstance(val, bool) -def is_hex_str(text): # pragma: no cover +def is_hex_str(text): """Check if text is a hex string""" if not isinstance(text, str): return False @@ -52,7 +52,7 @@ def is_hex_str(text): # pragma: no cover return True -def is_hash256_str(text): # pragma: no cover +def is_hash256_str(text): """Check if text is a sha256 hash""" if not isinstance(text, str): return False diff --git a/tests/test_electrum_protocol.py b/tests/test_electrum_protocol.py @@ -92,6 +92,16 @@ async def test_server_version(protocol, writer, method): data = await protocol.server_version(writer, {"params": params}) assert_equal(data["result"], expect["result"]) + params = ["obelisk", "0.0"] + expect = JsonRPCError.protonotsupported() + data = await protocol.server_version(writer, {"params": params}) + assert_equal(data, expect) + + params = ["obelisk"] + expect = JsonRPCError.invalidparams() + data = await protocol.server_version(writer, {"params": params}) + assert_equal(data, expect) + async def test_ping(protocol, writer, method): params = [] @@ -280,6 +290,12 @@ async def test_transaction_get(protocol, writer, method): data = await protocol.transaction_get(writer, {"params": i}) assert_equal(data["result"], expect["result"]) + params = [[], [1], ["foo"], ["dead beef"]] + for i in params: + expect = JsonRPCError.invalidparams() + data = await protocol.transaction_get(writer, {"params": i}) + assert_equal(data, expect) + async def test_transaction_get_merkle(protocol, writer, method): params = [ @@ -293,6 +309,24 @@ async def test_transaction_get_merkle(protocol, writer, method): data = await protocol.transaction_get_merkle(writer, {"params": i}) assert_equal(data["result"], expect["result"]) + params = [ + [], + ["foo", 1], + [3, 1], + [ + "a9c3c22cc2589284288b28e802ea81723d649210d59dfa7e03af00475f4cec20", + -4, + ], + [ + "a9c3c22cc2589284288b28e802ea81723d649210d59dfa7e03af00475f4cec20", + "foo", + ], + ] + for i in params: + expect = JsonRPCError.invalidparams() + data = await protocol.transaction_get_merkle(writer, {"params": i}) + assert_equal(data, expect) + async def test_transaction_id_from_pos(protocol, writer, method): params = [[1970700, 28], [1970700, 28, True]] @@ -301,6 +335,12 @@ async def test_transaction_id_from_pos(protocol, writer, method): data = await protocol.transaction_id_from_pos(writer, {"params": i}) assert_equal(data["result"], expect["result"]) + params = [[123], [-1, 1], [1, -1], [3, 42, 4]] + for i in params: + expect = JsonRPCError.invalidparams() + data = await protocol.transaction_id_from_pos(writer, {"params": i}) + assert_equal(data, expect) + async def test_get_fee_histogram(protocol, writer, method): data = await protocol.get_fee_histogram(writer, {"params": []})