2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
10 #include <common/compat/fcntl.h>
11 #include <common/payload.h>
12 #include <common/payload-view.h>
15 static const int TEST_COUNT
= 5;
18 int lttng_opt_quiet
= 1;
19 int lttng_opt_verbose
;
22 static void test_fd_push_pop_order(void)
25 struct lttng_payload payload
;
28 lttng_payload_init(&payload
);
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
;
35 LTTNG_ASSERT(fd
>= 0);
38 handle
= fd_handle_create(fd
);
41 ret
= lttng_payload_push_fd_handle(&payload
, handle
);
42 fd_handle_put(handle
);
48 ok(ret
== 0, "Added three file descriptors to an lttng_payload");
51 bool fail_pop
= false;
52 struct lttng_payload_view view
=
53 lttng_payload_view_from_payload(
56 for (i
= 0; i
< 3; i
++) {
57 struct fd_handle
*handle
=
58 lttng_payload_view_pop_fd_handle(&view
);
60 fail_pop
|= fd_handle_get_fd(handle
) != fds
[i
];
61 fd_handle_put(handle
);
64 ok(!fail_pop
, "File descriptors are popped from a payload view in the order of insertion");
67 lttng_payload_reset(&payload
);
70 static void test_fd_push_pop_imbalance(void)
73 struct lttng_payload payload
;
74 const char * const test_description
= "Error reported when popping more file descriptors than were pushed";
76 lttng_payload_init(&payload
);
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);
83 LTTNG_ASSERT(fd
>= 0);
85 handle
= fd_handle_create(fd
);
88 ret
= lttng_payload_push_fd_handle(&payload
, handle
);
89 fd_handle_put(handle
);
96 struct fd_handle
*handle
;
97 struct lttng_payload_view view
=
98 lttng_payload_view_from_payload(
101 for (i
= 0; i
< 10; i
++) {
102 handle
= lttng_payload_view_pop_fd_handle(&view
);
103 fd_handle_put(handle
);
109 handle
= lttng_payload_view_pop_fd_handle(&view
);
110 ok(!handle
, test_description
);
111 fd_handle_put(handle
);
114 lttng_payload_reset(&payload
);
117 fail(test_description
);
118 lttng_payload_reset(&payload
);
121 static void test_fd_pop_fd_root_views(void)
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";
129 LTTNG_ASSERT(fd
>= 0);
130 handle
= fd_handle_create(fd
);
131 LTTNG_ASSERT(handle
);
133 lttng_payload_init(&payload
);
135 diag("Validating root view fd pop behaviour");
136 ret
= lttng_payload_push_fd_handle(&payload
, handle
);
141 for (i
= 0; i
< 5; i
++) {
143 struct fd_handle
*view_handle
;
144 struct lttng_payload_view view
=
145 lttng_payload_view_from_payload(
148 view_handle
= lttng_payload_view_pop_fd_handle(&view
);
153 view_fd
= fd_handle_get_fd(view_handle
);
154 fd_handle_put(view_handle
);
155 if (view_fd
!= fd
|| view_handle
!= handle
) {
160 lttng_payload_reset(&payload
);
161 pass(test_description
);
162 fd_handle_put(handle
);
165 lttng_payload_reset(&payload
);
166 fail(test_description
);
167 fd_handle_put(handle
);
170 static void test_fd_pop_fd_descendant_views(void)
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";
180 lttng_payload_init(&payload
);
181 LTTNG_ASSERT(handle1
);
182 LTTNG_ASSERT(handle2
);
184 diag("Validating descendant view fd pop behaviour");
185 ret
= lttng_payload_push_fd_handle(&payload
, handle1
);
190 ret
= lttng_payload_push_fd_handle(&payload
, handle2
);
196 struct lttng_payload_view view1
=
197 lttng_payload_view_from_payload(
199 struct lttng_payload_view view2
=
200 lttng_payload_view_from_view(
203 view_handle1
= lttng_payload_view_pop_fd_handle(&view1
);
204 if (!view_handle1
|| fd_handle_get_fd(view_handle1
) != fd1
) {
208 view_handle2
= lttng_payload_view_pop_fd_handle(&view2
);
209 if (!view_handle2
|| fd_handle_get_fd(view_handle2
) != fd2
) {
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
);
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
);
232 plan_tests(TEST_COUNT
);
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();
239 return exit_status();
This page took 0.053745 seconds and 5 git commands to generate.