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
2 changes: 1 addition & 1 deletion pcapkit/protocols/internet/mh.py
Original file line number Diff line number Diff line change
Expand Up @@ -8050,7 +8050,7 @@ def _make_opt_mnp(self, type: 'Enum_Option', option: 'Optional[Data_MobileNetwor

prefix_val = ipaddress.ip_network(prefix)
if prefix_val.version != 6:
raise ProtocolError(f'{self.alias}: [OptNo {type}] invalid movile network prefix: {prefix!r}')
raise ProtocolError(f'{self.alias}: [OptNo {type}] invalid mobile network prefix: {prefix!r}')
prefix_length = prefix_val.prefixlen
prefix_addr = prefix_val.network_address

Expand Down
2 changes: 1 addition & 1 deletion pcapkit/protocols/schema/internet/ipv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def post_process(self, packet: 'dict[str, Any]') -> 'Schema':
ts_val = datetime.timedelta(milliseconds=ts)
timestamp.add(ip_val, ts_val)
else:
warn(f'IPv4: [OptNo {self.type}] invalid format: unknown timestmap flag: {ts_flag}', ProtocolWarning)
warn(f'IPv4: [OptNo {self.type}] invalid format: unknown timestamp flag: {ts_flag}', ProtocolWarning)
self.data = self.ts_data[:used]
timestamp = tuple(self.data)

Expand Down
53 changes: 53 additions & 0 deletions tests/protocols/internet/test_message_spelling_2171_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
"""Two user-facing messages are spelt correctly.

GitHub issue #2171: :meth:`TSOption.post_process
<pcapkit.protocols.schema.internet.ipv4.TSOption.post_process>` warned of an
"unknown timestmap flag", and :meth:`MH._make_opt_mnp
<pcapkit.protocols.internet.mh.MH._make_opt_mnp>` raised on an "invalid movile
network prefix".

Classes are imported inside each test, after
:func:`~tests._support.reimport_once_per_class`.

"""

import unittest
import warnings

from tests._support import reimport_once_per_class


class TestMessageSpelling(unittest.TestCase):
"""Pin the spelling of the IPv4 timestamp warning and the MH prefix error."""

def setUp(self) -> None:
reimport_once_per_class(self)

def test_unknown_timestamp_flag_warning(self) -> None:
from pcapkit.const.ipv4.option_number import OptionNumber
from pcapkit.const.ipv4.ts_flag import TSFlag
from pcapkit.protocols.schema.internet.ipv4 import TSOption
from pcapkit.utilities.warnings import ProtocolWarning

option = TSOption(type=OptionNumber.TS, length=12, pointer=13,
flags={'oflw': 0, 'flag': TSFlag.get(2)}, ts_data=[1, 2])
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter('always')
option.pack()
messages = [str(w.message) for w in caught if issubclass(w.category, ProtocolWarning)]
self.assertEqual(len(messages), 1, messages)
self.assertIn('unknown timestamp flag: 2', messages[0])

def test_invalid_mobile_network_prefix_error(self) -> None:
from pcapkit.const.mh.option import Option
from pcapkit.protocols.internet.mh import MH
from pcapkit.utilities.exceptions import ProtocolError

proto = object.__new__(MH)
with self.assertRaisesRegex(ProtocolError, "invalid mobile network prefix: '192.0.2.0/24'"):
proto._make_opt_mnp(Option.Mobile_Network_Prefix_Option, prefix='192.0.2.0/24')


if __name__ == '__main__':
unittest.main()
Loading