Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/btclib/obfuscation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
from btclib import var_bytes
from btclib.alias import BinaryData, Octets
from btclib.exceptions import BTClibTypeError, BTClibValueError
from btclib.utils import bytes_from_octets, bytesio_from_binarydata, is_integer
from btclib.utils import (
_message_text,
bytes_from_octets,
bytesio_from_binarydata,
is_integer,
)

__all__ = [
"KEY_SIZE",
Expand All @@ -47,7 +52,7 @@ def obfuscate(data: Octets, key: Octets, offset: int = 0) -> bytes:
data = bytes_from_octets(data)
key = bytes_from_octets(key, KEY_SIZE)
if not is_integer(offset):
raise BTClibTypeError(f"non-integer offset: {offset}")
raise BTClibTypeError(f"non-integer offset: {_message_text(offset)}")
if offset < 0:
raise BTClibValueError(f"negative offset: {offset}")

Expand Down
5 changes: 3 additions & 2 deletions src/btclib/var_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from btclib.exceptions import BTClibTypeError, BTClibValueError
from btclib.utils import (
_message_hex,
_message_text,
bytesio_from_binarydata,
hex_string,
is_integer,
Expand Down Expand Up @@ -70,7 +71,7 @@ def parse(stream: BinaryData, max_size: int = MAX_SIZE) -> int:
decodes to.
"""
if not is_integer(max_size):
raise BTClibTypeError(f"non-integer max_size: {max_size}")
raise BTClibTypeError(f"non-integer max_size: {_message_text(max_size)}")

stream = bytesio_from_binarydata(stream)

Expand Down Expand Up @@ -129,7 +130,7 @@ def serialize(i: int) -> bytes:
here to answer instead.
"""
if not is_integer(i):
raise BTClibTypeError(f"non-integer var_int: {i}")
raise BTClibTypeError(f"non-integer var_int: {_message_text(i)}")
if i < 0x00:
raise BTClibValueError("negative integer")
if i < 0xFD: # 1 byte
Expand Down
13 changes: 13 additions & 0 deletions tests/obfuscation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
v31.1 (9be056a8a7), cited by line where each is used.
"""

from fractions import Fraction
from io import BytesIO
from typing import Any

Expand Down Expand Up @@ -181,3 +182,15 @@ def test_offset_is_a_non_negative_integer() -> None:
obfuscation.obfuscate(b"data", bytes(8), bad) # type: ignore[arg-type]
with pytest.raises(BTClibValueError, match="negative offset"):
obfuscation.obfuscate(b"data", bytes(8), -1)


def test_an_offset_too_large_to_print_is_still_a_type_error() -> None:
"""An offset `str()` cannot write is described, as `var_int`'s are.

A Fraction whose numerator runs past the digit limit must not escape
through the refusal's own message as a plain ValueError. The message
describes it instead of quoting it.
"""
unwritable = r"a Fraction str\(\) cannot write$"
with pytest.raises(BTClibTypeError, match=f"non-integer offset: {unwritable}"):
obfuscation.obfuscate(b"data", bytes(8), Fraction(10**4400, 3)) # type: ignore[arg-type]
19 changes: 19 additions & 0 deletions tests/var_int_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"""Tests for the `btclib.var_int` module."""

from fractions import Fraction

import pytest
from hypothesis import given
from hypothesis import strategies as st
Expand Down Expand Up @@ -126,6 +128,23 @@ def test_a_bool_is_neither_a_count_nor_a_cap() -> None:
assert var_int.parse(b"\x01", max_size=1) == 1


def test_a_fraction_too_large_to_print_is_still_a_type_error() -> None:
"""A refusal's message quotes the refused value.

`str()` of an int past the digit limit raises ValueError -- so a
Fraction whose numerator runs past it would leave through the
refusal's own message as the wrong exception, not the library's. The
message describes such a value instead of quoting it, and both entry
points still raise the type error the caller contracted for.
"""
too_large_to_print = Fraction(10**4400, 3)
unwritable = r"a Fraction str\(\) cannot write$"
with pytest.raises(BTClibTypeError, match=f"non-integer var_int: {unwritable}"):
var_int.serialize(too_large_to_print) # type: ignore[arg-type]
with pytest.raises(BTClibTypeError, match=f"non-integer max_size: {unwritable}"):
var_int.parse(b"\x01", max_size=too_large_to_print) # type: ignore[arg-type]


def test_var_int_non_canonical() -> None:
"""Only the shortest encoding is valid, as in Bitcoin Core."""
# 1 fits in one byte, 0xfd in two, 0x10000 in four. fdfc00 and
Expand Down
Loading