| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 |
|
| 8 | from packaging.version import Version
|
| 9 | import platform
|
| 10 | import pytest
|
| 11 | import re
|
| 12 | import resource
|
| 13 | import subprocess
|
| 14 | from .base import HIDTestUdevRule
|
| 15 | from pathlib import Path
|
| 16 |
|
| 17 |
|
| 18 | @pytest.fixture(autouse=True)
|
| 19 | def hidtools_version_check():
|
| 20 | HIDTOOLS_VERSION = "0.12"
|
| 21 | try:
|
| 22 | import hidtools
|
| 23 |
|
| 24 | version = hidtools.__version__ |
| 25 | if Version(version) < Version(HIDTOOLS_VERSION):
|
| 26 | pytest.skip(reason=f"have hidtools {version}, require >={HIDTOOLS_VERSION}")
|
| 27 | except Exception:
|
| 28 | pytest.skip(reason=f"hidtools >={HIDTOOLS_VERSION} required")
|
| 29 |
|
| 30 |
|
| 31 | |
| 32 | |
| 33 | @pytest.fixture(autouse=True, scope="session")
|
| 34 | def udev_rules_session_setup():
|
| 35 | with HIDTestUdevRule.instance():
|
| 36 | yield
|
| 37 |
|
| 38 |
|
| 39 | @pytest.fixture(autouse=True, scope="session")
|
| 40 | def setup_rlimit():
|
| 41 | resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
|
| 42 |
|
| 43 |
|
| 44 | @pytest.fixture(autouse=True, scope="session")
|
| 45 | def start_udevd(pytestconfig):
|
| 46 | if pytestconfig.getoption("udevd"):
|
| 47 | import subprocess
|
| 48 |
|
| 49 | with subprocess.Popen("/usr/lib/systemd/systemd-udevd") as proc:
|
| 50 | yield
|
| 51 | proc.kill()
|
| 52 | else:
|
| 53 | yield
|
| 54 |
|
| 55 |
|
| 56 | def pytest_configure(config):
|
| 57 | config.addinivalue_line(
|
| 58 | "markers",
|
| 59 | "skip_if_uhdev(condition, message): mark test to skip if the condition on the uhdev device is met",
|
| 60 | )
|
| 61 |
|
| 62 |
|
| 63 | |
| 64 | |
| 65 | def pytest_generate_tests(metafunc):
|
| 66 | if "usbVidPid" in metafunc.fixturenames:
|
| 67 | modules = (
|
| 68 | Path("/lib/modules/")
|
| 69 | / platform.uname().release
|
| 70 | / "kernel"
|
| 71 | / "drivers"
|
| 72 | / "hid"
|
| 73 | )
|
| 74 |
|
| 75 | modalias_re = re.compile(r"alias:\s+hid:b0003g.*v([0-9a-fA-F]+)p([0-9a-fA-F]+)")
|
| 76 |
|
| 77 | params = []
|
| 78 | ids = []
|
| 79 | for module in modules.glob("*.ko"):
|
| 80 | p = subprocess.run(
|
| 81 | ["modinfo", module], capture_output=True, check=True, encoding="utf-8"
|
| 82 | )
|
| 83 | for line in p.stdout.split("\n"):
|
| 84 | m = modalias_re.match(line)
|
| 85 | if m is not None:
|
| 86 | vid, pid = m.groups()
|
| 87 | vid = int(vid, 16)
|
| 88 | pid = int(pid, 16)
|
| 89 | params.append([module.name.replace(".ko", ""), vid, pid])
|
| 90 | ids.append(f"{module.name} {vid:04x}:{pid:04x}")
|
| 91 | metafunc.parametrize("usbVidPid", params, ids=ids)
|
| 92 |
|
| 93 |
|
| 94 | def pytest_addoption(parser):
|
| 95 | parser.addoption("--udevd", action="store_true", default=False)
|