|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""The Quick-Start ``QSFunction`` enums accept the whole four-bit field. |
| 3 | +
|
| 4 | +GitHub issue #1350: RFC 4782 section 3.1 gives the Quick-Start option "a |
| 5 | +four-bit Function field", so every value in ``0..15`` is a valid wire value, |
| 6 | +and those without an assignment (``1..7`` and ``9..15``) resolve to an |
| 7 | +``Unassigned`` member. Values outside the field are still rejected. |
| 8 | +
|
| 9 | +The bound comes from ``FLAG`` in the vendor templates |
| 10 | +:mod:`pcapkit.vendor.ipv4.qs_function` and :mod:`pcapkit.vendor.ipv6.qs_function`, |
| 11 | +so the generated const files are also checked against the template's output. |
| 12 | +Both templates take their registry from a local ``DATA`` dict, so rendering |
| 13 | +them reaches no network. |
| 14 | +
|
| 15 | +Modules are imported inside each test, after |
| 16 | +:func:`~tests._support.reimport_once_per_class`, not at module load. |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +import importlib |
| 21 | +import os |
| 22 | +import tempfile |
| 23 | +import unittest |
| 24 | +from unittest import mock |
| 25 | + |
| 26 | +from tests._support import reimport_once_per_class |
| 27 | + |
| 28 | +NAMESPACES = ('ipv4', 'ipv6') |
| 29 | + |
| 30 | + |
| 31 | +class TestQSFunctionRange(unittest.TestCase): |
| 32 | + """Pin the accepted range of both ``QSFunction`` enums.""" |
| 33 | + |
| 34 | + def setUp(self) -> None: |
| 35 | + reimport_once_per_class(self) |
| 36 | + |
| 37 | + def test_unassigned_values_resolve(self) -> None: |
| 38 | + for ns in NAMESPACES: |
| 39 | + enum = importlib.import_module(f'pcapkit.const.{ns}.qs_function').QSFunction |
| 40 | + for value in range(16): |
| 41 | + with self.subTest(ns=ns, value=value): |
| 42 | + member = enum.get(value) |
| 43 | + self.assertEqual(int(member), value) |
| 44 | + if value not in (0, 8): |
| 45 | + self.assertEqual(enum(value), member) |
| 46 | + self.assertTrue(member.name.startswith('Unassigned'), member.name) |
| 47 | + |
| 48 | + def test_values_outside_the_field_are_rejected(self) -> None: |
| 49 | + for ns in NAMESPACES: |
| 50 | + enum = importlib.import_module(f'pcapkit.const.{ns}.qs_function').QSFunction |
| 51 | + for value in (-1, 16, 255): |
| 52 | + with self.subTest(ns=ns, value=value): |
| 53 | + with self.assertRaises(ValueError): |
| 54 | + enum(value) |
| 55 | + |
| 56 | + def test_const_file_matches_template_output(self) -> None: |
| 57 | + for ns in NAMESPACES: |
| 58 | + with self.subTest(ns=ns): |
| 59 | + vendor = importlib.import_module(f'pcapkit.vendor.{ns}.qs_function').QSFunction |
| 60 | + self.assertIsNone(vendor.LINK) |
| 61 | + const_file = vendor._dest_path() |
| 62 | + with tempfile.TemporaryDirectory() as tempdir: |
| 63 | + dest = os.path.join(tempdir, 'qs_function.py') |
| 64 | + with mock.patch.object(vendor, '_dest_path', return_value=dest): |
| 65 | + vendor() |
| 66 | + with open(dest, 'rb') as file: |
| 67 | + rendered = file.read() |
| 68 | + with open(const_file, 'rb') as file: |
| 69 | + self.assertEqual(file.read(), rendered) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == '__main__': |
| 73 | + unittest.main() |
0 commit comments