5
5
# Copyright (c) 2017 Benjamin Tissoires <[email protected] >
6
6
# Copyright (c) 2017 Red Hat, Inc.
7
7
8
+ import dataclasses
8
9
import libevdev
9
10
import os
10
11
import pytest
@@ -145,6 +146,18 @@ def __init__(self, name, application, rdesc_str=None, rdesc=None, input_info=Non
145
146
self .name = name
146
147
147
148
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
+
148
161
class BaseTestCase :
149
162
class TestUhid (object ):
150
163
syn_event = libevdev .InputEvent (libevdev .EV_SYN .SYN_REPORT ) # type: ignore
@@ -155,20 +168,20 @@ class TestUhid(object):
155
168
156
169
# List of kernel modules to load before starting the test
157
170
# 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 ] = []
161
174
162
175
# List of in kernel HID-BPF object files to load
163
176
# before starting the test
164
177
# Any existing pre-loaded HID-BPF module will be removed
165
178
# 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
169
182
# for one unbind and rebind before it can be sure the kernel is
170
183
# ready
171
- hid_bpfs : List [Tuple [ str , bool ] ] = []
184
+ hid_bpfs : List [HidBpf ] = []
172
185
173
186
def assertInputEventsIn (self , expected_events , effective_events ):
174
187
effective_events = effective_events .copy ()
@@ -232,25 +245,26 @@ def _load_kernel_module(self, kernel_driver, kernel_module):
232
245
233
246
@pytest .fixture ()
234
247
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 )
237
250
yield
238
251
239
252
def load_hid_bpfs (self ):
253
+ # this function will only work when run in the kernel tree
240
254
script_dir = Path (os .path .dirname (os .path .realpath (__file__ )))
241
255
root_dir = (script_dir / "../../../../.." ).resolve ()
242
256
bpf_dir = root_dir / "drivers/hid/bpf/progs"
243
257
258
+ if not bpf_dir .exists ():
259
+ pytest .skip ("looks like we are not in the kernel tree, skipping" )
260
+
244
261
udev_hid_bpf = shutil .which ("udev-hid-bpf" )
245
262
if not udev_hid_bpf :
246
263
pytest .skip ("udev-hid-bpf not found in $PATH, skipping" )
247
264
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 )
252
266
253
- for hid_bpf , _ in self .hid_bpfs :
267
+ for hid_bpf in self .hid_bpfs :
254
268
# We need to start `udev-hid-bpf` in the background
255
269
# and dispatch uhid events in case the kernel needs
256
270
# to fetch features on the device
@@ -260,13 +274,13 @@ def load_hid_bpfs(self):
260
274
"--verbose" ,
261
275
"add" ,
262
276
str (self .uhdev .sys_path ),
263
- str (bpf_dir / hid_bpf ),
277
+ str (bpf_dir / hid_bpf . object_name ),
264
278
],
265
279
)
266
280
while process .poll () is None :
267
281
self .uhdev .dispatch (1 )
268
282
269
- if process .poll () != 0 :
283
+ if process .returncode != 0 :
270
284
pytest .fail (
271
285
f"Couldn't insert hid-bpf program '{ hid_bpf } ', marking the test as failed"
272
286
)
0 commit comments