Skip to content

Commit 4fb41df

Browse files
author
Benjamin Tissoires
committed
selftests/hid: cleanup C tests by adding a common struct uhid_device
Allows to have an abstract class uhid_device which handles all of the uhid part without having to mess up with individual fds. struct attach_prog_args is now never used in hid_bpf.c, so drop it as well Acked-by: Shuah Khan <skhan@linuxfoundation.org> Link: https://patch.msgid.link/20241001-hid-bpf-hid-generic-v3-6-2ef1019468df@kernel.org Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
1 parent 0b838d7 commit 4fb41df

3 files changed

Lines changed: 87 additions & 100 deletions

File tree

tools/testing/selftests/hid/hid_bpf.c

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44
#include "hid_common.h"
55
#include <bpf/bpf.h>
66

7-
struct attach_prog_args {
8-
int prog_fd;
9-
unsigned int hid;
10-
int retval;
11-
int insert_head;
12-
};
13-
147
struct hid_hw_request_syscall_args {
158
__u8 data[10];
169
unsigned int hid;
@@ -21,11 +14,8 @@ struct hid_hw_request_syscall_args {
2114
};
2215

2316
FIXTURE(hid_bpf) {
24-
int dev_id;
25-
int uhid_fd;
17+
struct uhid_device hid;
2618
int hidraw_fd;
27-
int hid_id;
28-
pthread_t tid;
2919
struct hid *skel;
3020
struct bpf_link *hid_links[3]; /* max number of programs loaded in a single test */
3121
};
@@ -54,10 +44,10 @@ static void detach_bpf(FIXTURE_DATA(hid_bpf) * self)
5444
FIXTURE_TEARDOWN(hid_bpf) {
5545
void *uhid_err;
5646

57-
uhid_destroy(_metadata, self->uhid_fd);
47+
uhid_destroy(_metadata, &self->hid);
5848

5949
detach_bpf(self);
60-
pthread_join(self->tid, &uhid_err);
50+
pthread_join(self->hid.tid, &uhid_err);
6151
}
6252
#define TEARDOWN_LOG(fmt, ...) do { \
6353
TH_LOG(fmt, ##__VA_ARGS__); \
@@ -66,23 +56,10 @@ FIXTURE_TEARDOWN(hid_bpf) {
6656

6757
FIXTURE_SETUP(hid_bpf)
6858
{
69-
time_t t;
7059
int err;
7160

72-
/* initialize random number generator */
73-
srand((unsigned int)time(&t));
74-
75-
self->dev_id = rand() % 1024;
76-
77-
self->uhid_fd = setup_uhid(_metadata, self->dev_id);
78-
79-
/* locate the uev, self, variant);ent file of the created device */
80-
self->hid_id = get_hid_id(self->dev_id);
81-
ASSERT_GT(self->hid_id, 0)
82-
TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id);
83-
84-
err = uhid_start_listener(_metadata, &self->tid, self->uhid_fd);
85-
ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err);
61+
err = setup_uhid(_metadata, &self->hid);
62+
ASSERT_OK(err);
8663
}
8764

8865
struct test_program {
@@ -129,7 +106,7 @@ static void load_programs(const struct test_program programs[],
129106
ops_hid_id = bpf_map__initial_value(map, NULL);
130107
ASSERT_OK_PTR(ops_hid_id) TH_LOG("unable to retrieve struct_ops data");
131108

132-
*ops_hid_id = self->hid_id;
109+
*ops_hid_id = self->hid.hid_id;
133110
}
134111

135112
/* we disable the auto-attach feature of all maps because we
@@ -157,7 +134,7 @@ static void load_programs(const struct test_program programs[],
157134

158135
hid__attach(self->skel);
159136

160-
self->hidraw_fd = open_hidraw(self->dev_id);
137+
self->hidraw_fd = open_hidraw(&self->hid);
161138
ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
162139
}
163140

@@ -192,7 +169,7 @@ TEST_F(hid_bpf, raw_event)
192169
/* inject one event */
193170
buf[0] = 1;
194171
buf[1] = 42;
195-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
172+
uhid_send_event(_metadata, &self->hid, buf, 6);
196173

197174
/* check that hid_first_event() was executed */
198175
ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1");
@@ -208,7 +185,7 @@ TEST_F(hid_bpf, raw_event)
208185
memset(buf, 0, sizeof(buf));
209186
buf[0] = 1;
210187
buf[1] = 47;
211-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
188+
uhid_send_event(_metadata, &self->hid, buf, 6);
212189

213190
/* check that hid_first_event() was executed */
214191
ASSERT_EQ(self->skel->data->callback_check, 47) TH_LOG("callback_check1");
@@ -239,7 +216,7 @@ TEST_F(hid_bpf, subprog_raw_event)
239216
/* inject one event */
240217
buf[0] = 1;
241218
buf[1] = 42;
242-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
219+
uhid_send_event(_metadata, &self->hid, buf, 6);
243220

244221
/* read the data from hidraw */
245222
memset(buf, 0, sizeof(buf));
@@ -252,7 +229,7 @@ TEST_F(hid_bpf, subprog_raw_event)
252229
memset(buf, 0, sizeof(buf));
253230
buf[0] = 1;
254231
buf[1] = 47;
255-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
232+
uhid_send_event(_metadata, &self->hid, buf, 6);
256233

257234
/* read the data from hidraw */
258235
memset(buf, 0, sizeof(buf));
@@ -303,7 +280,7 @@ TEST_F(hid_bpf, test_attach_detach)
303280
/* inject one event */
304281
buf[0] = 1;
305282
buf[1] = 42;
306-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
283+
uhid_send_event(_metadata, &self->hid, buf, 6);
307284

308285
/* read the data from hidraw */
309286
memset(buf, 0, sizeof(buf));
@@ -326,14 +303,14 @@ TEST_F(hid_bpf, test_attach_detach)
326303
/* detach the program */
327304
detach_bpf(self);
328305

329-
self->hidraw_fd = open_hidraw(self->dev_id);
306+
self->hidraw_fd = open_hidraw(&self->hid);
330307
ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
331308

332309
/* inject another event */
333310
memset(buf, 0, sizeof(buf));
334311
buf[0] = 1;
335312
buf[1] = 47;
336-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
313+
uhid_send_event(_metadata, &self->hid, buf, 6);
337314

338315
/* read the data from hidraw */
339316
memset(buf, 0, sizeof(buf));
@@ -352,7 +329,7 @@ TEST_F(hid_bpf, test_attach_detach)
352329
memset(buf, 0, sizeof(buf));
353330
buf[0] = 1;
354331
buf[1] = 42;
355-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
332+
uhid_send_event(_metadata, &self->hid, buf, 6);
356333

357334
/* read the data from hidraw */
358335
memset(buf, 0, sizeof(buf));
@@ -382,7 +359,7 @@ TEST_F(hid_bpf, test_hid_change_report)
382359
/* inject one event */
383360
buf[0] = 1;
384361
buf[1] = 42;
385-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
362+
uhid_send_event(_metadata, &self->hid, buf, 6);
386363

387364
/* read the data from hidraw */
388365
memset(buf, 0, sizeof(buf));
@@ -412,7 +389,7 @@ TEST_F(hid_bpf, test_hid_user_input_report_call)
412389

413390
LOAD_BPF;
414391

415-
args.hid = self->hid_id;
392+
args.hid = self->hid.hid_id;
416393
args.data[0] = 1; /* report ID */
417394
args.data[1] = 2; /* report ID */
418395
args.data[2] = 42; /* report ID */
@@ -458,7 +435,7 @@ TEST_F(hid_bpf, test_hid_user_output_report_call)
458435

459436
LOAD_BPF;
460437

461-
args.hid = self->hid_id;
438+
args.hid = self->hid.hid_id;
462439
args.data[0] = 1; /* report ID */
463440
args.data[1] = 2; /* report ID */
464441
args.data[2] = 42; /* report ID */
@@ -506,7 +483,7 @@ TEST_F(hid_bpf, test_hid_user_raw_request_call)
506483

507484
LOAD_BPF;
508485

509-
args.hid = self->hid_id;
486+
args.hid = self->hid.hid_id;
510487
args.data[0] = 1; /* report ID */
511488

512489
prog_fd = bpf_program__fd(self->skel->progs.hid_user_raw_request);
@@ -539,7 +516,7 @@ TEST_F(hid_bpf, test_hid_filter_raw_request_call)
539516
/* inject one event */
540517
buf[0] = 1;
541518
buf[1] = 42;
542-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
519+
uhid_send_event(_metadata, &self->hid, buf, 6);
543520

544521
/* read the data from hidraw */
545522
memset(buf, 0, sizeof(buf));
@@ -565,7 +542,7 @@ TEST_F(hid_bpf, test_hid_filter_raw_request_call)
565542
/* detach the program */
566543
detach_bpf(self);
567544

568-
self->hidraw_fd = open_hidraw(self->dev_id);
545+
self->hidraw_fd = open_hidraw(&self->hid);
569546
ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
570547

571548
err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
@@ -641,7 +618,7 @@ TEST_F(hid_bpf, test_hid_filter_output_report_call)
641618
/* inject one event */
642619
buf[0] = 1;
643620
buf[1] = 42;
644-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
621+
uhid_send_event(_metadata, &self->hid, buf, 6);
645622

646623
/* read the data from hidraw */
647624
memset(buf, 0, sizeof(buf));
@@ -667,7 +644,7 @@ TEST_F(hid_bpf, test_hid_filter_output_report_call)
667644
/* detach the program */
668645
detach_bpf(self);
669646

670-
self->hidraw_fd = open_hidraw(self->dev_id);
647+
self->hidraw_fd = open_hidraw(&self->hid);
671648
ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
672649

673650
err = write(self->hidraw_fd, buf, 3);
@@ -742,7 +719,7 @@ TEST_F(hid_bpf, test_multiply_events_wq)
742719
/* inject one event */
743720
buf[0] = 1;
744721
buf[1] = 42;
745-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
722+
uhid_send_event(_metadata, &self->hid, buf, 6);
746723

747724
/* read the data from hidraw */
748725
memset(buf, 0, sizeof(buf));
@@ -780,7 +757,7 @@ TEST_F(hid_bpf, test_multiply_events)
780757
/* inject one event */
781758
buf[0] = 1;
782759
buf[1] = 42;
783-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
760+
uhid_send_event(_metadata, &self->hid, buf, 6);
784761

785762
/* read the data from hidraw */
786763
memset(buf, 0, sizeof(buf));
@@ -816,7 +793,7 @@ TEST_F(hid_bpf, test_hid_infinite_loop_input_report_call)
816793
buf[1] = 2;
817794
buf[2] = 42;
818795

819-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
796+
uhid_send_event(_metadata, &self->hid, buf, 6);
820797

821798
/* read the data from hidraw */
822799
memset(buf, 0, sizeof(buf));
@@ -867,7 +844,7 @@ TEST_F(hid_bpf, test_hid_attach_flags)
867844

868845
/* inject one event */
869846
buf[0] = 1;
870-
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
847+
uhid_send_event(_metadata, &self->hid, buf, 6);
871848

872849
/* read the data from hidraw */
873850
memset(buf, 0, sizeof(buf));

tools/testing/selftests/hid/hid_common.h

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
__typeof__(b) _b = (b); \
2020
_a < _b ? _a : _b; })
2121

