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