55# Copyright (c) 2017 Benjamin Tissoires <[email protected] > 66# Copyright (c) 2017 Red Hat, Inc.
77
8+ import dataclasses
89import libevdev
910import os
1011import pytest
@@ -145,6 +146,18 @@ def __init__(self, name, application, rdesc_str=None, rdesc=None, input_info=Non
145146 self .name = name
146147
147148
149+ @dataclasses .dataclass
150+ class HidBpf :
151+ object_name : str
152+ has_rdesc_fixup : bool
153+
154+
155+ @dataclasses .dataclass
156+ class KernelModule :
157+ driver_name : str
158+ module_name : str
159+
160+
148161class BaseTestCase :
149162 class TestUhid (object ):
150163 syn_event = libevdev .InputEvent (libevdev .EV_SYN .SYN_REPORT ) # type: ignore
@@ -155,20 +168,20 @@ class TestUhid(object):
155168
156169 # List of kernel modules to load before starting the test
157170 # if any module is not available (not compiled), the test will skip.
158- # Each element is a tuple '(kernel driver name, kernel module)',
159- # for example ("playstation", "hid-playstation")
160- kernel_modules : List [Tuple [ str , str ] ] = []
171+ # Each element is a KernelModule object, for example
172+ # KernelModule ("playstation", "hid-playstation")
173+ kernel_modules : List [KernelModule ] = []
161174
162175 # List of in kernel HID-BPF object files to load
163176 # before starting the test
164177 # Any existing pre-loaded HID-BPF module will be removed
165178 # before the ones in this list will be manually loaded.
166- # Each Element is a tuple '(hid_bpf_object, rdesc_fixup_present)',
167- # for example ' ("xppen-ArtistPro16Gen2.bpf.o", True)'
168- # If 'rdesc_fixup_present ' is True, the test needs to wait
179+ # Each Element is a HidBpf object, for example
180+ # 'HidBpf ("xppen-ArtistPro16Gen2.bpf.o", True)'
181+ # If 'has_rdesc_fixup ' is True, the test needs to wait
169182 # for one unbind and rebind before it can be sure the kernel is
170183 # ready
171- hid_bpfs : List [Tuple [ str , bool ] ] = []
184+ hid_bpfs : List [HidBpf ] = []
172185
173186 def assertInputEventsIn (self , expected_events , effective_events ):
174187 effective_events = effective_events .copy ()
@@ -232,25 +245,26 @@ def _load_kernel_module(self, kernel_driver, kernel_module):
232245
233246 @pytest .fixture ()
234247 def load_kernel_module (self ):
235- for kernel_driver , kernel_module in self .kernel_modules :
236- self ._load_kernel_module (kernel_driver , kernel_module )
248+ for k in self .kernel_modules :
249+ self ._load_kernel_module (k . driver_name , k . module_name )
237250 yield
238251
239252 def load_hid_bpfs (self ):
253+ # this function will only work when run in the kernel tree
240254 script_dir = Path (os .path .dirname (os .path .realpath (__file__ )))
241255 root_dir = (script_dir / "../../../../.." ).resolve ()
242256 bpf_dir = root_dir / "drivers/hid/bpf/progs"
243257
258+ if not bpf_dir .exists ():
259+ pytest .skip ("looks like we are not in the kernel tree, skipping" )
260+
244261 udev_hid_bpf = shutil .which ("udev-hid-bpf" )
245262 if not udev_hid_bpf :
246263 pytest .skip ("udev-hid-bpf not found in $PATH, skipping" )
247264
248- wait = False
249- for _ , rdesc_fixup in self .hid_bpfs :
250- if rdesc_fixup :
251- wait = True
265+ wait = any (b .has_rdesc_fixup for b in self .hid_bpfs )
252266
253- for hid_bpf , _ in self .hid_bpfs :
267+ for hid_bpf in self .hid_bpfs :
254268 # We need to start `udev-hid-bpf` in the background
255269 # and dispatch uhid events in case the kernel needs
256270 # to fetch features on the device
@@ -260,13 +274,13 @@ def load_hid_bpfs(self):
260274 "--verbose" ,
261275 "add" ,
262276 str (self .uhdev .sys_path ),
263- str (bpf_dir / hid_bpf ),
277+ str (bpf_dir / hid_bpf . object_name ),
264278 ],
265279 )
266280 while process .poll () is None :
267281 self .uhdev .dispatch (1 )
268282
269- if process .poll () != 0 :
283+ if process .returncode != 0 :
270284 pytest .fail (
271285 f"Couldn't insert hid-bpf program '{ hid_bpf } ', marking the test as failed"
272286 )
0 commit comments