fdarray.c 4.5 KB

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