|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""``ListField`` and ``SchemaField`` calls work on the copy, not the template. |
| 3 | +
|
| 4 | +GitHub issue #2173. Calling a field with a packet returns a fresh copy, which is |
| 5 | +what the schema then reads with. :meth:`ListField.__call__ |
| 6 | +<pcapkit.corekit.fields.collections.ListField.__call__>` handed its ``callback`` |
| 7 | +the template instead of that copy, so a callback adjusting its field mutated the |
| 8 | +template shared by every later packet and missed the copy actually in use. |
| 9 | +:meth:`SchemaField.__call__ <pcapkit.corekit.fields.misc.SchemaField.__call__>` |
| 10 | +sized the copy's ``template`` from the template's ``-1`` length rather than the |
| 11 | +copy's resolved one, so it stayed ``'1024s'``. Both read the copy. |
| 12 | +""" |
| 13 | + |
| 14 | +import unittest |
| 15 | +from typing import TYPE_CHECKING |
| 16 | + |
| 17 | +from pcapkit.corekit.fields.collections import ListField |
| 18 | +from pcapkit.corekit.fields.misc import SchemaField |
| 19 | +from pcapkit.corekit.fields.numbers import UInt8Field, UInt16Field |
| 20 | +from pcapkit.protocols.schema.schema import Schema |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from typing import Any |
| 24 | + |
| 25 | + |
| 26 | +class Pair(Schema): |
| 27 | + """Four-octet schema for :class:`SchemaField`.""" |
| 28 | + |
| 29 | + a: 'int' = UInt16Field() |
| 30 | + b: 'int' = UInt16Field() |
| 31 | + |
| 32 | + |
| 33 | +def tag_field(field: 'Any', packet: 'dict[str, Any]') -> 'None': |
| 34 | + """Callback that adjusts the field it is given, as a byte-order one would.""" |
| 35 | + field.tag = packet['n'] |
| 36 | + |
| 37 | + |
| 38 | +class ListFieldCallbackTests(unittest.TestCase): |
| 39 | + """``ListField.__call__`` passes its callback the copy.""" |
| 40 | + |
| 41 | + def test_callback_receives_the_copy(self) -> None: |
| 42 | + seen = [] # type: list[ListField] |
| 43 | + template = ListField(length=lambda pkt: pkt['n'], item_type=UInt8Field(), |
| 44 | + callback=lambda field, _: seen.append(field)) |
| 45 | + copy = template({'n': 2}) |
| 46 | + self.assertEqual(len(seen), 1) |
| 47 | + self.assertIs(seen[0], copy) |
| 48 | + self.assertIsNot(seen[0], template) |
| 49 | + |
| 50 | + def test_template_is_untouched_across_calls(self) -> None: |
| 51 | + template = ListField(length=lambda pkt: pkt['n'], item_type=UInt8Field(), callback=tag_field) |
| 52 | + first = template({'n': 2}) |
| 53 | + second = template({'n': 3}) |
| 54 | + self.assertFalse(hasattr(template, 'tag')) |
| 55 | + self.assertEqual((first.tag, second.tag), (2, 3)) # type: ignore[attr-defined] |
| 56 | + self.assertEqual((first.length, second.length), (2, 3)) |
| 57 | + |
| 58 | + |
| 59 | +class SchemaFieldTemplateTests(unittest.TestCase): |
| 60 | + """``SchemaField.__call__`` sizes the copy's template from the copy's length.""" |
| 61 | + |
| 62 | + def test_callable_length_sizes_the_template(self) -> None: |
| 63 | + template = SchemaField(length=lambda pkt: pkt['n'], schema=Pair) |
| 64 | + copy = template({'n': 24}) |
| 65 | + self.assertEqual(copy.length, 24) |
| 66 | + self.assertEqual(copy.template, '24s') |
| 67 | + self.assertEqual(template.template, '1024s') |
| 68 | + |
| 69 | + def test_negative_resolved_length_keeps_the_default(self) -> None: |
| 70 | + copy = SchemaField(length=lambda pkt: pkt['n'], schema=Pair)({'n': -1}) |
| 71 | + self.assertEqual(copy.template, '1024s') |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + unittest.main() |
0 commit comments