diff options
-rw-r--r-- | kvmd/apps/kvmd/api/hid.py | 4 | ||||
-rw-r--r-- | kvmd/apps/otgmsd/__init__.py | 4 | ||||
-rw-r--r-- | kvmd/plugins/auth/pam.py | 4 | ||||
-rw-r--r-- | kvmd/validators/basic.py | 4 | ||||
-rw-r--r-- | testenv/tests/validators/test_basic.py | 15 |
5 files changed, 25 insertions, 6 deletions
diff --git a/kvmd/apps/kvmd/api/hid.py b/kvmd/apps/kvmd/api/hid.py index 59824e11..3580c373 100644 --- a/kvmd/apps/kvmd/api/hid.py +++ b/kvmd/apps/kvmd/api/hid.py @@ -37,7 +37,7 @@ from ....plugins.hid import BaseHid from ....validators import raise_error from ....validators.basic import valid_bool -from ....validators.basic import valid_number +from ....validators.basic import valid_int_f0 from ....validators.os import valid_printable_filename @@ -98,7 +98,7 @@ class HidApi: @exposed_http("POST", "/hid/print") async def __print_handler(self, request: Request) -> Response: text = await request.text() - limit = int(valid_number(request.query.get("limit", "1024"), min=0, type=int)) + limit = int(valid_int_f0(request.query.get("limit", "1024"))) if limit > 0: text = text[:limit] symmap = self.__ensure_symmap(request.query.get("keymap", self.__default_keymap_name)) diff --git a/kvmd/apps/otgmsd/__init__.py b/kvmd/apps/otgmsd/__init__.py index 2e7122ac..a9891f0e 100644 --- a/kvmd/apps/otgmsd/__init__.py +++ b/kvmd/apps/otgmsd/__init__.py @@ -31,7 +31,7 @@ from typing import Optional import psutil from ...validators.basic import valid_bool -from ...validators.basic import valid_number +from ...validators.basic import valid_int_f0 from ...validators.os import valid_abs_file @@ -90,7 +90,7 @@ def main(argv: Optional[List[str]]=None) -> None: description="KVMD OTG-MSD low-level hand tool", parents=[parent_parser], ) - parser.add_argument("-i", "--instance", default=0, type=(lambda arg: valid_number(arg, min=0)), + parser.add_argument("-i", "--instance", default=0, type=valid_int_f0, metavar="<N>", help="Drive instance (0 for KVMD drive)") parser.add_argument("--unlock", action="store_true", help="Send SIGUSR1 to MSD kernel thread") diff --git a/kvmd/plugins/auth/pam.py b/kvmd/plugins/auth/pam.py index bcdde5e4..643a6657 100644 --- a/kvmd/plugins/auth/pam.py +++ b/kvmd/plugins/auth/pam.py @@ -30,7 +30,7 @@ import pam from ...yamlconf import Option -from ...validators.basic import valid_number +from ...validators.basic import valid_int_f0 from ...validators.auth import valid_users_list from ...logging import get_logger @@ -63,7 +63,7 @@ class Plugin(BaseAuthService): "service": Option("login"), "allow_users": Option([], type=valid_users_list), "deny_users": Option([], type=valid_users_list), - "allow_uids_at": Option(0, type=(lambda arg: valid_number(arg, min=0))), + "allow_uids_at": Option(0, type=valid_int_f0), } async def authorize(self, user: str, passwd: str) -> bool: diff --git a/kvmd/validators/basic.py b/kvmd/validators/basic.py index 24387be4..ee71be0f 100644 --- a/kvmd/validators/basic.py +++ b/kvmd/validators/basic.py @@ -84,6 +84,10 @@ def valid_number( return arg +def valid_int_f0(arg: Any) -> int: + return int(valid_number(arg, min=0)) + + def valid_int_f1(arg: Any) -> int: return int(valid_number(arg, min=1)) diff --git a/testenv/tests/validators/test_basic.py b/testenv/tests/validators/test_basic.py index 15964a54..85a84d5d 100644 --- a/testenv/tests/validators/test_basic.py +++ b/testenv/tests/validators/test_basic.py @@ -28,6 +28,7 @@ import pytest from kvmd.validators import ValidatorError from kvmd.validators.basic import valid_bool from kvmd.validators.basic import valid_number +from kvmd.validators.basic import valid_int_f0 from kvmd.validators.basic import valid_int_f1 from kvmd.validators.basic import valid_float_f0 from kvmd.validators.basic import valid_float_f01 @@ -83,6 +84,20 @@ def test_fail__valid_number__min_max(arg: Any) -> None: # pylint: disable=inval # ===== [email protected]("arg", [0, 1, 5, "5 "]) +def test_ok__valid_int_f0(arg: Any) -> None: + value = valid_int_f0(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, -6, "-6 ", "5.0"]) +def test_fail__valid_int_f0(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_int_f0(arg)) + + +# ===== @pytest.mark.parametrize("arg", [1, 5, "5 "]) def test_ok__valid_int_f1(arg: Any) -> None: value = valid_int_f1(arg) |