test: add support for pytesting BPF programs
This allows us to write fairly simple tests for our functionality.
Approach:
- recompile each bpf.c into a libtest-foo.so
- provide a ctypes wrapper and some accessors around that .so file
- write tests to verify the bpf does the right thing given some data
- run pytest
- ???
- profit
Example test for the invert-mouse BPF that we have in userhacks:
@pytest.mark.parametrize("source", ["10-mouse_invert_y"])
@pytest.mark.parametrize("y", [1, -1, 10, -256])
def test_event_userhacks_invert(bpf, y):
# this device has reports of size 9
values = (0, 0, 0, y, 0, 0, 0, 0, 0)
report = struct.pack("<3bh5b", *values)
values = bpf.hid_bpf_device_event("hid_y_event", report=report)
values = struct.unpack("<3bh5b", values)
y_out = values[3]
assert y_out == -y
Where source
is used by the bpf
fixture t load the right .so file.
We then compose a report, pass that to the given-by-name hid_bpf_device_event
hook and compare the data we got.