diff mbox series

[RFC,hid,v1,08/10] selftests: hid: add XK-24 tests

Message ID 20221124151603.807536-9-benjamin.tissoires@redhat.com
State New
Headers show
Series HID-BPF: add support for in-tree BPF programs | expand

Commit Message

Benjamin Tissoires Nov. 24, 2022, 3:16 p.m. UTC
this is the first device to be added in the kernel with a bpf program
associated, so we better use it to ensure we get the things right.

We define another fixture that will reuse everything from hid_bpf except
for the variant parameters. And then we can use that easily in a new
dedicated test.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
---
 tools/testing/selftests/hid/hid_bpf.c | 72 +++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c
index 738ddb2c6a62..4bdd1cfa7d13 100644
--- a/tools/testing/selftests/hid/hid_bpf.c
+++ b/tools/testing/selftests/hid/hid_bpf.c
@@ -520,6 +520,51 @@  FIXTURE_SETUP(hid_bpf)
 	ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err);
 }
 
+static unsigned char xk24_rdesc[] = {
+	0x05, 0x0c,                    // Usage Page (Consumer Devices)       0
+	0x09, 0x01,                    // Usage (Consumer Control)            2
+	0xa1, 0x01,                    // Collection (Application)            4
+	0xa1, 0x02,                    //  Collection (Logical)               6
+	0x05, 0x08,                    //   Usage Page (LEDs)                 8
+	0x09, 0x4b,                    //   Usage (Generic Indicator)         10
+	0x75, 0x08,                    //   Report Size (8)                   12
+	0x95, 0x23,                    //   Report Count (35)                 14
+	0x91, 0x02,                    //   Output (Data,Var,Abs)             16
+	0xc0,                          //  End Collection                     18
+	0xa1, 0x02,                    //  Collection (Logical)               19
+	0x05, 0x09,                    //   Usage Page (Button)               21
+	0x09, 0x4b,                    //   Usage (Vendor Usage 0x4b)         23
+	0x75, 0x08,                    //   Report Size (8)                   25
+	0x95, 0x20,                    //   Report Count (32)                 27
+	0x81, 0x02,                    //   Input (Data,Var,Abs)              29
+	0xc0,                          //  End Collection                     31
+	0xc0,                          // End Collection                      32
+};
+
+#define _test_data_hid_bpf_xkeys _test_data_hid_bpf
+#define _fixture_variant_hid_bpf_xkeys _fixture_variant_hid_bpf
+FIXTURE(hid_bpf_xkeys);
+FIXTURE_VARIANT(hid_bpf_xkeys);
+FIXTURE_VARIANT_ADD(hid_bpf_xkeys, xk24) {
+	.id = {
+		.bus = BUS_USB,
+		.vendor = 0x05F3,
+		.product = 0x0405,
+		.version = 0,
+		.rdesc = xk24_rdesc,
+		.rdesc_sz = sizeof(xk24_rdesc),
+	},
+};
+
+FIXTURE_SETUP(hid_bpf_xkeys)
+{
+	hid_bpf_setup(_metadata, self, variant);
+}
+FIXTURE_TEARDOWN(hid_bpf_xkeys)
+{
+	hid_bpf_teardown(_metadata, self, variant);
+}
+
 struct test_program {
 	const char *name;
 	int insert_head;
@@ -846,6 +891,33 @@  TEST_F(hid_bpf, test_rdesc_fixup)
 	ASSERT_EQ(rpt_desc.value[4], 0x42);
 }
 
+/*
+ * Attach an emulated XK24, which has an in-tree eBPF program, and ensure
+ * we got it loaded.
+ */
+TEST_F(hid_bpf_xkeys, test_xk24)
+{
+	struct hidraw_report_descriptor rpt_desc = {0};
+	int err, desc_size;
+
+	/* open the kernel provided hidraw node */
+	self->hidraw_fd = open_hidraw(self->dev_id, &variant->id);
+	ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
+
+	/* read the exposed report descriptor from hidraw */
+	err = ioctl(self->hidraw_fd, HIDIOCGRDESCSIZE, &desc_size);
+	ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESCSIZE: %d", err);
+
+	/* ensure the new size of the rdesc is bigger than the old one */
+	ASSERT_GT(desc_size, sizeof(xk24_rdesc));
+
+	rpt_desc.size = desc_size;
+	err = ioctl(self->hidraw_fd, HIDIOCGRDESC, &rpt_desc);
+	ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESC: %d", err);
+
+	ASSERT_EQ(rpt_desc.value[21], 0x09);
+}
+
 static int libbpf_print_fn(enum libbpf_print_level level,
 			   const char *format, va_list args)
 {