Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / tests / unit / test_payload.c
1 /*
2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <unistd.h>
9
10 #include <common/compat/fcntl.h>
11 #include <common/payload.h>
12 #include <common/payload-view.h>
13 #include <tap/tap.h>
14
15 static const int TEST_COUNT = 5;
16
17 /* For error.h */
18 int lttng_opt_quiet = 1;
19 int lttng_opt_verbose;
20 int lttng_opt_mi;
21
22 static void test_fd_push_pop_order(void)
23 {
24 int ret, i;
25 struct lttng_payload payload;
26 int fds[3];
27
28 lttng_payload_init(&payload);
29
30 diag("Validating fd push/pop order");
31 for (i = 0; i < 3; i++) {
32 int fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
33 struct fd_handle *handle;
34
35 LTTNG_ASSERT(fd >= 0);
36 fds[i] = fd;
37
38 handle = fd_handle_create(fd);
39 LTTNG_ASSERT(handle);
40
41 ret = lttng_payload_push_fd_handle(&payload, handle);
42 fd_handle_put(handle);
43 if (ret) {
44 break;
45 }
46 }
47
48 ok(ret == 0, "Added three file descriptors to an lttng_payload");
49
50 {
51 bool fail_pop = false;
52 struct lttng_payload_view view =
53 lttng_payload_view_from_payload(
54 &payload, 0, -1);
55
56 for (i = 0; i < 3; i++) {
57 struct fd_handle *handle =
58 lttng_payload_view_pop_fd_handle(&view);
59
60 fail_pop |= fd_handle_get_fd(handle) != fds[i];
61 fd_handle_put(handle);
62 }
63
64 ok(!fail_pop, "File descriptors are popped from a payload view in the order of insertion");
65 }
66
67 lttng_payload_reset(&payload);
68 }
69
70 static void test_fd_push_pop_imbalance(void)
71 {
72 int ret, i;
73 struct lttng_payload payload;
74 const char * const test_description = "Error reported when popping more file descriptors than were pushed";
75
76 lttng_payload_init(&payload);
77
78 diag("Validating fd pop imbalance");
79 for (i = 0; i < 10; i++) {
80 struct fd_handle *handle;
81 int fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
82
83 LTTNG_ASSERT(fd >= 0);
84
85 handle = fd_handle_create(fd);
86 LTTNG_ASSERT(handle);
87
88 ret = lttng_payload_push_fd_handle(&payload, handle);
89 fd_handle_put(handle);
90 if (ret) {
91 break;
92 }
93 }
94
95 {
96 struct fd_handle *handle;
97 struct lttng_payload_view view =
98 lttng_payload_view_from_payload(
99 &payload, 0, -1);
100
101 for (i = 0; i < 10; i++) {
102 handle = lttng_payload_view_pop_fd_handle(&view);
103 fd_handle_put(handle);
104 if (!handle) {
105 goto fail;
106 }
107 }
108
109 handle = lttng_payload_view_pop_fd_handle(&view);
110 ok(!handle, test_description);
111 fd_handle_put(handle);
112 }
113
114 lttng_payload_reset(&payload);
115 return;
116 fail:
117 fail(test_description);
118 lttng_payload_reset(&payload);
119 }
120
121 static void test_fd_pop_fd_root_views(void)
122 {
123 int ret, i;
124 int fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
125 struct fd_handle *handle;
126 struct lttng_payload payload;
127 const char * const test_description = "Same file descriptor returned when popping from different top-level views";
128
129 LTTNG_ASSERT(fd >= 0);
130 handle = fd_handle_create(fd);
131 LTTNG_ASSERT(handle);
132
133 lttng_payload_init(&payload);
134
135 diag("Validating root view fd pop behaviour");
136 ret = lttng_payload_push_fd_handle(&payload, handle);
137 if (ret) {
138 goto fail;
139 }
140
141 for (i = 0; i < 5; i++) {
142 int view_fd;
143 struct fd_handle *view_handle;
144 struct lttng_payload_view view =
145 lttng_payload_view_from_payload(
146 &payload, 0, -1);
147
148 view_handle = lttng_payload_view_pop_fd_handle(&view);
149 if (!view_handle) {
150 goto fail;
151 }
152
153 view_fd = fd_handle_get_fd(view_handle);
154 fd_handle_put(view_handle);
155 if (view_fd != fd || view_handle != handle) {
156 goto fail;
157 }
158 }
159
160 lttng_payload_reset(&payload);
161 pass(test_description);
162 fd_handle_put(handle);
163 return;
164 fail:
165 lttng_payload_reset(&payload);
166 fail(test_description);
167 fd_handle_put(handle);
168 }
169
170 static void test_fd_pop_fd_descendant_views(void)
171 {
172 int ret;
173 const int fd1 = 42, fd2 = 1837;
174 struct fd_handle *handle1 = fd_handle_create(fd1);
175 struct fd_handle *handle2 = fd_handle_create(fd2);
176 struct fd_handle *view_handle1 = NULL, *view_handle2 = NULL;
177 struct lttng_payload payload;
178 const char * const test_description = "Different file descriptors returned when popping from descendant views";
179
180 lttng_payload_init(&payload);
181 LTTNG_ASSERT(handle1);
182 LTTNG_ASSERT(handle2);
183
184 diag("Validating descendant view fd pop behaviour");
185 ret = lttng_payload_push_fd_handle(&payload, handle1);
186 if (ret) {
187 goto fail;
188 }
189
190 ret = lttng_payload_push_fd_handle(&payload, handle2);
191 if (ret) {
192 goto fail;
193 }
194
195 {
196 struct lttng_payload_view view1 =
197 lttng_payload_view_from_payload(
198 &payload, 0, -1);
199 struct lttng_payload_view view2 =
200 lttng_payload_view_from_view(
201 &view1, 0, -1);
202
203 view_handle1 = lttng_payload_view_pop_fd_handle(&view1);
204 if (!view_handle1 || fd_handle_get_fd(view_handle1) != fd1) {
205 goto fail;
206 }
207
208 view_handle2 = lttng_payload_view_pop_fd_handle(&view2);
209 if (!view_handle2 || fd_handle_get_fd(view_handle2) != fd2) {
210 goto fail;
211 }
212 }
213
214 lttng_payload_reset(&payload);
215 pass(test_description);
216 fd_handle_put(handle1);
217 fd_handle_put(handle2);
218 fd_handle_put(view_handle1);
219 fd_handle_put(view_handle2);
220 return;
221 fail:
222 lttng_payload_reset(&payload);
223 fail(test_description);
224 fd_handle_put(handle1);
225 fd_handle_put(handle2);
226 fd_handle_put(view_handle1);
227 fd_handle_put(view_handle2);
228 }
229
230 int main(void)
231 {
232 plan_tests(TEST_COUNT);
233
234 test_fd_push_pop_order();
235 test_fd_push_pop_imbalance();
236 test_fd_pop_fd_root_views();
237 test_fd_pop_fd_descendant_views();
238
239 return exit_status();
240 }
This page took 0.053745 seconds and 5 git commands to generate.