Debian Codegraph @

src/util/test-fdlist.c

raw
1 /*
2 * Test Utility FD List
3 */
4
5 #undef NDEBUG
6 #include <c-stdaux.h>
7 #include <stdlib.h>
8 #include <sys/epoll.h>
9 #include "util/fdlist.h"
10
11 static void test_setup(void) {
12 _c_cleanup_(fdlist_freep) FDList *l = NULL;
13 int r;
14
15 r = fdlist_new_with_fds(&l, NULL, 0);
16 c_assert(!r);
17
18 l = fdlist_free(l);
19 c_assert(!l);
20 }
21
22 static void test_dummy(void) {
23 FDList *l;
24 int r, dummies[] = { 7, 6, 5, 4, 3, 2, 1, 0 };
25 size_t i, j;
26
27 /*
28 * Allocate FDList objects with sizes 0-8, with data taken from
29 * @dummies. We verify that the file-descriptors are not touched (since
30 * they are dummy values that do not correspond to an FD), and that the
31 * fdlist returns the correct data.
32 */
33
34 for (i = 0; i < C_ARRAY_SIZE(dummies) + 1; ++i) {
35 r = fdlist_new_with_fds(&l, dummies, i);
36 c_assert(!r);
37
38 c_assert(fdlist_count(l) == i);
39
40 for (j = 0; j < i; ++j)
41 c_assert((size_t)fdlist_get(l, j) == C_ARRAY_SIZE(dummies) - j - 1);
42
43 l = fdlist_free(l);
44 }
45 }
46
47 static void test_consumer(void) {
48 int r, prev, p[2];
49 FDList *l;
50
51 /*
52 * Verify that the `consume' feature of FDList objects actually takes
53 * posession of passed FDs and closes them on destruction. We use
54 * epoll-fds as examples here, though any FD would work.
55 *
56 * We use the fact that FD spaces are sparse to verify that a given FD
57 * was actually properly closed.
58 */
59
60 prev = epoll_create1(EPOLL_CLOEXEC);
61 c_assert(prev >= 0);
62
63 p[0] = epoll_create1(EPOLL_CLOEXEC);
64 c_assert(p[0] >= 0);
65 c_assert(p[0] == prev + 1);
66
67 p[1] = epoll_create1(EPOLL_CLOEXEC);
68 c_assert(p[1] >= 0);
69 c_assert(p[1] == prev + 2);
70
71 r = fdlist_new_consume_fds(&l, p, C_ARRAY_SIZE(p));
72 c_assert(!r);
73
74 c_assert(fdlist_count(l) == C_ARRAY_SIZE(p));
75 c_assert(!memcmp(fdlist_data(l), p, sizeof(p)));
76
77 fdlist_truncate(l, 0);
78
79 r = epoll_create1(EPOLL_CLOEXEC);
80 c_assert(r == prev + 1);
81 close(r);
82
83 close(prev);
84
85 l = fdlist_free(l);
86 }
87
88 static void test_dup(void) {
89 int r, prev, p[2], expected[2];
90 FDList *l;
91
92 /*
93 * Verify that the `dup' feature of FDList objects actually duplicates
94 * the passed FDs and closes them on destruction. We use epoll-fds as
95 * examples here, though any FD would work.
96 *
97 * We use the fact that FD spaces are sparse to verify that a given FD
98 * was actually properly closed.
99 */
100
101 prev = epoll_create1(EPOLL_CLOEXEC);
102 c_assert(prev >= 0);
103
104 p[0] = epoll_create1(EPOLL_CLOEXEC);
105 c_assert(p[0] >= 0);
106 c_assert(p[0] == prev + 1);
107
108 p[1] = epoll_create1(EPOLL_CLOEXEC);
109 c_assert(p[1] >= 0);
110 c_assert(p[1] == prev + 2);
111
112 r = fdlist_new_dup_fds(&l, p, C_ARRAY_SIZE(p));
113 c_assert(!r);
114
115 expected[0] = prev + 3;
116 expected[1] = prev + 4;
117
118 c_assert(fdlist_count(l) == C_ARRAY_SIZE(p));
119 c_assert(!memcmp(fdlist_data(l), expected, sizeof(expected)));
120
121 fdlist_truncate(l, 0);
122
123 r = epoll_create1(EPOLL_CLOEXEC);
124 c_assert(r == prev + 3);
125 close(r);
126
127 close(p[1]);
128 close(p[0]);
129 close(prev);
130
131 l = fdlist_free(l);
132 }
133
134 int main(int argc, char **argv) {
135 test_setup();
136 test_dummy();
137 test_consumer();
138 test_dup();
139 return 0;
140 }