be97d49904481f6ae01819dec5ffe98993b8d760
[lttng-tools.git] / tests / unit / test_payload.c
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.h>
9 #include <common/payload-view.h>
10 #include <tap/tap.h>
11 #include <sys/eventfd.h>
12
13 static const int TEST_COUNT = 5;
14
15 /* For error.h */
16 int lttng_opt_quiet = 1;
17 int lttng_opt_verbose;
18 int lttng_opt_mi;
19
20 static void test_fd_push_pop_order(void)
21 {
22 int ret, i;
23 struct lttng_payload payload;
24 int fds[3];
25
26 lttng_payload_init(&payload);
27
28 diag("Validating fd push/pop order");
29 for (i = 0; i < 3; i++) {
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);
41 if (ret) {
42 break;
43 }
44 }
45
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++) {
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);
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
68 static 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++) {
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);
88 if (ret) {
89 break;
90 }
91 }
92
93 {
94 struct fd_handle *handle;
95 struct lttng_payload_view view =
96 lttng_payload_view_from_payload(
97 &payload, 0, -1);
98
99 for (i = 0; i < 10; i++) {
100 handle = lttng_payload_view_pop_fd_handle(&view);
101 fd_handle_put(handle);
102 if (!handle) {
103 goto fail;
104 }
105 }
106
107 handle = lttng_payload_view_pop_fd_handle(&view);
108 ok(!handle, test_description);
109 fd_handle_put(handle);
110 }
111
112 lttng_payload_reset(&payload);
113 return;
114 fail:
115 fail(test_description);
116 lttng_payload_reset(&payload);
117 }
118
119 static void test_fd_pop_fd_root_views(void)
120 {
121 int ret, i;
122 const int fd = eventfd(0, 0);
123 struct fd_handle *handle = fd_handle_create(fd);
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);
128 assert(handle);
129
130 diag("Validating root view fd pop behaviour");
131 ret = lttng_payload_push_fd_handle(&payload, handle);
132 if (ret) {
133 goto fail;
134 }
135
136 for (i = 0; i < 5; i++) {
137 int view_fd;
138 struct fd_handle *view_handle;
139 struct lttng_payload_view view =
140 lttng_payload_view_from_payload(
141 &payload, 0, -1);
142
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) {
151 goto fail;
152 }
153 }
154
155 lttng_payload_reset(&payload);
156 pass(test_description);
157 fd_handle_put(handle);
158 return;
159 fail:
160 lttng_payload_reset(&payload);
161 fail(test_description);
162 fd_handle_put(handle);
163 }
164
165 static void test_fd_pop_fd_descendant_views(void)
166 {
167 int ret;
168 const int fd1 = 42, fd2 = 1837;
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;
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);
176 assert(handle1);
177 assert(handle2);
178
179 diag("Validating descendant view fd pop behaviour");
180 ret = lttng_payload_push_fd_handle(&payload, handle1);
181 if (ret) {
182 goto fail;
183 }
184
185 ret = lttng_payload_push_fd_handle(&payload, handle2);
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
198 view_handle1 = lttng_payload_view_pop_fd_handle(&view1);
199 if (!view_handle1 || fd_handle_get_fd(view_handle1) != fd1) {
200 goto fail;
201 }
202
203 view_handle2 = lttng_payload_view_pop_fd_handle(&view2);
204 if (!view_handle2 || fd_handle_get_fd(view_handle2) != fd2) {
205 goto fail;
206 }
207 }
208
209 lttng_payload_reset(&payload);
210 pass(test_description);
211 fd_handle_put(handle1);
212 fd_handle_put(handle2);
213 fd_handle_put(view_handle1);
214 fd_handle_put(view_handle2);
215 return;
216 fail:
217 lttng_payload_reset(&payload);
218 fail(test_description);
219 fd_handle_put(handle1);
220 fd_handle_put(handle2);
221 fd_handle_put(view_handle1);
222 fd_handle_put(view_handle2);
223 }
224
225 int 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.046643 seconds and 3 git commands to generate.