Repository navigation
Stop BitField packing every named bit as set (#359) #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import unittest | ||
|
|
||
| from tests._support import purge_modules | ||
|
|
||
|
|
||
| class BitFieldTests(unittest.TestCase): | ||
| """Packing and parsing of :class:`~pcapkit.corekit.fields.strings.BitField`.""" | ||
|
|
||
| def setUp(self) -> None: | ||
| purge_modules(['pcapkit']) | ||
|
|
||
| from pcapkit.corekit.fields.strings import BitField | ||
|
|
||
| self.BitField = BitField | ||
| #: One octet of single-bit flags, as the protocol schemas declare them. | ||
| self.flags = BitField(length=1, namespace={ | ||
| 'a': (0, 1), | ||
| 'b': (1, 1), | ||
| 'c': (6, 1), | ||
| 'd': (7, 1), | ||
| }) | ||
|
|
||
| def test_cleared_bits_stay_cleared_when_a_sibling_is_set(self) -> None: | ||
| """A zero bit must not be packed as a one. | ||
|
|
||
| The buffer holds one ASCII digit per bit, and ``b'0'`` is itself a | ||
| non-zero byte, so a truthiness test over it reported every named bit as | ||
| set regardless of its value. | ||
|
|
||
| """ | ||
| self.assertEqual(self.flags.pre_process({'a': 0, 'b': 0, 'c': 0, 'd': 1}, {}), b'\x01') | ||
| self.assertEqual(self.flags.pre_process({'a': 1, 'b': 0, 'c': 0, 'd': 0}, {}), b'\x80') | ||
| self.assertEqual(self.flags.pre_process({'a': 0, 'b': 1, 'c': 1, 'd': 0}, {}), b'\x42') | ||
|
|
||
| def test_no_bits_set_packs_to_zero(self) -> None: | ||
| self.assertEqual(self.flags.pre_process({'a': 0, 'b': 0, 'c': 0, 'd': 0}, {}), b'\x00') | ||
|
|
||
| def test_every_bit_set_packs_to_its_mask(self) -> None: | ||
| self.assertEqual(self.flags.pre_process({'a': 1, 'b': 1, 'c': 1, 'd': 1}, {}), b'\xc3') | ||
|
|
||
| def test_multi_bit_subfields_keep_their_own_width(self) -> None: | ||
| field = self.BitField(length=1, namespace={'kind': (0, 3), 'value': (3, 5)}) | ||
|
|
||
| self.assertEqual(field.pre_process({'kind': 5, 'value': 17}, {}), b'\xb1') | ||
| self.assertEqual(field.pre_process({'kind': 0, 'value': 1}, {}), b'\x01') | ||
|
|
||
| def test_unnamed_bits_are_left_alone(self) -> None: | ||
| """Bits outside the namespace belong to nobody and must pack as zero.""" | ||
| field = self.BitField(length=2, namespace={'flag': (3, 1)}) | ||
|
|
||
| self.assertEqual(field.pre_process({'flag': 1}, {}), b'\x10\x00') | ||
| self.assertEqual(field.pre_process({'flag': 0}, {}), b'\x00\x00') | ||
|
|
||
| def test_a_subfield_reaching_past_the_field_is_rejected_at_declaration(self) -> None: | ||
| """A namespace entry wider than the field is a schema mistake. | ||
|
|
||
| Slice assignment on a :class:`bytearray` *grows* it, so declaring | ||
| ``ihl: (4, 8)`` on a one-octet field built a 12-bit buffer and then | ||
| failed in :meth:`int.to_bytes` with an opaque ``OverflowError``, far | ||
| from the declaration at fault. ``pcapkit/protocols/schema/internet/ | ||
| ipv4.py`` really did carry that declaration. | ||
|
|
||
| """ | ||
| from pcapkit.utilities.exceptions import FieldValueError | ||
|
|
||
| with self.assertRaises(FieldValueError): | ||
| self.BitField(length=1, namespace={'version': (0, 4), 'ihl': (4, 8)}) | ||
| with self.assertRaises(FieldValueError): | ||
| self.BitField(length=1, namespace={'flag': (8, 1)}) | ||
| with self.assertRaises(FieldValueError): | ||
| self.BitField(length=1, namespace={'flag': (-1, 1)}) | ||
|
|
||
| def test_a_value_too_wide_for_its_subfield_is_rejected(self) -> None: | ||
| from pcapkit.utilities.exceptions import FieldValueError | ||
|
|
||
| field = self.BitField(length=1, namespace={'kind': (0, 3), 'value': (3, 5)}) | ||
|
|
||
| with self.assertRaises(FieldValueError): | ||
| field.pre_process({'kind': 8, 'value': 0}, {}) | ||
| self.assertEqual(field.pre_process({'kind': 7, 'value': 31}, {}), b'\xff') | ||
|
|
||
| def test_ipv4_version_and_ihl_pack_into_one_octet(self) -> None: | ||
| """The real declaration this guard was written for.""" | ||
| from pcapkit.protocols.schema.internet.ipv4 import IPv4 | ||
|
|
||
| field = IPv4.__fields__['vihl'] | ||
|
|
||
| self.assertEqual(field.pre_process({'version': 4, 'ihl': 5}, {}), b'\x45') | ||
| self.assertEqual(field.post_process(b'\x45', {}), {'version': 4, 'ihl': 5}) | ||
|
|
||
| def test_pack_and_parse_round_trip(self) -> None: | ||
| for value in ({'a': 0, 'b': 0, 'c': 0, 'd': 0}, | ||
| {'a': 0, 'b': 0, 'c': 0, 'd': 1}, | ||
| {'a': 1, 'b': 0, 'c': 1, 'd': 0}, | ||
| {'a': 1, 'b': 1, 'c': 1, 'd': 1}): | ||
| with self.subTest(value=value): | ||
| packed = self.flags.pre_process(value, {}) | ||
| self.assertEqual(self.flags.post_process(packed, {}), value) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.