martinpitt / umockdev

Mock hardware devices for creating unit tests and bug reporting
https://launchpad.net/umockdev
GNU Lesser General Public License v2.1
308 stars 54 forks source link

Help with mocking input/evdev device #223

Closed sicelo closed 7 months ago

sicelo commented 7 months ago

I am working on support for (old-school) evdev-based proximity sensors in iio-sensor-proxy.

The test suite uses umockdev, and I have generated the required files using umockdev-record:

Feeding that data into evtest is successful:

nokia-n900:~/iio-sensor-proxy/builddir$ sudo umockdev-run -d ../tests/input-proximity-data/umockdev -i /dev/input/event4=../tests/input-proximity-data/ioctl -e /dev/input/event4=../tests/input-proximity-data/events -- evtest /dev/input/event4
[sudo] password for user: 
Input driver version is 1.0.1
Input device ID: bus 0x19 vendor 0x1 product 0x1 version 0x100
Input device name: "gpio_keys"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 152 (KEY_SCREENLOCK)
    Event code 212 (KEY_CAMERA)
    Event code 528 (KEY_CAMERA_FOCUS)
  Event type 5 (EV_SW)
    Event code 9 (SW_CAMERA_LENS_COVER) state 1
    Event code 10 (SW_KEYPAD_SLIDE) state 0
    Event code 11 (SW_FRONT_PROXIMITY) state 0
    Event code 16 (?) state 1
Properties:
Testing ... (interrupt to exit)
Event: time 1706552688.289347, type 5 (EV_SW), code 11 (SW_FRONT_PROXIMITY), value 1
Event: time 1706552688.289347, -------------- SYN_REPORT ------------
Event: time 1706552693.000007, type 5 (EV_SW), code 11 (SW_FRONT_PROXIMITY), value 0
Event: time 1706552693.000007, -------------- SYN_REPORT ------------
Event: time 1706552696.670935, type 5 (EV_SW), code 11 (SW_FRONT_PROXIMITY), value 1
Event: time 1706552696.670935, -------------- SYN_REPORT ------------
^C

However, with iio-sensor-proxy, the exact same data fails on the ioctl call in https://gitlab.freedesktop.org/hadess/iio-sensor-proxy/-/blob/e0995ca1287c6c2797f7e8b20cc5e7d3fee7ca0c/src/drv-input-proximity.c#L162-L163, as shown below:

