From: Benjamin Tissoires <benjamin.tissoires@redhat.com>
To: Greg KH <gregkh@linuxfoundation.org>,
Jiri Kosina <jikos@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: Tero Kristo <tero.kristo@linux.intel.com>,
linux-kernel@vger.kernel.org, linux-input@vger.kernel.org,
netdev@vger.kernel.org, bpf@vger.kernel.org,
Benjamin Tissoires <benjamin.tissoires@redhat.com>
Subject: [RFC hid v1 07/10] selftests: hid: Add a variant parameter so we can emulate specific devices
Date: Thu, 24 Nov 2022 16:16:00 +0100 [thread overview]
Message-ID: <20221124151603.807536-8-benjamin.tissoires@redhat.com> (raw)
In-Reply-To: <20221124151603.807536-1-benjamin.tissoires@redhat.com>
When testing with in-kernel bpf programs, we need to emulate a specific
device, because otherwise we won't be loading the matching bpf program.
Add a variant parameter to the fixture hid_bpf so that we can re-use it
later with different devices.
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
---
tools/testing/selftests/hid/hid_bpf.c | 75 +++++++++++++++++++--------
1 file changed, 53 insertions(+), 22 deletions(-)
diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c
index 6615c26fb5dd..738ddb2c6a62 100644
--- a/tools/testing/selftests/hid/hid_bpf.c
+++ b/tools/testing/selftests/hid/hid_bpf.c
@@ -114,6 +114,15 @@ static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER;
/* no need to protect uhid_stopped, only one thread accesses it */
static bool uhid_stopped;
+struct hid_device_id {
+ int bus;
+ int vendor;
+ int product;
+ int version;
+ unsigned char *rdesc;
+ const size_t rdesc_sz;
+};
+
static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uhid_event *ev)
{
ssize_t ret;
@@ -131,7 +140,8 @@ static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uh
}
}
-static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb)
+static int uhid_create(struct __test_metadata *_metadata, const struct hid_device_id *variant,
+ int fd, int rand_nb)
{
struct uhid_event ev;
char buf[25];
@@ -141,12 +151,12 @@ static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb)
memset(&ev, 0, sizeof(ev));
ev.type = UHID_CREATE;
strcpy((char *)ev.u.create.name, buf);
- ev.u.create.rd_data = rdesc;
- ev.u.create.rd_size = sizeof(rdesc);
- ev.u.create.bus = BUS_USB;
- ev.u.create.vendor = 0x0001;
- ev.u.create.product = 0x0a37;
- ev.u.create.version = 0;
+ ev.u.create.rd_data = variant->rdesc;
+ ev.u.create.rd_size = variant->rdesc_sz;
+ ev.u.create.bus = variant->bus;
+ ev.u.create.vendor = variant->vendor;
+ ev.u.create.product = variant->product;
+ ev.u.create.version = variant->version;
ev.u.create.country = 0;
sprintf(buf, "%d", rand_nb);
@@ -299,7 +309,8 @@ static int uhid_send_event(struct __test_metadata *_metadata, int fd, __u8 *buf,
return uhid_write(_metadata, fd, &ev);
}
-static int setup_uhid(struct __test_metadata *_metadata, int rand_nb)
+static int setup_uhid(struct __test_metadata *_metadata, const struct hid_device_id *variant,
+ int rand_nb)
{
int fd;
const char *path = "/dev/uhid";
@@ -308,7 +319,7 @@ static int setup_uhid(struct __test_metadata *_metadata, int rand_nb)
fd = open(path, O_RDWR | O_CLOEXEC);
ASSERT_GE(fd, 0) TH_LOG("open uhid-cdev failed; %d", fd);
- ret = uhid_create(_metadata, fd, rand_nb);
+ ret = uhid_create(_metadata, variant, fd, rand_nb);
ASSERT_EQ(0, ret) {
TH_LOG("create uhid device failed: %d", ret);
close(fd);
@@ -317,15 +328,19 @@ static int setup_uhid(struct __test_metadata *_metadata, int rand_nb)
return fd;
}
-static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir)
+static bool match_sysfs_device(int dev_id, const struct hid_device_id *variant,
+ const char *workdir, struct dirent *dir)
{
- const char *target = "0003:0001:0A37.*";
+ char target[17] = "0003:0001:0A37.*";
char phys[512];
char uevent[1024];
char temp[512];
int fd, nread;
bool found = false;
+ snprintf(target, sizeof(target), "0003:%04X:%04X.*",
+ variant->vendor, variant->product);
+
if (fnmatch(target, dir->d_name, 0))
return false;
@@ -347,7 +362,7 @@ static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *d
return found;
}
-static int get_hid_id(int dev_id)
+static int get_hid_id(int dev_id, const struct hid_device_id *variant)
{
const char *workdir = "/sys/devices/virtual/misc/uhid";
const char *str_id;
@@ -362,7 +377,7 @@ static int get_hid_id(int dev_id)
d = opendir(workdir);
if (d) {
while ((dir = readdir(d)) != NULL) {
- if (!match_sysfs_device(dev_id, workdir, dir))
+ if (!match_sysfs_device(dev_id, variant, workdir, dir))
continue;
str_id = dir->d_name + sizeof("0003:0001:0A37.");
@@ -379,7 +394,7 @@ static int get_hid_id(int dev_id)
return found;
}
-static int get_hidraw(int dev_id)
+static int get_hidraw(int dev_id, const struct hid_device_id *variant)
{
const char *workdir = "/sys/devices/virtual/misc/uhid";
char sysfs[1024];
@@ -396,7 +411,7 @@ static int get_hidraw(int dev_id)
continue;
while ((dir = readdir(d)) != NULL) {
- if (!match_sysfs_device(dev_id, workdir, dir))
+ if (!match_sysfs_device(dev_id, variant, workdir, dir))
continue;
sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name);
@@ -423,12 +438,12 @@ static int get_hidraw(int dev_id)
return found;
}
-static int open_hidraw(int dev_id)
+static int open_hidraw(int dev_id, const struct hid_device_id *variant)
{
int hidraw_number;
char hidraw_path[64] = { 0 };
- hidraw_number = get_hidraw(dev_id);
+ hidraw_number = get_hidraw(dev_id, variant);
if (hidraw_number < 0)
return hidraw_number;
@@ -445,6 +460,22 @@ FIXTURE(hid_bpf) {
pthread_t tid;
struct hid *skel;
};
+
+FIXTURE_VARIANT(hid_bpf) {
+ const struct hid_device_id id;
+};
+
+FIXTURE_VARIANT_ADD(hid_bpf, generic_device) {
+ .id = {
+ .bus = BUS_USB,
+ .vendor = 0x0001,
+ .product = 0x0a37,
+ .version = 0,
+ .rdesc = rdesc,
+ .rdesc_sz = sizeof(rdesc),
+ },
+};
+
static void detach_bpf(FIXTURE_DATA(hid_bpf) * self)
{
if (self->hidraw_fd)
@@ -478,10 +509,10 @@ FIXTURE_SETUP(hid_bpf)
self->dev_id = rand() % 1024;
- self->uhid_fd = setup_uhid(_metadata, self->dev_id);
+ self->uhid_fd = setup_uhid(_metadata, &variant->id, self->dev_id);
- /* locate the uev, self, variant);ent file of the created device */
- self->hid_id = get_hid_id(self->dev_id);
+ /* locate the file of the created device */
+ self->hid_id = get_hid_id(self->dev_id, &variant->id);
ASSERT_GT(self->hid_id, 0)
TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id);
@@ -546,7 +577,7 @@ static void load_programs(const struct test_program programs[],
ASSERT_OK(args.retval) TH_LOG("attach_hid(%s): %d", programs[i].name, args.retval);
}
- self->hidraw_fd = open_hidraw(self->dev_id);
+ self->hidraw_fd = open_hidraw(self->dev_id, &variant->id);
ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
}
@@ -644,7 +675,7 @@ TEST_F(hid_bpf, test_attach_detach)
/* detach the program */
detach_bpf(self);
- self->hidraw_fd = open_hidraw(self->dev_id);
+ self->hidraw_fd = open_hidraw(self->dev_id, &variant->id);
ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
/* inject another event */
--
2.38.1
next prev parent reply other threads:[~2022-11-24 15:19 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-24 15:15 [RFC hid v1 00/10] HID-BPF: add support for in-tree BPF programs Benjamin Tissoires
2022-11-24 15:15 ` [RFC hid v1 01/10] bpftool: generate json output of skeletons Benjamin Tissoires
2022-11-30 23:05 ` Andrii Nakryiko
2022-12-01 14:22 ` Benjamin Tissoires
2022-12-01 18:21 ` Andrii Nakryiko
2022-11-24 15:15 ` [RFC hid v1 02/10] WIP: bpf: allow to pin programs from the kernel when bpffs is mounted Benjamin Tissoires
2022-11-24 15:15 ` [RFC hid v1 03/10] HID: add a tool to convert a bpf source into a generic bpf loader Benjamin Tissoires
2022-11-24 15:15 ` [RFC hid v1 04/10] HID: add the bpf loader that can attach a generic hid-bpf program Benjamin Tissoires
2022-11-24 15:15 ` [RFC hid v1 05/10] HID: add report descriptor override for the X-Keys XK24 Benjamin Tissoires
2022-11-24 15:15 ` [RFC hid v1 06/10] selftests: hid: add vmtest.sh Benjamin Tissoires
2022-11-24 15:16 ` Benjamin Tissoires [this message]
2022-11-24 15:16 ` [RFC hid v1 08/10] selftests: hid: add XK-24 tests Benjamin Tissoires
2022-11-24 15:16 ` [RFC hid v1 09/10] selftests: hid: ensure the program is correctly pinned Benjamin Tissoires
2022-11-24 15:16 ` [RFC hid v1 10/10] wip: vmtest aarch64 Benjamin Tissoires
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221124151603.807536-8-benjamin.tissoires@redhat.com \
--to=benjamin.tissoires@redhat.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dmitry.torokhov@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=jikos@kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=tero.kristo@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).