Skip to content

Commit 530d068

Browse files
committed
docs(tests): state application, transport and vendor tests' behaviour without change-relative narrative (#1103)
Rewrites comments and docstrings that described a fix relative to the tree before it ("now", "used to", "pre-fix", "before the fix", "the old X", PR-round history) as statements of the behaviour and of the defect each test guards against. Prose only: the AST with docstrings stripped is unchanged in every file.
1 parent 9d4c409 commit 530d068

53 files changed

Lines changed: 1492 additions & 1869 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎tests/protocols/application/test_ftp_http1_raw_unit.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# -*- coding: utf-8 -*-
22
"""FTP and HTTP/1.* keep what they normalise. C.f. #1302, #1303, #1333.
33
4-
- #1302: HTTP/1.* field values decoded with a guessed charset were encoded back
5-
as UTF-8. The field lines are now kept as received (``raw_header``).
6-
- #1303: a multi-line FTP reply arriving whole in one segment did not parse. The
7-
lines after the first are now kept as received (``lines``).
4+
- #1302: HTTP/1.* field values decoded with a guessed charset must not be
5+
encoded back as UTF-8. The field lines are kept as received (``raw_header``).
6+
- #1303: a multi-line FTP reply arriving whole in one segment parses. The lines
7+
after the first are kept as received (``lines``).
88
- #1333: the FTP command case and the HTTP/1.* whitespace -- around start-line
99
tokens, around the field colon, and in ``obs-fold`` -- were normalised on
1010
rebuild. The parsed values stay normalised for lookup, and the original

‎tests/protocols/application/test_ftp_http1_roundtrip_unit.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
one.
1111
- #1241: text decoded with a detected charset was encoded back as UTF-8, so
1212
non-UTF-8 octets in FTP arguments and HTTP/1.* start lines changed on rebuild.
13-
The charset is now recorded on the data whenever UTF-8 would not reproduce the
13+
The charset is recorded on the data whenever UTF-8 would not reproduce the
1414
octets.
1515
1616
"""

