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