nokia-n900:~/iio-sensor-proxy/builddir$ sudo UMOCKDEV_DEBUG=all umockdev-run -d ../tests/input-proximity-data/umockdev -i /dev/input/event4=../tests/input-proximity-data/ioctl -e /dev/input/event4=../tests/inpu
t-proximity-data/events -- src/iio-sensor-proxy -rv
ioctl_simplestruct_init_from_text: adjusting ioctl ID 80204506 (size 32) to actual data length 256
ioctl_simplestruct_init_from_text: adjusting ioctl ID 80204520 (size 32) to actual data length 31
ioctl_simplestruct_init_from_text: adjusting ioctl ID 80204521 (size 32) to actual data length 767
ioctl_simplestruct_init_from_text: adjusting ioctl ID 8020451B (size 32) to actual data length 3072
ioctl_simplestruct_init_from_text: adjusting ioctl ID 80204525 (size 32) to actual data length 767
ioctl_simplestruct_init_from_text: adjusting ioctl ID 80204509 (size 32) to actual data length 124
isatty(1): is a terminal, ttyname /dev/pts/3
isatty(1): readlink(/tmp/umockdev.I4R8H2/dev/.ptymap/_dev_pts_3) failed: No such file or directory
testbed wrapped fopen(/usr/lib/charset.alias) -> /usr/lib/charset.alias
** (iio-sensor-proxy:12732): DEBUG: 23:56:38.125: Starting iio-sensor-proxy version 3.5
testbed wrapped open(/usr/share/locale/C.UTF-8/LC_MESSAGES/messages.mo) -> /usr/share/locale/C.UTF-8/LC_MESSAGES/messages.mo
script_record_open: fd -1 on device 0:0 is not recorded
testbed wrapped open(/usr/share/locale/C.utf8/LC_MESSAGES/messages.mo) -> /usr/share/locale/C.utf8/LC_MESSAGES/messages.mo
script_record_open: fd -1 on device 0:0 is not recorded
testbed wrapped open(/usr/share/locale/C/LC_MESSAGES/messages.mo) -> /usr/share/locale/C/LC_MESSAGES/messages.mo
script_record_open: fd -1 on device 0:0 is not recorded
testbed wrapped open(/usr/share/locale/C.UTF-8/LC_MESSAGES/glib20.mo) -> /usr/share/locale/C.UTF-8/LC_MESSAGES/glib20.mo
script_record_open: fd -1 on device 0:0 is not recorded
testbed wrapped open(/usr/share/locale/C.utf8/LC_MESSAGES/glib20.mo) -> /usr/share/locale/C.utf8/LC_MESSAGES/glib20.mo
script_record_open: fd -1 on device 0:0 is not recorded
testbed wrapped open(/usr/share/locale/C/LC_MESSAGES/glib20.mo) -> /usr/share/locale/C/LC_MESSAGES/glib20.mo
script_record_open: fd -1 on device 0:0 is not recorded
isatty(1): is a terminal, ttyname /dev/pts/3
isatty(1): readlink(/tmp/umockdev.I4R8H2/dev/.ptymap/_dev_pts_3) failed: No such file or directory
(iio-sensor-proxy:12732): GLib-GIO-DEBUG: 23:56:38.177: Using cross-namespace EXTERNAL authentication (this will deadlock if server is GDBus < 2.73.3)
testbed wrapped fopen(/etc/udev/udev.conf) -> /etc/udev/udev.conf
script_record_open: fd 7 on device 0:0 is not recorded
testbed wrapped socket: intercepting netlink, fd 7
testbed wrapped bind: intercepting netlink socket fd 7
testbed wrapped stat(/sys/subsystem) -> /tmp/umockdev.I4R8H2/sys/subsystem
testbed wrapped opendir(/sys/bus) -> /tmp/umockdev.I4R8H2/sys/bus
testbed wrapped opendir(/sys/class) -> /tmp/umockdev.I4R8H2/sys/class
testbed wrapped stat(/sys/subsystem) -> /tmp/umockdev.I4R8H2/sys/subsystem
testbed wrapped opendir(/sys/bus) -> /tmp/umockdev.I4R8H2/sys/bus
testbed wrapped opendir(/sys/class) -> /tmp/umockdev.I4R8H2/sys/class
testbed wrapped opendir(/sys/class/input) -> /tmp/umockdev.I4R8H2/sys/class/input
testbed wrapped stat(/sys/devices/platform/gpio_keys/input/input4/uevent) -> /tmp/umockdev.I4R8H2/sys/devices/platform/gpio_keys/input/input4/uevent
testbed wrapped stat(/sys/devices/platform/gpio_keys/input/input4/event4/uevent) -> /tmp/umockdev.I4R8H2/sys/devices/platform/gpio_keys/input/input4/event4/uevent
testbed wrapped stat(/sys/devices/platform/gpio_keys/input/input4/uevent) -> /tmp/umockdev.I4R8H2/sys/devices/platform/gpio_keys/input/input4/uevent
testbed wrapped fopen(/sys/devices/platform/gpio_keys/input/input4/uevent) -> /tmp/umockdev.I4R8H2/sys/devices/platform/gpio_keys/input/input4/uevent
testbed wrapped fopen(/run/udev/data/+input:input4) -> /tmp/umockdev.I4R8H2/run/udev/data/+input:input4
testbed wrapped stat(/sys/devices/platform/gpio_keys/input/input4/event4/uevent) -> /tmp/umockdev.I4R8H2/sys/devices/platform/gpio_keys/input/input4/event4/uevent
testbed wrapped fopen(/sys/devices/platform/gpio_keys/input/input4/event4/uevent) -> /tmp/umockdev.I4R8H2/sys/devices/platform/gpio_keys/input/input4/event4/uevent
testbed wrapped fopen(/run/udev/data/c13:68) -> /tmp/umockdev.I4R8H2/run/udev/data/c13:68
testbed wrapped stat(/sys/subsystem) -> /tmp/umockdev.I4R8H2/sys/subsystem
testbed wrapped opendir(/sys/bus) -> /tmp/umockdev.I4R8H2/sys/bus
testbed wrapped opendir(/sys/bus/platform/devices) -> /tmp/umockdev.I4R8H2/sys/bus/platform/devices
testbed wrapped stat(/sys/devices/platform/gpio_keys/uevent) -> /tmp/umockdev.I4R8H2/sys/devices/platform/gpio_keys/uevent
testbed wrapped opendir(/sys/class) -> /tmp/umockdev.I4R8H2/sys/class
testbed wrapped stat(/sys/devices/platform/gpio_keys/uevent) -> /tmp/umockdev.I4R8H2/sys/devices/platform/gpio_keys/uevent
testbed wrapped fopen(/sys/devices/platform/gpio_keys/uevent) -> /tmp/umockdev.I4R8H2/sys/devices/platform/gpio_keys/uevent
testbed wrapped fopen(/run/udev/data/+platform:gpio_keys) -> /tmp/umockdev.I4R8H2/run/udev/data/+platform:gpio_keys
testbed wrapped open(/sys/devices/platform/gpio_keys/input/input4/event4/../capabilities/sw) -> /tmp/umockdev.I4R8H2/sys/devices/platform/gpio_keys/input/input4/event4/../capabilities/sw
isatty(1): is a terminal, ttyname /dev/pts/3
isatty(1): readlink(/tmp/umockdev.I4R8H2/dev/.ptymap/_dev_pts_3) failed: No such file or directory
** (iio-sensor-proxy:12732): DEBUG: 23:56:38.336: Found input proximity sensor at /sys/devices/platform/gpio_keys/input/input4/event4
isatty(1): is a terminal, ttyname /dev/pts/3
isatty(1): readlink(/tmp/umockdev.I4R8H2/dev/.ptymap/_dev_pts_3) failed: No such file or directory
** (iio-sensor-proxy:12732): DEBUG: 23:56:38.336: Found device /sys/devices/platform/gpio_keys/input/input4/event4 of type proximity at Input proximity
isatty(1): is a terminal, ttyname /dev/pts/3
isatty(1): readlink(/tmp/umockdev.I4R8H2/dev/.ptymap/_dev_pts_3) failed: No such file or directory
** (iio-sensor-proxy:12732): DEBUG: 23:56:38.418: Handling driver refcounting method 'ClaimAccelerometer' for accelerometer device
isatty(1): is a terminal, ttyname /dev/pts/3
isatty(1): readlink(/tmp/umockdev.I4R8H2/dev/.ptymap/_dev_pts_3) failed: No such file or directory
** (iio-sensor-proxy:12732): DEBUG: 23:56:38.483: Handling driver refcounting method 'ClaimLight' for ambient light sensor device
isatty(1): is a terminal, ttyname /dev/pts/3
isatty(1): readlink(/tmp/umockdev.I4R8H2/dev/.ptymap/_dev_pts_3) failed: No such file or directory
** (iio-sensor-proxy:12732): DEBUG: 23:56:38.550: Handling driver refcounting method 'ClaimProximity' for proximity device
testbed wrapped open(/dev/input/event4) -> /tmp/umockdev.I4R8H2/dev/input/event4
ioctl_emulate_open fd 8 (/dev/input/event4): connected ioctl sockert
ioctl_tree_execute ioctl 8004451B
   ioctl_tree_execute: checking node EVIOCGVERSION(80044501, base id 80044501) 01000100
   ioctl_tree_execute: checking node EVIOCGID(80084502, base id 80084502) 1900010001000001
   ioctl_tree_execute: checking node EVIOCGNAME(81004506, base id 80204506) 6770696F5F6B65797300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
   ioctl_tree_execute: checking node EVIOCGBIT(801F4520, base id 80204520) 23000000000000000000000000000000000000000000000000000000000000
   ioctl_tree_execute: checking node EVIOCGBIT(82FF4521, base id 80204520) 0000000000000000000000000000000000000001000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
   ioctl_tree_execute: checking node EVIOCGSW(8C00451B, base id 8020451B) 000201000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
   ioctl_tree_execute: checking node EVIOCGBIT(82FF4525, base id 80204520) 000E010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
   ioctl_tree_execute: checking node EVIOCGPROP(807C4509, base id 80204509) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    -> full iteration with last == NULL, not found
