Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/hid: cleanup C tests by adding a common struct uhid_device

Allows to have an abstract class uhid_device which handles all of the
uhid part without having to mess up with individual fds.

struct attach_prog_args is now never used in hid_bpf.c, so drop it as well

Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Link: https://patch.msgid.link/20241001-hid-bpf-hid-generic-v3-6-2ef1019468df@kernel.org
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>

+87 -100
+27 -50
tools/testing/selftests/hid/hid_bpf.c
··· 4 4 #include "hid_common.h" 5 5 #include <bpf/bpf.h> 6 6 7 - struct attach_prog_args { 8 - int prog_fd; 9 - unsigned int hid; 10 - int retval; 11 - int insert_head; 12 - }; 13 - 14 7 struct hid_hw_request_syscall_args { 15 8 __u8 data[10]; 16 9 unsigned int hid; ··· 14 21 }; 15 22 16 23 FIXTURE(hid_bpf) { 17 - int dev_id; 18 - int uhid_fd; 24 + struct uhid_device hid; 19 25 int hidraw_fd; 20 - int hid_id; 21 - pthread_t tid; 22 26 struct hid *skel; 23 27 struct bpf_link *hid_links[3]; /* max number of programs loaded in a single test */ 24 28 }; ··· 44 54 FIXTURE_TEARDOWN(hid_bpf) { 45 55 void *uhid_err; 46 56 47 - uhid_destroy(_metadata, self->uhid_fd); 57 + uhid_destroy(_metadata, &self->hid); 48 58 49 59 detach_bpf(self); 50 - pthread_join(self->tid, &uhid_err); 60 + pthread_join(self->hid.tid, &uhid_err); 51 61 } 52 62 #define TEARDOWN_LOG(fmt, ...) do { \ 53 63 TH_LOG(fmt, ##__VA_ARGS__); \ ··· 56 66 57 67 FIXTURE_SETUP(hid_bpf) 58 68 { 59 - time_t t; 60 69 int err; 61 70 62 - /* initialize random number generator */ 63 - srand((unsigned int)time(&t)); 64 - 65 - self->dev_id = rand() % 1024; 66 - 67 - self->uhid_fd = setup_uhid(_metadata, self->dev_id); 68 - 69 - /* locate the uev, self, variant);ent file of the created device */ 70 - self->hid_id = get_hid_id(self->dev_id); 71 - ASSERT_GT(self->hid_id, 0) 72 - TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id); 73 - 74 - err = uhid_start_listener(_metadata, &self->tid, self->uhid_fd); 75 - ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err); 71 + err = setup_uhid(_metadata, &self->hid); 72 + ASSERT_OK(err); 76 73 } 77 74 78 75 struct test_program { ··· 106 129 ops_hid_id = bpf_map__initial_value(map, NULL); 107 130 ASSERT_OK_PTR(ops_hid_id) TH_LOG("unable to retrieve struct_ops data"); 108 131 109 - *ops_hid_id = self->hid_id; 132 + *ops_hid_id = self->hid.hid_id; 110 133 } 111 134 112 135 /* we disable the auto-attach feature of all maps because we ··· 134 157 135 158 hid__attach(self->skel); 136 159 137 - self->hidraw_fd = open_hidraw(self->dev_id); 160 + self->hidraw_fd = open_hidraw(&self->hid); 138 161 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 139 162 } 140 163 ··· 169 192 /* inject one event */ 170 193 buf[0] = 1; 171 194 buf[1] = 42; 172 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 195 + uhid_send_event(_metadata, &self->hid, buf, 6); 173 196 174 197 /* check that hid_first_event() was executed */ 175 198 ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1"); ··· 185 208 memset(buf, 0, sizeof(buf)); 186 209 buf[0] = 1; 187 210 buf[1] = 47; 188 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 211 + uhid_send_event(_metadata, &self->hid, buf, 6); 189 212 190 213 /* check that hid_first_event() was executed */ 191 214 ASSERT_EQ(self->skel->data->callback_check, 47) TH_LOG("callback_check1"); ··· 216 239 /* inject one event */ 217 240 buf[0] = 1; 218 241 buf[1] = 42; 219 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 242 + uhid_send_event(_metadata, &self->hid, buf, 6); 220 243 221 244 /* read the data from hidraw */ 222 245 memset(buf, 0, sizeof(buf)); ··· 229 252 memset(buf, 0, sizeof(buf)); 230 253 buf[0] = 1; 231 254 buf[1] = 47; 232 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 255 + uhid_send_event(_metadata, &self->hid, buf, 6); 233 256 234 257 /* read the data from hidraw */ 235 258 memset(buf, 0, sizeof(buf)); ··· 280 303 /* inject one event */ 281 304 buf[0] = 1; 282 305 buf[1] = 42; 283 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 306 + uhid_send_event(_metadata, &self->hid, buf, 6); 284 307 285 308 /* read the data from hidraw */ 286 309 memset(buf, 0, sizeof(buf)); ··· 303 326 /* detach the program */ 304 327 detach_bpf(self); 305 328 306 - self->hidraw_fd = open_hidraw(self->dev_id); 329 + self->hidraw_fd = open_hidraw(&self->hid); 307 330 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 308 331 309 332 /* inject another event */ 310 333 memset(buf, 0, sizeof(buf)); 311 334 buf[0] = 1; 312 335 buf[1] = 47; 313 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 336 + uhid_send_event(_metadata, &self->hid, buf, 6); 314 337 315 338 /* read the data from hidraw */ 316 339 memset(buf, 0, sizeof(buf)); ··· 329 352 memset(buf, 0, sizeof(buf)); 330 353 buf[0] = 1; 331 354 buf[1] = 42; 332 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 355 + uhid_send_event(_metadata, &self->hid, buf, 6); 333 356 334 357 /* read the data from hidraw */ 335 358 memset(buf, 0, sizeof(buf)); ··· 359 382 /* inject one event */ 360 383 buf[0] = 1; 361 384 buf[1] = 42; 362 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 385 + uhid_send_event(_metadata, &self->hid, buf, 6); 363 386 364 387 /* read the data from hidraw */ 365 388 memset(buf, 0, sizeof(buf)); ··· 389 412 390 413 LOAD_BPF; 391 414 392 - args.hid = self->hid_id; 415 + args.hid = self->hid.hid_id; 393 416 args.data[0] = 1; /* report ID */ 394 417 args.data[1] = 2; /* report ID */ 395 418 args.data[2] = 42; /* report ID */ ··· 435 458 436 459 LOAD_BPF; 437 460 438 - args.hid = self->hid_id; 461 + args.hid = self->hid.hid_id; 439 462 args.data[0] = 1; /* report ID */ 440 463 args.data[1] = 2; /* report ID */ 441 464 args.data[2] = 42; /* report ID */ ··· 483 506 484 507 LOAD_BPF; 485 508 486 - args.hid = self->hid_id; 509 + args.hid = self->hid.hid_id; 487 510 args.data[0] = 1; /* report ID */ 488 511 489 512 prog_fd = bpf_program__fd(self->skel->progs.hid_user_raw_request); ··· 516 539 /* inject one event */ 517 540 buf[0] = 1; 518 541 buf[1] = 42; 519 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 542 + uhid_send_event(_metadata, &self->hid, buf, 6); 520 543 521 544 /* read the data from hidraw */ 522 545 memset(buf, 0, sizeof(buf)); ··· 542 565 /* detach the program */ 543 566 detach_bpf(self); 544 567 545 - self->hidraw_fd = open_hidraw(self->dev_id); 568 + self->hidraw_fd = open_hidraw(&self->hid); 546 569 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 547 570 548 571 err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf); ··· 618 641 /* inject one event */ 619 642 buf[0] = 1; 620 643 buf[1] = 42; 621 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 644 + uhid_send_event(_metadata, &self->hid, buf, 6); 622 645 623 646 /* read the data from hidraw */ 624 647 memset(buf, 0, sizeof(buf)); ··· 644 667 /* detach the program */ 645 668 detach_bpf(self); 646 669 647 - self->hidraw_fd = open_hidraw(self->dev_id); 670 + self->hidraw_fd = open_hidraw(&self->hid); 648 671 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 649 672 650 673 err = write(self->hidraw_fd, buf, 3); ··· 719 742 /* inject one event */ 720 743 buf[0] = 1; 721 744 buf[1] = 42; 722 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 745 + uhid_send_event(_metadata, &self->hid, buf, 6); 723 746 724 747 /* read the data from hidraw */ 725 748 memset(buf, 0, sizeof(buf)); ··· 757 780 /* inject one event */ 758 781 buf[0] = 1; 759 782 buf[1] = 42; 760 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 783 + uhid_send_event(_metadata, &self->hid, buf, 6); 761 784 762 785 /* read the data from hidraw */ 763 786 memset(buf, 0, sizeof(buf)); ··· 793 816 buf[1] = 2; 794 817 buf[2] = 42; 795 818 796 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 819 + uhid_send_event(_metadata, &self->hid, buf, 6); 797 820 798 821 /* read the data from hidraw */ 799 822 memset(buf, 0, sizeof(buf)); ··· 844 867 845 868 /* inject one event */ 846 869 buf[0] = 1; 847 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 870 + uhid_send_event(_metadata, &self->hid, buf, 6); 848 871 849 872 /* read the data from hidraw */ 850 873 memset(buf, 0, sizeof(buf));
+50 -24
tools/testing/selftests/hid/hid_common.h
··· 19 19 __typeof__(b) _b = (b); \ 20 20 _a < _b ? _a : _b; }) 21 21 22 + struct uhid_device { 23 + int dev_id; /* uniq (random) number to identify the device */ 24 + int uhid_fd; 25 + int hid_id; /* HID device id in the system */ 26 + pthread_t tid; /* thread for reading uhid events */ 27 + }; 28 + 22 29 static unsigned char rdesc[] = { 23 30 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 24 31 0x09, 0x21, /* Usage (Vendor Usage 0x21) */ ··· 153 146 return uhid_write(_metadata, fd, &ev); 154 147 } 155 148 156 - static void uhid_destroy(struct __test_metadata *_metadata, int fd) 149 + static void uhid_destroy(struct __test_metadata *_metadata, struct uhid_device *hid) 157 150 { 158 151 struct uhid_event ev; 159 152 160 153 memset(&ev, 0, sizeof(ev)); 161 154 ev.type = UHID_DESTROY; 162 155 163 - uhid_write(_metadata, fd, &ev); 156 + uhid_write(_metadata, hid->uhid_fd, &ev); 164 157 } 165 158 166 159 static int uhid_event(struct __test_metadata *_metadata, int fd) ··· 288 281 return 0; 289 282 } 290 283 291 - static int uhid_send_event(struct __test_metadata *_metadata, int fd, __u8 *buf, size_t size) 284 + static int uhid_send_event(struct __test_metadata *_metadata, struct uhid_device *hid, 285 + __u8 *buf, size_t size) 292 286 { 293 287 struct uhid_event ev; 294 288 ··· 302 294 303 295 memcpy(ev.u.input2.data, buf, size); 304 296 305 - return uhid_write(_metadata, fd, &ev); 306 - } 307 - 308 - static int setup_uhid(struct __test_metadata *_metadata, int rand_nb) 309 - { 310 - int fd; 311 - const char *path = "/dev/uhid"; 312 - int ret; 313 - 314 - fd = open(path, O_RDWR | O_CLOEXEC); 315 - ASSERT_GE(fd, 0) TH_LOG("open uhid-cdev failed; %d", fd); 316 - 317 - ret = uhid_create(_metadata, fd, rand_nb); 318 - ASSERT_EQ(0, ret) { 319 - TH_LOG("create uhid device failed: %d", ret); 320 - close(fd); 321 - } 322 - 323 - return fd; 297 + return uhid_write(_metadata, hid->uhid_fd, &ev); 324 298 } 325 299 326 300 static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir) ··· 411 421 return found; 412 422 } 413 423 414 - static int open_hidraw(int dev_id) 424 + static int open_hidraw(struct uhid_device *hid) 415 425 { 416 426 int hidraw_number; 417 427 char hidraw_path[64] = { 0 }; 418 428 419 - hidraw_number = get_hidraw(dev_id); 429 + hidraw_number = get_hidraw(hid->dev_id); 420 430 if (hidraw_number < 0) 421 431 return hidraw_number; 422 432 423 433 /* open hidraw node to check the other side of the pipe */ 424 434 sprintf(hidraw_path, "/dev/hidraw%d", hidraw_number); 425 435 return open(hidraw_path, O_RDWR | O_NONBLOCK); 436 + } 437 + 438 + static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid) 439 + { 440 + const char *path = "/dev/uhid"; 441 + time_t t; 442 + int ret; 443 + 444 + /* initialize random number generator */ 445 + srand((unsigned int)time(&t)); 446 + 447 + hid->dev_id = rand() % 1024; 448 + 449 + hid->uhid_fd = open(path, O_RDWR | O_CLOEXEC); 450 + ASSERT_GE(hid->uhid_fd, 0) TH_LOG("open uhid-cdev failed; %d", hid->uhid_fd); 451 + 452 + ret = uhid_create(_metadata, hid->uhid_fd, hid->dev_id); 453 + ASSERT_EQ(0, ret) { 454 + TH_LOG("create uhid device failed: %d", ret); 455 + close(hid->uhid_fd); 456 + return ret; 457 + } 458 + 459 + /* locate the uevent file of the created device */ 460 + hid->hid_id = get_hid_id(hid->dev_id); 461 + ASSERT_GT(hid->hid_id, 0) 462 + TH_LOG("Could not locate uhid device id: %d", hid->hid_id); 463 + 464 + ret = uhid_start_listener(_metadata, &hid->tid, hid->uhid_fd); 465 + ASSERT_EQ(0, ret) { 466 + TH_LOG("could not start udev listener: %d", ret); 467 + close(hid->uhid_fd); 468 + return ret; 469 + } 470 + 471 + return 0; 426 472 }
+10 -26
tools/testing/selftests/hid/hidraw.c
··· 9 9 #endif /* HIDIOCREVOKE */ 10 10 11 11 FIXTURE(hidraw) { 12 - int dev_id; 13 - int uhid_fd; 12 + struct uhid_device hid; 14 13 int hidraw_fd; 15 - int hid_id; 16 - pthread_t tid; 17 14 }; 18 15 static void close_hidraw(FIXTURE_DATA(hidraw) * self) 19 16 { ··· 22 25 FIXTURE_TEARDOWN(hidraw) { 23 26 void *uhid_err; 24 27 25 - uhid_destroy(_metadata, self->uhid_fd); 28 + uhid_destroy(_metadata, &self->hid); 26 29 27 30 close_hidraw(self); 28 - pthread_join(self->tid, &uhid_err); 31 + pthread_join(self->hid.tid, &uhid_err); 29 32 } 30 33 #define TEARDOWN_LOG(fmt, ...) do { \ 31 34 TH_LOG(fmt, ##__VA_ARGS__); \ ··· 34 37 35 38 FIXTURE_SETUP(hidraw) 36 39 { 37 - time_t t; 38 40 int err; 39 41 40 - /* initialize random number generator */ 41 - srand((unsigned int)time(&t)); 42 + err = setup_uhid(_metadata, &self->hid); 43 + ASSERT_OK(err); 42 44 43 - self->dev_id = rand() % 1024; 44 - 45 - self->uhid_fd = setup_uhid(_metadata, self->dev_id); 46 - 47 - /* locate the uev, self, variant);ent file of the created device */ 48 - self->hid_id = get_hid_id(self->dev_id); 49 - ASSERT_GT(self->hid_id, 0) 50 - TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id); 51 - 52 - err = uhid_start_listener(_metadata, &self->tid, self->uhid_fd); 53 - ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err); 54 - 55 - self->hidraw_fd = open_hidraw(self->dev_id); 45 + self->hidraw_fd = open_hidraw(&self->hid); 56 46 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 57 47 } 58 48 ··· 63 79 /* inject one event */ 64 80 buf[0] = 1; 65 81 buf[1] = 42; 66 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 82 + uhid_send_event(_metadata, &self->hid, buf, 6); 67 83 68 84 /* read the data from hidraw */ 69 85 memset(buf, 0, sizeof(buf)); ··· 85 101 /* inject one event */ 86 102 buf[0] = 1; 87 103 buf[1] = 42; 88 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 104 + uhid_send_event(_metadata, &self->hid, buf, 6); 89 105 90 106 /* read the data from hidraw */ 91 107 memset(buf, 0, sizeof(buf)); ··· 101 117 /* inject one other event */ 102 118 buf[0] = 1; 103 119 buf[1] = 43; 104 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 120 + uhid_send_event(_metadata, &self->hid, buf, 6); 105 121 106 122 /* read the data from hidraw */ 107 123 memset(buf, 0, sizeof(buf)); ··· 145 161 /* inject one event */ 146 162 buf[0] = 1; 147 163 buf[1] = 42; 148 - uhid_send_event(_metadata, self->uhid_fd, buf, 6); 164 + uhid_send_event(_metadata, &self->hid, buf, 6); 149 165 150 166 while (true) { 151 167 ready = poll(pfds, 1, 5000);