master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
25/*
26 * The purpose of this test is to make sure that if CURLMOPT_SOCKETFUNCTION or
27 * CURLMOPT_TIMERFUNCTION returns error, the associated transfer should be
28 * aborted correctly.
29 */
31#include "first.h"
32#include "testtrace.h"
34static struct t530_ctx {
35 int socket_calls;
36 int max_socket_calls;
37 int timer_calls;
38 int max_timer_calls;
39 char buf[1024];
40} t530_ctx;
42static const char *t530_tag(void)
43{
44 curl_msnprintf(t530_ctx.buf, sizeof(t530_ctx.buf),
45 "[T530-%d-%d] [%d/%d]",
46 t530_ctx.max_socket_calls, t530_ctx.max_timer_calls,
47 t530_ctx.socket_calls, t530_ctx.timer_calls);
48 return t530_ctx.buf;
49}
51static void t530_msg(const char *msg)
52{
53 curl_mfprintf(stderr, "%s %s\n", t530_tag(), msg);
54}
56struct t530_Sockets {
57 curl_socket_t *sockets;
58 int count; /* number of sockets actually stored in array */
59 int max_count; /* max number of sockets that fit in allocated array */
60};
62struct t530_ReadWriteSockets {
63 struct t530_Sockets read, write;
64};
66/**
67 * Remove a file descriptor from a sockets array.
68 */
69static void t530_removeFd(struct t530_Sockets *sockets, curl_socket_t fd,
70 int mention)
71{
72 int i;
74 if(mention)
75 curl_mfprintf(stderr, "%s remove socket fd %" FMT_SOCKET_T "\n",
76 t530_tag(), fd);
78 for(i = 0; i < sockets->count; ++i) {
79 if(sockets->sockets[i] == fd) {
80 if(i < sockets->count - 1)
81 memmove(&sockets->sockets[i], &sockets->sockets[i + 1],
82 sizeof(curl_socket_t) * (sockets->count - (i + 1)));
83 --sockets->count;
84 }
85 }
86}
88/**
89 * Add a file descriptor to a sockets array.
90 * Return 0 on success, 1 on error.
91 */
92static int t530_addFd(struct t530_Sockets *sockets, curl_socket_t fd,
93 const char *what)
94{
95 /**
96 * To ensure we only have each file descriptor once, we remove it then add
97 * it again.
98 */
99 curl_mfprintf(stderr, "%s add socket fd %" FMT_SOCKET_T " for %s\n",
100 t530_tag(), fd, what);
101 t530_removeFd(sockets, fd, 0);
102 /*
103 * Allocate array storage when required.
104 */
105 if(!sockets->sockets) {
106 sockets->sockets = curlx_malloc(sizeof(curl_socket_t) * 20U);
107 if(!sockets->sockets)
108 return 1;
109 sockets->max_count = 20;
110 }
111 else if(sockets->count + 1 > sockets->max_count) {
112 curl_socket_t *ptr = curlx_realloc(sockets->sockets,
113 sizeof(curl_socket_t) *
114 (sockets->max_count + 20));
115 if(!ptr)
116 /* cleanup in test_cleanup */
117 return 1;
118 sockets->sockets = ptr;
119 sockets->max_count += 20;
120 }
121 /*
122 * Add file descriptor to array.
123 */
124 sockets->sockets[sockets->count] = fd;
125 ++sockets->count;
126 return 0;
127}
129/**
130 * Callback invoked by curl to poll reading / writing of a socket.
131 */
132static int t530_curlSocketCallback(CURL *curl, curl_socket_t s, int action,
133 void *userp, void *socketp)
134{
135 struct t530_ReadWriteSockets *sockets = userp;
137 (void)curl;
138 (void)socketp;
140 t530_ctx.socket_calls++;
141 t530_msg("-> CURLMOPT_SOCKETFUNCTION");
142 if(t530_ctx.socket_calls == t530_ctx.max_socket_calls) {
143 t530_msg("<- CURLMOPT_SOCKETFUNCTION returns error");
144 return -1;
145 }
147 if(action == CURL_POLL_IN || action == CURL_POLL_INOUT)
148 if(t530_addFd(&sockets->read, s, "read"))
149 return -1; /* bail out */
151 if(action == CURL_POLL_OUT || action == CURL_POLL_INOUT)
152 if(t530_addFd(&sockets->write, s, "write"))
153 return -1;
155 if(action == CURL_POLL_REMOVE) {
156 t530_removeFd(&sockets->read, s, 1);
157 t530_removeFd(&sockets->write, s, 0);
158 }
160 return 0;
161}
163/**
164 * Callback invoked by curl to set a timeout.
165 */
166static int t530_curlTimerCallback(CURLM *multi, long timeout_ms, void *userp)
167{
168 struct curltime *timeout = userp;
170 (void)multi;
171 t530_ctx.timer_calls++;
172 t530_msg("-> CURLMOPT_TIMERFUNCTION");
173 if(t530_ctx.timer_calls == t530_ctx.max_timer_calls) {
174 t530_msg("<- CURLMOPT_TIMERFUNCTION returns error");
175 return -1;
176 }
177 if(timeout_ms != -1) {
178 *timeout = curlx_now();
179 timeout->tv_usec += (int)timeout_ms * 1000;
180 }
181 else {
182 timeout->tv_sec = -1;
183 }
184 return 0;
185}
187/**
188 * Check for curl completion.
189 */
190static int t530_checkForCompletion(CURLM *multi, int *success)
191{
192 int result = 0;
193 *success = 0;
194 while(1) {
195 int numMessages;
196 CURLMsg *message = curl_multi_info_read(multi, &numMessages);
197 if(!message)
198 break;
199 if(message->msg == CURLMSG_DONE) {
200 result = 1;
201 if(message->data.result == CURLE_OK)
202 *success = 1;
203 else
204 *success = 0;
205 }
206 else {
207 curl_mfprintf(stderr, "%s got an unexpected message from curl: %d\n",
208 t530_tag(), (int)message->msg);
209 result = 1;
210 *success = 0;
211 }
212 }
213 return result;
214}
216static ssize_t t530_getMicroSecondTimeout(struct curltime *timeout)
217{
218 struct curltime now;
219 ssize_t result;
220 now = curlx_now();
221 result = (ssize_t)(((timeout->tv_sec - now.tv_sec) * 1000000) +
222 timeout->tv_usec - now.tv_usec);
223 if(result < 0)
224 result = 0;
226 return result;
227}
229/**
230 * Update a fd_set with all of the sockets in use.
231 */
232static void t530_updateFdSet(struct t530_Sockets *sockets, fd_set *fdset,
233 curl_socket_t *maxFd)
234{
235 int i;
236 for(i = 0; i < sockets->count; ++i) {
237 FD_SET(sockets->sockets[i], fdset);
238 if(*maxFd < sockets->sockets[i] + 1) {
239 *maxFd = sockets->sockets[i] + 1;
240 }
241 }
242}
244static CURLMcode socket_action(CURLM *multi, curl_socket_t s, int evBitmask,
245 const char *info)
246{
247 int numhandles = 0;
248 CURLMcode mresult = curl_multi_socket_action(multi, s, evBitmask,
249 &numhandles);
250 if(mresult != CURLM_OK) {
251 curl_mfprintf(stderr, "%s curl error on %s (%d) %s\n",
252 t530_tag(), info, mresult, curl_multi_strerror(mresult));
253 }
254 return mresult;
255}
257/**
258 * Invoke curl when a file descriptor is set.
259 */
260static CURLMcode t530_checkFdSet(CURLM *multi, struct t530_Sockets *sockets,
261 fd_set *fdset, int evBitmask,
262 const char *name)
263{
264 int i;
265 CURLMcode mresult = CURLM_OK;
266 for(i = 0; i < sockets->count; ++i) {
267 if(FD_ISSET(sockets->sockets[i], fdset)) {
268 mresult = socket_action(multi, sockets->sockets[i], evBitmask, name);
269 if(mresult)
270 break;
271 }
272 }
273 return mresult;
274}
276static CURLcode testone(const char *URL, int timer_fail_at, int socket_fail_at)
277{
278 CURLcode result = CURLE_OK;
279 CURL *curl = NULL;
280 CURLM *multi = NULL;
281 struct t530_ReadWriteSockets sockets = { { NULL, 0, 0 }, { NULL, 0, 0 } };
282 int success = 0;
283 struct curltime timeout = { 0 };
284 timeout.tv_sec = (time_t)-1;
286 /* set the limits */
287 memset(&t530_ctx, 0, sizeof(t530_ctx));
288 t530_ctx.max_timer_calls = timer_fail_at;
289 t530_ctx.max_socket_calls = socket_fail_at;
291 t530_msg("start");
292 start_test_timing();
294 res_global_init(CURL_GLOBAL_ALL);
295 if(result != CURLE_OK)
296 return result;
298 easy_init(curl);
300 /* specify target */
301 easy_setopt(curl, CURLOPT_URL, URL);
303 /* go verbose */
304 easy_setopt(curl, CURLOPT_DEBUGDATA, &debug_config);
305 easy_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
306 easy_setopt(curl, CURLOPT_VERBOSE, 1L);
308 multi_init(multi);
310 multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, t530_curlSocketCallback);
311 multi_setopt(multi, CURLMOPT_SOCKETDATA, &sockets);
313 multi_setopt(multi, CURLMOPT_TIMERFUNCTION, t530_curlTimerCallback);
314 multi_setopt(multi, CURLMOPT_TIMERDATA, &timeout);
316 multi_add_handle(multi, curl);
318 if(socket_action(multi, CURL_SOCKET_TIMEOUT, 0, "timeout")) {
319 result = TEST_ERR_MAJOR_BAD;
320 goto test_cleanup;
321 }
323 while(!t530_checkForCompletion(multi, &success)) {
324 fd_set readSet, writeSet;
325 curl_socket_t maxFd = 0;
326 struct timeval tv = { 0 };
327 tv.tv_sec = 10;
329 FD_ZERO(&readSet);
330 FD_ZERO(&writeSet);
331 t530_updateFdSet(&sockets.read, &readSet, &maxFd);
332 t530_updateFdSet(&sockets.write, &writeSet, &maxFd);
334 if(timeout.tv_sec != (time_t)-1) {
335 int usTimeout = curlx_sztosi(t530_getMicroSecondTimeout(&timeout));
336 tv.tv_sec = usTimeout / 1000000;
337 tv.tv_usec = usTimeout % 1000000;
338 }
339 else if(maxFd <= 0) {
340 tv.tv_sec = 0;
341 tv.tv_usec = 100000;
342 }
344 if(!maxFd) {
345 t530_msg("maxFd == 0");
346 result = TEST_ERR_MAJOR_BAD;
347 goto test_cleanup;
348 }
349 select_test((int)maxFd, &readSet, &writeSet, NULL, &tv);
351 /* Check the sockets for reading / writing */
352 if(t530_checkFdSet(multi, &sockets.read, &readSet, CURL_CSELECT_IN,
353 "read")) {
354 result = TEST_ERR_MAJOR_BAD;
355 goto test_cleanup;
356 }
357 if(t530_checkFdSet(multi, &sockets.write, &writeSet, CURL_CSELECT_OUT,
358 "write")) {
359 result = TEST_ERR_MAJOR_BAD;
360 goto test_cleanup;
361 }
363 if(timeout.tv_sec != (time_t)-1 &&
364 t530_getMicroSecondTimeout(&timeout) == 0 &&
365 /* curl's timer has elapsed. */
366 socket_action(multi, CURL_SOCKET_TIMEOUT, 0, "timeout")) {
367 result = TEST_ERR_BAD_TIMEOUT;
368 goto test_cleanup;
369 }
371 abort_on_test_timeout();
372 }
374 if(!success) {
375 t530_msg("Error getting file.");
376 result = TEST_ERR_MAJOR_BAD;
377 }
379test_cleanup:
381 /* proper cleanup sequence */
382 t530_msg("cleanup");
383 curl_multi_remove_handle(multi, curl);
384 curl_easy_cleanup(curl);
385 curl_multi_cleanup(multi);
386 curl_global_cleanup();
388 /* free local memory */
389 curlx_free(sockets.read.sockets);
390 curlx_free(sockets.write.sockets);
391 t530_msg("done");
393 return result;
394}
396static CURLcode test_lib530(const char *URL)
397{
398 CURLcode result;
399 /* rerun the same transfer multiple times and make it fail in different
400 callback calls */
401 result = testone(URL, 0, 0); /* no callback fails */
402 if(result)
403 curl_mfprintf(stderr, "%s FAILED: %d\n", t530_tag(), (int)result);
405 result = testone(URL, 1, 0); /* fail 1st call to timer callback */
406 if(!result)
407 curl_mfprintf(stderr, "%s FAILED: %d\n", t530_tag(), (int)result);
409 result = testone(URL, 2, 0); /* fail 2nd call to timer callback */
410 if(!result)
411 curl_mfprintf(stderr, "%s FAILED: %d\n", t530_tag(), (int)result);
413 result = testone(URL, 0, 2); /* fail 2nd call to socket callback */
414 if(!result)
415 curl_mfprintf(stderr, "%s FAILED: %d\n", t530_tag(), (int)result);
417 result = testone(URL, 0, 3); /* fail 3rd call to socket callback */
418 if(!result)
419 curl_mfprintf(stderr, "%s FAILED: %d\n", t530_tag(), (int)result);
421 return CURLE_OK;
422}
Diagnostics4 git calls · 23 ms
CallStatusSizeTime023 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2003.7 KiB · hit18 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=18 git.response.bytes=3794 git.response.cache=HIT git.response.duration_ms=18 git.fetch.wants=a5d953eeaaf4e482ffb46c88182f61a756f8b86c git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3794 git.pkt.payload.max=3773 git.sideband.band1.bytes=3772 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e33959cd87f162
- Request
- 9be35759…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format