diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/__init__.py | 59 | ||||
-rw-r--r-- | tests/test_aioregion.py | 115 | ||||
-rw-r--r-- | tests/test_app_cleanup.py | 34 | ||||
-rw-r--r-- | tests/test_gpio.py | 49 | ||||
-rw-r--r-- | tests/test_logging.py | 35 | ||||
-rw-r--r-- | tests/test_validators_auth.py | 119 | ||||
-rw-r--r-- | tests/test_validators_basic.py | 107 | ||||
-rw-r--r-- | tests/test_validators_fs.py | 91 | ||||
-rw-r--r-- | tests/test_validators_hw.py | 72 | ||||
-rw-r--r-- | tests/test_validators_kvm.py | 98 | ||||
-rw-r--r-- | tests/test_validators_net.py | 122 |
11 files changed, 901 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..d1faace6 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,59 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +import sys + +from typing import Dict +from typing import Optional + +import fake_rpi.RPi + + +# ===== +class _GPIO(fake_rpi.RPi._GPIO): # pylint: disable=protected-access + def __init__(self) -> None: + super().__init__() + self.__states: Dict[int, int] = {} + + @fake_rpi.RPi.printf + def setup(self, channel: int, state: int, initial: int=0, pull_up_down: Optional[int]=None) -> None: + _ = state # Makes linter happy + _ = pull_up_down # Makes linter happy + self.__states[int(channel)] = int(initial) + + @fake_rpi.RPi.printf + def output(self, channel: int, state: int) -> None: + self.__states[int(channel)] = int(state) + + @fake_rpi.RPi.printf + def input(self, channel: int) -> int: # pylint: disable=arguments-differ + return self.__states[int(channel)] + + @fake_rpi.RPi.printf + def cleanup(self, channel: Optional[int]=None) -> None: # pylint: disable=arguments-differ + _ = channel # Makes linter happy + self.__states = {} + + +# ===== +fake_rpi.RPi.GPIO = _GPIO() +sys.modules["RPi"] = fake_rpi.RPi diff --git a/tests/test_aioregion.py b/tests/test_aioregion.py new file mode 100644 index 00000000..72435adf --- /dev/null +++ b/tests/test_aioregion.py @@ -0,0 +1,115 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +import asyncio + +import pytest + +from kvmd.aioregion import RegionIsBusyError +from kvmd.aioregion import AioExclusiveRegion + + +# ===== +async def test_aioregion__one__ok(event_loop: asyncio.AbstractEventLoop) -> None: + _ = event_loop + region = AioExclusiveRegion(RegionIsBusyError) + + async def func() -> None: + assert not region.is_busy() + with region: + assert region.is_busy() + assert not region.is_busy() + + await func() + + assert not region.is_busy() + region.exit() + assert not region.is_busy() + + +async def test_aioregion__one__fail(event_loop: asyncio.AbstractEventLoop) -> None: + _ = event_loop + region = AioExclusiveRegion(RegionIsBusyError) + + async def func() -> None: + assert not region.is_busy() + with region: + assert region.is_busy() + region.enter() + assert not region.is_busy() + + with pytest.raises(RegionIsBusyError): + await func() + + assert not region.is_busy() + region.exit() + assert not region.is_busy() + + +# ===== +async def test_aioregion__two__ok(event_loop: asyncio.AbstractEventLoop) -> None: + region = AioExclusiveRegion(RegionIsBusyError) + + async def func1() -> None: + with region: + await asyncio.sleep(1, loop=event_loop) + print("done func1()") + + async def func2() -> None: + await asyncio.sleep(2) + print("waiking up func2()") + with region: + await asyncio.sleep(1, loop=event_loop) + print("done func2()") + + await asyncio.gather(func1(), func2()) + + assert not region.is_busy() + region.exit() + assert not region.is_busy() + + +async def test_aioregion__two__fail(event_loop: asyncio.AbstractEventLoop) -> None: + region = AioExclusiveRegion(RegionIsBusyError) + + async def func1() -> None: + with region: + await asyncio.sleep(2, loop=event_loop) + print("done func1()") + + async def func2() -> None: + await asyncio.sleep(1) + with region: + await asyncio.sleep(1, loop=event_loop) + print("done func2()") + + results = await asyncio.gather(func1(), func2(), loop=event_loop, return_exceptions=True) + assert results[0] is None + assert type(results[1]) == RegionIsBusyError # pylint: disable=unidiomatic-typecheck + + assert not region.is_busy() + region.exit() + assert not region.is_busy() diff --git a/tests/test_app_cleanup.py b/tests/test_app_cleanup.py new file mode 100644 index 00000000..8d9db421 --- /dev/null +++ b/tests/test_app_cleanup.py @@ -0,0 +1,34 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +from kvmd.apps.cleanup import main + + +# ===== +def test_main() -> None: + open("/tmp/foobar.sock", "w").close() + main([ + "kvmd-cleanup", + "--set-options", + "kvmd/hid/device=/dev/null", + "kvmd/streamer/unix=/tmp/foobar.sock", + ]) diff --git a/tests/test_gpio.py b/tests/test_gpio.py new file mode 100644 index 00000000..a40020c0 --- /dev/null +++ b/tests/test_gpio.py @@ -0,0 +1,49 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +from kvmd import gpio + + +# ===== +def test_gpio__loopback_initial_false() -> None: + # pylint: disable=singleton-comparison + with gpio.bcm(): + assert gpio.set_output(0) == 0 + assert gpio.read(0) == False # noqa: E712 + gpio.write(0, True) + assert gpio.read(0) == True # noqa: E712 + + +def test_gpio__loopback_initial_true() -> None: + # pylint: disable=singleton-comparison + with gpio.bcm(): + assert gpio.set_output(0, True) == 0 + assert gpio.read(0) == True # noqa: E712 + gpio.write(0, False) + assert gpio.read(0) == False # noqa: E712 + + +def test_gpio__input() -> None: + # pylint: disable=singleton-comparison + with gpio.bcm(): + assert gpio.set_input(0) == 0 + assert gpio.read(0) == False # noqa: E712 diff --git a/tests/test_logging.py b/tests/test_logging.py new file mode 100644 index 00000000..be6b889e --- /dev/null +++ b/tests/test_logging.py @@ -0,0 +1,35 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +import pytest + +from kvmd.logging import get_logger + + +# ===== [email protected]("depth, name", [ + (0, "tests.test_logging"), + (1, "_pytest.python"), + (2, "pluggy.callers"), +]) +def test_get_logger(depth: int, name: str) -> None: + assert get_logger(depth).name == name diff --git a/tests/test_validators_auth.py b/tests/test_validators_auth.py new file mode 100644 index 00000000..fd13a450 --- /dev/null +++ b/tests/test_validators_auth.py @@ -0,0 +1,119 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +from typing import Any + +import pytest + +from kvmd.validators import ValidatorError +from kvmd.validators.auth import valid_user +from kvmd.validators.auth import valid_passwd +from kvmd.validators.auth import valid_auth_token +from kvmd.validators.auth import valid_auth_type + + +# ===== [email protected]("arg", [ + "test-", + "glados", + "test", + "_", + "_foo_bar_", + " aix", +]) +def test_ok__valid_user(arg: Any) -> None: + assert valid_user(arg) == arg.strip() + + [email protected]("arg", [ + "тест", + "-molestia", + "te~st", + "-", + "-foo_bar", + " ", + "", + None, +]) +def test_fail__valid_user(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_user(arg)) + + +# ===== [email protected]("arg", [ + "glados", + "test", + "_", + "_foo_bar_", + " aix", + " ", + "", + " O(*#&@)FD*S)D(F ", +]) +def test_ok__valid_passwd(arg: Any) -> None: + assert valid_passwd(arg) == arg + + [email protected]("arg", [ + "тест", + "\n", + " \n", + "\n\n", + "\r", + None, +]) +def test_fail__valid_passwd(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_passwd(arg)) + + +# ===== [email protected]("arg", [ + ("0" * 64) + " ", + ("f" * 64) + " ", +]) +def test_ok__valid_auth_token(arg: Any) -> None: + assert valid_auth_token(arg) == arg.strip() + + [email protected]("arg", [ + ("F" * 64), + "0" * 63, + "0" * 65, + "", + None, +]) +def test_fail__valid_auth_token(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_auth_token(arg)) + + [email protected]("arg", ["BASIC ", "basic"]) +def test_ok__valid_auth_type(arg: Any) -> None: + assert valid_auth_type(arg) == arg.strip().lower() + + [email protected]("arg", ["test", "", None]) +def test_fail__valid_auth_type(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_auth_type(arg)) diff --git a/tests/test_validators_basic.py b/tests/test_validators_basic.py new file mode 100644 index 00000000..0231e29a --- /dev/null +++ b/tests/test_validators_basic.py @@ -0,0 +1,107 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +from typing import Any + +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_f1 +from kvmd.validators.basic import valid_float_f01 + + +# ===== [email protected]("arg, retval", [ + ("1", True), + ("true", True), + ("TRUE", True), + ("yes ", True), + (1, True), + (True, True), + ("0", False), + ("false", False), + ("FALSE", False), + ("no ", False), + (0, False), + (False, False), +]) +def test_ok__valid_bool(arg: Any, retval: bool) -> None: + assert valid_bool(arg) == retval + + [email protected]("arg", ["test", "", None, -1, "x"]) +def test_fail__valid_bool(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_bool(arg)) + + +# ===== [email protected]("arg", ["1 ", "-1", 1, -1, 0, 100500]) +def test_ok__valid_number(arg: Any) -> None: + assert valid_number(arg) == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, "1x", 100500.0]) +def test_fail__valid_number(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_number(arg)) + + [email protected]("arg", [-5, 0, 5, "-5 ", "0 ", "5 "]) +def test_ok__valid_number__min_max(arg: Any) -> None: + assert valid_number(arg, -5, 5) == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, -6, 6, "-6 ", "6 "]) +def test_fail__valid_number__min_max(arg: Any) -> None: # pylint: disable=invalid-name + with pytest.raises(ValidatorError): + print(valid_number(arg, -5, 5)) + + +# ===== [email protected]("arg", [1, 5, "5 "]) +def test_ok__valid_int_f1(arg: Any) -> None: + value = valid_int_f1(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, -6, "-6 ", 0, "0 ", "5.0"]) +def test_fail__valid_int_f1(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_int_f1(arg)) + + +# ===== [email protected]("arg", [0.1, 1, 5, "5 ", "5.0 "]) +def test_ok__valid_float_f01(arg: Any) -> None: + value = valid_float_f01(arg) + assert type(value) == float # pylint: disable=unidiomatic-typecheck + assert value == float(str(arg).strip()) + + [email protected]("arg", ["test", "", None, 0.0, "0.0", -6, "-6", 0, "0"]) +def test_fail__valid_float_f01(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_float_f01(arg)) diff --git a/tests/test_validators_fs.py b/tests/test_validators_fs.py new file mode 100644 index 00000000..854d025c --- /dev/null +++ b/tests/test_validators_fs.py @@ -0,0 +1,91 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +import os + +from typing import Any + +import pytest + +from kvmd.validators import ValidatorError +from kvmd.validators.fs import valid_abs_path +from kvmd.validators.fs import valid_abs_path_exists +from kvmd.validators.fs import valid_unix_mode + + +# ===== [email protected]("arg, retval", [ + ("/..", "/"), + ("/root/..", "/"), + ("/root", "/root"), + ("/f/o/o/b/a/r", "/f/o/o/b/a/r"), + ("~", os.path.abspath(".") + "/~"), + ("/foo~", "/foo~"), + ("/foo/~", "/foo/~"), + (".", os.path.abspath(".")), +]) +def test_ok__valid_abs_path(arg: Any, retval: str) -> None: + assert valid_abs_path(arg) == retval + + [email protected]("arg", ["", " ", None]) +def test_fail__valid_abs_path(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_abs_path(arg)) + + +# ===== [email protected]("arg, retval", [ + ("/..", "/"), + ("/root/..", "/"), + ("/root", "/root"), + (".", os.path.abspath(".")), +]) +def test_ok__valid_abs_path_exists(arg: Any, retval: str) -> None: + assert valid_abs_path_exists(arg) == retval + + [email protected]("arg", [ + "/f/o/o/b/a/r", + "~", + "/foo~", + "/foo/~", + "", + None, +]) +def test_fail__valid_abs_path_exists(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_abs_path_exists(arg)) + + +# ===== [email protected]("arg", [0, 5, "1000"]) +def test_ok__valid_unix_mode(arg: Any) -> None: + value = valid_unix_mode(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(value).strip()) + + [email protected]("arg", ["test", "", None, -6, "-6", "5.0"]) +def test_fail__valid_unix_mode(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_unix_mode(arg)) diff --git a/tests/test_validators_hw.py b/tests/test_validators_hw.py new file mode 100644 index 00000000..2c93d3eb --- /dev/null +++ b/tests/test_validators_hw.py @@ -0,0 +1,72 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +from typing import Any + +import pytest + +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 + + +# ===== [email protected]("arg", ["1200 ", 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]) +def test_ok__valid_tty_speed(arg: Any) -> None: + value = valid_tty_speed(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, 0, 1200.1]) +def test_fail__valid_tty_speed(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_tty_speed(arg)) + + +# ===== [email protected]("arg", ["0 ", 0, 1, 13]) +def test_ok__valid_gpio_pin(arg: Any) -> None: + value = valid_gpio_pin(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, -1, -13, 1.1]) +def test_fail__valid_gpio_pin(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_gpio_pin(arg)) + + +# ===== [email protected]("arg", ["0 ", -1, 0, 1, 13]) +def test_ok__valid_gpio_pin_optional(arg: Any) -> None: + value = valid_gpio_pin_optional(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, -2, -13, 1.1]) +def test_fail__valid_gpio_pin_optional(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_gpio_pin_optional(arg)) diff --git a/tests/test_validators_kvm.py b/tests/test_validators_kvm.py new file mode 100644 index 00000000..fad34986 --- /dev/null +++ b/tests/test_validators_kvm.py @@ -0,0 +1,98 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +from typing import Any + +import pytest + +from kvmd.validators import ValidatorError +from kvmd.validators.kvm import valid_atx_button +from kvmd.validators.kvm import valid_kvm_target +from kvmd.validators.kvm import valid_log_seek +from kvmd.validators.kvm import valid_stream_quality +from kvmd.validators.kvm import valid_stream_fps + + +# ===== [email protected]("arg", ["POWER ", "POWER_LONG ", "RESET "]) +def test_ok__valid_atx_button(arg: Any) -> None: + assert valid_atx_button(arg) == arg.strip().lower() + + [email protected]("arg", ["test", "", None]) +def test_fail__valid_atx_button(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_atx_button(arg)) + + +# ===== [email protected]("arg", ["KVM ", "SERVER "]) +def test_ok__valid_kvm_target(arg: Any) -> None: + assert valid_kvm_target(arg) == arg.strip().lower() + + [email protected]("arg", ["test", "", None]) +def test_fail__valid_kvm_target(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_kvm_target(arg)) + + +# ===== [email protected]("arg", ["0 ", 0, 1, 13]) +def test_ok__valid_log_seek(arg: Any) -> None: + value = valid_log_seek(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, -1, -13, 1.1]) +def test_fail__valid_log_seek(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_log_seek(arg)) + + +# ===== [email protected]("arg", ["1 ", 20, 100]) +def test_ok__valid_stream_quality(arg: Any) -> None: + value = valid_stream_quality(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, 0, 101, 1.1]) +def test_fail__valid_stream_quality(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_stream_quality(arg)) + + +# ===== [email protected]("arg", ["1 ", 30]) +def test_ok__valid_stream_fps(arg: Any) -> None: + value = valid_stream_fps(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, 31, 1.1]) +def test_fail__valid_stream_fps(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_stream_fps(arg)) diff --git a/tests/test_validators_net.py b/tests/test_validators_net.py new file mode 100644 index 00000000..33c31f6d --- /dev/null +++ b/tests/test_validators_net.py @@ -0,0 +1,122 @@ +# ========================================================================== # +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +from typing import Any + +import pytest + +from kvmd.validators import ValidatorError +from kvmd.validators.net import valid_ip_or_host +from kvmd.validators.net import valid_ip +from kvmd.validators.net import valid_rfc_host +from kvmd.validators.net import valid_port + + +# ===== [email protected]("arg", [ + "yandex.ru ", + "foobar", + "foo-bar.ru", + "127.0.0.1", + "8.8.8.8", + "::", + "::1", + "2001:500:2f::f", +]) +def test_ok__valid_ip_or_host(arg: Any) -> None: + assert valid_ip_or_host(arg) == arg.strip() + + [email protected]("arg", [ + "foo_bar.ru", + "1.1.1.", + ":", + "", + None, +]) +def test_fail__valid_ip_or_host(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_ip_or_host(arg)) + + +# ===== [email protected]("arg", [ + "127.0.0.1 ", + "8.8.8.8", + "::", + "::1", + "2001:500:2f::f", +]) +def test_ok__valid_ip(arg: Any) -> None: + assert valid_ip(arg) == arg.strip() + + [email protected]("arg", [ + "ya.ru", + "1", + "1.1.1", + "1.1.1.", + ":", + "", + None, +]) +def test__fail_valid_ip(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_ip(arg)) + + +# ===== [email protected]("arg", [ + "yandex.ru ", + "foobar", + "foo-bar.ru", + "z0r.de", + "11.ru", + "127.0.0.1", +]) +def test_ok__valid_rfc_host(arg: Any) -> None: + assert valid_rfc_host(arg) == arg.strip() + + [email protected]("arg", [ + "foobar.ru.", + "foo_bar.ru", + "", + None, +]) +def test_fail__valid_rfc_host(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_rfc_host(arg)) + + +# ===== [email protected]("arg", ["0 ", 0, "22", 443, 65535]) +def test_ok__valid_port(arg: Any) -> None: + value = valid_port(arg) + assert type(value) == int # pylint: disable=unidiomatic-typecheck + assert value == int(str(arg).strip()) + + [email protected]("arg", ["test", "", None, 1.1]) +def test_fail__valid_port(arg: Any) -> None: + with pytest.raises(ValidatorError): + print(valid_port(arg)) |