From nobody Mon Sep 16 19:31:11 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 11750C433FE for ; Thu, 24 Nov 2022 15:20:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230125AbiKXPUB (ORCPT ); Thu, 24 Nov 2022 10:20:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230095AbiKXPTH (ORCPT ); Thu, 24 Nov 2022 10:19:07 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9434416F0E1 for ; Thu, 24 Nov 2022 07:16:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669302987; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ypQ0SpHVkNHir4A6yQp/geR9p4ucQgVAX8Ozo4Lqh9o=; b=VeF6BdDNnpGiJxF1hgW9XkenGgjrocLnYKdOQBCAXH4Swq7NKhb+VZfKNT17uNt+hR9Hio stSP8+5AXA02Sxz3WhYEXi62oufztoYGxoZD+5WWa3ZTr4HqPeSubbnMkeWbuR5x+XH5I1 LvG8picdAST0Akup+W8Qz6R4Y8dHgcA= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-312-k0Kc5RVSP-2hPsqur3AHug-1; Thu, 24 Nov 2022 10:16:24 -0500 X-MC-Unique: k0Kc5RVSP-2hPsqur3AHug-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C691E1C05B0C; Thu, 24 Nov 2022 15:16:23 +0000 (UTC) Received: from plouf.redhat.com (unknown [10.39.193.200]) by smtp.corp.redhat.com (Postfix) with ESMTP id D9C1840C2064; Thu, 24 Nov 2022 15:16:21 +0000 (UTC) From: Benjamin Tissoires To: Greg KH , Jiri Kosina , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Dmitry Torokhov Cc: Tero Kristo , linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires 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 Message-Id: <20221124151603.807536-8-benjamin.tissoires@redhat.com> In-Reply-To: <20221124151603.807536-1-benjamin.tissoires@redhat.com> References: <20221124151603.807536-1-benjamin.tissoires@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 --- 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/selftest= s/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 =3D PTHREAD_COND_IN= ITIALIZER; /* no need to protect uhid_stopped, only one thread accesses it */ static bool uhid_stopped; =20 +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 str= uct uhid_event *ev) { ssize_t ret; @@ -131,7 +140,8 @@ static int uhid_write(struct __test_metadata *_metadata= , int fd, const struct uh } } =20 -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 *_metad= ata, int fd, int rand_nb) memset(&ev, 0, sizeof(ev)); ev.type =3D UHID_CREATE; strcpy((char *)ev.u.create.name, buf); - ev.u.create.rd_data =3D rdesc; - ev.u.create.rd_size =3D sizeof(rdesc); - ev.u.create.bus =3D BUS_USB; - ev.u.create.vendor =3D 0x0001; - ev.u.create.product =3D 0x0a37; - ev.u.create.version =3D 0; + ev.u.create.rd_data =3D variant->rdesc; + ev.u.create.rd_size =3D variant->rdesc_sz; + ev.u.create.bus =3D variant->bus; + ev.u.create.vendor =3D variant->vendor; + ev.u.create.product =3D variant->product; + ev.u.create.version =3D variant->version; ev.u.create.country =3D 0; =20 sprintf(buf, "%d", rand_nb); @@ -299,7 +309,8 @@ static int uhid_send_event(struct __test_metadata *_met= adata, int fd, __u8 *buf, return uhid_write(_metadata, fd, &ev); } =20 -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 =3D "/dev/uhid"; @@ -308,7 +319,7 @@ static int setup_uhid(struct __test_metadata *_metadata= , int rand_nb) fd =3D open(path, O_RDWR | O_CLOEXEC); ASSERT_GE(fd, 0) TH_LOG("open uhid-cdev failed; %d", fd); =20 - ret =3D uhid_create(_metadata, fd, rand_nb); + ret =3D 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 *_metada= ta, int rand_nb) return fd; } =20 -static bool match_sysfs_device(int dev_id, const char *workdir, struct dir= ent *dir) +static bool match_sysfs_device(int dev_id, const struct hid_device_id *var= iant, + const char *workdir, struct dirent *dir) { - const char *target =3D "0003:0001:0A37.*"; + char target[17] =3D "0003:0001:0A37.*"; char phys[512]; char uevent[1024]; char temp[512]; int fd, nread; bool found =3D false; =20 + snprintf(target, sizeof(target), "0003:%04X:%04X.*", + variant->vendor, variant->product); + if (fnmatch(target, dir->d_name, 0)) return false; =20 @@ -347,7 +362,7 @@ static bool match_sysfs_device(int dev_id, const char *= workdir, struct dirent *d return found; } =20 -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 =3D "/sys/devices/virtual/misc/uhid"; const char *str_id; @@ -362,7 +377,7 @@ static int get_hid_id(int dev_id) d =3D opendir(workdir); if (d) { while ((dir =3D readdir(d)) !=3D NULL) { - if (!match_sysfs_device(dev_id, workdir, dir)) + if (!match_sysfs_device(dev_id, variant, workdir, dir)) continue; =20 str_id =3D dir->d_name + sizeof("0003:0001:0A37."); @@ -379,7 +394,7 @@ static int get_hid_id(int dev_id) return found; } =20 -static int get_hidraw(int dev_id) +static int get_hidraw(int dev_id, const struct hid_device_id *variant) { const char *workdir =3D "/sys/devices/virtual/misc/uhid"; char sysfs[1024]; @@ -396,7 +411,7 @@ static int get_hidraw(int dev_id) continue; =20 while ((dir =3D readdir(d)) !=3D NULL) { - if (!match_sysfs_device(dev_id, workdir, dir)) + if (!match_sysfs_device(dev_id, variant, workdir, dir)) continue; =20 sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name); @@ -423,12 +438,12 @@ static int get_hidraw(int dev_id) return found; } =20 -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] =3D { 0 }; =20 - hidraw_number =3D get_hidraw(dev_id); + hidraw_number =3D get_hidraw(dev_id, variant); if (hidraw_number < 0) return hidraw_number; =20 @@ -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 =3D { + .bus =3D BUS_USB, + .vendor =3D 0x0001, + .product =3D 0x0a37, + .version =3D 0, + .rdesc =3D rdesc, + .rdesc_sz =3D sizeof(rdesc), + }, +}; + static void detach_bpf(FIXTURE_DATA(hid_bpf) * self) { if (self->hidraw_fd) @@ -478,10 +509,10 @@ FIXTURE_SETUP(hid_bpf) =20 self->dev_id =3D rand() % 1024; =20 - self->uhid_fd =3D setup_uhid(_metadata, self->dev_id); + self->uhid_fd =3D setup_uhid(_metadata, &variant->id, self->dev_id); =20 - /* locate the uev, self, variant);ent file of the created device */ - self->hid_id =3D get_hid_id(self->dev_id); + /* locate the file of the created device */ + self->hid_id =3D 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); =20 @@ -546,7 +577,7 @@ static void load_programs(const struct test_program pro= grams[], ASSERT_OK(args.retval) TH_LOG("attach_hid(%s): %d", programs[i].name, ar= gs.retval); } =20 - self->hidraw_fd =3D open_hidraw(self->dev_id); + self->hidraw_fd =3D open_hidraw(self->dev_id, &variant->id); ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); } =20 @@ -644,7 +675,7 @@ TEST_F(hid_bpf, test_attach_detach) /* detach the program */ detach_bpf(self); =20 - self->hidraw_fd =3D open_hidraw(self->dev_id); + self->hidraw_fd =3D open_hidraw(self->dev_id, &variant->id); ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); =20 /* inject another event */ --=20 2.38.1