Debian Codegraph @

src/test/test-curl-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-event.h"
6
7 #include "alloc-util.h"
8 #include "curl-util.h"
9 #include "fd-util.h"
10 #include "fs-util.h"
11 #include "io-util.h"
12 #include "string-util.h"
13 #include "tests.h"
14 #include "tmpfile-util.h"
15
16 #define ASSERT_CURL_OK(expr) \
17 ({ \
18 CURLcode _code = (expr); \
19 if (_code != CURLE_OK) \
20 log_test_failed("Expected \"%s\" to be CURLE_OK, but got %d/%s",\
21 #expr, (int) _code, sym_curl_easy_strerror(_code)); \
22 })
23
24 /* Per-request context: the write callback appends bytes to ->body, and the
25 * on_finished callback stashes the CURLcode plus a "fired" flag. Each test
26 * uses one or more of these and cleans them up via context_done(). */
27 typedef struct Context {
28 sd_event *event;
29 char *body;
30 size_t body_len;
31 bool finished;
32 CURLcode result;
33 } Context;
34
35 static void context_done(Context *f) {
36 f->event = sd_event_unref(f->event);
37 f->body = mfree(f->body);
38 }
39
40 static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
41 Context *f = ASSERT_PTR(userdata);
42 size_t sz = size * nmemb;
43
44 if (!GREEDY_REALLOC(f->body, f->body_len + sz + 1))
45 return 0;
46 memcpy(f->body + f->body_len, contents, sz);
47 f->body[f->body_len + sz] = 0;
48 f->body_len += sz;
49 return sz;
50 }
51
52 static int on_finished(CurlSlot *slot, CURL *curl, CURLcode code, void *userdata) {
53 Context *f = ASSERT_PTR(userdata);
54
55 f->finished = true;
56 f->result = code;
57
58 return sd_event_exit(f->event, 0);
59 }
60
61 static int make_tmp_url(char **ret_path, char **ret_url, const char *body) {
62 const char *t;
63 ASSERT_OK(tmp_dir(&t));
64
65 _cleanup_(unlink_and_freep) char *path = ASSERT_NOT_NULL(strjoin(t, "/test-curl-util.XXXXXX"));
66
67 _cleanup_close_ int fd = ASSERT_OK(mkostemp_safe(path));
68 ASSERT_OK(loop_write(fd, body, strlen(body)));
69
70 char *url = ASSERT_NOT_NULL(strjoin("file://", path));
71
72 *ret_url = url;
73 *ret_path = TAKE_PTR(path);
74 return 0;
75 }
76
77 static int build_easy(const char *url, Context *f, CURL **ret) {
78 _cleanup_(curl_easy_cleanupp) CURL *easy = NULL;
79 ASSERT_OK(curl_glue_make(&easy, url));
80
81 ASSERT_CURL_OK(sym_curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_callback));
82 ASSERT_CURL_OK(sym_curl_easy_setopt(easy, CURLOPT_WRITEDATA, f));
83
84 *ret = TAKE_PTR(easy);
85 return 0;
86 }
87
88 TEST(curl_glue_lifecycle) {
89 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
90 ASSERT_OK(sd_event_default(&event));
91
92 _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
93 ASSERT_OK(curl_glue_new(&g, event));
94
95 /* ref/unref roundtrip */
96 ASSERT_PTR_EQ(curl_glue_ref(g), g);
97 ASSERT_NULL(curl_glue_unref(g));
98 }
99
100 TEST(curl_glue_make) {
101 _cleanup_(curl_easy_cleanupp) CURL *easy = NULL;
102 ASSERT_OK(curl_glue_make(&easy, "file:///dev/null"));
103 ASSERT_NOT_NULL(easy);
104 }
105
106 TEST(curl_perform_floating) {
107 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
108 ASSERT_OK(sd_event_default(&event));
109
110 _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
111 ASSERT_OK(curl_glue_new(&g, event));
112
113 _cleanup_(unlink_and_freep) char *path = NULL;
114 _cleanup_free_ char *url = NULL;
115 ASSERT_OK(make_tmp_url(&path, &url, "hello world"));
116
117 _cleanup_(context_done) Context f = { .event = sd_event_ref(event) };
118
119 _cleanup_(curl_easy_cleanupp) CURL *easy = NULL;
120 ASSERT_OK(build_easy(url, &f, &easy));
121
122 /* Floating: pass NULL for ret_slot. The glue owns the slot until completion. */
123 ASSERT_OK(curl_glue_perform_async(g, easy, on_finished, &f, /* ret_slot= */ NULL));
124 TAKE_PTR(easy);
125
126 ASSERT_OK(sd_event_loop(event));
127
128 ASSERT_TRUE(f.finished);
129 ASSERT_CURL_OK(f.result);
130 ASSERT_STREQ(f.body, "hello world");
131 }
132
133 TEST(curl_perform_slot) {
134 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
135 ASSERT_OK(sd_event_default(&event));
136
137 _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
138 ASSERT_OK(curl_glue_new(&g, event));
139
140 _cleanup_(unlink_and_freep) char *path = NULL;
141 _cleanup_free_ char *url = NULL;
142 ASSERT_OK(make_tmp_url(&path, &url, "slot test"));
143
144 _cleanup_(context_done) Context f = { .event = sd_event_ref(event) };
145
146 _cleanup_(curl_easy_cleanupp) CURL *easy = NULL;
147 ASSERT_OK(build_easy(url, &f, &easy));
148
149 _cleanup_(curl_slot_unrefp) CurlSlot *slot = NULL;
150 ASSERT_OK(curl_glue_perform_async(g, easy, on_finished, &f, &slot));
151 TAKE_PTR(easy);
152
153 ASSERT_NOT_NULL(slot);
154 ASSERT_NOT_NULL(curl_slot_get_easy(slot));
155 ASSERT_PTR_EQ(curl_slot_get_glue(slot), g);
156
157 ASSERT_OK(sd_event_loop(event));
158
159 ASSERT_TRUE(f.finished);
160 ASSERT_CURL_OK(f.result);
161 ASSERT_STREQ(f.body, "slot test");
162
163 /* After completion, disconnect has cleared the slot's back-pointers; the slot itself
164 * is still alive because we hold a ref. Releasing it must be a clean no-op. */
165 ASSERT_NULL(curl_slot_get_easy(slot));
166 ASSERT_NULL(curl_slot_get_glue(slot));
167 }
168
169 TEST(curl_perform_cancel) {
170 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
171 ASSERT_OK(sd_event_default(&event));
172
173 _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
174 ASSERT_OK(curl_glue_new(&g, event));
175
176 _cleanup_(unlink_and_freep) char *path = NULL;
177 _cleanup_free_ char *url = NULL;
178 ASSERT_OK(make_tmp_url(&path, &url, "payload"));
179
180 /* Two requests: cancelled is unref'd before we run the loop; sentinel runs to
181 * completion and exits the loop. After the loop returns we know the dispatcher had
182 * an opportunity to fire any pending completion — so cancelled.finished staying false
183 * means our cancel actually prevented the callback from running, not just outraced it. */
184 _cleanup_(context_done) Context cancelled = { .event = sd_event_ref(event) };
185 _cleanup_(context_done) Context sentinel = { .event = sd_event_ref(event) };
186
187 _cleanup_(curl_easy_cleanupp) CURL *easy_cancelled = NULL, *easy_sentinel = NULL;
188 ASSERT_OK(build_easy(url, &cancelled, &easy_cancelled));
189 ASSERT_OK(build_easy(url, &sentinel, &easy_sentinel));
190
191 _cleanup_(curl_slot_unrefp) CurlSlot *slot = NULL;
192 ASSERT_OK(curl_glue_perform_async(g, easy_cancelled, on_finished, &cancelled, &slot));
193 TAKE_PTR(easy_cancelled);
194
195 /* Cancel by dropping our only reference: removes the easy handle from the multi and
196 * cleans it up. The callback must not fire afterwards. */
197 slot = curl_slot_unref(slot);
198
199 /* The sentinel runs as floating; its callback will exit the loop on completion. */
200 ASSERT_OK(curl_glue_perform_async(g, easy_sentinel, on_finished, &sentinel, /* ret_slot= */ NULL));
201 TAKE_PTR(easy_sentinel);
202
203 ASSERT_OK(sd_event_loop(event));
204
205 ASSERT_TRUE(sentinel.finished);
206 ASSERT_FALSE(cancelled.finished);
207 }
208
209 typedef struct ConcurrentReq {
210 Context ctx;
211 const char *expected;
212 unsigned *remaining;
213 } ConcurrentReq;
214
215 static int concurrent_on_finished(CurlSlot *slot, CURL *curl, CURLcode code, void *userdata) {
216 ConcurrentReq *cr = ASSERT_PTR(userdata);
217
218 cr->ctx.finished = true;
219 cr->ctx.result = code;
220
221 (*cr->remaining)--;
222 if (*cr->remaining == 0)
223 return sd_event_exit(cr->ctx.event, 0);
224 return 0;
225 }
226
227 TEST(curl_concurrent) {
228 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
229 ASSERT_OK(sd_event_default(&event));
230
231 _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
232 ASSERT_OK(curl_glue_new(&g, event));
233
234 _cleanup_(unlink_and_freep) char *path_a = NULL, *path_b = NULL, *path_c = NULL;
235 _cleanup_free_ char *url_a = NULL, *url_b = NULL, *url_c = NULL;
236 ASSERT_OK(make_tmp_url(&path_a, &url_a, "alpha"));
237 ASSERT_OK(make_tmp_url(&path_b, &url_b, "bravo"));
238 ASSERT_OK(make_tmp_url(&path_c, &url_c, "charlie"));
239
240 unsigned remaining = 3;
241 ConcurrentReq reqs[3] = {
242 { .ctx = { .event = sd_event_ref(event) }, .expected = "alpha", .remaining = &remaining },
243 { .ctx = { .event = sd_event_ref(event) }, .expected = "bravo", .remaining = &remaining },
244 { .ctx = { .event = sd_event_ref(event) }, .expected = "charlie", .remaining = &remaining },
245 };
246
247 _cleanup_(curl_easy_cleanupp) CURL *ea = NULL, *eb = NULL, *ec = NULL;
248 ASSERT_OK(build_easy(url_a, &reqs[0].ctx, &ea));
249 ASSERT_OK(build_easy(url_b, &reqs[1].ctx, &eb));
250 ASSERT_OK(build_easy(url_c, &reqs[2].ctx, &ec));
251
252 /* All three fire as floating slots; the only way the loop exits is through the
253 * remaining-counter hitting zero, which means every callback fired with the right
254 * userdata routed to its respective body. */
255 ASSERT_OK(curl_glue_perform_async(g, ea, concurrent_on_finished, &reqs[0], NULL));
256 TAKE_PTR(ea);
257 ASSERT_OK(curl_glue_perform_async(g, eb, concurrent_on_finished, &reqs[1], NULL));
258 TAKE_PTR(eb);
259 ASSERT_OK(curl_glue_perform_async(g, ec, concurrent_on_finished, &reqs[2], NULL));
260 TAKE_PTR(ec);
261
262 ASSERT_OK(sd_event_loop(event));
263
264 ASSERT_EQ(remaining, 0u);
265
266 FOREACH_ARRAY(r, reqs, ELEMENTSOF(reqs)) {
267 ASSERT_TRUE(r->ctx.finished);
268 ASSERT_CURL_OK(r->ctx.result);
269 ASSERT_STREQ(r->ctx.body, r->expected);
270 context_done(&r->ctx);
271 }
272 }
273
274 static int intro(void) {
275 if (dlopen_curl(LOG_DEBUG) < 0)
276 return log_tests_skipped("libcurl not available");
277 return EXIT_SUCCESS;
278 }
279
280 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);