‎tests/protocols/application/test_ftp_http_unassigned_code_1443_unit.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# -*- coding: utf-8 -*-
22
"""FTP reply codes and HTTP status codes parse and rebuild for every three digits.
33
4-
GitHub issue #1443: :class:`~pcapkit.const.ftp.return_code.ReturnCode` resolved
5-
only 100 to 659 and :class:`~pcapkit.const.http.status_code.StatusCode` only
6-
100 to 599, so a reply such as ``999 hi`` or a status line such as
7-
``HTTP/1.1 999 X`` raised :exc:`~pcapkit.utilities.exceptions.EnumValueError`
8-
and could not be parsed at all. Both wire fields are three digits, so every
9-
value from 000 to 999 now resolves, to ``Unassigned`` inside the registry's
10-
range and ``Unknown`` outside it, and the packet rebuilds byte for byte.
4+
GitHub issue #1443: guards against
5+
:class:`~pcapkit.const.ftp.return_code.ReturnCode` resolving only 100 to 659 and
6+
:class:`~pcapkit.const.http.status_code.StatusCode` only 100 to 599, which made a
7+
reply such as ``999 hi`` or a status line such as ``HTTP/1.1 999 X`` raise
8+
:exc:`~pcapkit.utilities.exceptions.EnumValueError` and fail to parse at all.
9+
Both wire fields are three digits, so every value from 000 to 999 resolves, to
10+
``Unassigned`` inside the registry's range and ``Unknown`` outside it, and the
11+
packet rebuilds byte for byte.
1112
``make()`` also writes the code as three digits, so ``099`` keeps its zero.
1213
1314
Every case builds its own octets in memory and reads no capture. Classes are

‎tests/protocols/application/test_ftp_unit.py‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,20 @@ def test_command_get_is_case_insensitive(self) -> None:
116116
# form raised too.
117117
self.assertIs(Command('retr'), Command.RETR)
118118

119-
# GitHub issue #860: a genuinely unknown command no longer registers
119+
# GitHub issue #860: a genuinely unknown command does not register
120120
# at all -- the owner's ruling is that ``get``/``_missing_`` have no
121121
# way to supply ``feat``/``desc``/``type``/``conf``, so minting one
122122
# would register a permanently hollowed-out member; only
123123
# ``register()`` can do that properly. The *value* keeps the
124-
# caller's own casing (unchanged from before #860, and the same
125-
# convention :class:`~pcapkit.const.ftp.command.FEATCode` already
126-
# used) -- only the *name* is canonicalised -- so a repeated call
127-
# with the same casing is *equal* but never *identical* (nothing is
128-
# cached to be identical to any more), while a different-case call
129-
# is a genuinely different value and is correctly *not* equal: on
130-
# ``main`` minting's cache silently returned the first casing seen
131-
# for every later call regardless of case, which is exactly the
132-
# "registered enum out of an unrecognised value" #860 removes.
124+
# caller's own casing (the same convention
125+
# :class:`~pcapkit.const.ftp.command.FEATCode` uses) -- only the
126+
# *name* is canonicalised -- so a repeated call with the same casing
127+
# is *equal* but never *identical* (nothing is cached to be
128+
# identical to), while a different-case call is a genuinely
129+
# different value and is correctly *not* equal. A minting cache
130+
# would return the first casing seen for every later call
131+
# regardless of case, which is exactly the "registered enum out of
132+
# an unrecognised value" #860 rules out.
133133
unknown = Command.get('xyzw')
134134
self.assertEqual(unknown._name_, 'XYZW')
135135
self.assertEqual(unknown, 'xyzw')
@@ -146,7 +146,8 @@ def test_command_get_is_case_insensitive(self) -> None:
146146
def test_ftp_read_parses_a_lowercase_request(self) -> None:
147147
"""The case-insensitivity is reachable from wire data: ``ftp.py``
148148
compiles ``FTP_REQUEST`` with :data:`re.I` and passes the match
149-
verbatim, so a lowercase request used to crash the parse -- #582.
149+
verbatim, so the lookup must accept a lowercase request rather than
150+
crash the parse -- #582.
150151
"""
151152
from pcapkit.const.ftp.command import Command
152153
from pcapkit.protocols.application.ftp import FTP, Type

‎tests/protocols/application/test_http_from_data_1154_unit.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# -*- coding: utf-8 -*-
22
"""``HTTP.from_data`` rebuilds the message it was parsed from. C.f. #1154.
33
4-
:meth:`HTTP._make_data <pcapkit.protocols.application.http.HTTP._make_data>`
5-
chose the versioned class from a ``version`` key that neither HTTP/1.* nor
6-
HTTP/2 data carries, so every ``HTTP.from_data(http.info)`` raised
7-
``ProtocolError: invalid HTTP version: 0``. It now dispatches on the class of
8-
the data, and passes the ``version`` that :meth:`HTTP.make
9-
<pcapkit.protocols.application.http.HTTP.make>` dispatches on.
4+
Guards against :meth:`HTTP._make_data
5+
<pcapkit.protocols.application.http.HTTP._make_data>` choosing the versioned
6+
class from a ``version`` key that neither HTTP/1.* nor HTTP/2 data carries,
7+
which made every ``HTTP.from_data(http.info)`` raise ``ProtocolError: invalid
8+
HTTP version: 0``. It dispatches on the class of the data, and passes the
9+
``version`` that :meth:`HTTP.make <pcapkit.protocols.application.http.HTTP.make>`
10+
dispatches on.
1011
1112
Behind that, :meth:`httpv2.HTTP._make_data
12-
<pcapkit.protocols.application.httpv2.HTTP._make_data>` returned the frame's
13-
``length``, which ``ProtocolBase.__init__`` takes as the number of octets to
14-
parse back -- the payload alone, nine short of the frame -- so every HTTP/2
15-
``from_data`` raised as well. ``make`` computes the Length itself, so the key
16-
is no longer returned.
13+
<pcapkit.protocols.application.httpv2.HTTP._make_data>` must not return the
14+
frame's ``length``, which ``ProtocolBase.__init__`` takes as the number of
15+
octets to parse back -- the payload alone, nine short of the frame -- so every
16+
HTTP/2 ``from_data`` would raise as well. ``make`` computes the Length itself,
17+
so the key is not returned.
1718
1819
The frames are the Scapy-built ones of
1920
``test_http_guess_http2_1141_unit.LEGAL_FRAMES``, embedded as bytes.
@@ -93,7 +94,7 @@ def test_dispatcher_rebuilds_http2_frames(self) -> None:
9394
self._round_trip(HTTP, bytes.fromhex(octets))
9495

9596
def test_httpv2_rebuilds_its_own_frames(self) -> None:
96-
"""``httpv2.HTTP.from_data`` no longer parses back only Length octets."""
97+
"""``httpv2.HTTP.from_data`` parses back the whole frame, not only Length octets."""
9798
from pcapkit.protocols.application.httpv2 import HTTP as HTTPv2
9899

99100
for name, octets in HTTP2_FRAMES:

‎tests/protocols/application/test_http_guess_http2_1141_unit.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# -*- coding: utf-8 -*-
22
"""HTTP's version guess takes a bare frame for HTTP/2 only if a sender could write it. C.f. #1141.
33
4-
Since #1138 an HTTP/2 frame's Length counts the payload alone
5-
(:rfc:`9113#section-4.1`), so any nine octets whose Length fits the buffer are
6-
self-consistent. :meth:`HTTP._guess_version
4+
An HTTP/2 frame's Length counts the payload alone (:rfc:`9113#section-4.1`,
5+
#1138), so any nine octets whose Length fits the buffer are self-consistent.
6+
:meth:`HTTP._guess_version
77
<pcapkit.protocols.application.http.HTTP._guess_version>` falls through to an
8-
HTTP/2 trial parse when neither the preface nor an HTTP/1 start line matches, and
9-
that trial accepted most zero-heavy binary: nine zero octets came back as
10-
``HTTP/2 type=0``, a ``DATA`` frame on stream 0, which :rfc:`9113#section-6.1`
11-
makes a protocol error.
8+
HTTP/2 trial parse when neither the preface nor an HTTP/1 start line matches. A
9+
trial that accepts any self-consistent header would take most zero-heavy binary:
10+
nine zero octets would come back as ``HTTP/2 type=0``, a ``DATA`` frame on stream
11+
0, which :rfc:`9113#section-6.1` makes a protocol error.
1212
13-
The guess now requires a frame type :rfc:`9113` defines, no flag that type
13+
The guess requires a frame type :rfc:`9113` defines, no flag that type
1414
leaves undefined, a clear reserved bit, and a stream that type may be sent on.
15-
Explicit ``version=2`` parsing is not affected, and is asserted unchanged here.
15+
Explicit ``version=2`` parsing is not subject to the guess, as asserted here too.
1616
1717
The legal frames in :data:`LEGAL_FRAMES` were built with Scapy 2.7.0's
1818
:mod:`scapy.contrib.http2` (``H2Frame`` over each frame class) and are embedded
@@ -70,8 +70,9 @@
7070
('ORIGIN, not an RFC 9113 type', '0000000c0000000000'),
7171
)
7272

