^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <api/fd/array.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include "util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "tests/tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) static void fdarray__init_revents(struct fdarray *fda, short revents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) fda->nr = fda->nr_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) for (fd = 0; fd < fda->nr; ++fd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) fda->entries[fd].fd = fda->nr - fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) fda->entries[fd].events = revents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) fda->entries[fd].revents = revents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int fdarray__fprintf_prefix(struct fdarray *fda, const char *prefix, FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int printed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (verbose <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) printed += fprintf(fp, "\n%s: ", prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return printed + fdarray__fprintf(fda, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int test__fdarray__filter(struct test *test __maybe_unused, int subtest __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int nr_fds, err = TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct fdarray *fda = fdarray__new(5, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (fda == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) pr_debug("\nfdarray__new() failed!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) fdarray__init_revents(fda, POLLIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) nr_fds = fdarray__filter(fda, POLLHUP, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (nr_fds != fda->nr_alloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) pr_debug("\nfdarray__filter()=%d != %d shouldn't have filtered anything",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) nr_fds, fda->nr_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) fdarray__init_revents(fda, POLLHUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) nr_fds = fdarray__filter(fda, POLLHUP, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (nr_fds != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) pr_debug("\nfdarray__filter()=%d != %d, should have filtered all fds",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) nr_fds, fda->nr_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) fdarray__init_revents(fda, POLLHUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) fda->entries[2].revents = POLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) pr_debug("\nfiltering all but fda->entries[2]:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) fdarray__fprintf_prefix(fda, "before", stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) nr_fds = fdarray__filter(fda, POLLHUP, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) fdarray__fprintf_prefix(fda, " after", stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (nr_fds != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_debug("\nfdarray__filter()=%d != 1, should have left just one event", nr_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) fdarray__init_revents(fda, POLLHUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) fda->entries[0].revents = POLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) fda->entries[3].revents = POLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pr_debug("\nfiltering all but (fda->entries[0], fda->entries[3]):");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fdarray__fprintf_prefix(fda, "before", stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) nr_fds = fdarray__filter(fda, POLLHUP, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) fdarray__fprintf_prefix(fda, " after", stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (nr_fds != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pr_debug("\nfdarray__filter()=%d != 2, should have left just two events",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) nr_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) out_delete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) fdarray__delete(fda);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int test__fdarray__add(struct test *test __maybe_unused, int subtest __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int err = TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct fdarray *fda = fdarray__new(2, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (fda == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) pr_debug("\nfdarray__new() failed!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define FDA_CHECK(_idx, _fd, _revents) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (fda->entries[_idx].fd != _fd) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) pr_debug("\n%d: fda->entries[%d](%d) != %d!", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) __LINE__, _idx, fda->entries[1].fd, _fd); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) goto out_delete; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (fda->entries[_idx].events != (_revents)) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pr_debug("\n%d: fda->entries[%d].revents(%d) != %d!", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) __LINE__, _idx, fda->entries[_idx].fd, _revents); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto out_delete; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #define FDA_ADD(_idx, _fd, _revents, _nr) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (fdarray__add(fda, _fd, _revents, fdarray_flag__default) < 0) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) pr_debug("\n%d: fdarray__add(fda, %d, %d) failed!", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) __LINE__,_fd, _revents); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) goto out_delete; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (fda->nr != _nr) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pr_debug("\n%d: fdarray__add(fda, %d, %d)=%d != %d", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) __LINE__,_fd, _revents, fda->nr, _nr); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto out_delete; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) FDA_CHECK(_idx, _fd, _revents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) FDA_ADD(0, 1, POLLIN, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) FDA_ADD(1, 2, POLLERR, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) fdarray__fprintf_prefix(fda, "before growing array", stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) FDA_ADD(2, 35, POLLHUP, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (fda->entries == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) pr_debug("\nfdarray__add(fda, 35, POLLHUP) should have allocated fda->pollfd!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) fdarray__fprintf_prefix(fda, "after 3rd add", stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) FDA_ADD(3, 88, POLLIN | POLLOUT, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) fdarray__fprintf_prefix(fda, "after 4th add", stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) FDA_CHECK(0, 1, POLLIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) FDA_CHECK(1, 2, POLLERR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) FDA_CHECK(2, 35, POLLHUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) FDA_CHECK(3, 88, POLLIN | POLLOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #undef FDA_ADD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #undef FDA_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) out_delete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) fdarray__delete(fda);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }