|
| 1 | +# Backend-generic HIDAPI (unit-)tests, run against a virtual HID device. |
| 2 | +# |
| 3 | +# The tests are written against the public HIDAPI API and the backend-agnostic |
| 4 | +# test_virtual_device interface (test_virtual_device.h), so the same test runs |
| 5 | +# against any backend for which a virtual-device provider exists. Each provider |
| 6 | +# implements the pre-recorded "scenario" protocol: the test triggers a scenario |
| 7 | +# by sending a Feature report, and the device replays a canned input report. |
| 8 | +# |
| 9 | +# Providers: |
| 10 | +# - Linux / hidraw : test_virtual_device_uhid.c (kernel /dev/uhid) |
| 11 | +# - Linux / libusb : test_virtual_device_rawgadget.c (/dev/raw-gadget + dummy_hcd) |
| 12 | +# - Windows / winapi : test_virtual_device_win.c (modified vhidmini2 UMDF2 driver) |
| 13 | +# - macOS / darwin : test_virtual_device_mac.c (IOHIDUserDevice) |
| 14 | +# |
| 15 | +# The libusb (raw-gadget), Windows and macOS virtual devices need privileged |
| 16 | +# out-of-band setup (kernel modules / a signed driver / an entitlement) that is |
| 17 | +# only performed by the dedicated CI jobs. Whenever the virtual device cannot be |
| 18 | +# created or does not enumerate, the test returns CTest's SKIP code (77) instead |
| 19 | +# of failing, so ordinary builds on any host stay green. |
| 20 | + |
| 21 | +find_package(Threads REQUIRED) |
| 22 | + |
| 23 | +# Define a device-I/O test <name> built from test_device_io.c + <provider>, |
| 24 | +# linked against the HIDAPI <backend>, and registered with CTest (it self-skips |
| 25 | +# via return code 77 when no virtual device is present). |
| 26 | +function(hidapi_add_vdev_test name provider backend) |
| 27 | + add_executable(${name} test_device_io.c ${provider}) |
| 28 | + set_target_properties(${name} PROPERTIES |
| 29 | + C_STANDARD 11 |
| 30 | + C_STANDARD_REQUIRED TRUE |
| 31 | + ) |
| 32 | + target_link_libraries(${name} PRIVATE ${backend} Threads::Threads) |
| 33 | + if(HIDAPI_ENABLE_ASAN AND NOT MSVC) |
| 34 | + target_link_options(${name} PRIVATE -fsanitize=address) |
| 35 | + endif() |
| 36 | + add_test(NAME ${name} COMMAND ${name}) |
| 37 | + set_tests_properties(${name} PROPERTIES |
| 38 | + SKIP_RETURN_CODE 77 |
| 39 | + TIMEOUT 60 |
| 40 | + ) |
| 41 | +endfunction() |
| 42 | + |
| 43 | +# --- Linux: hidraw backend via /dev/uhid ----------------------------------- |
| 44 | +if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND TARGET hidapi_hidraw) |
| 45 | + hidapi_add_vdev_test(DeviceIO_hidraw test_virtual_device_uhid.c hidapi_hidraw) |
| 46 | +endif() |
| 47 | + |
| 48 | +# --- Linux: libusb backend via /dev/raw-gadget (+ dummy_hcd) ---------------- |
| 49 | +# Built whenever the libusb backend is, so it keeps compiling; at runtime it |
| 50 | +# self-skips unless the raw-gadget virtual device has been set up (CI job). |
| 51 | +if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND TARGET hidapi_libusb) |
| 52 | + hidapi_add_vdev_test(DeviceIO_libusb test_virtual_device_rawgadget.c hidapi_libusb) |
| 53 | +endif() |
| 54 | + |
| 55 | +# --- Windows: winapi backend via a modified vhidmini2 UMDF driver ----------- |
| 56 | +if(WIN32 AND TARGET hidapi_winapi) |
| 57 | + hidapi_add_vdev_test(DeviceIO_winapi test_virtual_device_win.c hidapi_winapi) |
| 58 | + # HidD_GetPreparsedData / HidP_GetCaps used by the Windows provider. |
| 59 | + target_link_libraries(DeviceIO_winapi PRIVATE hid) |
| 60 | + # Run from the directory holding the hidapi DLL so a shared build can find |
| 61 | + # it at launch (there is no rpath on Windows). |
| 62 | + set_tests_properties(DeviceIO_winapi PROPERTIES |
| 63 | + WORKING_DIRECTORY "$<TARGET_FILE_DIR:hidapi_winapi>") |
| 64 | + # With ASan (MSVC) the test exe needs the ASan runtime DLL, which lives next |
| 65 | + # to the MSVC tools; add it to PATH (CMake >= 3.22). |
| 66 | + if(HIDAPI_ENABLE_ASAN AND MSVC AND NOT CMAKE_VERSION VERSION_LESS "3.22") |
| 67 | + get_filename_component(MSVC_BUILD_TOOLS_DIR "${CMAKE_LINKER}" DIRECTORY) |
| 68 | + set_property(TEST DeviceIO_winapi PROPERTY |
| 69 | + ENVIRONMENT_MODIFICATION "PATH=path_list_append:${MSVC_BUILD_TOOLS_DIR}") |
| 70 | + endif() |
| 71 | +endif() |
| 72 | + |
| 73 | +# --- macOS: IOKit backend via IOHIDUserDevice ------------------------------ |
| 74 | +# Self-skips where the virtual-device entitlement / user consent isn't |
| 75 | +# available (e.g. hosted CI runners); usable locally / on a self-hosted Mac. |
| 76 | +if(APPLE AND TARGET hidapi_darwin) |
| 77 | + hidapi_add_vdev_test(DeviceIO_darwin test_virtual_device_mac.c hidapi_darwin) |
| 78 | + target_link_libraries(DeviceIO_darwin PRIVATE |
| 79 | + "-framework IOKit" "-framework CoreFoundation") |
| 80 | +endif() |
0 commit comments