From 4a1aba3c02acfe9c25384508f55ceebbb76448ce Mon Sep 17 00:00:00 2001 From: Delete228 <31237303+Delete228@users.noreply.github.com> Date: Tue, 20 Oct 2026 16:42:24 +0300 Subject: [PATCH] test: cover vectored controller reads Cover readv on original and virtual controller rings, an overlay-style read-to-readv fallback, event-aligned vectors, empty vectors, nonblocking errors, and ordinary descriptor passthrough. --- tools/tests/test_fakeinput_ring.py | 140 ++++++++++++++++++++++++++--- 1 file changed, 127 insertions(+), 13 deletions(-) diff --git a/tools/tests/test_fakeinput_ring.py b/tools/tests/test_fakeinput_ring.py index 3c87bd11e2..42d6f85b68 100644 --- a/tools/tests/test_fakeinput_ring.py +++ b/tools/tests/test_fakeinput_ring.py @@ -18,24 +18,44 @@ READER = r""" #include #include #include +#include #include -int main(void) +static int vector_count; + +static ssize_t controller_read(int fd, void *buffer, size_t size) { + if (!vector_count) + return read(fd, buffer, size); + struct iovec vectors[3] = { + {buffer, sizeof(struct input_event)}, + {(char *)buffer + sizeof(struct input_event), 0}, + {(char *)buffer + sizeof(struct input_event), size - sizeof(struct input_event)}, + }; + if (vector_count == 1) { + vectors[0].iov_len = size; + return readv(fd, vectors, 1); + } + return readv(fd, vectors, 3); +} + +int main(int argc, char **argv) +{ + vector_count = argc > 1 ? (argv[1][0] == '1' ? 1 : 3) : 0; struct input_event events[64]; int fd = open("/dev/input/event16", O_RDONLY | O_NONBLOCK); if (fd < 0) { perror("open"); return 1; } - while (read(fd, events, sizeof(events)) > 0) + while (controller_read(fd, events, sizeof(events)) > 0) ; puts("ready"); fflush(stdout); char go; if (read(0, &go, 1) != 1) return 1; - ssize_t got = read(fd, events, sizeof(events)); + ssize_t got = controller_read(fd, events, sizeof(events)); for (ssize_t i = 0; i < got / (ssize_t)sizeof(events[0]); i++) printf("%ld.%06ld %u %u %d\n", (long)events[i].time.tv_sec, (long)events[i].time.tv_usec, events[i].type, events[i].code, events[i].value); @@ -65,6 +85,61 @@ int main(void) return 0; } """ +READV_CHECKS = r""" +#include +#include +#include +#include +#include +#include +#include + +int main(void) +{ + struct input_event events[64]; + int fd = open("/dev/input/event16", O_RDONLY | O_NONBLOCK); + if (fd < 0) + return 1; + while (read(fd, events, sizeof(events)) > 0) + ; + struct iovec vector = {events, sizeof(events)}; + if (readv(fd, &vector, 1) != -1 || errno != EAGAIN) + return 2; + if (readv(fd, NULL, 0) != 0) + return 3; + vector.iov_len = 0; + if (readv(fd, &vector, 1) != 0) + return 4; + vector.iov_len = sizeof(struct input_event) - 1; + if (readv(fd, &vector, 1) != -1 || errno != EINVAL) + return 5; + ssize_t (*invoke)(int, const struct iovec *, int) = readv; + if (invoke(fd, NULL, -1) != -1 || errno != EINVAL) + return 6; + if (invoke(fd, NULL, 1025) != -1 || errno != EINVAL) + return 7; + if (invoke(fd, NULL, 1) != -1 || errno != EFAULT) + return 8; + int pipefd[2]; + char text[5] = {0}; + struct iovec parts[2] = {{text, 1}, {text + 1, 3}}; + if (pipe(pipefd) || write(pipefd[1], "test", 4) != 4 || + readv(pipefd[0], parts, 2) != 4 || strcmp(text, "test")) + return 9; + puts("ok"); + return 0; +} +""" +OVERLAY_READ = r""" +#include +#include + +ssize_t read(int fd, void *buffer, size_t size) +{ + struct iovec vector = {buffer, size}; + return readv(fd, &vector, 1); +} +""" def can_build(bits): @@ -82,15 +157,16 @@ class FakeinputRingTest(unittest.TestCase): self.tmp = Path(tempfile.mkdtemp()) self.addCleanup(shutil.rmtree, self.tmp, True) - def ring(self): + def ring(self, virtual=True): hooks = self.tmp / "hooks" hooks.mkdir() - (hooks / "event16").touch() - ring = bytearray(HEADER + CAPACITY * EVENT + DESCRIPTION) + (hooks / ("event16" if virtual else "event0")).touch() + ring = bytearray(HEADER + CAPACITY * EVENT + (DESCRIPTION if virtual else 0)) struct.pack_into("