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
8 changes: 6 additions & 2 deletions pcapkit/protocols/link/loopback.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,16 @@ def make(self,

Raises:
ProtocolError: If ``byteorder`` is neither ``'big'`` nor
``'little'``, or ``family`` does not fit in 4 octets.
``'little'``, or ``family`` is not an :obj:`int` (a
:obj:`bool` included, :issue:`2289`) or does not fit in 4
octets.

"""
if byteorder not in ('big', 'little'):
raise ProtocolError(f'Loopback: invalid byte order: {byteorder!r}')
if not 0 <= family <= 0xFFFF_FFFF:
# NOTE: ``bool`` is an ``int`` subclass, so without the explicit check
# ``True`` would pack as address family 1 (#2289).
if isinstance(family, bool) or not isinstance(family, int) or not 0 <= family <= 0xFFFF_FFFF:
raise ProtocolError(f'Loopback: invalid address family: {family!r}')

return Schema_Loopback(
Expand Down
13 changes: 13 additions & 0 deletions tests/protocols/link/test_loopback_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ def test_make_rejects_what_does_not_fit(self) -> None:
with self.subTest(**kwargs), self.assertRaises(ProtocolError):
Loopback(**kwargs)

def test_make_rejects_a_family_that_is_not_an_int(self) -> None:
# #2289: a bool is an int and packs as family 1; a non-int fails the
# range comparison or ``to_bytes`` with a bare ``TypeError`` or
# ``AttributeError``. Each must raise ``ProtocolError``.
from pcapkit.protocols.link.loopback import Loopback
from pcapkit.utilities.exceptions import ProtocolError

for family in (True, False, '2', 2.0, None):
with self.subTest(family=family):
with self.assertRaises(ProtocolError) as ctx:
Loopback(family=family)
self.assertEqual(str(ctx.exception), f'Loopback: invalid address family: {family!r}')

def test_registered_for_null_and_loop(self) -> None:
from pcapkit.const.reg.linktype import LinkType
from pcapkit.corekit.module import ModuleDescriptor
Expand Down
Loading