diff options
-rw-r--r-- | kvmd/apps/__init__.py | 8 | ||||
-rw-r--r-- | kvmd/validators/hw.py | 9 | ||||
-rw-r--r-- | testenv/tests/validators/test_hw.py | 44 |
3 files changed, 58 insertions, 3 deletions
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py index bfb4263e..9fd536a5 100644 --- a/kvmd/apps/__init__.py +++ b/kvmd/apps/__init__.py @@ -68,6 +68,8 @@ from ..validators.kvm import valid_stream_quality from ..validators.kvm import valid_stream_fps from ..validators.hw import valid_gpio_pin_optional +from ..validators.hw import valid_otg_gadget +from ..validators.hw import valid_otg_id # ===== @@ -247,9 +249,9 @@ def _get_config_scheme() -> Dict: }, "otg": { - "gadget": Option("kvmd"), - "vendor_id": Option(0x1D6B, type=valid_number), # Linux Foundation - "product_id": Option(0x0104, type=valid_number), # Multifunction Composite Gadget + "gadget": Option("kvmd", type=valid_otg_gadget), + "vendor_id": Option(0x1D6B, type=valid_otg_id), # Linux Foundation + "product_id": Option(0x0104, type=valid_otg_id), # Multifunction Composite Gadget "manufacturer": Option("Pi-KVM"), "product": Option("Composite KVM Device"), "serial_number": Option("CAFEBABE"), diff --git a/kvmd/validators/hw.py b/kvmd/validators/hw.py index a68743ad..82d1221a 100644 --- a/kvmd/validators/hw.py +++ b/kvmd/validators/hw.py @@ -23,6 +23,7 @@ from typing import Any from . import check_in_list +from . import check_re_match from .basic import valid_number @@ -40,3 +41,11 @@ def valid_gpio_pin(arg: Any) -> int: def valid_gpio_pin_optional(arg: Any) -> int: return int(valid_number(arg, min=-1, name="optional GPIO pin")) + + +def valid_otg_gadget(arg: Any) -> str: + return check_re_match(arg, "OTG gadget name", r"^[a-z_][a-z0-9_-]*$")[:255] + + +def valid_otg_id(arg: Any) -> int: + return int(valid_number(arg, min=0, max=65535, name="OTG ID")) diff --git a/testenv/tests/validators/test_hw.py b/testenv/tests/validators/test_hw.py index 2c93d3eb..a10a5feb 100644 --- a/testenv/tests/validators/test_hw.py +++ b/testenv/tests/validators/test_hw.py @@ -28,6 +28,8 @@ from kvmd.validators import ValidatorError from kvmd.validators.hw import valid_tty_speed from kvmd.validators.hw import valid_gpio_pin from kvmd.validators.hw import valid_gpio_pin_optional +from kvmd.validators.hw import valid_otg_gadget +from kvmd.validators.hw import valid_otg_id # ===== @@ -70,3 +72,45 @@ def test_ok__valid_gpio_pin_optional(arg: Any) -> None: def test_fail__valid_gpio_pin_optional(arg: Any) -> None: with pytest.raises(ValidatorError): print(valid_gpio_pin_optional(arg)) + + +# ===== [email protected]("arg", [ + "test-", + "glados", + "test", + "_", + "_foo_bar_", + " aix", +]) +def test_ok__valid_otg_gadget(arg: Any) -> None: + assert valid_otg_gadget(arg) == arg.strip() + + [email protected]("arg", [ + "ัะตัั", + "-molestia", + "te~st", + "-", + "-foo_bar", + " ", + "", + None, +]) +def test_fail__valid_otg_gadget(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_otg_gadget(arg)) + + +# ===== [email protected]("arg", ["0 ", 0, 1, 13, 65535]) +def test_ok__valid_otg_id(arg: Any) -> None: + value = valid_otg_id(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, -1, -13, 1.1, 65534.5, 65536]) +def test_fail__valid_otg_id(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_otg_id(arg)) |