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 static unsigned char rdesc[] = { 20 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 21 0x09, 0x21, /* Usage (Vendor Usage 0x21) */ 22 0xa1, 0x01, /* COLLECTION (Application) */ 23 0x09, 0x01, /* Usage (Vendor Usage 0x01) */ 24 0xa1, 0x00, /* COLLECTION (Physical) */ 25 0x85, 0x02, /* REPORT_ID (2) */ 26 0x19, 0x01, /* USAGE_MINIMUM (1) */ 27 0x29, 0x08, /* USAGE_MAXIMUM (3) */ 28 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 29 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */ 30 0x95, 0x08, /* REPORT_COUNT (8) */ 31 0x75, 0x08, /* REPORT_SIZE (8) */ 32 0x81, 0x02, /* INPUT (Data,Var,Abs) */ 33 0xc0, /* END_COLLECTION */ 34 0x09, 0x01, /* Usage (Vendor Usage 0x01) */ 35 0xa1, 0x00, /* COLLECTION (Physical) */ 36 0x85, 0x01, /* REPORT_ID (1) */ 37 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 38 0x19, 0x01, /* USAGE_MINIMUM (1) */ 39 0x29, 0x03, /* USAGE_MAXIMUM (3) */ 40 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 41 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 42 0x95, 0x03, /* REPORT_COUNT (3) */ 43 0x75, 0x01, /* REPORT_SIZE (1) */ 44 0x81, 0x02, /* INPUT (Data,Var,Abs) */ 45 0x95, 0x01, /* REPORT_COUNT (1) */ 46 0x75, 0x05, /* REPORT_SIZE (5) */ 47 0x81, 0x01, /* INPUT (Cnst,Var,Abs) */ 48 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ 49 0x09, 0x30, /* USAGE (X) */ 50 0x09, 0x31, /* USAGE (Y) */ 51 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */ 52 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */ 53 0x75, 0x10, /* REPORT_SIZE (16) */ 54 0x95, 0x02, /* REPORT_COUNT (2) */ 55 0x81, 0x06, /* INPUT (Data,Var,Rel) */ 56 57 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 58 0x19, 0x01, /* USAGE_MINIMUM (1) */ 59 0x29, 0x03, /* USAGE_MAXIMUM (3) */ 60 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 61 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 62 0x95, 0x03, /* REPORT_COUNT (3) */ 63 0x75, 0x01, /* REPORT_SIZE (1) */ 64 0x91, 0x02, /* Output (Data,Var,Abs) */ 65 0x95, 0x01, /* REPORT_COUNT (1) */ 66 0x75, 0x05, /* REPORT_SIZE (5) */ 67 0x91, 0x01, /* Output (Cnst,Var,Abs) */ 68 69 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 70 0x19, 0x06, /* USAGE_MINIMUM (6) */ 71 0x29, 0x08, /* USAGE_MAXIMUM (8) */ 72 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 73 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 74 0x95, 0x03, /* REPORT_COUNT (3) */ 75 0x75, 0x01, /* REPORT_SIZE (1) */ 76 0xb1, 0x02, /* Feature (Data,Var,Abs) */ 77 0x95, 0x01, /* REPORT_COUNT (1) */ 78 0x75, 0x05, /* REPORT_SIZE (5) */ 79 0x91, 0x01, /* Output (Cnst,Var,Abs) */ 80 81 0xc0, /* END_COLLECTION */ 82 0xc0, /* END_COLLECTION */ 83 }; 84 85 struct attach_prog_args { 86 int prog_fd; 87 unsigned int hid; 88 int retval; 89 }; 90 91 #define ASSERT_OK(data) ASSERT_FALSE(data) 92 #define ASSERT_OK_PTR(ptr) ASSERT_NE(NULL, ptr) 93 94 #define UHID_LOG(fmt, ...) do { \ 95 if (SHOW_UHID_DEBUG) \ 96 TH_LOG(fmt, ##__VA_ARGS__); \ 97 } while (0) 98 99 static pthread_mutex_t uhid_started_mtx = PTHREAD_MUTEX_INITIALIZER; 100 static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER; 101 102 /* no need to protect uhid_stopped, only one thread accesses it */ 103 static bool uhid_stopped; 104 105 static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uhid_event *ev) 106 { 107 ssize_t ret; 108 109 ret = write(fd, ev, sizeof(*ev)); 110 if (ret < 0) { 111 TH_LOG("Cannot write to uhid: %m"); 112 return -errno; 113 } else if (ret != sizeof(*ev)) { 114 TH_LOG("Wrong size written to uhid: %zd != %zu", 115 ret, sizeof(ev)); 116 return -EFAULT; 117 } else { 118 return 0; 119 } 120 } 121 122 static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb) 123 { 124 struct uhid_event ev; 125 char buf[25]; 126 127 sprintf(buf, "test-uhid-device-%d", rand_nb); 128 129 memset(&ev, 0, sizeof(ev)); 130 ev.type = UHID_CREATE; 131 strcpy((char *)ev.u.create.name, buf); 132 ev.u.create.rd_data = rdesc; 133 ev.u.create.rd_size = sizeof(rdesc); 134 ev.u.create.bus = BUS_USB; 135 ev.u.create.vendor = 0x0001; 136 ev.u.create.product = 0x0a37; 137 ev.u.create.version = 0; 138 ev.u.create.country = 0; 139 140 sprintf(buf, "%d", rand_nb); 141 strcpy((char *)ev.u.create.phys, buf); 142 143 return uhid_write(_metadata, fd, &ev); 144 } 145 146 static void uhid_destroy(struct __test_metadata *_metadata, int fd) 147 { 148 struct uhid_event ev; 149 150 memset(&ev, 0, sizeof(ev)); 151 ev.type = UHID_DESTROY; 152 153 uhid_write(_metadata, fd, &ev); 154 } 155 156 static int uhid_event(struct __test_metadata *_metadata, int fd) 157 { 158 struct uhid_event ev; 159 ssize_t ret; 160 161 memset(&ev, 0, sizeof(ev)); 162 ret = read(fd, &ev, sizeof(ev)); 163 if (ret == 0) { 164 UHID_LOG("Read HUP on uhid-cdev"); 165 return -EFAULT; 166 } else if (ret < 0) { 167 UHID_LOG("Cannot read uhid-cdev: %m"); 168 return -errno; 169 } else if (ret != sizeof(ev)) { 170 UHID_LOG("Invalid size read from uhid-dev: %zd != %zu", 171 ret, sizeof(ev)); 172 return -EFAULT; 173 } 174 175 switch (ev.type) { 176 case UHID_START: 177 pthread_mutex_lock(&uhid_started_mtx); 178 pthread_cond_signal(&uhid_started); 179 pthread_mutex_unlock(&uhid_started_mtx); 180 181 UHID_LOG("UHID_START from uhid-dev"); 182 break; 183 case UHID_STOP: 184 uhid_stopped = true; 185 186 UHID_LOG("UHID_STOP from uhid-dev"); 187 break; 188 case UHID_OPEN: 189 UHID_LOG("UHID_OPEN from uhid-dev"); 190 break; 191 case UHID_CLOSE: 192 UHID_LOG("UHID_CLOSE from uhid-dev"); 193 break; 194 case UHID_OUTPUT: 195 UHID_LOG("UHID_OUTPUT from uhid-dev"); 196 break; 197 case UHID_GET_REPORT: 198 UHID_LOG("UHID_GET_REPORT from uhid-dev"); 199 break; 200 case UHID_SET_REPORT: 201 UHID_LOG("UHID_SET_REPORT from uhid-dev"); 202 break; 203 default: 204 TH_LOG("Invalid event from uhid-dev: %u", ev.type); 205 } 206 207 return 0; 208 } 209 210 struct uhid_thread_args { 211 int fd; 212 struct __test_metadata *_metadata; 213 }; 214 static void *uhid_read_events_thread(void *arg) 215 { 216 struct uhid_thread_args *args = (struct uhid_thread_args *)arg; 217 struct __test_metadata *_metadata = args->_metadata; 218 struct pollfd pfds[1]; 219 int fd = args->fd; 220 int ret = 0; 221 222 pfds[0].fd = fd; 223 pfds[0].events = POLLIN; 224 225 uhid_stopped = false; 226 227 while (!uhid_stopped) { 228 ret = poll(pfds, 1, 100); 229 if (ret < 0) { 230 TH_LOG("Cannot poll for fds: %m"); 231 break; 232 } 233 if (pfds[0].revents & POLLIN) { 234 ret = uhid_event(_metadata, fd); 235 if (ret) 236 break; 237 } 238 } 239 240 return (void *)(long)ret; 241 } 242 243 static int uhid_start_listener(struct __test_metadata *_metadata, pthread_t *tid, int uhid_fd) 244 { 245 struct uhid_thread_args args = { 246 .fd = uhid_fd, 247 ._metadata = _metadata, 248 }; 249 int err; 250 251 pthread_mutex_lock(&uhid_started_mtx); 252 err = pthread_create(tid, NULL, uhid_read_events_thread, (void *)&args); 253 ASSERT_EQ(0, err) { 254 TH_LOG("Could not start the uhid thread: %d", err); 255 pthread_mutex_unlock(&uhid_started_mtx); 256 close(uhid_fd); 257 return -EIO; 258 } 259 pthread_cond_wait(&uhid_started, &uhid_started_mtx); 260 pthread_mutex_unlock(&uhid_started_mtx); 261 262 return 0; 263 } 264 265 static int uhid_send_event(struct __test_metadata *_metadata, int fd, __u8 *buf, size_t size) 266 { 267 struct uhid_event ev; 268 269 if (size > sizeof(ev.u.input.data)) 270 return -E2BIG; 271 272 memset(&ev, 0, sizeof(ev)); 273 ev.type = UHID_INPUT2; 274 ev.u.input2.size = size; 275 276 memcpy(ev.u.input2.data, buf, size); 277 278 return uhid_write(_metadata, fd, &ev); 279 } 280 281 static int setup_uhid(struct __test_metadata *_metadata, int rand_nb) 282 { 283 int fd; 284 const char *path = "/dev/uhid"; 285 int ret; 286 287 fd = open(path, O_RDWR | O_CLOEXEC); 288 ASSERT_GE(fd, 0) TH_LOG("open uhid-cdev failed; %d", fd); 289 290 ret = uhid_create(_metadata, fd, rand_nb); 291 ASSERT_EQ(0, ret) { 292 TH_LOG("create uhid device failed: %d", ret); 293 close(fd); 294 } 295 296 return fd; 297 } 298 299 static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir) 300 { 301 const char *target = "0003:0001:0A37.*"; 302 char phys[512]; 303 char uevent[1024]; 304 char temp[512]; 305 int fd, nread; 306 bool found = false; 307 308 if (fnmatch(target, dir->d_name, 0)) 309 return false; 310 311 /* we found the correct VID/PID, now check for phys */ 312 sprintf(uevent, "%s/%s/uevent", workdir, dir->d_name); 313 314 fd = open(uevent, O_RDONLY | O_NONBLOCK); 315 if (fd < 0) 316 return false; 317 318 sprintf(phys, "PHYS=%d", dev_id); 319 320 nread = read(fd, temp, ARRAY_SIZE(temp)); 321 if (nread > 0 && (strstr(temp, phys)) != NULL) 322 found = true; 323 324 close(fd); 325 326 return found; 327 } 328 329 static int get_hid_id(int dev_id) 330 { 331 const char *workdir = "/sys/devices/virtual/misc/uhid"; 332 const char *str_id; 333 DIR *d; 334 struct dirent *dir; 335 int found = -1, attempts = 3; 336 337 /* it would be nice to be able to use nftw, but the no_alu32 target doesn't support it */ 338 339 while (found < 0 && attempts > 0) { 340 attempts--; 341 d = opendir(workdir); 342 if (d) { 343 while ((dir = readdir(d)) != NULL) { 344 if (!match_sysfs_device(dev_id, workdir, dir)) 345 continue; 346 347 str_id = dir->d_name + sizeof("0003:0001:0A37."); 348 found = (int)strtol(str_id, NULL, 16); 349 350 break; 351 } 352 closedir(d); 353 } 354 if (found < 0) 355 usleep(100000); 356 } 357 358 return found; 359 } 360 361 static int get_hidraw(int dev_id) 362 { 363 const char *workdir = "/sys/devices/virtual/misc/uhid"; 364 char sysfs[1024]; 365 DIR *d, *subd; 366 struct dirent *dir, *subdir; 367 int i, found = -1; 368 369 /* retry 5 times in case the system is loaded */ 370 for (i = 5; i > 0; i--) { 371 usleep(10); 372 d = opendir(workdir); 373 374 if (!d) 375 continue; 376 377 while ((dir = readdir(d)) != NULL) { 378 if (!match_sysfs_device(dev_id, workdir, dir)) 379 continue; 380 381 sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name); 382 383 subd = opendir(sysfs); 384 if (!subd) 385 continue; 386 387 while ((subdir = readdir(subd)) != NULL) { 388 if (fnmatch("hidraw*", subdir->d_name, 0)) 389 continue; 390 391 found = atoi(subdir->d_name + strlen("hidraw")); 392 } 393 394 closedir(subd); 395 396 if (found > 0) 397 break; 398 } 399 closedir(d); 400 } 401 402 return found; 403 } 404 405 static int open_hidraw(int dev_id) 406 { 407 int hidraw_number; 408 char hidraw_path[64] = { 0 }; 409 410 hidraw_number = get_hidraw(dev_id); 411 if (hidraw_number < 0) 412 return hidraw_number; 413 414 /* open hidraw node to check the other side of the pipe */ 415 sprintf(hidraw_path, "/dev/hidraw%d", hidraw_number); 416 return open(hidraw_path, O_RDWR | O_NONBLOCK); 417 } 418 419 FIXTURE(hid_bpf) { 420 int dev_id; 421 int uhid_fd; 422 int hidraw_fd; 423 int hid_id; 424 pthread_t tid; 425 struct hid *skel; 426 }; 427 static void detach_bpf(FIXTURE_DATA(hid_bpf) * self) 428 { 429 if (self->hidraw_fd) 430 close(self->hidraw_fd); 431 self->hidraw_fd = 0; 432 433 hid__destroy(self->skel); 434 self->skel = NULL; 435 } 436 437 FIXTURE_TEARDOWN(hid_bpf) { 438 void *uhid_err; 439 440 uhid_destroy(_metadata, self->uhid_fd); 441 442 detach_bpf(self); 443 pthread_join(self->tid, &uhid_err); 444 } 445 #define TEARDOWN_LOG(fmt, ...) do { \ 446 TH_LOG(fmt, ##__VA_ARGS__); \ 447 hid_bpf_teardown(_metadata, self, variant); \ 448 } while (0) 449 450 FIXTURE_SETUP(hid_bpf) 451 { 452 time_t t; 453 int err; 454 455 /* initialize random number generator */ 456 srand((unsigned int)time(&t)); 457 458 self->dev_id = rand() % 1024; 459 460 self->uhid_fd = setup_uhid(_metadata, self->dev_id); 461 462 /* locate the uev, self, variant);ent file of the created device */ 463 self->hid_id = get_hid_id(self->dev_id); 464 ASSERT_GT(self->hid_id, 0) 465 TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id); 466 467 err = uhid_start_listener(_metadata, &self->tid, self->uhid_fd); 468 ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err); 469 } 470 471 struct test_program { 472 const char *name; 473 }; 474 #define LOAD_PROGRAMS(progs) \ 475 load_programs(progs, ARRAY_SIZE(progs), _metadata, self, variant) 476 #define LOAD_BPF \ 477 load_programs(NULL, 0, _metadata, self, variant) 478 static void load_programs(const struct test_program programs[], 479 const size_t progs_count, 480 struct __test_metadata *_metadata, 481 FIXTURE_DATA(hid_bpf) * self, 482 const FIXTURE_VARIANT(hid_bpf) * variant) 483 { 484 int attach_fd, err = -EINVAL; 485 struct attach_prog_args args = { 486 .retval = -1, 487 }; 488 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr, 489 .ctx_in = &args, 490 .ctx_size_in = sizeof(args), 491 ); 492 493 /* open the bpf file */ 494 self->skel = hid__open(); 495 ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open"); 496 497 for (int i = 0; i < progs_count; i++) { 498 struct bpf_program *prog; 499 500 prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj, 501 programs[i].name); 502 ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name); 503 504 bpf_program__set_autoload(prog, true); 505 } 506 507 err = hid__load(self->skel); 508 ASSERT_OK(err) TH_LOG("hid_skel_load failed: %d", err); 509 510 attach_fd = bpf_program__fd(self->skel->progs.attach_prog); 511 ASSERT_GE(attach_fd, 0) TH_LOG("locate attach_prog: %d", attach_fd); 512 513 for (int i = 0; i < progs_count; i++) { 514 struct bpf_program *prog; 515 516 prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj, 517 programs[i].name); 518 ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name); 519 520 args.prog_fd = bpf_program__fd(prog); 521 args.hid = self->hid_id; 522 err = bpf_prog_test_run_opts(attach_fd, &tattr); 523 ASSERT_OK(args.retval) TH_LOG("attach_hid(%s): %d", programs[i].name, args.retval); 524 } 525 526 self->hidraw_fd = open_hidraw(self->dev_id); 527 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 528 } 529 530 /* 531 * A simple test to see if the fixture is working fine. 532 * If this fails, none of the other tests will pass. 533 */ 534 TEST_F(hid_bpf, test_create_uhid) 535 { 536 } 537 538 /* 539 * Attach hid_first_event to the given uhid device, 540 * retrieve and open the matching hidraw node, 541 * inject one event in the uhid device, 542 * check that the program sees it and can change the data 543 */ 544 TEST_F(hid_bpf, raw_event) 545 { 546 const struct test_program progs[] = { 547 { .name = "hid_first_event" }, 548 }; 549 __u8 buf[10] = {0}; 550 int err; 551 552 LOAD_PROGRAMS(progs); 553 554 /* check that the program is correctly loaded */ 555 ASSERT_EQ(self->skel->data->callback_check, 52) TH_LOG("callback_check1"); 556 ASSERT_EQ(self->skel->data->callback2_check, 52) TH_LOG("callback2_check1"); 557 558 /* inject one event */ 559 buf[0] = 1; 560 buf[1] = 42; 561 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 562 563 /* check that hid_first_event() was executed */ 564 ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1"); 565 566 /* read the data from hidraw */ 567 memset(buf, 0, sizeof(buf)); 568 err = read(self->hidraw_fd, buf, sizeof(buf)); 569 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 570 ASSERT_EQ(buf[0], 1); 571 ASSERT_EQ(buf[2], 47); 572 573 /* inject another event */ 574 memset(buf, 0, sizeof(buf)); 575 buf[0] = 1; 576 buf[1] = 47; 577 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 578 579 /* check that hid_first_event() was executed */ 580 ASSERT_EQ(self->skel->data->callback_check, 47) TH_LOG("callback_check1"); 581 582 /* read the data from hidraw */ 583 memset(buf, 0, sizeof(buf)); 584 err = read(self->hidraw_fd, buf, sizeof(buf)); 585 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 586 ASSERT_EQ(buf[2], 52); 587 } 588 589 /* 590 * Ensures that we can attach/detach programs 591 */ 592 TEST_F(hid_bpf, test_attach_detach) 593 { 594 const struct test_program progs[] = { 595 { .name = "hid_first_event" }, 596 }; 597 __u8 buf[10] = {0}; 598 int err; 599 600 LOAD_PROGRAMS(progs); 601 602 /* inject one event */ 603 buf[0] = 1; 604 buf[1] = 42; 605 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 606 607 /* read the data from hidraw */ 608 memset(buf, 0, sizeof(buf)); 609 err = read(self->hidraw_fd, buf, sizeof(buf)); 610 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 611 ASSERT_EQ(buf[0], 1); 612 ASSERT_EQ(buf[2], 47); 613 614 /* pin the program and immediately unpin it */ 615 #define PIN_PATH "/sys/fs/bpf/hid_first_event" 616 bpf_program__pin(self->skel->progs.hid_first_event, PIN_PATH); 617 remove(PIN_PATH); 618 #undef PIN_PATH 619 usleep(100000); 620 621 /* detach the program */ 622 detach_bpf(self); 623 624 self->hidraw_fd = open_hidraw(self->dev_id); 625 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 626 627 /* inject another event */ 628 memset(buf, 0, sizeof(buf)); 629 buf[0] = 1; 630 buf[1] = 47; 631 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 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_no_bpf"); 637 ASSERT_EQ(buf[0], 1); 638 ASSERT_EQ(buf[1], 47); 639 ASSERT_EQ(buf[2], 0); 640 641 /* re-attach our program */ 642 643 LOAD_PROGRAMS(progs); 644 645 /* inject one event */ 646 memset(buf, 0, sizeof(buf)); 647 buf[0] = 1; 648 buf[1] = 42; 649 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 650 651 /* read the data from hidraw */ 652 memset(buf, 0, sizeof(buf)); 653 err = read(self->hidraw_fd, buf, sizeof(buf)); 654 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 655 ASSERT_EQ(buf[0], 1); 656 ASSERT_EQ(buf[2], 47); 657 } 658 659 /* 660 * Attach hid_change_report_id to the given uhid device, 661 * retrieve and open the matching hidraw node, 662 * inject one event in the uhid device, 663 * check that the program sees it and can change the data 664 */ 665 TEST_F(hid_bpf, test_hid_change_report) 666 { 667 const struct test_program progs[] = { 668 { .name = "hid_change_report_id" }, 669 }; 670 __u8 buf[10] = {0}; 671 int err; 672 673 LOAD_PROGRAMS(progs); 674 675 /* inject one event */ 676 buf[0] = 1; 677 buf[1] = 42; 678 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 679 680 /* read the data from hidraw */ 681 memset(buf, 0, sizeof(buf)); 682 err = read(self->hidraw_fd, buf, sizeof(buf)); 683 ASSERT_EQ(err, 9) TH_LOG("read_hidraw"); 684 ASSERT_EQ(buf[0], 2); 685 ASSERT_EQ(buf[1], 42); 686 ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test"); 687 } 688 689 static int libbpf_print_fn(enum libbpf_print_level level, 690 const char *format, va_list args) 691 { 692 char buf[1024]; 693 694 if (level == LIBBPF_DEBUG) 695 return 0; 696 697 snprintf(buf, sizeof(buf), "# %s", format); 698 699 vfprintf(stdout, buf, args); 700 return 0; 701 } 702 703 static void __attribute__((constructor)) __constructor_order_last(void) 704 { 705 if (!__constructor_order) 706 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; 707 } 708 709 int main(int argc, char **argv) 710 { 711 /* Use libbpf 1.0 API mode */ 712 libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 713 libbpf_set_print(libbpf_print_fn); 714 715 return test_harness_run(argc, argv); 716 } 717