xref: /linux/tools/testing/selftests/hid/hid_bpf.c (revision d40981350844c2cfa437abfc80596e10ea8f1149)
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 	int err = -EINVAL;
536 
537 	ASSERT_LE(progs_count, ARRAY_SIZE(self->hid_links))
538 		TH_LOG("too many programs are to be loaded");
539 
540 	/* open the bpf file */
541 	self->skel = hid__open();
542 	ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open");
543 
544 	for (int i = 0; i < progs_count; i++) {
545 		struct bpf_program *prog;
546 		struct bpf_map *map;
547 		int *ops_hid_id;
548 
549 		prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj,
550 							programs[i].name);
551 		ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name);
552 
553 		bpf_program__set_autoload(prog, true);
554 
555 		map = bpf_object__find_map_by_name(*self->skel->skeleton->obj,
556 							  programs[i].name + 4);
557 		ASSERT_OK_PTR(map) TH_LOG("can not find struct_ops by name '%s'",
558 					  programs[i].name + 4);
559 
560 		/* hid_id is the first field of struct hid_bpf_ops */
561 		ops_hid_id = bpf_map__initial_value(map, NULL);
562 		ASSERT_OK_PTR(ops_hid_id) TH_LOG("unable to retrieve struct_ops data");
563 
564 		*ops_hid_id = self->hid_id;
565 	}
566 
567 	err = hid__load(self->skel);
568 	ASSERT_OK(err) TH_LOG("hid_skel_load failed: %d", err);
569 
570 	for (int i = 0; i < progs_count; i++) {
571 		struct bpf_map *map;
572 
573 		map = bpf_object__find_map_by_name(*self->skel->skeleton->obj,
574 							  programs[i].name + 4);
575 		ASSERT_OK_PTR(map) TH_LOG("can not find struct_ops by name '%s'",
576 					  programs[i].name + 4);
577 
578 		self->hid_links[i] = bpf_map__attach_struct_ops(map);
579 		ASSERT_OK_PTR(self->hid_links[i]) TH_LOG("failed to attach struct ops '%s'",
580 							 programs[i].name + 4);
581 	}
582 
583 	hid__attach(self->skel);
584 
585 	self->hidraw_fd = open_hidraw(self->dev_id);
586 	ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
587 }
588 
589 /*
590  * A simple test to see if the fixture is working fine.
591  * If this fails, none of the other tests will pass.
592  */
593 TEST_F(hid_bpf, test_create_uhid)
594 {
595 }
596 
597 /*
598  * Attach hid_first_event to the given uhid device,
599  * retrieve and open the matching hidraw node,
600  * inject one event in the uhid device,
601  * check that the program sees it and can change the data
602  */
603 TEST_F(hid_bpf, raw_event)
604 {
605 	const struct test_program progs[] = {
606 		{ .name = "hid_first_event" },
607 	};
608 	__u8 buf[10] = {0};
609 	int err;
610 
611 	LOAD_PROGRAMS(progs);
612 
613 	/* check that the program is correctly loaded */
614 	ASSERT_EQ(self->skel->data->callback_check, 52) TH_LOG("callback_check1");
615 	ASSERT_EQ(self->skel->data->callback2_check, 52) TH_LOG("callback2_check1");
616 
617 	/* inject one event */
618 	buf[0] = 1;
619 	buf[1] = 42;
620 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
621 
622 	/* check that hid_first_event() was executed */
623 	ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1");
624 
625 	/* read the data from hidraw */
626 	memset(buf, 0, sizeof(buf));
627 	err = read(self->hidraw_fd, buf, sizeof(buf));
628 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
629 	ASSERT_EQ(buf[0], 1);
630 	ASSERT_EQ(buf[2], 47);
631 
632 	/* inject another event */
633 	memset(buf, 0, sizeof(buf));
634 	buf[0] = 1;
635 	buf[1] = 47;
636 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
637 
638 	/* check that hid_first_event() was executed */
639 	ASSERT_EQ(self->skel->data->callback_check, 47) TH_LOG("callback_check1");
640 
641 	/* read the data from hidraw */
642 	memset(buf, 0, sizeof(buf));
643 	err = read(self->hidraw_fd, buf, sizeof(buf));
644 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
645 	ASSERT_EQ(buf[2], 52);
646 }
647 
648 /*
649  * Attach hid_first_event to the given uhid device,
650  * retrieve and open the matching hidraw node,
651  * inject one event in the uhid device,
652  * check that the program sees it and can change the data
653  */
654 TEST_F(hid_bpf, subprog_raw_event)
655 {
656 	const struct test_program progs[] = {
657 		{ .name = "hid_subprog_first_event" },
658 	};
659 	__u8 buf[10] = {0};
660 	int err;
661 
662 	LOAD_PROGRAMS(progs);
663 
664 	/* inject one event */
665 	buf[0] = 1;
666 	buf[1] = 42;
667 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
668 
669 	/* read the data from hidraw */
670 	memset(buf, 0, sizeof(buf));
671 	err = read(self->hidraw_fd, buf, sizeof(buf));
672 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
673 	ASSERT_EQ(buf[0], 1);
674 	ASSERT_EQ(buf[2], 47);
675 
676 	/* inject another event */
677 	memset(buf, 0, sizeof(buf));
678 	buf[0] = 1;
679 	buf[1] = 47;
680 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
681 
682 	/* read the data from hidraw */
683 	memset(buf, 0, sizeof(buf));
684 	err = read(self->hidraw_fd, buf, sizeof(buf));
685 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
686 	ASSERT_EQ(buf[2], 52);
687 }
688 
689 /*
690  * Ensures that we can attach/detach programs
691  */
692 TEST_F(hid_bpf, test_attach_detach)
693 {
694 	const struct test_program progs[] = {
695 		{ .name = "hid_first_event" },
696 		{ .name = "hid_second_event" },
697 	};
698 	struct bpf_link *link;
699 	__u8 buf[10] = {0};
700 	int err, link_fd;
701 
702 	LOAD_PROGRAMS(progs);
703 
704 	link = self->hid_links[0];
705 	ASSERT_OK_PTR(link) TH_LOG("HID-BPF link not created");
706 
707 	link_fd = bpf_link__fd(link);
708 	ASSERT_GE(link_fd, 0) TH_LOG("HID-BPF link FD not valid");
709 
710 	/* inject one event */
711 	buf[0] = 1;
712 	buf[1] = 42;
713 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
714 
715 	/* read the data from hidraw */
716 	memset(buf, 0, sizeof(buf));
717 	err = read(self->hidraw_fd, buf, sizeof(buf));
718 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
719 	ASSERT_EQ(buf[0], 1);
720 	ASSERT_EQ(buf[2], 47);
721 
722 	/* make sure both programs are run */
723 	ASSERT_EQ(buf[3], 52);
724 
725 	/* pin the first program and immediately unpin it */
726 #define PIN_PATH "/sys/fs/bpf/hid_first_event"
727 	err = bpf_obj_pin(link_fd, PIN_PATH);
728 	ASSERT_OK(err) TH_LOG("error while calling bpf_obj_pin");
729 	remove(PIN_PATH);
730 #undef PIN_PATH
731 	usleep(100000);
732 
733 	/* detach the program */
734 	detach_bpf(self);
735 
736 	self->hidraw_fd = open_hidraw(self->dev_id);
737 	ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
738 
739 	/* inject another event */
740 	memset(buf, 0, sizeof(buf));
741 	buf[0] = 1;
742 	buf[1] = 47;
743 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
744 
745 	/* read the data from hidraw */
746 	memset(buf, 0, sizeof(buf));
747 	err = read(self->hidraw_fd, buf, sizeof(buf));
748 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw_no_bpf");
749 	ASSERT_EQ(buf[0], 1);
750 	ASSERT_EQ(buf[1], 47);
751 	ASSERT_EQ(buf[2], 0);
752 	ASSERT_EQ(buf[3], 0);
753 
754 	/* re-attach our program */
755 
756 	LOAD_PROGRAMS(progs);
757 
758 	/* inject one event */
759 	memset(buf, 0, sizeof(buf));
760 	buf[0] = 1;
761 	buf[1] = 42;
762 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
763 
764 	/* read the data from hidraw */
765 	memset(buf, 0, sizeof(buf));
766 	err = read(self->hidraw_fd, buf, sizeof(buf));
767 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
768 	ASSERT_EQ(buf[0], 1);
769 	ASSERT_EQ(buf[2], 47);
770 	ASSERT_EQ(buf[3], 52);
771 }
772 
773 /*
774  * Attach hid_change_report_id to the given uhid device,
775  * retrieve and open the matching hidraw node,
776  * inject one event in the uhid device,
777  * check that the program sees it and can change the data
778  */
779 TEST_F(hid_bpf, test_hid_change_report)
780 {
781 	const struct test_program progs[] = {
782 		{ .name = "hid_change_report_id" },
783 	};
784 	__u8 buf[10] = {0};
785 	int err;
786 
787 	LOAD_PROGRAMS(progs);
788 
789 	/* inject one event */
790 	buf[0] = 1;
791 	buf[1] = 42;
792 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
793 
794 	/* read the data from hidraw */
795 	memset(buf, 0, sizeof(buf));
796 	err = read(self->hidraw_fd, buf, sizeof(buf));
797 	ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
798 	ASSERT_EQ(buf[0], 2);
799 	ASSERT_EQ(buf[1], 42);
800 	ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
801 }
802 
803 /*
804  * Call hid_bpf_input_report against the given uhid device,
805  * check that the program is called and does the expected.
806  */
807 TEST_F(hid_bpf, test_hid_user_input_report_call)
808 {
809 	struct hid_hw_request_syscall_args args = {
810 		.retval = -1,
811 		.size = 10,
812 	};
813 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
814 			    .ctx_in = &args,
815 			    .ctx_size_in = sizeof(args),
816 	);
817 	__u8 buf[10] = {0};
818 	int err, prog_fd;
819 
820 	LOAD_BPF;
821 
822 	args.hid = self->hid_id;
823 	args.data[0] = 1; /* report ID */
824 	args.data[1] = 2; /* report ID */
825 	args.data[2] = 42; /* report ID */
826 
827 	prog_fd = bpf_program__fd(self->skel->progs.hid_user_input_report);
828 
829 	/* check that there is no data to read from hidraw */
830 	memset(buf, 0, sizeof(buf));
831 	err = read(self->hidraw_fd, buf, sizeof(buf));
832 	ASSERT_EQ(err, -1) TH_LOG("read_hidraw");
833 
834 	err = bpf_prog_test_run_opts(prog_fd, &tattrs);
835 
836 	ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts");
837 
838 	ASSERT_EQ(args.retval, 0);
839 
840 	/* read the data from hidraw */
841 	memset(buf, 0, sizeof(buf));
842 	err = read(self->hidraw_fd, buf, sizeof(buf));
843 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
844 	ASSERT_EQ(buf[0], 1);
845 	ASSERT_EQ(buf[1], 2);
846 	ASSERT_EQ(buf[2], 42);
847 }
848 
849 /*
850  * Call hid_bpf_hw_output_report against the given uhid device,
851  * check that the program is called and does the expected.
852  */
853 TEST_F(hid_bpf, test_hid_user_output_report_call)
854 {
855 	struct hid_hw_request_syscall_args args = {
856 		.retval = -1,
857 		.size = 10,
858 	};
859 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
860 			    .ctx_in = &args,
861 			    .ctx_size_in = sizeof(args),
862 	);
863 	int err, cond_err, prog_fd;
864 	struct timespec time_to_wait;
865 
866 	LOAD_BPF;
867 
868 	args.hid = self->hid_id;
869 	args.data[0] = 1; /* report ID */
870 	args.data[1] = 2; /* report ID */
871 	args.data[2] = 42; /* report ID */
872 
873 	prog_fd = bpf_program__fd(self->skel->progs.hid_user_output_report);
874 
875 	pthread_mutex_lock(&uhid_output_mtx);
876 
877 	memset(output_report, 0, sizeof(output_report));
878 	clock_gettime(CLOCK_REALTIME, &time_to_wait);
879 	time_to_wait.tv_sec += 2;
880 
881 	err = bpf_prog_test_run_opts(prog_fd, &tattrs);
882 	cond_err = pthread_cond_timedwait(&uhid_output_cond, &uhid_output_mtx, &time_to_wait);
883 
884 	ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts");
885 	ASSERT_OK(cond_err) TH_LOG("error while calling waiting for the condition");
886 
887 	ASSERT_EQ(args.retval, 3);
888 
889 	ASSERT_EQ(output_report[0], 1);
890 	ASSERT_EQ(output_report[1], 2);
891 	ASSERT_EQ(output_report[2], 42);
892 
893 	pthread_mutex_unlock(&uhid_output_mtx);
894 }
895 
896 /*
897  * Call hid_hw_raw_request against the given uhid device,
898  * check that the program is called and does the expected.
899  */
900 TEST_F(hid_bpf, test_hid_user_raw_request_call)
901 {
902 	struct hid_hw_request_syscall_args args = {
903 		.retval = -1,
904 		.type = HID_FEATURE_REPORT,
905 		.request_type = HID_REQ_GET_REPORT,
906 		.size = 10,
907 	};
908 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
909 			    .ctx_in = &args,
910 			    .ctx_size_in = sizeof(args),
911 	);
912 	int err, prog_fd;
913 
914 	LOAD_BPF;
915 
916 	args.hid = self->hid_id;
917 	args.data[0] = 1; /* report ID */
918 
919 	prog_fd = bpf_program__fd(self->skel->progs.hid_user_raw_request);
920 
921 	err = bpf_prog_test_run_opts(prog_fd, &tattrs);
922 	ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts");
923 
924 	ASSERT_EQ(args.retval, 2);
925 
926 	ASSERT_EQ(args.data[1], 2);
927 }
928 
929 /*
930  * Call hid_hw_raw_request against the given uhid device,
931  * check that the program is called and prevents the
932  * call to uhid.
933  */
934 TEST_F(hid_bpf, test_hid_filter_raw_request_call)
935 {
936 	const struct test_program progs[] = {
937 		{ .name = "hid_test_filter_raw_request" },
938 	};
939 	__u8 buf[10] = {0};
940 	int err;
941 
942 	LOAD_PROGRAMS(progs);
943 
944 	/* first check that we did not attach to device_event */
945 
946 	/* inject one event */
947 	buf[0] = 1;
948 	buf[1] = 42;
949 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
950 
951 	/* read the data from hidraw */
952 	memset(buf, 0, sizeof(buf));
953 	err = read(self->hidraw_fd, buf, sizeof(buf));
954 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
955 	ASSERT_EQ(buf[0], 1);
956 	ASSERT_EQ(buf[1], 42);
957 	ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
958 
959 	/* now check that our program is preventing hid_hw_raw_request() */
960 
961 	/* emit hid_hw_raw_request from hidraw */
962 	/* Get Feature */
963 	memset(buf, 0, sizeof(buf));
964 	buf[0] = 0x1; /* Report Number */
965 	err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
966 	ASSERT_LT(err, 0) TH_LOG("unexpected success while reading HIDIOCGFEATURE: %d", err);
967 	ASSERT_EQ(errno, 20) TH_LOG("unexpected error code while reading HIDIOCGFEATURE: %d",
968 				    errno);
969 
970 	/* remove our bpf program and check that we can now emit commands */
971 
972 	/* detach the program */
973 	detach_bpf(self);
974 
975 	self->hidraw_fd = open_hidraw(self->dev_id);
976 	ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
977 
978 	err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
979 	ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGFEATURE: %d", err);
980 }
981 
982 /*
983  * Call hid_hw_raw_request against the given uhid device,
984  * check that the program is called and can issue the call
985  * to uhid and transform the answer.
986  */
987 TEST_F(hid_bpf, test_hid_change_raw_request_call)
988 {
989 	const struct test_program progs[] = {
990 		{ .name = "hid_test_hidraw_raw_request" },
991 	};
992 	__u8 buf[10] = {0};
993 	int err;
994 
995 	LOAD_PROGRAMS(progs);
996 
997 	/* emit hid_hw_raw_request from hidraw */
998 	/* Get Feature */
999 	memset(buf, 0, sizeof(buf));
1000 	buf[0] = 0x1; /* Report Number */
1001 	err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
1002 	ASSERT_EQ(err, 3) TH_LOG("unexpected returned size while reading HIDIOCGFEATURE: %d", err);
1003 
1004 	ASSERT_EQ(buf[0], 2);
1005 	ASSERT_EQ(buf[1], 3);
1006 	ASSERT_EQ(buf[2], 4);
1007 }
1008 
1009 /*
1010  * Call hid_hw_raw_request against the given uhid device,
1011  * check that the program is not making infinite loops.
1012  */
1013 TEST_F(hid_bpf, test_hid_infinite_loop_raw_request_call)
1014 {
1015 	const struct test_program progs[] = {
1016 		{ .name = "hid_test_infinite_loop_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 
1031 /*
1032  * Call hid_hw_output_report against the given uhid device,
1033  * check that the program is called and prevents the
1034  * call to uhid.
1035  */
1036 TEST_F(hid_bpf, test_hid_filter_output_report_call)
1037 {
1038 	const struct test_program progs[] = {
1039 		{ .name = "hid_test_filter_output_report" },
1040 	};
1041 	__u8 buf[10] = {0};
1042 	int err;
1043 
1044 	LOAD_PROGRAMS(progs);
1045 
1046 	/* first check that we did not attach to device_event */
1047 
1048 	/* inject one event */
1049 	buf[0] = 1;
1050 	buf[1] = 42;
1051 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1052 
1053 	/* read the data from hidraw */
1054 	memset(buf, 0, sizeof(buf));
1055 	err = read(self->hidraw_fd, buf, sizeof(buf));
1056 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1057 	ASSERT_EQ(buf[0], 1);
1058 	ASSERT_EQ(buf[1], 42);
1059 	ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
1060 
1061 	/* now check that our program is preventing hid_hw_output_report() */
1062 
1063 	buf[0] = 1; /* report ID */
1064 	buf[1] = 2;
1065 	buf[2] = 42;
1066 
1067 	err = write(self->hidraw_fd, buf, 3);
1068 	ASSERT_LT(err, 0) TH_LOG("unexpected success while sending hid_hw_output_report: %d", err);
1069 	ASSERT_EQ(errno, 25) TH_LOG("unexpected error code while sending hid_hw_output_report: %d",
1070 				    errno);
1071 
1072 	/* remove our bpf program and check that we can now emit commands */
1073 
1074 	/* detach the program */
1075 	detach_bpf(self);
1076 
1077 	self->hidraw_fd = open_hidraw(self->dev_id);
1078 	ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
1079 
1080 	err = write(self->hidraw_fd, buf, 3);
1081 	ASSERT_GE(err, 0) TH_LOG("error while sending hid_hw_output_report: %d", err);
1082 }
1083 
1084 /*
1085  * Call hid_hw_output_report against the given uhid device,
1086  * check that the program is called and can issue the call
1087  * to uhid and transform the answer.
1088  */
1089 TEST_F(hid_bpf, test_hid_change_output_report_call)
1090 {
1091 	const struct test_program progs[] = {
1092 		{ .name = "hid_test_hidraw_output_report" },
1093 	};
1094 	__u8 buf[10] = {0};
1095 	int err;
1096 
1097 	LOAD_PROGRAMS(progs);
1098 
1099 	/* emit hid_hw_output_report from hidraw */
1100 	buf[0] = 1; /* report ID */
1101 	buf[1] = 2;
1102 	buf[2] = 42;
1103 
1104 	err = write(self->hidraw_fd, buf, 10);
1105 	ASSERT_EQ(err, 2) TH_LOG("unexpected returned size while sending hid_hw_output_report: %d",
1106 				 err);
1107 }
1108 
1109 /*
1110  * Call hid_hw_output_report against the given uhid device,
1111  * check that the program is not making infinite loops.
1112  */
1113 TEST_F(hid_bpf, test_hid_infinite_loop_output_report_call)
1114 {
1115 	const struct test_program progs[] = {
1116 		{ .name = "hid_test_infinite_loop_output_report" },
1117 	};
1118 	__u8 buf[10] = {0};
1119 	int err;
1120 
1121 	LOAD_PROGRAMS(progs);
1122 
1123 	/* emit hid_hw_output_report from hidraw */
1124 	buf[0] = 1; /* report ID */
1125 	buf[1] = 2;
1126 	buf[2] = 42;
1127 
1128 	err = write(self->hidraw_fd, buf, 8);
1129 	ASSERT_EQ(err, 2) TH_LOG("unexpected returned size while sending hid_hw_output_report: %d",
1130 				 err);
1131 }
1132 
1133 /*
1134  * Attach hid_multiply_event_wq to the given uhid device,
1135  * retrieve and open the matching hidraw node,
1136  * inject one event in the uhid device,
1137  * check that the program sees it and can add extra data
1138  */
1139 TEST_F(hid_bpf, test_multiply_events_wq)
1140 {
1141 	const struct test_program progs[] = {
1142 		{ .name = "hid_test_multiply_events_wq" },
1143 	};
1144 	__u8 buf[10] = {0};
1145 	int err;
1146 
1147 	LOAD_PROGRAMS(progs);
1148 
1149 	/* inject one event */
1150 	buf[0] = 1;
1151 	buf[1] = 42;
1152 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1153 
1154 	/* read the data from hidraw */
1155 	memset(buf, 0, sizeof(buf));
1156 	err = read(self->hidraw_fd, buf, sizeof(buf));
1157 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1158 	ASSERT_EQ(buf[0], 1);
1159 	ASSERT_EQ(buf[1], 47);
1160 
1161 	usleep(100000);
1162 
1163 	/* read the data from hidraw */
1164 	memset(buf, 0, sizeof(buf));
1165 	err = read(self->hidraw_fd, buf, sizeof(buf));
1166 	ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
1167 	ASSERT_EQ(buf[0], 2);
1168 	ASSERT_EQ(buf[1], 3);
1169 }
1170 
1171 /*
1172  * Attach hid_multiply_event to the given uhid device,
1173  * retrieve and open the matching hidraw node,
1174  * inject one event in the uhid device,
1175  * check that the program sees it and can add extra data
1176  */
1177 TEST_F(hid_bpf, test_multiply_events)
1178 {
1179 	const struct test_program progs[] = {
1180 		{ .name = "hid_test_multiply_events" },
1181 	};
1182 	__u8 buf[10] = {0};
1183 	int err;
1184 
1185 	LOAD_PROGRAMS(progs);
1186 
1187 	/* inject one event */
1188 	buf[0] = 1;
1189 	buf[1] = 42;
1190 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1191 
1192 	/* read the data from hidraw */
1193 	memset(buf, 0, sizeof(buf));
1194 	err = read(self->hidraw_fd, buf, sizeof(buf));
1195 	ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
1196 	ASSERT_EQ(buf[0], 2);
1197 	ASSERT_EQ(buf[1], 47);
1198 
1199 	/* read the data from hidraw */
1200 	memset(buf, 0, sizeof(buf));
1201 	err = read(self->hidraw_fd, buf, sizeof(buf));
1202 	ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
1203 	ASSERT_EQ(buf[0], 2);
1204 	ASSERT_EQ(buf[1], 52);
1205 }
1206 
1207 /*
1208  * Call hid_bpf_input_report against the given uhid device,
1209  * check that the program is not making infinite loops.
1210  */
1211 TEST_F(hid_bpf, test_hid_infinite_loop_input_report_call)
1212 {
1213 	const struct test_program progs[] = {
1214 		{ .name = "hid_test_infinite_loop_input_report" },
1215 	};
1216 	__u8 buf[10] = {0};
1217 	int err;
1218 
1219 	LOAD_PROGRAMS(progs);
1220 
1221 	/* emit hid_hw_output_report from hidraw */
1222 	buf[0] = 1; /* report ID */
1223 	buf[1] = 2;
1224 	buf[2] = 42;
1225 
1226 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1227 
1228 	/* read the data from hidraw */
1229 	memset(buf, 0, sizeof(buf));
1230 	err = read(self->hidraw_fd, buf, sizeof(buf));
1231 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1232 	ASSERT_EQ(buf[0], 1);
1233 	ASSERT_EQ(buf[1], 3);
1234 
1235 	/* read the data from hidraw: hid_bpf_try_input_report should work exactly one time */
1236 	memset(buf, 0, sizeof(buf));
1237 	err = read(self->hidraw_fd, buf, sizeof(buf));
1238 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1239 	ASSERT_EQ(buf[0], 1);
1240 	ASSERT_EQ(buf[1], 4);
1241 
1242 	/* read the data from hidraw: there should be none */
1243 	memset(buf, 0, sizeof(buf));
1244 	err = read(self->hidraw_fd, buf, sizeof(buf));
1245 	ASSERT_EQ(err, -1) TH_LOG("read_hidraw");
1246 }
1247 
1248 /*
1249  * Attach hid_insert{0,1,2} to the given uhid device,
1250  * retrieve and open the matching hidraw node,
1251  * inject one event in the uhid device,
1252  * check that the programs have been inserted in the correct order.
1253  */
1254 TEST_F(hid_bpf, test_hid_attach_flags)
1255 {
1256 	const struct test_program progs[] = {
1257 		{
1258 			.name = "hid_test_insert2",
1259 			.insert_head = 0,
1260 		},
1261 		{
1262 			.name = "hid_test_insert1",
1263 			.insert_head = 1,
1264 		},
1265 		{
1266 			.name = "hid_test_insert3",
1267 			.insert_head = 0,
1268 		},
1269 	};
1270 	__u8 buf[10] = {0};
1271 	int err;
1272 
1273 	LOAD_PROGRAMS(progs);
1274 
1275 	/* inject one event */
1276 	buf[0] = 1;
1277 	uhid_send_event(_metadata, self->uhid_fd, buf, 6);
1278 
1279 	/* read the data from hidraw */
1280 	memset(buf, 0, sizeof(buf));
1281 	err = read(self->hidraw_fd, buf, sizeof(buf));
1282 	ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
1283 	ASSERT_EQ(buf[1], 1);
1284 	ASSERT_EQ(buf[2], 2);
1285 	ASSERT_EQ(buf[3], 3);
1286 }
1287 
1288 /*
1289  * Attach hid_rdesc_fixup to the given uhid device,
1290  * retrieve and open the matching hidraw node,
1291  * check that the hidraw report descriptor has been updated.
1292  */
1293 TEST_F(hid_bpf, test_rdesc_fixup)
1294 {
1295 	struct hidraw_report_descriptor rpt_desc = {0};
1296 	const struct test_program progs[] = {
1297 		{ .name = "hid_rdesc_fixup" },
1298 	};
1299 	int err, desc_size;
1300 
1301 	LOAD_PROGRAMS(progs);
1302 
1303 	/* check that hid_rdesc_fixup() was executed */
1304 	ASSERT_EQ(self->skel->data->callback2_check, 0x21);
1305 
1306 	/* read the exposed report descriptor from hidraw */
1307 	err = ioctl(self->hidraw_fd, HIDIOCGRDESCSIZE, &desc_size);
1308 	ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESCSIZE: %d", err);
1309 
1310 	/* ensure the new size of the rdesc is bigger than the old one */
1311 	ASSERT_GT(desc_size, sizeof(rdesc));
1312 
1313 	rpt_desc.size = desc_size;
1314 	err = ioctl(self->hidraw_fd, HIDIOCGRDESC, &rpt_desc);
1315 	ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESC: %d", err);
1316 
1317 	ASSERT_EQ(rpt_desc.value[4], 0x42);
1318 }
1319 
1320 static int libbpf_print_fn(enum libbpf_print_level level,
1321 			   const char *format, va_list args)
1322 {
1323 	char buf[1024];
1324 
1325 	if (level == LIBBPF_DEBUG)
1326 		return 0;
1327 
1328 	snprintf(buf, sizeof(buf), "# %s", format);
1329 
1330 	vfprintf(stdout, buf, args);
1331 	return 0;
1332 }
1333 
1334 static void __attribute__((constructor)) __constructor_order_last(void)
1335 {
1336 	if (!__constructor_order)
1337 		__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
1338 }
1339 
1340 int main(int argc, char **argv)
1341 {
1342 	/* Use libbpf 1.0 API mode */
1343 	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1344 	libbpf_set_print(libbpf_print_fn);
1345 
1346 	return test_harness_run(argc, argv);
1347 }
1348