73-
#: The seeded fuzz. Pre-fix, this corpus produced 566 HTTP/2 labels, of which
74-
#: 249 were ``DATA`` (most on stream 0) and 317 frames of unknown type.
73+
#: The seeded fuzz. Measured without the guess's checks, this corpus produced
74+
#: 566 HTTP/2 labels, of which 249 were ``DATA`` (most on stream 0) and 317
75+
#: frames of unknown type.
7576
FUZZ_SEED = 1141
7677
FUZZ_COUNT = 5000
7778
PRE_FIX_HTTP2_LABELS = 566
@@ -111,7 +112,7 @@ def test_nine_zero_octets_are_not_guessed_as_http2(self) -> None:
111112
self._guess(bytes(9))
112113

113114
def test_explicit_version_still_parses_nine_zero_octets(self) -> None:
114-
"""Explicit HTTP/2 parsing stays RFC-exact: only the guess is tightened."""
115+
"""Explicit HTTP/2 parsing is RFC-exact: the checks apply to the guess only."""
115116
from pcapkit.protocols.application.http import HTTP
116117

117118
http = HTTP(bytes(9), 9, version=2)

‎tests/protocols/application/test_http_runtime.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,14 @@ def test_malformed_http_payload_falls_back_to_raw(self) -> None:
104104
self.assertEqual(tcp.payload.name, 'Unknown')
105105

106106
# The registry key that selected the protocol whose parse then failed --
107-
# TCP port 80 here. It used to be :data:`None`, because ``beholder`` was
108-
# the one path to :class:`Raw` that did not forward ``alias``; that made a
109-
# *registered* number less informative than an unregistered one, which
110-
# reaches Raw through ``_import_next_layer`` and keeps its enumeration.
107+
# TCP port 80 here. Guards against it reading :data:`None`, as it would if
108+
# ``beholder`` did not forward ``alias`` on its path to :class:`Raw`; that
109+
# would make a *registered* number less informative than an unregistered
110+
# one, which reaches Raw through ``_import_next_layer`` and keeps its
111+
# enumeration.
111112
# ``Data_Raw.protocol`` is "the original enumeration of this protocol", and
112113
# 80 is what this payload arrived as, so the value belongs here. The
113-
# protochain still reads ``Raw``: a plain :obj:`int` has no ``name`` to
114+
# protochain reads ``Raw`` all the same: a plain :obj:`int` has no ``name`` to
114115
# render, and only an enumeration key -- SCTP's payload protocol
115116
# identifier, say -- shows through.
116117
self.assertEqual(tcp.payload.info.protocol, 80)

‎tests/protocols/application/test_http_test_start_line_1519_unit.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
44
The predicate :meth:`HTTP._guess_version
55
<pcapkit.protocols.application.http.HTTP._guess_version>` uses to recognise an
6-
HTTP/1.* start line was ``httpv1._test_start_line``, imported lazily by the
7-
dispatcher in ``http.py``. It is now
8-
:func:`~pcapkit.protocols.application.http.test_start_line`, and the patterns it
6+
HTTP/1.* start line is :func:`~pcapkit.protocols.application.http.test_start_line`,
7+
not a private ``httpv1._test_start_line`` imported lazily, and the patterns it
98
shares with the HTTP/1.* parser -- ``_RE_METHOD``, ``_RE_VERSION``,
10-
``_RE_STATUS`` and ``_HTTP2_PREFACE_HEADER`` -- moved with it, so ``httpv1.py``
9+
``_RE_STATUS`` and ``_HTTP2_PREFACE_HEADER`` -- live beside it, so ``httpv1.py``
1110
imports them from ``http.py`` rather than keeping a copy of its own.
1211
1312
The predicate is imported inside each test, never at module level: pytest

0 commit comments

Comments
 (0)