22+
struct uhid_device {
23+
int dev_id; /* uniq (random) number to identify the device */
24+
int uhid_fd;
25+
int hid_id; /* HID device id in the system */
26+
pthread_t tid; /* thread for reading uhid events */
27+
};
28+
2229
static unsigned char rdesc[] = {
2330
0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
2431
0x09, 0x21, /* Usage (Vendor Usage 0x21) */
@@ -146,14 +153,14 @@ static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb)
146153
return uhid_write(_metadata, fd, &ev);
147154
}
148155

149-
static void uhid_destroy(struct __test_metadata *_metadata, int fd)
156+
static void uhid_destroy(struct __test_metadata *_metadata, struct uhid_device *hid)
150157
{
151158
struct uhid_event ev;
152159

153160
memset(&ev, 0, sizeof(ev));
154161
ev.type = UHID_DESTROY;
155162

156-
uhid_write(_metadata, fd, &ev);
163+
uhid_write(_metadata, hid->uhid_fd, &ev);
157164
}
158165

159166
static int uhid_event(struct __test_metadata *_metadata, int fd)
@@ -281,7 +288,8 @@ static int uhid_start_listener(struct __test_metadata *_metadata, pthread_t *tid
281288
return 0;
282289
}
283290

284-
static int uhid_send_event(struct __test_metadata *_metadata, int fd, __u8 *buf, size_t size)
291+
static int uhid_send_event(struct __test_metadata *_metadata, struct uhid_device *hid,
292+
__u8 *buf, size_t size)
285293
{
286294
struct uhid_event ev;
287295

@@ -294,25 +302,7 @@ static int uhid_send_event(struct __test_metadata *_metadata, int fd, __u8 *buf,
294302

295303
memcpy(ev.u.input2.data, buf, size);
296304

297-
return uhid_write(_metadata, fd, &ev);
298-
}
299-
300-
static int setup_uhid(struct __test_metadata *_metadata, int rand_nb)
301-
{
302-
int fd;
303-
const char *path = "/dev/uhid";
304-
int ret;
305-
306-
fd = open(path, O_RDWR | O_CLOEXEC);
307-
ASSERT_GE(fd, 0) TH_LOG("open uhid-cdev failed; %d", fd);
308-
309-
ret = uhid_create(_metadata, fd, rand_nb);
310-
ASSERT_EQ(0, ret) {
311-
TH_LOG("create uhid device failed: %d", ret);
312-
close(fd);
313-
}
314-
315-
return fd;
305+
return uhid_write(_metadata, hid->uhid_fd, &ev);
316306
}
317307

318308
static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir)
@@ -421,16 +411,52 @@ static int get_hidraw(int dev_id)
421411
return found;
422412
}
423413

