From nobody Tue Apr 23 20:52:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 62EF8C7EE2D for ; Fri, 26 May 2023 08:22:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242364AbjEZIWW (ORCPT ); Fri, 26 May 2023 04:22:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236878AbjEZIWU (ORCPT ); Fri, 26 May 2023 04:22:20 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9914BD9; Fri, 26 May 2023 01:22:18 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 17F5064DF7; Fri, 26 May 2023 08:22:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EC7ECC433D2; Fri, 26 May 2023 08:22:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685089337; bh=z68pfjdEhosaPEsjtJpzRfNnBwTmmXtSizLI17MVt90=; h=From:To:Cc:Subject:Date:From; b=tH0+LdPG5GHk0K0zQGVYcMQ8UUUtQaZUEQphsdeqeybxs3s+1gc8zbvKYQNfbaONw GdaZr/F0LTinx+0VEbUq1tjNlfmI019nDWtxmg17yH0vMSzL1Tjii2n1ZWZyRYB/jo HQPwBfUXpu8CWVDNvPDSdjvtDSotVV/9uXlCY1jsJIH7Ye266qrc/nqlXpzavPJekK lTkxbamBP91cJMuaRBO3U/xg7zpXA8C+hC/N7QHNRX7gy8g4qWthaSdDGGu1+nOMRY THWELS1p0rm4/PrKC0KEA3/GxzeHz+xl382KvvE3MWbpuUDP2RpMU/nIUW6hQGxR3N gZi+wtkmeOfjA== From: eballetbo@kernel.org To: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Shuah Khan , Dmitry Torokhov , Javier Martinez Canillas , Dana Elfassy , linux-input@vger.kernel.org, phuttere@redhat.com, Benjamin Tissoires , Enric Balletbo i Serra Subject: [PATCH] selftests/input: Introduce basic tests for evdev ioctls Date: Fri, 26 May 2023 10:22:05 +0200 Message-Id: <20230526082205.6297-1-eballetbo@kernel.org> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Enric Balletbo i Serra This provides a basic infrastructure for the creation of tests for the evdev interface. Most of this is code is adapted from the libevdev wrapper library. While most of evdev ioctls are covered and tested using libevdev tests there are some evdev ioctls that aren't because are not supported (and will not be supported) by libevdev. So, adding, at least those tests, would make sense. The test creates an uinput device (and an evdev device) so you can call the wanted ioctl from userspace. So, to run those tests you need to have support for uinput and evdev as well. Signed-off-by: Enric Balletbo i Serra --- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/input/.gitignore | 2 + tools/testing/selftests/input/Makefile | 5 + tools/testing/selftests/input/config | 3 + tools/testing/selftests/input/evioc-test.c | 282 +++++++++++++++++++++ 5 files changed, 293 insertions(+) create mode 100644 tools/testing/selftests/input/.gitignore create mode 100644 tools/testing/selftests/input/Makefile create mode 100644 tools/testing/selftests/input/config create mode 100644 tools/testing/selftests/input/evioc-test.c diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Mak= efile index 90a62cf75008..29fc77168aa7 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -28,6 +28,7 @@ TARGETS +=3D futex TARGETS +=3D gpio TARGETS +=3D hid TARGETS +=3D intel_pstate +TARGETS +=3D input TARGETS +=3D iommu TARGETS +=3D ipc TARGETS +=3D ir diff --git a/tools/testing/selftests/input/.gitignore b/tools/testing/selft= ests/input/.gitignore new file mode 100644 index 000000000000..37f5dff3255b --- /dev/null +++ b/tools/testing/selftests/input/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +evioc-test diff --git a/tools/testing/selftests/input/Makefile b/tools/testing/selftes= ts/input/Makefile new file mode 100644 index 000000000000..031729be0628 --- /dev/null +++ b/tools/testing/selftests/input/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS +=3D -D_GNU_SOURCE -std=3Dgnu99 -Wl,-no-as-needed -Wall $(KHDR_INC= LUDES) + +TEST_GEN_PROGS :=3D evioc-test +include ../lib.mk diff --git a/tools/testing/selftests/input/config b/tools/testing/selftests= /input/config new file mode 100644 index 000000000000..b7512f3e6d8d --- /dev/null +++ b/tools/testing/selftests/input/config @@ -0,0 +1,3 @@ +CONFIG_INPUT=3Dy +CONFIG_INPUT_EVDEV=3Dy +CONFIG_INPUT_UINPUT=3Dm diff --git a/tools/testing/selftests/input/evioc-test.c b/tools/testing/sel= ftests/input/evioc-test.c new file mode 100644 index 000000000000..4c0c8ebed378 --- /dev/null +++ b/tools/testing/selftests/input/evioc-test.c @@ -0,0 +1,282 @@ +// SPDX-License-Identifier: MIT +/* + * Copyright =C2=A9 2023 Red Hat, Inc. + * + * Part of the code in this file is inspired and copied from the libevdev = wrapper library + * for evdev devices written by Peter Hutterer. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +#define TEST_DEVICE_NAME "selftest input device" + +struct selftest_uinput { + int uinput_fd; /** file descriptor to uinput */ + int evdev_fd; /** file descriptor to evdev */ + char *name; /** device name */ + char *syspath; /** /sys path */ + char *devnode; /** device node */ +}; + +static int is_event_device(const struct dirent *dent) +{ + return strncmp("event", dent->d_name, 5) =3D=3D 0; +} + +static char *fetch_device_node(const char *path) +{ + struct dirent **namelist; + char *devnode =3D NULL; + int ndev, i; + + ndev =3D scandir(path, &namelist, is_event_device, alphasort); + if (ndev <=3D 0) + return NULL; + + /* ndev should only ever be 1 */ + + for (i =3D 0; i < ndev; i++) { + if (!devnode && asprintf(&devnode, "/dev/input/%s", + namelist[i]->d_name) =3D=3D -1) + devnode =3D NULL; + free(namelist[i]); + } + + free(namelist); + + return devnode; +} + +static int is_input_device(const struct dirent *dent) +{ + return strncmp("input", dent->d_name, 5) =3D=3D 0; +} + +/* + * Device node is guessed based on the sysfs path, the sysfs path contains= a + * eventN file, that corresponds to our /dev/input/eventN number. + */ +static int fetch_syspath_and_devnode(struct selftest_uinput *uidev) +{ +#define SYS_INPUT_DIR "/sys/devices/virtual/input/" + struct dirent **namelist; + int ndev, i; + int rc; + char buf[sizeof(SYS_INPUT_DIR) + 64] =3D SYS_INPUT_DIR; + + rc =3D ioctl(uidev->uinput_fd, + UI_GET_SYSNAME(sizeof(buf) - strlen(SYS_INPUT_DIR)), + &buf[strlen(SYS_INPUT_DIR)]); + if (rc !=3D -1) { + uidev->syspath =3D strdup(buf); + uidev->devnode =3D fetch_device_node(buf); + return 0; + } + + ndev =3D scandir(SYS_INPUT_DIR, &namelist, is_input_device, alphasort); + if (ndev <=3D 0) + return -1; + + for (i =3D 0; i < ndev; i++) { + int fd, len; + + rc =3D snprintf(buf, sizeof(buf), "%s%s/name", SYS_INPUT_DIR, + namelist[i]->d_name); + if (rc < 0 || (size_t)rc >=3D sizeof(buf)) + continue; + + /* created within time frame */ + fd =3D open(buf, O_RDONLY); + if (fd < 0) + continue; + + len =3D read(fd, buf, sizeof(buf)); + close(fd); + if (len <=3D 0) + continue; + + buf[len - 1] =3D '\0'; /* file contains \n */ + if (strcmp(buf, uidev->name) =3D=3D 0) { + if (uidev->syspath) { + fprintf(stderr, + "multiple identical devices found. syspath is unreliable\n"); + break; + } + + rc =3D snprintf(buf, sizeof(buf), "%s%s", SYS_INPUT_DIR, + namelist[i]->d_name); + + if (rc < 0 || (size_t)rc >=3D sizeof(buf)) { + fprintf(stderr, + "Invalid syspath, syspath is unreliable\n"); + break; + } + + uidev->syspath =3D strdup(buf); + uidev->devnode =3D fetch_device_node(buf); + } + } + + for (i =3D 0; i < ndev; i++) + free(namelist[i]); + free(namelist); + + return uidev->devnode ? 0 : -1; +#undef SYS_INPUT_DIR +} + +void selftest_uinput_destroy(struct selftest_uinput *uidev) +{ + if (!uidev) + return; + + if (uidev->uinput_fd >=3D 0) + ioctl(uidev->uinput_fd, UI_DEV_DESTROY, NULL); + + close(uidev->evdev_fd); + close(uidev->uinput_fd); + + free(uidev->syspath); + free(uidev->devnode); + free(uidev->name); + free(uidev); +} + +int selftest_uinput_create_device(struct selftest_uinput **uidev, ...) +{ + struct selftest_uinput *new_device; + struct uinput_setup setup; + va_list args; + int rc, fd; + int type; + + new_device =3D calloc(1, sizeof(struct selftest_uinput)); + if (!new_device) + return -ENOMEM; + + memset(&setup, 0, sizeof(setup)); + strncpy(setup.name, TEST_DEVICE_NAME, UINPUT_MAX_NAME_SIZE - 1); + setup.id.vendor =3D 0x1234; /* sample vendor */ + setup.id.product =3D 0x5678; /* sample product */ + setup.id.bustype =3D BUS_USB; + + fd =3D open("/dev/uinput", O_WRONLY | O_NONBLOCK); + if (fd < 0) { + fprintf(stderr, "cannot open uinput (%d): %m\n", errno); + goto error; + } + + va_start(args, uidev); + rc =3D 0; + do { + type =3D va_arg(args, int); + if (type =3D=3D -1) + break; + rc =3D ioctl(fd, UI_SET_EVBIT, type); + } while (rc =3D=3D 0); + va_end(args); + + rc =3D ioctl(fd, UI_DEV_SETUP, &setup); + if (rc =3D=3D -1) + goto error; + + rc =3D ioctl(fd, UI_DEV_CREATE, NULL); + if (rc =3D=3D -1) + goto error; + + new_device->name =3D strdup(TEST_DEVICE_NAME); + new_device->uinput_fd =3D fd; + + if (fetch_syspath_and_devnode(new_device) =3D=3D -1) { + fprintf(stderr, "unable to fetch syspath or device node.\n"); + errno =3D ENODEV; + goto error; + } + + fd =3D open(new_device->devnode, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "cannot open uinput (%d): %m\n", errno); + goto error; + } + new_device->evdev_fd =3D fd; + + *uidev =3D new_device; + + return 0; + +error: + rc =3D -errno; + selftest_uinput_destroy(new_device); + return rc; +} + +const char *selftest_uinput_get_devnode(struct selftest_uinput *uidev) +{ + return uidev->devnode; +} + +TEST(eviocgname_get_device_name) +{ + struct selftest_uinput *uidev; + char buf[256]; + int rc; + + rc =3D selftest_uinput_create_device(&uidev); + ASSERT_EQ(0, rc); + ASSERT_NE(NULL, uidev); + + memset(buf, 0, sizeof(buf)); + /* ioctl to get the name */ + rc =3D ioctl(uidev->evdev_fd, EVIOCGNAME(sizeof(buf) - 1), buf); + ASSERT_GE(rc, 0); + ASSERT_STREQ(TEST_DEVICE_NAME, buf); + + selftest_uinput_destroy(uidev); +} + +TEST(eviocgrep_get_repeat_settings) +{ + struct selftest_uinput *uidev; + int rep_values[2]; + int rc; + + memset(rep_values, 0, sizeof(rep_values)); + + rc =3D selftest_uinput_create_device(&uidev); + ASSERT_EQ(0, rc); + ASSERT_NE(NULL, uidev); + + /* ioctl to get the repeat rates values */ + rc =3D ioctl(uidev->evdev_fd, EVIOCSREP, rep_values); + /* should fail because EV_REP is not set */ + ASSERT_EQ(-1, rc); + + selftest_uinput_destroy(uidev); + + rc =3D selftest_uinput_create_device(&uidev, EV_REP); + ASSERT_EQ(0, rc); + ASSERT_NE(NULL, uidev); + + /* ioctl to get the repeat rates values */ + rc =3D ioctl(uidev->evdev_fd, EVIOCGREP, rep_values); + ASSERT_EQ(0, rc); + /* should get the default delay an period values set by the kernel */ + ASSERT_EQ(rep_values[0], 250); + ASSERT_EQ(rep_values[1], 33); + + selftest_uinput_destroy(uidev); +} + +TEST_HARNESS_MAIN --=20 2.40.1