ioctl fd 8 request 8004451B: emulated, result -1
isatty(2): is a terminal, ttyname /dev/pts/3
isatty(2): readlink(/tmp/umockdev.I4R8H2/dev/.ptymap/_dev_pts_3) failed: No such file or directory

** (iio-sensor-proxy:12732): WARNING **: 23:56:38.842: could not determine sensor state, ioctl EVIOCGSW on /sys/devices/platform/gpio_keys/input/input4/event4 failed
ioctl_emulate_close: closing ioctl socket fd 8

On the actual hardware (on which the umockdev data was generated), the code runs fine and iio-sensor-proxy successfully monitors the state of the proximity sensor as expected.

I would appreciate any ideas for where I might have made a mistake, and/or how to get the above test to succeed.

benzea commented 7 months ago

In ioctl_tree.c, I see:

I_NAMED_SIMPLE_STRUCT_IN(EVIOCGSW(32), "EVIOCGSW", 0, ioctl_insertion_parent_stateless),

half thinking that it only accepts 32 bytes for the buffer length (in this case SW_MAX / 8). Maybe that should be something like:

I_NAMED_SIMPLE_STRUCT_IN(EVIOCGSW(0), "EVIOCGSW", 32, ioctl_insertion_parent_stateless),

Instead?

sicelo commented 7 months ago

I got it working by using iio-sensor-proxy itself during the ioctl recording stage, instead of evtest :-)