Skip to content

Commit 75d28cd

Browse files
authored
fix(const): the Quick-Start function enums reject 9-15, though the field is 4 bits wide (#1353)
RFC 4782 section 3.1 gives the Quick-Start option a four-bit Function field. The vendor templates bounded QSFunction at 0 <= value <= 8, so 9-15 raised ValueError instead of resolving to Unassigned. Widen the bound to 0 <= value <= 15 in both templates, matching the other 4-bit const modules (hip.hit_suite, hip.di, mh.dsmip6_tls_packet), and regenerate both const files from the templates. 16 and up still raise. Closes #1350
1 parent 66950b1 commit 75d28cd

5 files changed

Lines changed: 77 additions & 4 deletions

File tree

‎pcapkit/const/ipv4/qs_function.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ def _missing_(cls, value: 'int') -> 'QSFunction':
3232
value: Value to get enum item.
3333
3434
"""
35-
if not (isinstance(value, int) and 0 <= value <= 8):
35+
if not (isinstance(value, int) and 0 <= value <= 15):
3636
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
3737
return cls._unregistered_member(value, 'Unassigned')

‎pcapkit/const/ipv6/qs_function.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ def _missing_(cls, value: 'int') -> 'QSFunction':
3232
value: Value to get enum item.
3333
3434
"""
35-
if not (isinstance(value, int) and 0 <= value <= 8):
35+
if not (isinstance(value, int) and 0 <= value <= 15):
3636
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
3737
return cls._unregistered_member(value, 'Unassigned')

‎pcapkit/vendor/ipv4/qs_function.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class QSFunction(Vendor):
3131
"""QS Functions"""
3232

3333
#: Value limit checker.
34-
FLAG = 'isinstance(value, int) and 0 <= value <= 8'
34+
FLAG = 'isinstance(value, int) and 0 <= value <= 15'
3535

3636
def request(self) -> 'dict[int, str]': # type: ignore[override] # pylint: disable=arguments-differ
3737
"""Fetch registry data.

‎pcapkit/vendor/ipv6/qs_function.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class QSFunction(Vendor):
3131
"""QS Functions"""
3232

3333
#: Value limit checker.
34-
FLAG = 'isinstance(value, int) and 0 <= value <= 8'
34+
FLAG = 'isinstance(value, int) and 0 <= value <= 15'
3535

3636
def request(self) -> 'dict[int, str]': # type: ignore[override] # pylint: disable=arguments-differ
3737
"""Fetch registry data.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)