xref: /linux/tools/testing/selftests/hid/hid_bpf.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Red Hat */
3 #include "hid.skel.h"
4 
5 #include "../kselftest_harness.h"
6 
7 #include <bpf/bpf.h>
8 #include <fcntl.h>
9 #include <fnmatch.h>
10 #include <dirent.h>
11 #include <poll.h>
12 #include <pthread.h>
13 #include <stdbool.h>
14 #include <linux/hidraw.h>
15 #include <linux/uhid.h>
16 
17 #define SHOW_UHID_DEBUG 0
18 
19 #define min(a, b) \
20 	({ __typeof__(a) _a = (a); \
21 	__typeof__(b) _b = (b); \
22 	_a < _b ? _a : _b; })
23 
24 static unsigned char rdesc[] = {
25 	0x06, 0x00, 0xff,	/* Usage Page (Vendor Defined Page 1) */
26 	0x09, 0x21,		/* Usage (Vendor Usage 0x21) */
27 	0xa1, 0x01,		/* COLLECTION (Application) */
28 	0x09, 0x01,			/* Usage (Vendor Usage 0x01) */
29 	0xa1, 0x00,			/* COLLECTION (Physical) */
30 	0x85, 0x02,				/* REPORT_ID (2) */
31 	0x19, 0x01,				/* USAGE_MINIMUM (1) */
32 	0x29, 0x08,				/* USAGE_MAXIMUM (3) */
33 	0x15, 0x00,				/* LOGICAL_MINIMUM (0) */
34 	0x25, 0xff,				/* LOGICAL_MAXIMUM (255) */
35 	0x95, 0x08,				/* REPORT_COUNT (8) */
36 	0x75, 0x08,				/* REPORT_SIZE (8) */
37 	0x81, 0x02,				/* INPUT (Data,Var,Abs) */
38 	0xc0,				/* END_COLLECTION */
39 	0x09, 0x01,			/* Usage (Vendor Usage 0x01) */
40 	0xa1, 0x00,			/* COLLECTION (Physical) */
41 	0x85, 0x01,				/* REPORT_ID (1) */
42 	0x06, 0x00, 0xff,			/* Usage Page (Vendor Defined Page 1) */
43 	0x19, 0x01,				/* USAGE_MINIMUM (1) */
44 	0x29, 0x03,				/* USAGE_MAXIMUM (3) */
45 	0x15, 0x00,				/* LOGICAL_MINIMUM (0) */
46 	0x25, 0x01,				/* LOGICAL_MAXIMUM (1) */
47 	0x95, 0x03,				/* REPORT_COUNT (3) */
48 	0x75, 0x01,				/* REPORT_SIZE (1) */
49 	0x81, 0x02,				/* INPUT (Data,Var,Abs) */
50 	0x95, 0x01,				/* REPORT_COUNT (1) */
51 	0x75, 0x05,				/* REPORT_SIZE (5) */
52 	0x81, 0x01,				/* INPUT (Cnst,Var,Abs) */
53 	0x05, 0x01,				/* USAGE_PAGE (Generic Desktop) */
54 	0x09, 0x30,				/* USAGE (X) */
55 	0x09, 0x31,				/* USAGE (Y) */
56 	0x15, 0x81,				/* LOGICAL_MINIMUM (-127) */
57 	0x25, 0x7f,				/* LOGICAL_MAXIMUM (127) */
58 	0x75, 0x10,				/* REPORT_SIZE (16) */
59 	0x95, 0x02,				/* REPORT_COUNT (2) */
60 	0x81, 0x06,				/* INPUT (Data,Var,Rel) */
61 
62 	0x06, 0x00, 0xff,			/* Usage Page (Vendor Defined Page 1) */
63 	0x19, 0x01,				/* USAGE_MINIMUM (1) */
64 	0x29, 0x03,				/* USAGE_MAXIMUM (3) */
65 	0x15, 0x00,				/* LOGICAL_MINIMUM (0) */
66 	0x25, 0x01,				/* LOGICAL_MAXIMUM (1) */
67 	0x95, 0x03,				/* REPORT_COUNT (3) */
68 	0x75, 0x01,				/* REPORT_SIZE (1) */
69 	0x91, 0x02,				/* Output (Data,Var,Abs) */
70 	0x95, 0x01,				/* REPORT_COUNT (1) */
71 	0x75, 0x05,				/* REPORT_SIZE (5) */
72 	0x91, 0x01,				/* Output (Cnst,Var,Abs) */
73 
74 	0x06, 0x00, 0xff,			/* Usage Page (Vendor Defined Page 1) */
75 	0x19, 0x06,				/* USAGE_MINIMUM (6) */
76 	0x29, 0x08,				/* USAGE_MAXIMUM (8) */
77 	0x15, 0x00,				/* LOGICAL_MINIMUM (0) */
78 	0x25, 0x01,				/* LOGICAL_MAXIMUM (1) */
79 	0x95, 0x03,				/* REPORT_COUNT (3) */
80 	0x75, 0x01,				/* REPORT_SIZE (1) */
81 	0xb1, 0x02,				/* Feature (Data,Var,Abs) */
82 	0x95, 0x01,				/* REPORT_COUNT (1) */
83 	0x75, 0x05,				/* REPORT_SIZE (5) */
84 	0x91, 0x01,				/* Output (Cnst,Var,Abs) */
85 
86 	0xc0,				/* END_COLLECTION */
87 	0xc0,			/* END_COLLECTION */
88 };
89 
90 static __u8 feature_data[] = { 1, 2 };
91 
92 struct attach_prog_args {
93 	int prog_fd;
94 	unsigned int hid;
95 	int retval;
96 	int insert_head;
97 };
98 
99 struct hid_hw_request_syscall_args {
100 	__u8 data[10];
101 	unsigned int hid;
102 	int retval;
103 	size_t size;
104 	enum hid_report_type type;
105 	__u8 request_type;
106 };
107 
108 #define ASSERT_OK(data) ASSERT_FALSE(data)
109 #define ASSERT_OK_PTR(ptr) ASSERT_NE(NULL, ptr)
110 
111 #define UHID_LOG(fmt, ...) do { \
112 	if (SHOW_UHID_DEBUG) \
113 		TH_LOG(fmt, ##__VA_ARGS__); \
114 } while (0)
115 
116 static pthread_mutex_t uhid_started_mtx = PTHREAD_MUTEX_INITIALIZER;
117 static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER;
118 
119 static pthread_mutex_t uhid_output_mtx = PTHREAD_MUTEX_INITIALIZER;
120 static pthread_cond_t uhid_output_cond = PTHREAD_COND_INITIALIZER;
121 static unsigned char output_report[10];
122 
123 /* no need to protect uhid_stopped, only one thread accesses it */
124 static bool uhid_stopped;
125 
126 static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uhid_event *ev)
127 {
128 	ssize_t ret;
129 
130 	ret = write(fd, ev, sizeof(*ev));
131 	if (ret < 0) {
132 		TH_LOG("Cannot write to uhid: %m");
133 		return -errno;
134 	} else if (ret != sizeof(*ev)) {
135 		TH_LOG("Wrong size written to uhid: %zd != %zu",
136 			ret, sizeof(ev));
137 		return -EFAULT;
138 	} else {
139 		return 0;
140 	}
141 }
142 
143 static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb)
144 {
145 	struct uhid_event ev;
146 	char buf[25];
147 
148 	sprintf(buf, "test-uhid-device-%d", rand_nb);
149 
150 	memset(&ev, 0, sizeof(ev));
151 	ev.type = UHID_CREATE;
152 	strcpy((char *)ev.u.create.name, buf);
153 	ev.u.create.rd_data = rdesc;
154 	ev.u.create.rd_size = sizeof(rdesc);
155 	ev.u.create.bus = BUS_USB;
156 	ev.u.create.vendor = 0x0001;
157 	ev.u.create.product = 0x0a37;
158 	ev.u.create.version = 0;
159 	ev.u.create.country = 0;
160 
161 	sprintf(buf, "%d", rand_nb);
162 	strcpy((char *)ev.u.create.phys, buf);
163 
164 	return uhid_write(_metadata, fd, &ev);
165 }
166 
167 static void uhid_destroy(struct __test_metadata *_metadata, int fd)
168 {
169 	struct uhid_event ev;
170 
171 	memset(&ev, 0, sizeof(ev));
172 	ev.type = UHID_DESTROY;
173 
174 	uhid_write(_metadata, fd, &ev);
175 }
176 
177 static int uhid_event(struct __test_metadata *_metadata, int fd)
178 {
179 	struct uhid_event ev, answer;
180 	ssize_t ret;
181 
182 	memset(&ev, 0, sizeof(ev));
183 	ret = read(fd, &ev, sizeof(ev));
184 	if (ret == 0) {
185 		UHID_LOG("Read HUP on uhid-cdev");
186 		return -EFAULT;
187 	} else if (ret < 0) {
188 		UHID_LOG("Cannot read uhid-cdev: %m");
189 		return -errno;
190 	} else if (ret != sizeof(ev)) {
191 		UHID_LOG("Invalid size read from uhid-dev: %zd != %zu",
192 			ret, sizeof(ev));
193 		return -EFAULT;
194 	}
195 
196 	switch (ev.type) {
197 	case UHID_START:
198 		pthread_mutex_lock(&uhid_started_mtx);
199 		pthread_cond_signal(&uhid_started);
200 		pthread_mutex_unlock(&uhid_started_mtx);
201 
202 		UHID_LOG("UHID_START from uhid-dev");
203 		break;
204 	case UHID_STOP:
205 		uhid_stopped = true;
206 
207 		UHID_LOG("UHID_STOP from uhid-dev");
208 		break;
209 	case UHID_OPEN:
210 		UHID_LOG("UHID_OPEN from uhid-dev");
211 		break;
212 	case UHID_CLOSE:
213 		UHID_LOG("UHID_CLOSE from uhid-dev");
214 		break;
215 	case UHID_OUTPUT:
216 		UHID_LOG("UHID_OUTPUT from uhid-dev");
217 
218 		pthread_mutex_lock(&uhid_output_mtx);
219 		memcpy(output_report,
220 		       ev.u.output.data,
221 		       min(ev.u.output.size, sizeof(output_report)));
222 		pthread_cond_signal(&uhid_output_cond);
223 		pthread_mutex_unlock(&uhid_output_mtx);
224 		break;
225 	case UHID_GET_REPORT:
226 		UHID_LOG("UHID_GET_REPORT from uhid-dev");
227 
228 		answer.type = UHID_GET_REPORT_REPLY;
229 		answer.u.get_report_reply.id = ev.u.get_report.id;
230 		answer.u.get_report_reply.err = ev.u.get_report.rnum == 1 ? 0 : -EIO;
231 		answer.u.get_report_reply.size = sizeof(feature_data);
232 		memcpy(answer.u.get_report_reply.data, feature_data, sizeof(feature_data));
233 
234 		uhid_write(_metadata, fd, &answer);
235 
236 		break;
237 	case UHID_SET_REPORT:
238 		UHID_LOG("UHID_SET_REPORT from uhid-dev");
239 		break;
240 	default:
241 		TH_LOG("Invalid event from uhid-dev: %u", ev.type);
242 	}
243 
244 	return 0;
245 }
246 
247 struct uhid_thread_args {
248 	int fd;
249 	struct __test_metadata *_metadata;
250 };
251 static void *uhid_read_events_thread(void *arg)
252 {
253 	struct uhid_thread_args *args = (struct uhid_thread_args *)arg;
254 	struct __test_metadata *_metadata = args->_metadata;
255 	struct pollfd pfds[1];
256 	int fd = args->fd;
257 	int ret = 0;
258 
259 	pfds[0].fd = fd;
260 	pfds[0].events = POLLIN;
261 
262 	uhid_stopped = false;
263 
264 	while (!uhid_stopped) {
265 		ret = poll(pfds, 1, 100);
266 		if (ret < 0) {
267 			TH_LOG("Cannot poll for fds: %m");
268 			break;
269 		}
270 		if (pfds[0].revents & POLLIN) {
271 			ret = uhid_event(_metadata, fd);
272 			if (ret)
273 				break;
274 		}
275 	}
276 
277 	return (void *)(long)ret;
278 }
279 
280 static int uhid_start_listener(struct __test_metadata *_metadata, pthread_t *tid, int uhid_fd)
281 {
282 	struct uhid_thread_args args = {
283 		.fd = uhid_fd,
284 		._metadata = _metadata,
285 	};
286 	int err;
287 
288 	pthread_mutex_lock(&uhid_started_mtx);
289 	err = pthread_create(tid, NULL, uhid_read_events_thread, (void *)&args);
290 	ASSERT_EQ(0, err) {
291 		TH_LOG("Could not start the uhid thread: %d", err);
292 		pthread_mutex_unlock(&uhid_started_mtx);
293 		close(uhid_fd);
294 		return -EIO;
295 	}
296 	pthread_cond_wait(&uhid_started, &uhid_started_mtx);
297 	pthread_mutex_unlock(&uhid_started_mtx);
298 
299 	return 0;
300 }
301 
302 static int uhid_send_event(struct __test_metadata *_metadata, int fd, __u8 *buf, size_t size)
303 {
304 	struct uhid_event ev;
305 
306 	if (size > sizeof(ev.u.input.data))
307 		return -E2BIG;
308 
309 	memset(&ev, 0, sizeof(ev));
310 	ev.type = UHID_INPUT2;
311 	ev.u.input2.size = size;
312 
313 	memcpy(ev.u.input2.data, buf, size);
314 
315 	return uhid_write(_metadata, fd, &ev);
316 }
317 
318 static int setup_uhid(struct __test_metadata *_metadata, int rand_nb)
319 {
320 	int fd;
321 	const char *path = "/dev/uhid";
322 	int ret;
323 
324 	fd = open(path, O_RDWR | O_CLOEXEC);
325 	ASSERT_GE(fd, 0) TH_LOG("open uhid-cdev failed; %d", fd);
326 
327 	ret = uhid_create(_metadata, fd, rand_nb);
328 	ASSERT_EQ(0, ret) {
329 		TH_LOG("create uhid device failed: %d", ret);
330 		close(fd);
331 	}
332 
333 	return fd;
334 }
335 
336 static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir)
337 {
338 	const char *target = "0003:0001:0A37.*";
339 	char phys[512];
340 	char uevent[1024];
341 	char temp[512];
342 	int fd, nread;
343 	bool found = false;
344 
345 	if (fnmatch(target, dir->d_name, 0))
346 		return false;
347 
348 	/* we found the correct VID/PID, now check for phys */
349 	sprintf(uevent, "%s/%s/uevent", workdir, dir->d_name);
350 
351 	fd = open(uevent, O_RDONLY | O_NONBLOCK);
352 	if (fd < 0)
353 		return false;
354 
355 	sprintf(phys, "PHYS=%d", dev_id);
356 
357 	nread = read(fd, temp, ARRAY_SIZE(temp));
358 	if (nread > 0 && (strstr(temp, phys)) != NULL)
359 		found = true;
360 
361 	close(fd);
362 
363 	return found;
364 }
365 
366 static int get_hid_id(int dev_id)
367 {
368 	const char *workdir = "/sys/devices/virtual/misc/uhid";
369 	const char *str_id;
370 	DIR *d;
371 	struct dirent *dir;
372 	int found = -1, attempts = 3;
373 
374 	/* it would be nice to be able to use nftw, but the no_alu32 target doesn't support it */
375 
376 	while (found < 0 && attempts > 0) {
377 		attempts--;
378 		d = opendir(workdir);
379 		if (d) {
380 			while ((dir = readdir(d)) != NULL) {
381 				if (!match_sysfs_device(dev_id, workdir, dir))
382 					continue;
383 
384 				str_id = dir->d_name + sizeof("0003:0001:0A37.");
385 				found = (int)strtol(str_id, NULL, 16);
386 
387 				break;
388 			}
389 			closedir(d);
390 		}
391 		if (found < 0)
392 			usleep(100000);
393 	}
394 
395 	return found;
396 }
397 
398 static int get_hidraw(int dev_id)
399 {
400 	const char *workdir = "/sys/devices/virtual/misc/uhid";
401 	char sysfs[1024];
402 	DIR *d, *subd;
403 	struct dirent *dir, *subdir;
404 	int i, found = -1;
405 
406 	/* retry 5 times in case the system is loaded */
407 	for (i = 5; i > 0; i--) {
408 		usleep(10);
409 		d = opendir(workdir);
410 
411 		if (!d)
412 			continue;
413 
414 		while ((dir = readdir(d)) != NULL) {
415 			if (!match_sysfs_device(dev_id, workdir, dir))
416 				continue;
417 
418 			sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name);
419 
420 			subd = opendir(sysfs);
421 			if (!subd)
422 				continue;
423 
424 			while ((subdir = readdir(subd)) != NULL) {
425 				if (fnmatch("hidraw*", subdir->d_name, 0))
426 					continue;
427 
428 				found = atoi(subdir->d_name + strlen("hidraw"));
429 			}
430 
431 			closedir(subd);
432 
433 			if (found > 0)
434 				break;
435 		}
436 		closedir(d);
437 	}
438 
439 	return found;
440 }
441 
442 static int open_hidraw(int dev_id)
443 {
444 	int hidraw_number;
445 	char hidraw_path[64] = { 0 };
446 
447 	hidraw_number = get_hidraw(dev_id);
448 	if (hidraw_number < 0)
449 		return hidraw_number;
450 
451 	/* open hidraw node to check the other side of the pipe */
452 	sprintf(hidraw_path, "/dev/hidraw%d", hidraw_number);
453 	return open(hidraw_path, O_RDWR | O_NONBLOCK);
454 }
455 
456 FIXTURE(hid_bpf) {
457 	int dev_id;
458 	int uhid_fd;
459 	int hidraw_fd;
460 	int hid_id;
461 	pthread_t tid;
462 	struct hid *skel;
463 	struct bpf_link *hid_links[3]; /* max number of programs loaded in a single test */
464 };
465 static void detach_bpf(FIXTURE_DATA(hid_bpf) * self)
466 {
467 	int i;
468 
469 	if (self->hidraw_fd)
470 		close(self->hidraw_fd);
471 	self->hidraw_fd = 0;
472 
473 	if (!self->skel)
474 		return;
475 
476 	hid__detach(self->skel);
477 
478 	for (i = 0; i < ARRAY_SIZE(self->hid_links); i++) {
479 		if (self->hid_links[i])
480 			bpf_link__destroy(self->hid_links[i]);
481 	}
482 
483 	hid__destroy(self->skel);
484 	self->skel = NULL;
485 }
486 
487 FIXTURE_TEARDOWN(hid_bpf) {
488 	void *uhid_err;
489 
490 	uhid_destroy(_metadata, self->uhid_fd);
491 
492 	detach_bpf(self);
493 	pthread_join(self->tid, &uhid_err);
494 }
495 #define TEARDOWN_LOG(fmt, ...) do { \
496 	TH_LOG(fmt, ##__VA_ARGS__); \
497 	hid_bpf_teardown(_metadata, self, variant); \
498 } while (0)
499 
500 FIXTURE_SETUP(hid_bpf)
501 {
502 	time_t t;
503 	int err;
504 
505 	/* initialize random number generator */
506 	srand((unsigned int)time(&t));
507 
508 	self->dev_id = rand() % 1024;
509 
510 	self->uhid_fd = setup_uhid(_metadata, self->dev_id);
511 
512 	/* locate the uev, self, variant);ent file of the created device */
513 	self->hid_id = get_hid_id(self->dev_id);
514 	ASSERT_GT(self->hid_id, 0)
515 		TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id);
516 
517 	err = uhid_start_listener(_metadata, &self->tid, self->uhid_fd);
518 	ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err);
519 }
520 
521 struct test_program {
522 	const char *name;
523 	int insert_head;
524 };
525 #define LOAD_PROGRAMS(progs) \
526 	load_programs(progs, ARRAY_SIZE(progs), _metadata, self, variant)
527 #define LOAD_BPF \
528 	load_programs(NULL, 0, _metadata, self, variant)
529 static void load_programs(const struct test_program programs[],
530 			  const size_t progs_count,
531 			  struct __test_metadata *_metadata,
532 			  FIXTURE_DATA(hid_bpf) * self,
533 			  const FIXTURE_VARIANT(hid_bpf) * variant)
534 {
535 	struct bpf_map *iter_map;
536 	int err = -EINVAL;
537 
538 	ASSERT_LE(progs_count, ARRAY_SIZE(self->hid_links))
539 		TH_LOG("too many programs are to be loaded");
540 
541 	/* open the bpf file */
542 	self->skel = hid__open();
543 	ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open");
544 
545 	for (int i = 0; i < progs_count; i++) {
546 		struct bpf_program *prog;
547 		struct bpf_map *map;
548 		int *ops_hid_id;
549 
550 		prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj,
551 							programs[i].name);
552 		ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name);
553 
554 		bpf_program__set_autoload(prog, true);
555 
556 		map = bpf_object__find_map_by_name(*self->skel->skeleton->obj,
557 							  programs[i].name + 4);
558 		ASSERT_OK_PTR(map) TH_LOG("can not find struct_ops by name '%s'",
559 					  programs[i].name + 4);
560 
561 		/* hid_id is the first field of struct hid_bpf_ops */
562 		ops_hid_id = bpf_map__initial_value(map, NULL);
563 		ASSERT_OK_PTR(ops_hid_id) TH_LOG("unable to retrieve struct_ops data");
564 
565 		*ops_hid_id = self->hid_id;
566 	}
567 
568 	/* we disable the auto-attach feature of all maps because we
569 	 * only want the tested one to be manually attached in the next
570 	 * call to bpf_map__attach_struct_ops()
571 	 */
572 	bpf_object__for_each_map(iter_map, *self->skel->skeleton->obj)
573 		bpf_map__set_autoattach(iter_map, false);
574 
575 	err = hid__load(self->skel);
576 	ASSERT_OK(err) TH_LOG("hid_skel_load failed: %d", err);
577 
578 	for (int i = 0; i < progs_count; i++) {
579 		struct bpf_map *map;
580 
581 		map = bpf_object__find_map_by_name(*self->skel->skeleton->obj,
582 							  programs[i].name + 4);
583 		ASSERT_OK_PTR(map) TH_LOG("can not find struct_ops by name '%s'",
584 					  programs[i].name + 4);
585 
586 		self->hid_links[i] = bpf_map__attach_struct_ops(map);
587 		ASSERT_OK_PTR(self->hid_links[i]) TH_LOG("failed to attach struct ops '%s'",
588 							 programs[i].name + 4);
589 	}
590 
591 	hid__attach(self->skel);
592 
593 	self->hidraw_fd = open_hidraw(self->dev_id);
594 	ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
595 }
596 
597 /*
598  * A simple test to see if the fixture is working fine.
599  * If this fails, none of the other tests will pass.
600  */
601 TEST_F(hid_bpf, test_create_uhid)
602 {
603 }
604 
605 /*
606  * Attach hid_first_event to the given uhid device,
607  * retrieve and open the matching hidraw node,
608  * inject one event in the uhid device,
609  * check that the program sees it and can change the data
610  */
611 TEST_F(hid_bpf, raw_event)
612 {
613 	const struct test_program progs[] = {
614 		{ .name = "hid_first_event" },
615 	};
616 	__u8 buf[10] = {0};
617 	int err;
618 
619 	LOAD_PROGRAMS(progs);
620 
621 	/* check that the program is correctly loaded */
622 	ASSERT_EQ(self->skel->data->callback_check, 52) TH_LOG("callback_check1");
623 	ASSERT_EQ(self->skel->data->callback2_check, 52) TH_LOG("callback2_check1");
624 
625 	/* inject one event */
626 	buf[0] = 1;
627 	buf[1] = 42;
628 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
629 
630 	/* check that hid_first_event() was executed */
631 	ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1");
632 
633 	/* read the data from hidraw */
634 	memset(buf, 0, sizeof(buf));
635 	err = read(self->hidraw_fd, buf, sizeof(buf));
636 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
637 	ASSERT_EQ(buf[0], 1);
638 	ASSERT_EQ(buf[2], 47);
639 
640 	/* inject another event */
641 	memset(buf, 0, sizeof(buf));
642 	buf[0] = 1;
643 	buf[1] = 47;
644 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
645 
646 	/* check that hid_first_event() was executed */
647 	ASSERT_EQ(self->skel->data->callback_check, 47) TH_LOG("callback_check1");
648 
649 	/* read the data from hidraw */
650 	memset(buf, 0, sizeof(buf));
651 	err = read(self->hidraw_fd, buf, sizeof(buf));
652 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
653 	ASSERT_EQ(buf[2], 52);
654 }
655 
656 /*
657  * Attach hid_first_event to the given uhid device,
658  * retrieve and open the matching hidraw node,
659  * inject one event in the uhid device,
660  * check that the program sees it and can change the data
661  */
662 TEST_F(hid_bpf, subprog_raw_event)
663 {
664 	const struct test_program progs[] = {
665 		{ .name = "hid_subprog_first_event" },
666 	};
667 	__u8 buf[10] = {0};
668 	int err;
669 
670 	LOAD_PROGRAMS(progs);
671 
672 	/* inject one event */
673 	buf[0] = 1;
674 	buf[1] = 42;
675 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
676 
677 	/* read the data from hidraw */
678 	memset(buf, 0, sizeof(buf));
679 	err = read(self->hidraw_fd, buf, sizeof(buf));
680 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
681 	ASSERT_EQ(buf[0], 1);
682 	ASSERT_EQ(buf[2], 47);
683 
684 	/* inject another event */
685 	memset(buf, 0, sizeof(buf));
686 	buf[0] = 1;
687 	buf[1] = 47;
688 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
689 
690 	/* read the data from hidraw */
691 	memset(buf, 0, sizeof(buf));
692 	err = read(self->hidraw_fd, buf, sizeof(buf));
693 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
694 	ASSERT_EQ(buf[2], 52);
695 }
696 
697 /*
698  * Attach hid_first_event to the given uhid device,
699  * attempt at re-attaching it, we should not lock and
700  * return an invalid struct bpf_link
701  */
702 TEST_F(hid_bpf, multiple_attach)
703 {
704 	const struct test_program progs[] = {
705 		{ .name = "hid_first_event" },
706 	};
707 	struct bpf_link *link;
708 
709 	LOAD_PROGRAMS(progs);
710 
711 	link = bpf_map__attach_struct_ops(self->skel->maps.first_event);
712 	ASSERT_NULL(link) TH_LOG("unexpected return value when re-attaching the struct_ops");
713 }
714 
715 /*
716  * Ensures that we can attach/detach programs
717  */
718 TEST_F(hid_bpf, test_attach_detach)
719 {
720 	const struct test_program progs[] = {
721 		{ .name = "hid_first_event" },
722 		{ .name = "hid_second_event" },
723 	};
724 	struct bpf_link *link;
725 	__u8 buf[10] = {0};
726 	int err, link_fd;
727 
728 	LOAD_PROGRAMS(progs);
729 
730 	link = self->hid_links[0];
731 	ASSERT_OK_PTR(link) TH_LOG("HID-BPF link not created");
732 
733 	link_fd = bpf_link__fd(link);
734 	ASSERT_GE(link_fd, 0) TH_LOG("HID-BPF link FD not valid");
735 
736 	/* inject one event */
737 	buf[0] = 1;
738 	buf[1] = 42;
739 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
740 
741 	/* read the data from hidraw */
742 	memset(buf, 0, sizeof(buf));
743 	err = read(self->hidraw_fd, buf, sizeof(buf));
744 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
745 	ASSERT_EQ(buf[0], 1);
746 	ASSERT_EQ(buf[2], 47);
747 
748 	/* make sure both programs are run */
749 	ASSERT_EQ(buf[3], 52);
750 
751 	/* pin the first program and immediately unpin it */
752 #define PIN_PATH "/sys/fs/bpf/hid_first_event"
753 	err = bpf_obj_pin(link_fd, PIN_PATH);
754 	ASSERT_OK(err) TH_LOG("error while calling bpf_obj_pin");
755 	remove(PIN_PATH);
756 #undef PIN_PATH
757 	usleep(100000);
758 
759 	/* detach the program */
760 	detach_bpf(self);
761 
762 	self->hidraw_fd = open_hidraw(self->dev_id);
763 	ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
764 
765 	/* inject another event */
766 	memset(buf, 0, sizeof(buf));
767 	buf[0] = 1;
768 	buf[1] = 47;
769 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
770 
771 	/* read the data from hidraw */
772 	memset(buf, 0, sizeof(buf));
773 	err = read(self->hidraw_fd, buf, sizeof(buf));
774 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw_no_bpf");
775 	ASSERT_EQ(buf[0], 1);
776 	ASSERT_EQ(buf[1], 47);
777 	ASSERT_EQ(buf[2], 0);
778 	ASSERT_EQ(buf[3], 0);
779 
780 	/* re-attach our program */
781 
782 	LOAD_PROGRAMS(progs);
783 
784 	/* inject one event */
785 	memset(buf, 0, sizeof(buf));
786 	buf[0] = 1;
787 	buf[1] = 42;
788 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
789 
790 	/* read the data from hidraw */
791 	memset(buf, 0, sizeof(buf));
792 	err = read(self->hidraw_fd, buf, sizeof(buf));
793 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
794 	ASSERT_EQ(buf[0], 1);
795 	ASSERT_EQ(buf[2], 47);
796 	ASSERT_EQ(buf[3], 52);
797 }
798 
799 /*
800  * Attach hid_change_report_id to the given uhid device,
801  * retrieve and open the matching hidraw node,
802  * inject one event in the uhid device,
803  * check that the program sees it and can change the data
804  */
805 TEST_F(hid_bpf, test_hid_change_report)
806 {
807 	const struct test_program progs[] = {
808 		{ .name = "hid_change_report_id" },
809 	};
810 	__u8 buf[10] = {0};
811 	int err;
812 
813 	LOAD_PROGRAMS(progs);
814 
815 	/* inject one event */
816 	buf[0] = 1;
817 	buf[1] = 42;
818 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
819 
820 	/* read the data from hidraw */
821 	memset(buf, 0, sizeof(buf));
822 	err = read(self->hidraw_fd, buf, sizeof(buf));
823 	ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
824 	ASSERT_EQ(buf[0], 2);
825 	ASSERT_EQ(buf[1], 42);
826 	ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
827 }
828 
829 /*
830  * Call hid_bpf_input_report against the given uhid device,
831  * check that the program is called and does the expected.
832  */
833 TEST_F(hid_bpf, test_hid_user_input_report_call)
834 {
835 	struct hid_hw_request_syscall_args args = {
836 		.retval = -1,
837 		.size = 10,
838 	};
839 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
840 			    .ctx_in = &args,
841 			    .ctx_size_in = sizeof(args),
842 	);
843 	__u8 buf[10] = {0};
844 	int err, prog_fd;
845 
846 	LOAD_BPF;
847 
848 	args.hid = self->hid_id;
849 	args.data[0] = 1; /* report ID */
850 	args.data[1] = 2; /* report ID */
851 	args.data[2] = 42; /* report ID */
852 
853 	prog_fd = bpf_program__fd(self->skel->progs.hid_user_input_report);
854 
855 	/* check that there is no data to read from hidraw */
856 	memset(buf, 0, sizeof(buf));
857 	err = read(self->hidraw_fd, buf, sizeof(buf));
858 	ASSERT_EQ(err, -1) TH_LOG("read_hidraw");
859 
860 	err = bpf_prog_test_run_opts(prog_fd, &tattrs);
861 
862 	ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts");
863 
864 	ASSERT_EQ(args.retval, 0);
865 
866 	/* read the data from hidraw */
867 	memset(buf, 0, sizeof(buf));
868 	err = read(self->hidraw_fd, buf, sizeof(buf));
869 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
870 	ASSERT_EQ(buf[0], 1);
871 	ASSERT_EQ(buf[1], 2);
872 	ASSERT_EQ(buf[2], 42);
873 }
874 
875 /*
876  * Call hid_bpf_hw_output_report against the given uhid device,
877  * check that the program is called and does the expected.
878  */
879 TEST_F(hid_bpf, test_hid_user_output_report_call)
880 {
881 	struct hid_hw_request_syscall_args args = {
882 		.retval = -1,
883 		.size = 10,
884 	};
885 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
886 			    .ctx_in = &args,
887 			    .ctx_size_in = sizeof(args),
888 	);
889 	int err, cond_err, prog_fd;
890 	struct timespec time_to_wait;
891 
892 	LOAD_BPF;
893 
894 	args.hid = self->hid_id;
895 	args.data[0] = 1; /* report ID */
896 	args.data[1] = 2; /* report ID */
897 	args.data[2] = 42; /* report ID */
898 
899 	prog_fd = bpf_program__fd(self->skel->progs.hid_user_output_report);
900 
901 	pthread_mutex_lock(&uhid_output_mtx);
902 
903 	memset(output_report, 0, sizeof(output_report));
904 	clock_gettime(CLOCK_REALTIME, &time_to_wait);
905 	time_to_wait.tv_sec += 2;
906 
907 	err = bpf_prog_test_run_opts(prog_fd, &tattrs);
908 	cond_err = pthread_cond_timedwait(&uhid_output_cond, &uhid_output_mtx, &time_to_wait);
909 
910 	ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts");
911 	ASSERT_OK(cond_err) TH_LOG("error while calling waiting for the condition");
912 
913 	ASSERT_EQ(args.retval, 3);
914 
915 	ASSERT_EQ(output_report[0], 1);
916 	ASSERT_EQ(output_report[1], 2);
917 	ASSERT_EQ(output_report[2], 42);
918 
919 	pthread_mutex_unlock(&uhid_output_mtx);
920 }
921 
922 /*
923  * Call hid_hw_raw_request against the given uhid device,
924  * check that the program is called and does the expected.
925  */
926 TEST_F(hid_bpf, test_hid_user_raw_request_call)
927 {
928 	struct hid_hw_request_syscall_args args = {
929 		.retval = -1,
930 		.type = HID_FEATURE_REPORT,
931 		.request_type = HID_REQ_GET_REPORT,
932 		.size = 10,
933 	};
934 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
935 			    .ctx_in = &args,
936 			    .ctx_size_in = sizeof(args),
937 	);
938 	int err, prog_fd;
939 
940 	LOAD_BPF;
941 
942 	args.hid = self->hid_id;
943 	args.data[0] = 1; /* report ID */
944 
945 	prog_fd = bpf_program__fd(self->skel->progs.hid_user_raw_request);
946 
947 	err = bpf_prog_test_run_opts(prog_fd, &tattrs);
948 	ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts");
949 
950 	ASSERT_EQ(args.retval, 2);
951 
952 	ASSERT_EQ(args.data[1], 2);
953 }
954 
955 /*
956  * Call hid_hw_raw_request against the given uhid device,
957  * check that the program is called and prevents the
958  * call to uhid.
959  */
960 TEST_F(hid_bpf, test_hid_filter_raw_request_call)
961 {
962 	const struct test_program progs[] = {
963 		{ .name = "hid_test_filter_raw_request" },
964 	};
965 	__u8 buf[10] = {0};
966 	int err;
967 
968 	LOAD_PROGRAMS(progs);
969 
970 	/* first check that we did not attach to device_event */
971 
972 	/* inject one event */
973 	buf[0] = 1;
974 	buf[1] = 42;
975 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
976 
977 	/* read the data from hidraw */
978 	memset(buf, 0, sizeof(buf));
979 	err = read(self->hidraw_fd, buf, sizeof(buf));
980 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
981 	ASSERT_EQ(buf[0], 1);
982 	ASSERT_EQ(buf[1], 42);
983 	ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
984 
985 	/* now check that our program is preventing hid_hw_raw_request() */
986 
987 	/* emit hid_hw_raw_request from hidraw */
988 	/* Get Feature */
989 	memset(buf, 0, sizeof(buf));
990 	buf[0] = 0x1; /* Report Number */
991 	err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
992 	ASSERT_LT(err, 0) TH_LOG("unexpected success while reading HIDIOCGFEATURE: %d", err);
993 	ASSERT_EQ(errno, 20) TH_LOG("unexpected error code while reading HIDIOCGFEATURE: %d",
994 				    errno);
995 
996 	/* remove our bpf program and check that we can now emit commands */
997 
998 	/* detach the program */
999 	detach_bpf(self);
1000 
1001 	self->hidraw_fd = open_hidraw(self->dev_id);
1002 	ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
1003 
1004 	err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
1005 	ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGFEATURE: %d", err);
1006 }
1007 
1008 /*
1009  * Call hid_hw_raw_request against the given uhid device,
1010  * check that the program is called and can issue the call
1011  * to uhid and transform the answer.
1012  */
1013 TEST_F(hid_bpf, test_hid_change_raw_request_call)
1014 {
1015 	const struct test_program progs[] = {
1016 		{ .name = "hid_test_hidraw_raw_request" },
1017 	};
1018 	__u8 buf[10] = {0};
1019 	int err;
1020 
1021 	LOAD_PROGRAMS(progs);
1022 
1023 	/* emit hid_hw_raw_request from hidraw */
1024 	/* Get Feature */
1025 	memset(buf, 0, sizeof(buf));
1026 	buf[0] = 0x1; /* Report Number */
1027 	err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
1028 	ASSERT_EQ(err, 3) TH_LOG("unexpected returned size while reading HIDIOCGFEATURE: %d", err);
1029 
1030 	ASSERT_EQ(buf[0], 2);
1031 	ASSERT_EQ(buf[1], 3);
1032 	ASSERT_EQ(buf[2], 4);
1033 }
1034 
1035 /*
1036  * Call hid_hw_raw_request against the given uhid device,
1037  * check that the program is not making infinite loops.
1038  */
1039 TEST_F(hid_bpf, test_hid_infinite_loop_raw_request_call)
1040 {
1041 	const struct test_program progs[] = {
1042 		{ .name = "hid_test_infinite_loop_raw_request" },
1043 	};
1044 	__u8 buf[10] = {0};
1045 	int err;
1046 
1047 	LOAD_PROGRAMS(progs);
1048 
1049 	/* emit hid_hw_raw_request from hidraw */
1050 	/* Get Feature */
1051 	memset(buf, 0, sizeof(buf));
1052 	buf[0] = 0x1; /* Report Number */
1053 	err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
1054 	ASSERT_EQ(err, 3) TH_LOG("unexpected returned size while reading HIDIOCGFEATURE: %d", err);
1055 }
1056 
1057 /*
1058  * Call hid_hw_output_report against the given uhid device,
1059  * check that the program is called and prevents the
1060  * call to uhid.
1061  */
1062 TEST_F(hid_bpf, test_hid_filter_output_report_call)
1063 {
1064 	const struct test_program progs[] = {
1065 		{ .name = "hid_test_filter_output_report" },
1066 	};
1067 	__u8 buf[10] = {0};
1068 	int err;
1069 
1070 	LOAD_PROGRAMS(progs);
1071 
1072 	/* first check that we did not attach to device_event */
1073 
1074 	/* inject one event */
1075 	buf[0] = 1;
1076 	buf[1] = 42;
1077 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1078 
1079 	/* read the data from hidraw */
1080 	memset(buf, 0, sizeof(buf));
1081 	err = read(self->hidraw_fd, buf, sizeof(buf));
1082 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1083 	ASSERT_EQ(buf[0], 1);
1084 	ASSERT_EQ(buf[1], 42);
1085 	ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
1086 
1087 	/* now check that our program is preventing hid_hw_output_report() */
1088 
1089 	buf[0] = 1; /* report ID */
1090 	buf[1] = 2;
1091 	buf[2] = 42;
1092 
1093 	err = write(self->hidraw_fd, buf, 3);
1094 	ASSERT_LT(err, 0) TH_LOG("unexpected success while sending hid_hw_output_report: %d", err);
1095 	ASSERT_EQ(errno, 25) TH_LOG("unexpected error code while sending hid_hw_output_report: %d",
1096 				    errno);
1097 
1098 	/* remove our bpf program and check that we can now emit commands */
1099 
1100 	/* detach the program */
1101 	detach_bpf(self);
1102 
1103 	self->hidraw_fd = open_hidraw(self->dev_id);
1104 	ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
1105 
1106 	err = write(self->hidraw_fd, buf, 3);
1107 	ASSERT_GE(err, 0) TH_LOG("error while sending hid_hw_output_report: %d", err);
1108 }
1109 
1110 /*
1111  * Call hid_hw_output_report against the given uhid device,
1112  * check that the program is called and can issue the call
1113  * to uhid and transform the answer.
1114  */
1115 TEST_F(hid_bpf, test_hid_change_output_report_call)
1116 {
1117 	const struct test_program progs[] = {
1118 		{ .name = "hid_test_hidraw_output_report" },
1119 	};
1120 	__u8 buf[10] = {0};
1121 	int err;
1122 
1123 	LOAD_PROGRAMS(progs);
1124 
1125 	/* emit hid_hw_output_report from hidraw */
1126 	buf[0] = 1; /* report ID */
1127 	buf[1] = 2;
1128 	buf[2] = 42;
1129 
1130 	err = write(self->hidraw_fd, buf, 10);
1131 	ASSERT_EQ(err, 2) TH_LOG("unexpected returned size while sending hid_hw_output_report: %d",
1132 				 err);
1133 }
1134 
1135 /*
1136  * Call hid_hw_output_report against the given uhid device,
1137  * check that the program is not making infinite loops.
1138  */
1139 TEST_F(hid_bpf, test_hid_infinite_loop_output_report_call)
1140 {
1141 	const struct test_program progs[] = {
1142 		{ .name = "hid_test_infinite_loop_output_report" },
1143 	};
1144 	__u8 buf[10] = {0};
1145 	int err;
1146 
1147 	LOAD_PROGRAMS(progs);
1148 
1149 	/* emit hid_hw_output_report from hidraw */
1150 	buf[0] = 1; /* report ID */
1151 	buf[1] = 2;
1152 	buf[2] = 42;
1153 
1154 	err = write(self->hidraw_fd, buf, 8);
1155 	ASSERT_EQ(err, 2) TH_LOG("unexpected returned size while sending hid_hw_output_report: %d",
1156 				 err);
1157 }
1158 
1159 /*
1160  * Attach hid_multiply_event_wq to the given uhid device,
1161  * retrieve and open the matching hidraw node,
1162  * inject one event in the uhid device,
1163  * check that the program sees it and can add extra data
1164  */
1165 TEST_F(hid_bpf, test_multiply_events_wq)
1166 {
1167 	const struct test_program progs[] = {
1168 		{ .name = "hid_test_multiply_events_wq" },
1169 	};
1170 	__u8 buf[10] = {0};
1171 	int err;
1172 
1173 	LOAD_PROGRAMS(progs);
1174 
1175 	/* inject one event */
1176 	buf[0] = 1;
1177 	buf[1] = 42;
1178 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1179 
1180 	/* read the data from hidraw */
1181 	memset(buf, 0, sizeof(buf));
1182 	err = read(self->hidraw_fd, buf, sizeof(buf));
1183 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1184 	ASSERT_EQ(buf[0], 1);
1185 	ASSERT_EQ(buf[1], 47);
1186 
1187 	usleep(100000);
1188 
1189 	/* read the data from hidraw */
1190 	memset(buf, 0, sizeof(buf));
1191 	err = read(self->hidraw_fd, buf, sizeof(buf));
1192 	ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
1193 	ASSERT_EQ(buf[0], 2);
1194 	ASSERT_EQ(buf[1], 3);
1195 }
1196 
1197 /*
1198  * Attach hid_multiply_event to the given uhid device,
1199  * retrieve and open the matching hidraw node,
1200  * inject one event in the uhid device,
1201  * check that the program sees it and can add extra data
1202  */
1203 TEST_F(hid_bpf, test_multiply_events)
1204 {
1205 	const struct test_program progs[] = {
1206 		{ .name = "hid_test_multiply_events" },
1207 	};
1208 	__u8 buf[10] = {0};
1209 	int err;
1210 
1211 	LOAD_PROGRAMS(progs);
1212 
1213 	/* inject one event */
1214 	buf[0] = 1;
1215 	buf[1] = 42;
1216 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1217 
1218 	/* read the data from hidraw */
1219 	memset(buf, 0, sizeof(buf));
1220 	err = read(self->hidraw_fd, buf, sizeof(buf));
1221 	ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
1222 	ASSERT_EQ(buf[0], 2);
1223 	ASSERT_EQ(buf[1], 47);
1224 
1225 	/* read the data from hidraw */
1226 	memset(buf, 0, sizeof(buf));
1227 	err = read(self->hidraw_fd, buf, sizeof(buf));
1228 	ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
1229 	ASSERT_EQ(buf[0], 2);
1230 	ASSERT_EQ(buf[1], 52);
1231 }
1232 
1233 /*
1234  * Call hid_bpf_input_report against the given uhid device,
1235  * check that the program is not making infinite loops.
1236  */
1237 TEST_F(hid_bpf, test_hid_infinite_loop_input_report_call)
1238 {
1239 	const struct test_program progs[] = {
1240 		{ .name = "hid_test_infinite_loop_input_report" },
1241 	};
1242 	__u8 buf[10] = {0};
1243 	int err;
1244 
1245 	LOAD_PROGRAMS(progs);
1246 
1247 	/* emit hid_hw_output_report from hidraw */
1248 	buf[0] = 1; /* report ID */
1249 	buf[1] = 2;
1250 	buf[2] = 42;
1251 
1252 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1253 
1254 	/* read the data from hidraw */
1255 	memset(buf, 0, sizeof(buf));
1256 	err = read(self->hidraw_fd, buf, sizeof(buf));
1257 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1258 	ASSERT_EQ(buf[0], 1);
1259 	ASSERT_EQ(buf[1], 3);
1260 
1261 	/* read the data from hidraw: hid_bpf_try_input_report should work exactly one time */
1262 	memset(buf, 0, sizeof(buf));
1263 	err = read(self->hidraw_fd, buf, sizeof(buf));
1264 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1265 	ASSERT_EQ(buf[0], 1);
1266 	ASSERT_EQ(buf[1], 4);
1267 
1268 	/* read the data from hidraw: there should be none */
1269 	memset(buf, 0, sizeof(buf));
1270 	err = read(self->hidraw_fd, buf, sizeof(buf));
1271 	ASSERT_EQ(err, -1) TH_LOG("read_hidraw");
1272 }
1273 
1274 /*
1275  * Attach hid_insert{0,1,2} to the given uhid device,
1276  * retrieve and open the matching hidraw node,
1277  * inject one event in the uhid device,
1278  * check that the programs have been inserted in the correct order.
1279  */
1280 TEST_F(hid_bpf, test_hid_attach_flags)
1281 {
1282 	const struct test_program progs[] = {
1283 		{
1284 			.name = "hid_test_insert2",
1285 			.insert_head = 0,
1286 		},
1287 		{
1288 			.name = "hid_test_insert1",
1289 			.insert_head = 1,
1290 		},
1291 		{
1292 			.name = "hid_test_insert3",
1293 			.insert_head = 0,
1294 		},
1295 	};
1296 	__u8 buf[10] = {0};
1297 	int err;
1298 
1299 	LOAD_PROGRAMS(progs);
1300 
1301 	/* inject one event */
1302 	buf[0] = 1;
1303 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1304 
1305 	/* read the data from hidraw */
1306 	memset(buf, 0, sizeof(buf));
1307 	err = read(self->hidraw_fd, buf, sizeof(buf));
1308 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1309 	ASSERT_EQ(buf[1], 1);
1310 	ASSERT_EQ(buf[2], 2);
1311 	ASSERT_EQ(buf[3], 3);
1312 }
1313 
1314 /*
1315  * Attach hid_rdesc_fixup to the given uhid device,
1316  * retrieve and open the matching hidraw node,
1317  * check that the hidraw report descriptor has been updated.
1318  */
1319 TEST_F(hid_bpf, test_rdesc_fixup)
1320 {
1321 	struct hidraw_report_descriptor rpt_desc = {0};
1322 	const struct test_program progs[] = {
1323 		{ .name = "hid_rdesc_fixup" },
1324 	};
1325 	int err, desc_size;
1326 
1327 	LOAD_PROGRAMS(progs);
1328 
1329 	/* check that hid_rdesc_fixup() was executed */
1330 	ASSERT_EQ(self->skel->data->callback2_check, 0x21);
1331 
1332 	/* read the exposed report descriptor from hidraw */
1333 	err = ioctl(self->hidraw_fd, HIDIOCGRDESCSIZE, &desc_size);
1334 	ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESCSIZE: %d", err);
1335 
1336 	/* ensure the new size of the rdesc is bigger than the old one */
1337 	ASSERT_GT(desc_size, sizeof(rdesc));
1338 
1339 	rpt_desc.size = desc_size;
1340 	err = ioctl(self->hidraw_fd, HIDIOCGRDESC, &rpt_desc);
1341 	ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESC: %d", err);
1342 
1343 	ASSERT_EQ(rpt_desc.value[4], 0x42);
1344 }
1345 
1346 static int libbpf_print_fn(enum libbpf_print_level level,
1347 			   const char *format, va_list args)
1348 {
1349 	char buf[1024];
1350 
1351 	if (level == LIBBPF_DEBUG)
1352 		return 0;
1353 
1354 	snprintf(buf, sizeof(buf), "# %s", format);
1355 
1356 	vfprintf(stdout, buf, args);
1357 	return 0;
1358 }
1359 
1360 static void __attribute__((constructor)) __constructor_order_last(void)
1361 {
1362 	if (!__constructor_order)
1363 		__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
1364 }
1365 
1366 int main(int argc, char **argv)
1367 {
1368 	/* Use libbpf 1.0 API mode */
1369 	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1370 	libbpf_set_print(libbpf_print_fn);
1371 
1372 	return test_harness_run(argc, argv);
1373 }
1374