2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/compat/fcntl.hpp>
9 #include <common/payload-view.hpp>
10 #include <common/payload.hpp>
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()
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
= lttng_payload_view_from_payload(&payload
, 0, -1);
54 for (i
= 0; i
< 3; i
++) {
55 struct fd_handle
*handle
= lttng_payload_view_pop_fd_handle(&view
);
57 fail_pop
|= fd_handle_get_fd(handle
) != fds
[i
];
58 fd_handle_put(handle
);
62 "File descriptors are popped from a payload view in the order of insertion");
65 lttng_payload_reset(&payload
);
68 static void test_fd_push_pop_imbalance()
71 struct lttng_payload payload
;
72 const char *const test_description
=
73 "Error reported when popping more file descriptors than were pushed";
75 lttng_payload_init(&payload
);
77 diag("Validating fd pop imbalance");
78 for (i
= 0; i
< 10; i
++) {
79 struct fd_handle
*handle
;
80 int fd
= fcntl(STDOUT_FILENO
, F_DUPFD
, 0);
82 LTTNG_ASSERT(fd
>= 0);
84 handle
= fd_handle_create(fd
);
87 ret
= lttng_payload_push_fd_handle(&payload
, handle
);
88 fd_handle_put(handle
);
95 struct fd_handle
*handle
;
96 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
98 for (i
= 0; i
< 10; i
++) {
99 handle
= lttng_payload_view_pop_fd_handle(&view
);
100 fd_handle_put(handle
);
106 handle
= lttng_payload_view_pop_fd_handle(&view
);
107 ok(!handle
, "%s", test_description
);
108 fd_handle_put(handle
);
111 lttng_payload_reset(&payload
);
114 fail("%s", test_description
);
115 lttng_payload_reset(&payload
);
118 static void test_fd_pop_fd_root_views()
121 int fd
= fcntl(STDOUT_FILENO
, F_DUPFD
, 0);
122 struct fd_handle
*handle
;
123 struct lttng_payload payload
;
124 const char *const test_description
=
125 "Same file descriptor returned when popping from different top-level views";
127 LTTNG_ASSERT(fd
>= 0);
128 handle
= fd_handle_create(fd
);
129 LTTNG_ASSERT(handle
);
131 lttng_payload_init(&payload
);
133 diag("Validating root view fd pop behaviour");
134 ret
= lttng_payload_push_fd_handle(&payload
, handle
);
139 for (i
= 0; i
< 5; i
++) {
141 struct fd_handle
*view_handle
;
142 struct lttng_payload_view view
= lttng_payload_view_from_payload(&payload
, 0, -1);
144 view_handle
= lttng_payload_view_pop_fd_handle(&view
);
149 view_fd
= fd_handle_get_fd(view_handle
);
150 fd_handle_put(view_handle
);
151 if (view_fd
!= fd
|| view_handle
!= handle
) {
156 lttng_payload_reset(&payload
);
157 pass("%s", test_description
);
158 fd_handle_put(handle
);
161 lttng_payload_reset(&payload
);
162 fail("%s", test_description
);
163 fd_handle_put(handle
);
166 static void test_fd_pop_fd_descendant_views()
169 const int fd1
= 42, fd2
= 1837;
170 struct fd_handle
*handle1
= fd_handle_create(fd1
);
171 struct fd_handle
*handle2
= fd_handle_create(fd2
);
172 struct fd_handle
*view_handle1
= nullptr, *view_handle2
= nullptr;
173 struct lttng_payload payload
;
174 const char *const test_description
=
175 "Different file descriptors returned when popping from descendant views";
177 lttng_payload_init(&payload
);
178 LTTNG_ASSERT(handle1
);
179 LTTNG_ASSERT(handle2
);
181 diag("Validating descendant view fd pop behaviour");
182 ret
= lttng_payload_push_fd_handle(&payload
, handle1
);
187 ret
= lttng_payload_push_fd_handle(&payload
, handle2
);
193 struct lttng_payload_view view1
= lttng_payload_view_from_payload(&payload
, 0, -1);
194 struct lttng_payload_view view2
= lttng_payload_view_from_view(&view1
, 0, -1);
196 view_handle1
= lttng_payload_view_pop_fd_handle(&view1
);
197 if (!view_handle1
|| fd_handle_get_fd(view_handle1
) != fd1
) {
201 view_handle2
= lttng_payload_view_pop_fd_handle(&view2
);
202 if (!view_handle2
|| fd_handle_get_fd(view_handle2
) != fd2
) {
207 lttng_payload_reset(&payload
);
208 pass("%s", test_description
);
209 fd_handle_put(handle1
);
210 fd_handle_put(handle2
);
211 fd_handle_put(view_handle1
);
212 fd_handle_put(view_handle2
);
215 lttng_payload_reset(&payload
);
216 fail("%s", test_description
);
217 fd_handle_put(handle1
);
218 fd_handle_put(handle2
);
219 fd_handle_put(view_handle1
);
220 fd_handle_put(view_handle2
);
225 plan_tests(TEST_COUNT
);
227 test_fd_push_pop_order();
228 test_fd_push_pop_imbalance();
229 test_fd_pop_fd_root_views();
230 test_fd_pop_fd_descendant_views();
232 return exit_status();
This page took 0.032967 seconds and 4 git commands to generate.