Coverity warning: sessiond: uncaught exception in main
[lttng-tools.git] / tests / unit / test_payload.cpp
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 <common/payload-view.hpp>
9 #include <common/payload.hpp>
10
11 #include <fcntl.h>
12 #include <tap/tap.h>
13 #include <unistd.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()
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 = lttng_payload_view_from_payload(&payload, 0, -1);
53
54 for (i = 0; i < 3; i++) {
55 struct fd_handle *handle = lttng_payload_view_pop_fd_handle(&view);
56
57 fail_pop |= fd_handle_get_fd(handle) != fds[i];
58 fd_handle_put(handle);
59 }
60
61 ok(!fail_pop,
62 "File descriptors are popped from a payload view in the order of insertion");
63 }
64
65 lttng_payload_reset(&payload);
66 }
67
68 static void test_fd_push_pop_imbalance()
69 {
70 int ret, i;
71 struct lttng_payload payload;
72 const char *const test_description =
73 "Error reported when popping more file descriptors than were pushed";
74
75 lttng_payload_init(&payload);
76
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);
81
82 LTTNG_ASSERT(fd >= 0);
83
84 handle = fd_handle_create(fd);
85 LTTNG_ASSERT(handle);
86
87 ret = lttng_payload_push_fd_handle(&payload, handle);
88 fd_handle_put(handle);
89 if (ret) {
90 break;
91 }
92 }
93
94 {
95 struct fd_handle *handle;
96 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
97
98 for (i = 0; i < 10; i++) {
99 handle = lttng_payload_view_pop_fd_handle(&view);
100 fd_handle_put(handle);
101 if (!handle) {
102 goto fail;
103 }
104 }
105
106 handle = lttng_payload_view_pop_fd_handle(&view);
107 ok(!handle, "%s", test_description);
108 fd_handle_put(handle);
109 }
110
111 lttng_payload_reset(&payload);
112 return;
113 fail:
114 fail("%s", test_description);
115 lttng_payload_reset(&payload);
116 }
117
118 static void test_fd_pop_fd_root_views()
119 {
120 int ret, i;
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";
126
127 LTTNG_ASSERT(fd >= 0);
128 handle = fd_handle_create(fd);
129 LTTNG_ASSERT(handle);
130
131 lttng_payload_init(&payload);
132
133 diag("Validating root view fd pop behaviour");
134 ret = lttng_payload_push_fd_handle(&payload, handle);
135 if (ret) {
136 goto fail;
137 }
138
139 for (i = 0; i < 5; i++) {
140 int view_fd;
141 struct fd_handle *view_handle;
142 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
143
144 view_handle = lttng_payload_view_pop_fd_handle(&view);
145 if (!view_handle) {
146 goto fail;
147 }
148
149 view_fd = fd_handle_get_fd(view_handle);
150 fd_handle_put(view_handle);
151 if (view_fd != fd || view_handle != handle) {
152 goto fail;
153 }
154 }
155
156 lttng_payload_reset(&payload);
157 pass("%s", test_description);
158 fd_handle_put(handle);
159 return;
160 fail:
161 lttng_payload_reset(&payload);
162 fail("%s", test_description);
163 fd_handle_put(handle);
164 }
165
166 static void test_fd_pop_fd_descendant_views()
167 {
168 int ret;
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";
176
177 lttng_payload_init(&payload);
178 LTTNG_ASSERT(handle1);
179 LTTNG_ASSERT(handle2);
180
181 diag("Validating descendant view fd pop behaviour");
182 ret = lttng_payload_push_fd_handle(&payload, handle1);
183 if (ret) {
184 goto fail;
185 }
186
187 ret = lttng_payload_push_fd_handle(&payload, handle2);
188 if (ret) {
189 goto fail;
190 }
191
192 {
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);
195
196 view_handle1 = lttng_payload_view_pop_fd_handle(&view1);
197 if (!view_handle1 || fd_handle_get_fd(view_handle1) != fd1) {
198 goto fail;
199 }
200
201 view_handle2 = lttng_payload_view_pop_fd_handle(&view2);
202 if (!view_handle2 || fd_handle_get_fd(view_handle2) != fd2) {
203 goto fail;
204 }
205 }
206
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);
213 return;
214 fail:
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);
221 }
222
223 int main()
224 {
225 plan_tests(TEST_COUNT);
226
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();
231
232 return exit_status();
233 }
This page took 0.034678 seconds and 4 git commands to generate.