424-
static int open_hidraw(int dev_id)
414+
static int open_hidraw(struct uhid_device *hid)
425415
{
426416
int hidraw_number;
427417
char hidraw_path[64] = { 0 };
428418

429-
hidraw_number = get_hidraw(dev_id);
419+
hidraw_number = get_hidraw(hid->dev_id);
430420
if (hidraw_number < 0)
431421
return hidraw_number;
432422

433423
/* open hidraw node to check the other side of the pipe */
434424
sprintf(hidraw_path, "/dev/hidraw%d", hidraw_number);
435425
return open(hidraw_path, O_RDWR | O_NONBLOCK);
436426
}
427+
428+
static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid)
429+
{
430+
const char *path = "/dev/uhid";
431+
time_t t;
432+
int ret;
433+
434+
/* initialize random number generator */
435+
srand((unsigned int)time(&t));
436+
437+
hid->dev_id = rand() % 1024;
438+
439+
hid->uhid_fd = open(path, O_RDWR | O_CLOEXEC);
440+
ASSERT_GE(hid->uhid_fd, 0) TH_LOG("open uhid-cdev failed; %d", hid->uhid_fd);
441+
442+
ret = uhid_create(_metadata, hid->uhid_fd, hid->dev_id);
443+
ASSERT_EQ(0, ret) {
444+
TH_LOG("create uhid device failed: %d", ret);
445+
close(hid->uhid_fd);
446+
return ret;
447+
}
448+
449+
/* locate the uevent file of the created device */
450+
hid->hid_id = get_hid_id(hid->dev_id);
451+
ASSERT_GT(hid->hid_id, 0)
452+
TH_LOG("Could not locate uhid device id: %d", hid->hid_id);
453+
454+
ret = uhid_start_listener(_metadata, &hid->tid, hid->uhid_fd);
455+
ASSERT_EQ(0, ret) {
456+
TH_LOG("could not start udev listener: %d", ret);
457+
close(hid->uhid_fd);
458+
return ret;
459+
}
460+
461+
return 0;
462+
}

0 commit comments

Comments
 (0)