|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""``HTTP.make`` builds every HTTP/2 frame type without a ``frame`` argument. |
| 3 | +
|
| 4 | +GitHub issue #2274: ``make`` defaulted ``frame`` to ``b''``, which is packed as |
| 5 | +raw octets with Length 0. A frame type whose payload is of fixed or minimum size |
| 6 | +is longer than that, so ``HTTP(type=<T>, sid=...)`` could not build one at all: |
| 7 | +``PRIORITY``, ``RST_STREAM``, ``PING`` and ``WINDOW_UPDATE`` raised |
| 8 | +``HTTP/2: [Type <T>] invalid format`` and ``PUSH_PROMISE`` and ``GOAWAY`` raised |
| 9 | +``Field <f> resolved to a negative length``, each after a ``SchemaWarning: |
| 10 | +packet length < 0``. ``frame={}``, which dispatches to the frame type's own |
| 11 | +constructor, built them. |
| 12 | +
|
| 13 | +``frame`` now defaults to :data:`None` and takes that dispatch, so the default |
| 14 | +call and ``frame={}`` give the same octets for every frame type. An explicit |
| 15 | +:obj:`bytes` value, ``b''`` included, is still packed raw. ``SETTINGS`` needed |
| 16 | +the other half of the fix: ``_make_http_settings`` rejected its own |
| 17 | +``settings=None`` default, so ``frame={}`` raised there instead, and |
| 18 | +:data:`None` now builds the parameterless ``SETTINGS`` frame |
| 19 | +:rfc:`9113#section-6.5` permits. |
| 20 | +
|
| 21 | +Every case builds its own octets in memory and reads no capture. Classes are |
| 22 | +imported inside each test, after :func:`~tests._support.reimport_once_per_class`. |
| 23 | +
|
| 24 | +""" |
| 25 | + |
| 26 | +import unittest |
| 27 | +from typing import Any |
| 28 | + |
| 29 | +from tests._support import reimport_once_per_class |
| 30 | + |
| 31 | +#: Octets ``HTTP(type=<name>, sid=<sid>)`` builds, by frame type name and Stream |
| 32 | +#: Identifier. Every one of them is also what ``frame={}`` builds. |
| 33 | +DEFAULT_FRAMES = { |
| 34 | + 'DATA': (1, bytes.fromhex('000000000000000001')), |
| 35 | + 'HEADERS': (1, bytes.fromhex('000000010000000001')), |
| 36 | + 'PRIORITY': (1, bytes.fromhex('000005020000000001' '00000000' '00')), |
| 37 | + 'RST_STREAM': (1, bytes.fromhex('000004030000000001' '00000000')), |
| 38 | + 'SETTINGS': (0, bytes.fromhex('000000040000000000')), |
| 39 | + 'PUSH_PROMISE': (1, bytes.fromhex('000004050000000001' '00000000')), |
| 40 | + 'PING': (0, bytes.fromhex('000008060000000000' '0000000000000000')), |
| 41 | + 'GOAWAY': (0, bytes.fromhex('000008070000000000' '00000000' '00000000')), |
| 42 | + 'WINDOW_UPDATE': (1, bytes.fromhex('000004080000000001' '00000000')), |
| 43 | + 'CONTINUATION': (1, bytes.fromhex('000000090000000001')), |
| 44 | +} |
| 45 | + |
| 46 | +#: An unassigned frame type, whose payload has no fixed size, so the default and |
| 47 | +#: ``frame={}`` agreed on it before the fix as well. |
| 48 | +UNASSIGNED_TYPE = 0xfa |
| 49 | + |
| 50 | + |
| 51 | +class TestHTTPv2DefaultFrame(unittest.TestCase): |
| 52 | + """Pin what ``make`` builds when ``frame`` is left out.""" |
| 53 | + |
| 54 | + def setUp(self) -> None: |
| 55 | + reimport_once_per_class(self) |
| 56 | + |
| 57 | + def build(self, type: 'Any', sid: 'int', **kwargs: 'Any') -> 'Any': |
| 58 | + """Build the HTTP/2 frame of ``type`` on stream ``sid``.""" |
| 59 | + from pcapkit.protocols.application.httpv2 import HTTP |
| 60 | + |
| 61 | + return HTTP(type=type, sid=sid, **kwargs) |
| 62 | + |
| 63 | + def test_default_builds_every_frame_type(self) -> None: |
| 64 | + from pcapkit.const.http.frame import Frame |
| 65 | + |
| 66 | + for name, (sid, wire) in DEFAULT_FRAMES.items(): |
| 67 | + with self.subTest(frame=name): |
| 68 | + self.assertEqual(bytes(self.build(Frame[name], sid)).hex(), wire.hex()) |
| 69 | + |
| 70 | + def test_default_agrees_with_an_empty_mapping(self) -> None: |
| 71 | + from pcapkit.const.http.frame import Frame |
| 72 | + |
| 73 | + for name, (sid, _) in DEFAULT_FRAMES.items(): |
| 74 | + with self.subTest(frame=name): |
| 75 | + self.assertEqual(bytes(self.build(Frame[name], sid)).hex(), |
| 76 | + bytes(self.build(Frame[name], sid, frame={})).hex()) |
| 77 | + |
| 78 | + def test_default_frames_round_trip(self) -> None: |
| 79 | + from pcapkit.const.http.frame import Frame |
| 80 | + from pcapkit.protocols.application.httpv2 import HTTP |
| 81 | + |
| 82 | + for name, (sid, wire) in DEFAULT_FRAMES.items(): |
| 83 | + with self.subTest(frame=name): |
| 84 | + made = bytes(self.build(Frame[name], sid)) |
| 85 | + parsed = HTTP(made, len(made)) |
| 86 | + self.assertEqual(parsed.info.type, Frame[name]) |
| 87 | + self.assertEqual(parsed.info.sid, sid) |
| 88 | + self.assertEqual(bytes(HTTP.from_data(parsed.info)).hex(), wire.hex()) |
| 89 | + |
| 90 | + def test_an_unassigned_type_is_unchanged(self) -> None: |
| 91 | + wire = bytes.fromhex('000000fa0000000001') |
| 92 | + for label, kwargs in (('default', {}), ('mapping', {'frame': {}}), ('bytes', {'frame': b''})): |
| 93 | + with self.subTest(frame=label): |
| 94 | + self.assertEqual(bytes(self.build(UNASSIGNED_TYPE, 1, **kwargs)).hex(), wire.hex()) |
| 95 | + |
| 96 | + def test_explicit_bytes_are_still_packed_raw(self) -> None: |
| 97 | + from pcapkit.const.http.frame import Frame |
| 98 | + |
| 99 | + # Raw octets are packed as given, rather than through the frame type's |
| 100 | + # constructor, so they need not be the payload that type would build. |
| 101 | + made = bytes(self.build(Frame.RST_STREAM, 1, frame=b'\x00\x00\x00\x08')) |
| 102 | + self.assertEqual(made.hex(), '000004030000000001' '00000008') |
| 103 | + self.assertEqual(bytes(self.build(Frame.DATA, 1, frame=b'abc')).hex(), |
| 104 | + '000003000000000001' '616263') |
| 105 | + |
| 106 | + def test_empty_bytes_still_build_a_zero_length_frame(self) -> None: |
| 107 | + from pcapkit.const.http.frame import Frame |
| 108 | + |
| 109 | + # ``b''`` is an explicit request for no payload, which is what a frame |
| 110 | + # type of no fixed size builds anyway. |
| 111 | + for name in ('DATA', 'HEADERS', 'SETTINGS', 'CONTINUATION'): |
| 112 | + with self.subTest(frame=name): |
| 113 | + sid, wire = DEFAULT_FRAMES[name] |
| 114 | + self.assertEqual(bytes(self.build(Frame[name], sid, frame=b'')).hex(), wire.hex()) |
| 115 | + |
| 116 | + |
| 117 | +class TestHTTPv2EmptySettings(unittest.TestCase): |
| 118 | + """Pin the ``SETTINGS`` frame ``_make_http_settings`` builds from ``None``.""" |
| 119 | + |
| 120 | + def setUp(self) -> None: |
| 121 | + reimport_once_per_class(self) |
| 122 | + |
| 123 | + def test_none_builds_a_parameterless_settings_frame(self) -> None: |
| 124 | + from pcapkit.const.http.frame import Frame |
| 125 | + from pcapkit.protocols.application.httpv2 import HTTP |
| 126 | + |
| 127 | + for label, frame in (('default', {}), ('explicit', {'settings': None})): |
| 128 | + with self.subTest(settings=label): |
| 129 | + made = bytes(HTTP(type=Frame.SETTINGS, sid=0, frame=frame)) |
| 130 | + self.assertEqual(made.hex(), DEFAULT_FRAMES['SETTINGS'][1].hex()) |
| 131 | + self.assertEqual(HTTP(made, len(made)).info.length, 0) |
| 132 | + |
| 133 | + def test_the_schema_carries_no_entries(self) -> None: |
| 134 | + from pcapkit.protocols.application.httpv2 import HTTP |
| 135 | + |
| 136 | + schema, flags = HTTP.__new__(HTTP)._make_http_settings() |
| 137 | + self.assertEqual(list(schema.settings), []) |
| 138 | + self.assertEqual(int(flags), 0) |
| 139 | + |
| 140 | + def test_an_empty_settings_frame_still_takes_the_ack_flag(self) -> None: |
| 141 | + from pcapkit.const.http.frame import Frame |
| 142 | + from pcapkit.protocols.application.httpv2 import HTTP |
| 143 | + |
| 144 | + made = bytes(HTTP(type=Frame.SETTINGS, sid=0, frame={'ack': True})) |
| 145 | + self.assertEqual(made.hex(), '000000040100000000') |
| 146 | + |
| 147 | + def test_other_settings_types_are_still_rejected(self) -> None: |
| 148 | + from pcapkit.const.http.frame import Frame |
| 149 | + from pcapkit.protocols.application.httpv2 import HTTP |
| 150 | + from pcapkit.utilities.exceptions import ProtocolError |
| 151 | + |
| 152 | + for settings, tail in ((42, '42'), ('x', "'x'"), (4.5, '4.5')): |
| 153 | + with self.subTest(settings=settings): |
| 154 | + with self.assertRaises(ProtocolError) as ctx: |
| 155 | + bytes(HTTP(type=Frame.SETTINGS, frame={'settings': settings})) |
| 156 | + self.assertEqual(str(ctx.exception), |
| 157 | + f'HTTP/2: [Type 4] invalid settings: {tail}') |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == '__main__': |
| 161 | + unittest.main() |
0 commit comments