-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlinux_backend_test_hooks.cpp
More file actions
2628 lines (2292 loc) · 95.4 KB
/
Copy pathlinux_backend_test_hooks.cpp
File metadata and controls
2628 lines (2292 loc) · 95.4 KB
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
/**
* @file tests/fixtures/linux_backend_test_hooks.cpp
* @brief Linux backend test hook definitions.
*/
// standard includes
#include <algorithm>
#include <array>
#include <atomic>
#include <bit>
#include <cerrno>
#include <chrono>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <map>
#include <memory>
#include <mutex>
#include <numbers>
#include <optional>
#include <set>
#include <span>
#include <sstream>
#include <string>
#include <string_view>
#include <system_error>
#include <thread>
#include <utility>
#include <vector>
// platform includes
#if defined(__linux__)
#include <fcntl.h>
#ifndef __user
#define __user
#endif
#include <linux/input.h>
#include <linux/uhid.h>
#include <linux/uinput.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#if defined(LIBVIRTUALHID_HAVE_XTEST)
#include <X11/extensions/XTest.h>
#include <X11/keysym.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif
#endif
// lib includes
#if defined(__linux__)
#include <libevdev/libevdev-uinput.h>
#include <libevdev/libevdev.h>
#endif
// local includes
#include "fixtures/linux_backend_test_hooks.hpp"
#if defined(__linux__)
namespace lvh::detail::test {
namespace {
struct FakeLibevdevDevice {
std::string name;
std::uint16_t bustype = 0;
std::uint16_t vendor = 0;
std::uint16_t product = 0;
std::uint16_t version = 0;
std::vector<std::uint32_t> event_types;
std::vector<LinuxLibevdevEventCode> event_codes;
std::vector<std::uint32_t> properties;
};
struct FakeLibevdevUinput {
FakeLibevdevDevice device;
};
template<typename Target, typename Source>
Target opaque_test_handle(Source *source) noexcept {
static_assert(sizeof(Target) == sizeof(source), "opaque test handles must preserve pointer size");
return std::bit_cast<Target>(source);
}
libevdev *libevdev_handle(FakeLibevdevDevice *device) noexcept {
return opaque_test_handle<libevdev *>(device);
}
FakeLibevdevDevice *fake_libevdev_device(libevdev *device) noexcept {
return opaque_test_handle<FakeLibevdevDevice *>(device);
}
const FakeLibevdevDevice *fake_libevdev_device(const libevdev *device) noexcept {
return opaque_test_handle<const FakeLibevdevDevice *>(device);
}
libevdev_uinput *libevdev_uinput_handle(FakeLibevdevUinput *device) noexcept {
return opaque_test_handle<libevdev_uinput *>(device);
}
FakeLibevdevUinput *fake_libevdev_uinput(libevdev_uinput *device) noexcept {
return opaque_test_handle<FakeLibevdevUinput *>(device);
}
#if defined(LIBVIRTUALHID_HAVE_XTEST)
Display *fake_display_handle() noexcept {
static std::byte display_handle {};
return opaque_test_handle<Display *>(&display_handle);
}
#endif
struct LinuxTestSyscalls { // NOSONAR(cpp:S1820): Test-only fake syscall state keeps related Linux hook controls visible at call sites.
bool override_access = false;
int access_result = 0;
bool override_open = false;
int open_result = 100000;
int last_open_flags = 0;
bool override_write = false;
std::atomic_int write_call_count = 0;
int fail_write_call = -1;
int short_write_call = -1;
std::size_t short_write_size = 1;
bool override_ioctl = false;
std::atomic_int ioctl_call_count = 0;
int fail_ioctl_call = -1;
bool override_poll = false;
std::atomic_int poll_call_count = 0;
std::vector<int> poll_results;
std::vector<short> poll_revents;
std::vector<int> poll_errors;
bool override_read = false;
std::atomic_int read_call_count = 0;
std::vector<std::ptrdiff_t> read_results;
std::vector<int> read_errors;
uhid_event read_event {};
std::vector<uhid_event> read_events;
std::vector<input_event> read_input_events;
ff_effect uploaded_ff_effect {};
std::vector<ff_effect> uploaded_ff_effects;
std::atomic_size_t uploaded_ff_effect_index = 0;
bool provide_uploaded_ff_effect = false;
bool override_xtest_query = false;
bool xtest_query_result = true;
bool override_x_keycode = false;
std::atomic_int x_keycode_call_count = 0;
int fail_x_keycode_call = -1;
bool override_libevdev = false;
bool libevdev_new_returns_null = false;
bool fail_libevdev_event_type = false;
bool fail_libevdev_event_code = false;
bool fail_libevdev_property = false;
bool fail_libevdev_create = false;
std::vector<std::unique_ptr<FakeLibevdevDevice>> owned_libevdev_devices;
std::vector<std::unique_ptr<FakeLibevdevUinput>> owned_libevdev_uinput_devices;
std::vector<FakeLibevdevDevice> libevdev_devices;
std::size_t libevdev_destroy_count = 0;
};
LinuxTestSyscalls *&active_test_syscalls() {
static LinuxTestSyscalls *syscalls = nullptr;
return syscalls;
}
class ScopedLinuxTestSyscalls {
public:
explicit ScopedLinuxTestSyscalls(LinuxTestSyscalls &syscalls):
previous_ {active_test_syscalls()} {
active_test_syscalls() = &syscalls;
}
ScopedLinuxTestSyscalls(const ScopedLinuxTestSyscalls &) = delete;
ScopedLinuxTestSyscalls &operator=(const ScopedLinuxTestSyscalls &) = delete;
ScopedLinuxTestSyscalls(ScopedLinuxTestSyscalls &&) noexcept = delete;
ScopedLinuxTestSyscalls &operator=(ScopedLinuxTestSyscalls &&) noexcept = delete;
~ScopedLinuxTestSyscalls() {
active_test_syscalls() = previous_;
}
private:
LinuxTestSyscalls *previous_ = nullptr;
};
} // namespace
} // namespace lvh::detail::test
namespace {
constexpr auto free_real_libevdev = &::libevdev_free;
constexpr auto destroy_real_libevdev_uinput = &::libevdev_uinput_destroy;
} // namespace
int lvh_linux_test_access(const char *path, int mode) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_access) {
if (lvh::detail::test::active_test_syscalls()->access_result < 0) {
errno = EACCES;
}
return lvh::detail::test::active_test_syscalls()->access_result;
}
return ::access(path, mode);
}
int lvh_linux_test_open(const char *path, int flags) {
if (lvh::detail::test::active_test_syscalls() != nullptr) {
lvh::detail::test::active_test_syscalls()->last_open_flags = flags;
}
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_open) {
if (lvh::detail::test::active_test_syscalls()->open_result < 0) {
errno = ENOENT;
return lvh::detail::test::active_test_syscalls()->open_result;
}
const auto fd = ::open("/dev/null", O_RDWR);
if (fd < 0) {
errno = EIO;
}
return fd;
}
return ::open(path, flags);
}
std::ptrdiff_t lvh_linux_test_write(int fd, const std::byte *buffer, std::size_t size) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_write) {
const auto call_count = ++lvh::detail::test::active_test_syscalls()->write_call_count;
if (lvh::detail::test::active_test_syscalls()->fail_write_call == call_count) {
errno = EIO;
return -1;
}
if (lvh::detail::test::active_test_syscalls()->short_write_call == call_count) {
return static_cast<std::ptrdiff_t>(lvh::detail::test::active_test_syscalls()->short_write_size);
}
return static_cast<std::ptrdiff_t>(size);
}
return static_cast<std::ptrdiff_t>(::write(fd, buffer, size));
}
int lvh_linux_test_ioctl(int fd, unsigned long request, unsigned long argument) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_ioctl) {
if (const auto call_count = ++lvh::detail::test::active_test_syscalls()->ioctl_call_count; lvh::detail::test::active_test_syscalls()->fail_ioctl_call == call_count) {
errno = EINVAL;
return -1;
}
if (
request == UI_BEGIN_FF_UPLOAD &&
lvh::detail::test::active_test_syscalls()->provide_uploaded_ff_effect
) {
auto *upload = std::bit_cast<uinput_ff_upload *>(argument);
auto &syscalls = *lvh::detail::test::active_test_syscalls();
const auto upload_index = syscalls.uploaded_ff_effect_index++;
upload->effect = upload_index < syscalls.uploaded_ff_effects.size() ?
syscalls.uploaded_ff_effects[upload_index] :
syscalls.uploaded_ff_effect;
}
return 0;
}
return ::ioctl(fd, request, argument);
}
int lvh_linux_test_poll(pollfd *descriptors, nfds_t descriptor_count, int timeout) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_poll) {
const auto call_index = static_cast<std::size_t>(lvh::detail::test::active_test_syscalls()->poll_call_count++);
auto result = 0;
if (call_index < lvh::detail::test::active_test_syscalls()->poll_results.size()) {
result = lvh::detail::test::active_test_syscalls()->poll_results[call_index];
}
if (descriptor_count > 0) {
descriptors[0].revents = 0;
if (result > 0 && call_index < lvh::detail::test::active_test_syscalls()->poll_revents.size()) {
descriptors[0].revents = lvh::detail::test::active_test_syscalls()->poll_revents[call_index];
}
}
if (result < 0) {
errno = call_index < lvh::detail::test::active_test_syscalls()->poll_errors.size() ?
lvh::detail::test::active_test_syscalls()->poll_errors[call_index] :
EIO;
}
return result;
}
return ::poll(descriptors, descriptor_count, timeout);
}
std::ptrdiff_t lvh_linux_test_read(int fd, std::byte *buffer, std::size_t size) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_read) {
const auto call_index = static_cast<std::size_t>(lvh::detail::test::active_test_syscalls()->read_call_count++);
auto result = std::ptrdiff_t {0};
if (call_index < lvh::detail::test::active_test_syscalls()->read_results.size()) {
result = lvh::detail::test::active_test_syscalls()->read_results[call_index];
}
if (result < 0) {
errno = call_index < lvh::detail::test::active_test_syscalls()->read_errors.size() ?
lvh::detail::test::active_test_syscalls()->read_errors[call_index] :
EIO;
return result;
}
if (result > 0) {
if (const auto &input_events = lvh::detail::test::active_test_syscalls()->read_input_events; call_index < input_events.size()) {
const auto bytes = std::min<std::size_t>(static_cast<std::size_t>(result), std::min(size, sizeof(input_event)));
std::memcpy(buffer, &input_events[call_index], bytes);
return static_cast<std::ptrdiff_t>(bytes);
}
const auto bytes = std::min<std::size_t>(static_cast<std::size_t>(result), std::min(size, sizeof(uhid_event)));
const auto &read_events = lvh::detail::test::active_test_syscalls()->read_events;
const auto &event = call_index < read_events.size() ?
read_events[call_index] :
lvh::detail::test::active_test_syscalls()->read_event;
std::memcpy(buffer, &event, bytes);
return static_cast<std::ptrdiff_t>(bytes);
}
return result;
}
return static_cast<std::ptrdiff_t>(::read(fd, buffer, size));
}
#if defined(LIBVIRTUALHID_HAVE_XTEST)
Display *lvh_linux_test_x_open_display(const char *) {
return lvh::detail::test::fake_display_handle();
}
int lvh_linux_test_x_close_display(Display *) {
return 0;
}
Bool lvh_linux_test_xtest_query_extension(Display *, int *, int *, int *, int *) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_xtest_query) {
return lvh::detail::test::active_test_syscalls()->xtest_query_result ? True : False;
}
return True;
}
KeyCode lvh_linux_test_x_keysym_to_keycode(Display *, KeySym keysym) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_x_keycode) {
const auto call_count = ++lvh::detail::test::active_test_syscalls()->x_keycode_call_count;
if (lvh::detail::test::active_test_syscalls()->fail_x_keycode_call == call_count) {
return 0;
}
}
return keysym == NoSymbol ? 0 : 1;
}
int lvh_linux_test_x_flush(Display *) {
return 0;
}
int lvh_linux_test_default_screen(Display *) {
return 0;
}
int lvh_linux_test_display_width(Display *, int) {
return 1920;
}
int lvh_linux_test_display_height(Display *, int) {
return 1080;
}
int lvh_linux_test_xtest_fake_key_event(Display *, unsigned int, Bool, unsigned long) {
return 1;
}
int lvh_linux_test_xtest_fake_button_event(Display *, unsigned int, Bool, unsigned long) {
return 1;
}
int lvh_linux_test_xtest_fake_motion_event(Display *, int, int, int, unsigned long) {
return 1;
}
int lvh_linux_test_xtest_fake_relative_motion_event(Display *, int, int, unsigned long) {
return 1;
}
#endif
extern "C" libevdev *lvh_linux_test_libevdev_new() {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
if (lvh::detail::test::active_test_syscalls()->libevdev_new_returns_null) {
return nullptr;
}
auto fake_device = std::make_unique<lvh::detail::test::FakeLibevdevDevice>();
auto *created_device = fake_device.get();
lvh::detail::test::active_test_syscalls()->owned_libevdev_devices.push_back(std::move(fake_device));
return lvh::detail::test::libevdev_handle(created_device);
}
return ::libevdev_new();
}
extern "C" void lvh_linux_test_libevdev_free(libevdev *raw_libevdev_handle) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
auto *fake_device_handle = lvh::detail::test::fake_libevdev_device(raw_libevdev_handle);
static_cast<void>(std::erase_if(
lvh::detail::test::active_test_syscalls()->owned_libevdev_devices,
[fake_device_handle](const auto &owned_device) {
return owned_device.get() == fake_device_handle;
}
));
return;
}
free_real_libevdev(raw_libevdev_handle);
}
extern "C" void lvh_linux_test_libevdev_set_name(libevdev *device, const char *name) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
lvh::detail::test::fake_libevdev_device(device)->name = name == nullptr ? "" : name;
return;
}
::libevdev_set_name(device, name);
}
extern "C" void lvh_linux_test_libevdev_set_id_bustype(libevdev *device, int bustype) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
lvh::detail::test::fake_libevdev_device(device)->bustype = static_cast<std::uint16_t>(bustype);
return;
}
::libevdev_set_id_bustype(device, bustype);
}
extern "C" void lvh_linux_test_libevdev_set_id_vendor(libevdev *device, int vendor) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
lvh::detail::test::fake_libevdev_device(device)->vendor = static_cast<std::uint16_t>(vendor);
return;
}
::libevdev_set_id_vendor(device, vendor);
}
extern "C" void lvh_linux_test_libevdev_set_id_product(libevdev *device, int product) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
lvh::detail::test::fake_libevdev_device(device)->product = static_cast<std::uint16_t>(product);
return;
}
::libevdev_set_id_product(device, product);
}
extern "C" void lvh_linux_test_libevdev_set_id_version(libevdev *device, int version) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
lvh::detail::test::fake_libevdev_device(device)->version = static_cast<std::uint16_t>(version);
return;
}
::libevdev_set_id_version(device, version);
}
extern "C" int lvh_linux_test_libevdev_enable_event_type(libevdev *device, unsigned int type) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
if (lvh::detail::test::active_test_syscalls()->fail_libevdev_event_type) {
return -EIO;
}
lvh::detail::test::fake_libevdev_device(device)->event_types.push_back(type);
return 0;
}
return ::libevdev_enable_event_type(device, type);
}
extern "C" int lvh_linux_test_libevdev_enable_event_code(
libevdev *device,
unsigned int type,
unsigned int code,
const void *data
) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
if (lvh::detail::test::active_test_syscalls()->fail_libevdev_event_code) {
return -EIO;
}
lvh::detail::test::LinuxLibevdevEventCode event_code {
.type = type,
.code = code,
};
if (type == EV_ABS && data != nullptr) {
const auto *absinfo = static_cast<const input_absinfo *>(data);
event_code.has_absinfo = true;
event_code.minimum = absinfo->minimum;
event_code.maximum = absinfo->maximum;
event_code.fuzz = absinfo->fuzz;
event_code.flat = absinfo->flat;
event_code.resolution = absinfo->resolution;
}
lvh::detail::test::fake_libevdev_device(device)->event_codes.push_back(event_code);
return 0;
}
return ::libevdev_enable_event_code(device, type, code, data);
}
extern "C" int lvh_linux_test_libevdev_enable_property(libevdev *device, unsigned int property) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
if (lvh::detail::test::active_test_syscalls()->fail_libevdev_property) {
return -EIO;
}
lvh::detail::test::fake_libevdev_device(device)->properties.push_back(property);
return 0;
}
return ::libevdev_enable_property(device, property);
}
extern "C" int lvh_linux_test_libevdev_uinput_create_from_device(
const libevdev *device,
int uinput_fd,
libevdev_uinput **uinput_device
) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
if (lvh::detail::test::active_test_syscalls()->fail_libevdev_create || uinput_fd < 0) {
return -EIO;
}
const auto &source_device = *lvh::detail::test::fake_libevdev_device(device);
lvh::detail::test::active_test_syscalls()->libevdev_devices.push_back(source_device);
auto owned_uinput_device =
std::make_unique<lvh::detail::test::FakeLibevdevUinput>(lvh::detail::test::FakeLibevdevUinput {source_device});
auto *created_uinput_device = owned_uinput_device.get();
lvh::detail::test::active_test_syscalls()->owned_libevdev_uinput_devices.push_back(std::move(owned_uinput_device));
*uinput_device = lvh::detail::test::libevdev_uinput_handle(created_uinput_device);
return 0;
}
return ::libevdev_uinput_create_from_device(device, uinput_fd, uinput_device);
}
extern "C" void lvh_linux_test_libevdev_uinput_destroy(libevdev_uinput *raw_uinput_handle) {
if (lvh::detail::test::active_test_syscalls() != nullptr && lvh::detail::test::active_test_syscalls()->override_libevdev) {
++lvh::detail::test::active_test_syscalls()->libevdev_destroy_count;
auto *fake_uinput_handle = lvh::detail::test::fake_libevdev_uinput(raw_uinput_handle);
static_cast<void>(std::erase_if(
lvh::detail::test::active_test_syscalls()->owned_libevdev_uinput_devices,
[fake_uinput_handle](const auto &owned_device) {
return owned_device.get() == fake_uinput_handle;
}
));
return;
}
destroy_real_libevdev_uinput(raw_uinput_handle);
}
#define access lvh_linux_test_access
#define ioctl lvh_linux_test_ioctl
#define open lvh_linux_test_open
#define poll lvh_linux_test_poll
#define read lvh_linux_test_read
#define write lvh_linux_test_write
#define libevdev_enable_event_code lvh_linux_test_libevdev_enable_event_code
#define libevdev_enable_event_type lvh_linux_test_libevdev_enable_event_type
#define libevdev_enable_property lvh_linux_test_libevdev_enable_property
#define libevdev_free lvh_linux_test_libevdev_free
#define libevdev_new lvh_linux_test_libevdev_new
#define libevdev_set_id_bustype lvh_linux_test_libevdev_set_id_bustype
#define libevdev_set_id_product lvh_linux_test_libevdev_set_id_product
#define libevdev_set_id_vendor lvh_linux_test_libevdev_set_id_vendor
#define libevdev_set_id_version lvh_linux_test_libevdev_set_id_version
#define libevdev_set_name lvh_linux_test_libevdev_set_name
#define libevdev_uinput_create_from_device lvh_linux_test_libevdev_uinput_create_from_device
#define libevdev_uinput_destroy lvh_linux_test_libevdev_uinput_destroy
#if defined(LIBVIRTUALHID_HAVE_XTEST)
#if defined(DefaultScreen)
#undef DefaultScreen // NOSONAR(cpp:S959): X11 exposes this as a macro; the test hook replaces it while including the backend.
#endif
#if defined(DisplayHeight)
#undef DisplayHeight // NOSONAR(cpp:S959): X11 exposes this as a macro; the test hook replaces it while including the backend.
#endif
#if defined(DisplayWidth)
#undef DisplayWidth // NOSONAR(cpp:S959): X11 exposes this as a macro; the test hook replaces it while including the backend.
#endif
#define DefaultScreen lvh_linux_test_default_screen
#define DisplayHeight lvh_linux_test_display_height
#define DisplayWidth lvh_linux_test_display_width
#define XCloseDisplay lvh_linux_test_x_close_display
#define XFlush lvh_linux_test_x_flush
#define XKeysymToKeycode lvh_linux_test_x_keysym_to_keycode
#define XOpenDisplay lvh_linux_test_x_open_display
#define XTestFakeButtonEvent lvh_linux_test_xtest_fake_button_event
#define XTestFakeKeyEvent lvh_linux_test_xtest_fake_key_event
#define XTestFakeMotionEvent lvh_linux_test_xtest_fake_motion_event
#define XTestFakeRelativeMotionEvent lvh_linux_test_xtest_fake_relative_motion_event
#define XTestQueryExtension lvh_linux_test_xtest_query_extension
#endif
#define create_platform_backend create_platform_backend_for_linux_backend_test_hooks
#include "../../src/platform/linux/uhid_backend.cpp"
#undef create_platform_backend
#if defined(LIBVIRTUALHID_HAVE_XTEST)
#undef XTestQueryExtension
#undef XTestFakeRelativeMotionEvent
#undef XTestFakeMotionEvent
#undef XTestFakeKeyEvent
#undef XTestFakeButtonEvent
#undef XOpenDisplay
#undef XKeysymToKeycode
#undef XFlush
#undef XCloseDisplay
#undef DisplayWidth
#undef DisplayHeight
#undef DefaultScreen
#endif
#undef write
#undef read
#undef poll
#undef open
#undef ioctl
#undef access
#undef libevdev_uinput_destroy
#undef libevdev_uinput_create_from_device
#undef libevdev_set_name
#undef libevdev_set_id_version
#undef libevdev_set_id_vendor
#undef libevdev_set_id_product
#undef libevdev_set_id_bustype
#undef libevdev_new
#undef libevdev_free
#undef libevdev_enable_property
#undef libevdev_enable_event_type
#undef libevdev_enable_event_code
namespace lvh::detail::test {
namespace {
constexpr auto fake_fd = 100000;
int open_test_fd() {
return ::open("/dev/null", O_RDWR);
}
std::vector<LinuxInputEventRecord> read_input_events_until_eof(int fd) {
std::vector<LinuxInputEventRecord> records;
input_event event {};
while (::read(fd, &event, sizeof(event)) == sizeof(event)) {
records.push_back({
.type = event.type,
.code = event.code,
.value = event.value,
});
}
return records;
}
bool write_uhid_event(int fd, const uhid_event &event) {
const auto data = std::as_bytes(std::span {&event, 1U});
std::size_t written = 0;
while (written < data.size()) {
const auto result = ::write(fd, data.data() + written, data.size() - written);
if (result <= 0) {
return false;
}
written += static_cast<std::size_t>(result);
}
return true;
}
bool read_uhid_event(int fd, uhid_event &event) {
pollfd descriptor {};
descriptor.fd = fd;
descriptor.events = POLLIN;
if (const auto poll_result = ::poll(&descriptor, 1, 1000); poll_result <= 0 || (descriptor.revents & POLLIN) == 0) {
return false;
}
const auto data = std::as_writable_bytes(std::span {&event, 1U});
std::size_t read_size = 0;
while (read_size < data.size()) {
const auto result = ::read(fd, data.data() + read_size, data.size() - read_size);
if (result <= 0) {
return false;
}
read_size += static_cast<std::size_t>(result);
}
return true;
}
bool read_uhid_event_type(int fd, unsigned int event_type, uhid_event &event) {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds {1};
while (std::chrono::steady_clock::now() < deadline) {
if (!read_uhid_event(fd, event)) {
return false;
}
if (event.type == event_type) {
return true;
}
}
return false;
}
OperationStatus create_started_uhid_gamepad(
UhidGamepad &gamepad,
DeviceId id,
const CreateGamepadOptions &options,
int peer_fd,
uhid_event &create_event,
bool &saw_create,
bool &waited_for_start
) {
auto create_status = OperationStatus::failure(ErrorCode::backend_failure, "UHID create thread did not run");
std::atomic_bool create_returned = false;
std::jthread create_thread {[&]() {
create_status = gamepad.create(id, options);
create_returned = true;
}};
saw_create = read_uhid_event_type(peer_fd, UHID_CREATE2, create_event);
std::this_thread::sleep_for(std::chrono::milliseconds {20});
waited_for_start = !create_returned;
uhid_event start_event {};
start_event.type = UHID_START;
static_cast<void>(write_uhid_event(peer_fd, start_event));
create_thread.join();
return create_status;
}
uhid_event create_started_profile_uhid_gamepad(
UhidGamepad &gamepad,
DeviceId id,
const CreateGamepadOptions &options,
int peer_fd,
std::uint16_t expected_bus,
LinuxUhidRoundTripResult &result
) {
uhid_event event {};
result.create_status = create_started_uhid_gamepad(
gamepad,
id,
options,
peer_fd,
event,
result.creation.saw_create,
result.creation.waited_for_start
);
if (result.creation.saw_create) {
result.creation.saw_create = event.u.create2.vendor == options.profile.vendor_id &&
event.u.create2.product == options.profile.product_id &&
event.u.create2.bus == expected_bus;
result.creation.name = reinterpret_cast<const char *>(event.u.create2.name);
}
return event;
}
std::uint32_t read_u32_le(const std::uint8_t *buffer) {
return static_cast<std::uint32_t>(buffer[0]) |
(static_cast<std::uint32_t>(buffer[1]) << 8U) |
(static_cast<std::uint32_t>(buffer[2]) << 16U) |
(static_cast<std::uint32_t>(buffer[3]) << 24U);
}
void enable_fake_device_syscalls(LinuxTestSyscalls &syscalls) {
syscalls.override_access = true;
syscalls.override_open = true;
syscalls.override_write = true;
syscalls.override_ioctl = true;
syscalls.override_libevdev = true;
}
bool wait_for_poll_calls(const LinuxTestSyscalls &syscalls, int expected_calls) {
using namespace std::chrono_literals;
for (auto attempt = 0; attempt < 100; ++attempt) {
if (syscalls.poll_call_count.load() >= expected_calls) {
return true;
}
std::this_thread::sleep_for(1ms);
}
return false;
}
OperationStatus run_fake_uhid_read_loop(LinuxTestSyscalls &syscalls, int expected_poll_calls) {
syscalls.override_write = true;
syscalls.override_poll = true;
syscalls.override_read = true;
syscalls.poll_results.insert(syscalls.poll_results.begin(), 1);
syscalls.poll_revents.insert(syscalls.poll_revents.begin(), POLLIN);
syscalls.poll_errors.insert(syscalls.poll_errors.begin(), 0);
syscalls.read_results.insert(syscalls.read_results.begin(), static_cast<std::ptrdiff_t>(sizeof(uhid_event)));
syscalls.read_errors.insert(syscalls.read_errors.begin(), 0);
uhid_event start_event {};
start_event.type = UHID_START;
syscalls.read_events.insert(syscalls.read_events.begin(), start_event);
ScopedLinuxTestSyscalls scoped_syscalls {syscalls};
CreateGamepadOptions options;
options.profile = profiles::generic_gamepad();
const auto fd = open_test_fd();
if (fd < 0) {
return system_error_status(ErrorCode::backend_failure, "failed to open test file descriptor", errno);
}
UhidGamepad gamepad {fd};
if (const auto status = gamepad.create(1, options); !status.ok()) {
return status;
}
const auto saw_expected_polls = wait_for_poll_calls(syscalls, expected_poll_calls + 1);
const auto close_status = gamepad.close();
if (!saw_expected_polls) {
return OperationStatus::failure(ErrorCode::backend_failure, "fake UHID read loop did not consume the scripted poll calls");
}
return close_status;
}
DeviceProfile profile_for_uinput_device_type(DeviceType device_type) {
switch (device_type) {
case DeviceType::keyboard:
return profiles::keyboard();
case DeviceType::mouse:
return profiles::mouse();
case DeviceType::touchscreen:
return profiles::touchscreen();
case DeviceType::trackpad:
return profiles::trackpad();
case DeviceType::pen_tablet:
return profiles::pen_tablet();
case DeviceType::gamepad:
return profiles::xbox_series();
}
return profiles::mouse();
}
OperationStatus create_uinput_device_by_type(
int fd,
DeviceType device_type,
std::optional<GamepadProfileKind> gamepad_kind = std::nullopt
) {
switch (device_type) {
case DeviceType::keyboard:
{
CreateKeyboardOptions options;
options.profile = profile_for_uinput_device_type(device_type);
UinputKeyboard keyboard {fd};
return keyboard.create(1, options);
}
case DeviceType::mouse:
{
CreateMouseOptions options;
options.profile = profile_for_uinput_device_type(device_type);
UinputMouse mouse {fd};
return mouse.create(1, options);
}
case DeviceType::touchscreen:
{
CreateTouchscreenOptions options;
options.profile = profile_for_uinput_device_type(device_type);
UinputTouchscreen touchscreen {fd};
return touchscreen.create(1, options);
}
case DeviceType::trackpad:
{
CreateTrackpadOptions options;
options.profile = profile_for_uinput_device_type(device_type);
UinputTrackpad trackpad {fd};
return trackpad.create(1, options);
}
case DeviceType::pen_tablet:
{
CreatePenTabletOptions options;
options.profile = profile_for_uinput_device_type(device_type);
UinputPenTablet pen_tablet {fd};
return pen_tablet.create(1, options);
}
case DeviceType::gamepad:
{
CreateGamepadOptions options;
options.profile = gamepad_kind ? *profiles::gamepad_profile(*gamepad_kind) : profile_for_uinput_device_type(device_type);
UinputGamepad gamepad {fd, options.profile.gamepad_kind};
return gamepad.create(1, options);
}
}
return OperationStatus::failure(ErrorCode::unsupported_profile, "unsupported fake uinput device type");
}
template<typename ConfigureFailure>
LinuxLibevdevCreationResult create_fake_libevdev_device(
DeviceType device_type,
ConfigureFailure configure_failure,
std::optional<GamepadProfileKind> gamepad_kind = std::nullopt
) {
LinuxTestSyscalls syscalls;
syscalls.override_libevdev = true;
configure_failure(syscalls);
ScopedLinuxTestSyscalls scoped_syscalls {syscalls};
LinuxLibevdevCreationResult result;
const auto fd = open_test_fd();
if (fd < 0) {
result.status = system_error_status(ErrorCode::backend_failure, "failed to open test file descriptor", errno);
return result;
}
result.status = create_uinput_device_by_type(fd, device_type, gamepad_kind);
if (!syscalls.libevdev_devices.empty()) {
const auto &device = syscalls.libevdev_devices.back();
result.name = device.name;
result.bustype = device.bustype;
result.vendor = device.vendor;
result.product = device.product;
result.version = device.version;
result.event_types = device.event_types;
result.event_codes = device.event_codes;
result.properties = device.properties;
}
result.destroy_count = syscalls.libevdev_destroy_count;
if (result.status.ok()) {
result.close_status = OperationStatus::success();
}
return result;
}
void keep_fake_libevdev_successful(const LinuxTestSyscalls &syscalls) {
static_cast<void>(syscalls);
}
LinuxLibevdevCreationResult create_fake_libevdev_device(DeviceType device_type) {
return create_fake_libevdev_device(device_type, keep_fake_libevdev_successful);
}
LinuxLibevdevCreationResult create_fake_libevdev_gamepad(GamepadProfileKind kind) {
return create_fake_libevdev_device(DeviceType::gamepad, keep_fake_libevdev_successful, kind);
}
template<typename TouchDevice>
LinuxInputSubmissionResult touch_contact_reuse_pipe() {
std::array<int, 2> descriptors {-1, -1};
if (::pipe(descriptors.data()) != 0) {
return {system_error_status(ErrorCode::backend_failure, "failed to create pipe", errno), {}};
}
TouchDevice device {descriptors[1]};
auto status = device.place_contact({.id = 0, .x = 0.1F, .y = 0.1F, .pressure = 0.5F});
if (status.ok()) {
status = device.place_contact({.id = 1, .x = 0.2F, .y = 0.2F, .pressure = 0.5F});
}
if (status.ok()) {
status = device.release_contact(0, PointerTransition::release);
}
if (status.ok()) {
status = device.place_contact({.id = 0, .x = 0.3F, .y = 0.3F, .pressure = 0.5F});
}
if (status.ok()) {
status = device.place_contact({.id = 1, .x = 0.25F, .y = 0.25F, .pressure = 0.5F});
}
static_cast<void>(device.close());
auto records = read_input_events_until_eof(descriptors[0]);
static_cast<void>(::close(descriptors[0]));
return {std::move(status), std::move(records)};
}
} // namespace
std::string linux_copy_string_char_buffer(const std::string &source) {
std::array<char, 5> destination {};
copy_string(destination, source);
return destination.data();
}
int linux_key_code(KeyboardKeyCode key_code) {
return key_code_to_linux(key_code, true);
}
int linux_mouse_button(MouseButton button) {
return mouse_button_to_linux(button);
}
std::uint16_t linux_uhid_bus(BusType bus_type) {
return to_uhid_bus(bus_type);
}
std::uint16_t linux_gamepad_uhid_bus(GamepadProfileKind kind) {
const auto profile = profiles::gamepad_profile(kind);
return profile ? to_uhid_bus(*profile) : to_uhid_bus(BusType::unknown);
}
std::uint16_t linux_uinput_bus(BusType bus_type) {
return to_uinput_bus(bus_type);
}
int linux_absolute_axis(std::int32_t value, std::int32_t limit) {
return scale_absolute_axis(value, limit);
}
std::vector<std::uint32_t> linux_decode_utf8(const std::string &text) {
return decode_utf8(text);
}
std::string linux_uppercase_hex(std::uint32_t codepoint) {
return uppercase_hex(codepoint);
}
KeyboardKeyCode linux_hex_digit_key_code(char digit) {
return hex_digit_key_code(digit);
}