Add condition-targeting error query
[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
7b0027b3
MJ
8#include <unistd.h>
9
10#include <common/compat/fcntl.h>
9e620ea7
JG
11#include <common/payload.h>
12#include <common/payload-view.h>
c0a66c84
JG
13#include <tap/tap.h>
14
15static const int TEST_COUNT = 5;
16
17/* For error.h */
18int lttng_opt_quiet = 1;
19int lttng_opt_verbose;
20int lttng_opt_mi;
21
22static void test_fd_push_pop_order(void)
23{
24 int ret, i;
25 struct lttng_payload payload;
fe489250 26 int fds[3];
c0a66c84
JG
27
28 lttng_payload_init(&payload);
29
30 diag("Validating fd push/pop order");
31 for (i = 0; i < 3; i++) {
7b0027b3 32 int fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
fe489250
JG
33 struct fd_handle *handle;
34
35 assert(fd >= 0);
36 fds[i] = fd;
37
38 handle = fd_handle_create(fd);
39 assert(handle);
40
41 ret = lttng_payload_push_fd_handle(&payload, handle);
42 fd_handle_put(handle);
c0a66c84
JG
43 if (ret) {
44 break;
45 }
46 }
fe489250 47
c0a66c84
JG
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++) {
fe489250
JG
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);
c0a66c84
JG
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
70static 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++) {
fe489250 80 struct fd_handle *handle;
7b0027b3 81 int fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
fe489250
JG
82
83 assert(fd >= 0);
84
85 handle = fd_handle_create(fd);
86 assert(handle);
87
88 ret = lttng_payload_push_fd_handle(&payload, handle);
89 fd_handle_put(handle);
c0a66c84
JG
90 if (ret) {
91 break;
92 }
93 }
94
95 {
fe489250 96 struct fd_handle *handle;
c0a66c84
JG
97 struct lttng_payload_view view =
98 lttng_payload_view_from_payload(
99 &payload, 0, -1);
100
101 for (i = 0; i < 10; i++) {
fe489250
JG
102 handle = lttng_payload_view_pop_fd_handle(&view);
103 fd_handle_put(handle);
104 if (!handle) {
c0a66c84
JG
105 goto fail;
106 }
107 }
108
fe489250
JG
109 handle = lttng_payload_view_pop_fd_handle(&view);
110 ok(!handle, test_description);
111 fd_handle_put(handle);
c0a66c84
JG
112 }
113
114 lttng_payload_reset(&payload);
115 return;
116fail:
117 fail(test_description);
118 lttng_payload_reset(&payload);
119}
120
121static void test_fd_pop_fd_root_views(void)
122{
123 int ret, i;
7b0027b3
MJ
124 int fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
125 struct fd_handle *handle;
c0a66c84
JG
126 struct lttng_payload payload;
127 const char * const test_description = "Same file descriptor returned when popping from different top-level views";
128
7b0027b3
MJ
129 assert(fd >= 0);
130 handle = fd_handle_create(fd);
fe489250 131 assert(handle);
c0a66c84 132
7b0027b3
MJ
133 lttng_payload_init(&payload);
134
c0a66c84 135 diag("Validating root view fd pop behaviour");
fe489250 136 ret = lttng_payload_push_fd_handle(&payload, handle);
c0a66c84
JG
137 if (ret) {
138 goto fail;
139 }
140
141 for (i = 0; i < 5; i++) {
fe489250
JG
142 int view_fd;
143 struct fd_handle *view_handle;
c0a66c84
JG
144 struct lttng_payload_view view =
145 lttng_payload_view_from_payload(
146 &payload, 0, -1);
147
fe489250
JG
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) {
c0a66c84
JG
156 goto fail;
157 }
158 }
159
160 lttng_payload_reset(&payload);
161 pass(test_description);
fe489250 162 fd_handle_put(handle);
c0a66c84
JG
163 return;
164fail:
165 lttng_payload_reset(&payload);
166 fail(test_description);
fe489250 167 fd_handle_put(handle);
c0a66c84
JG
168}
169
170static void test_fd_pop_fd_descendant_views(void)
171{
172 int ret;
173 const int fd1 = 42, fd2 = 1837;
fe489250
JG
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;
c0a66c84
JG
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);
fe489250
JG
181 assert(handle1);
182 assert(handle2);
c0a66c84
JG
183
184 diag("Validating descendant view fd pop behaviour");
fe489250 185 ret = lttng_payload_push_fd_handle(&payload, handle1);
c0a66c84
JG
186 if (ret) {
187 goto fail;
188 }
189
fe489250 190 ret = lttng_payload_push_fd_handle(&payload, handle2);
c0a66c84
JG
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
fe489250
JG
203 view_handle1 = lttng_payload_view_pop_fd_handle(&view1);
204 if (!view_handle1 || fd_handle_get_fd(view_handle1) != fd1) {
c0a66c84
JG
205 goto fail;
206 }
207
fe489250
JG
208 view_handle2 = lttng_payload_view_pop_fd_handle(&view2);
209 if (!view_handle2 || fd_handle_get_fd(view_handle2) != fd2) {
c0a66c84
JG
210 goto fail;
211 }
212 }
213
214 lttng_payload_reset(&payload);
215 pass(test_description);
fe489250
JG
216 fd_handle_put(handle1);
217 fd_handle_put(handle2);
218 fd_handle_put(view_handle1);
219 fd_handle_put(view_handle2);
c0a66c84
JG
220 return;
221fail:
222 lttng_payload_reset(&payload);
223 fail(test_description);
fe489250
JG
224 fd_handle_put(handle1);
225 fd_handle_put(handle2);
226 fd_handle_put(view_handle1);
227 fd_handle_put(view_handle2);
c0a66c84
JG
228}
229
230int 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.037091 seconds and 4 git commands to generate.