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"
33#ifdef USE_OPENSSL
35#include <openssl/ssl.h>
37#if OPENSSL_VERSION_NUMBER >= 0x30000000L
38#define T578_ENABLED
39#endif
40#endif
42#ifdef T578_ENABLED
44#include "testtrace.h"
46static struct t758_ctx {
47 int socket_calls;
48 int max_socket_calls;
49 int timer_calls;
50 int max_timer_calls;
51 int fake_async_cert_verification_pending;
52 int fake_async_cert_verification_finished;
53 int number_of_cert_verify_callbacks;
54 char buf[1024];
55} t758_ctx;
57static const char *t758_tag(void)
58{
59 curl_msnprintf(t758_ctx.buf, sizeof(t758_ctx.buf), "[T758-%d-%d] [%d/%d]",
60 t758_ctx.max_socket_calls, t758_ctx.max_timer_calls,
61 t758_ctx.socket_calls, t758_ctx.timer_calls);
62 return t758_ctx.buf;
63}
65static void t758_msg(const char *msg)
66{
67 curl_mfprintf(stderr, "%s %s\n", t758_tag(), msg);
68}
70struct t758_Sockets {
71 curl_socket_t *sockets;
72 int count; /* number of sockets actually stored in array */
73 int max_count; /* max number of sockets that fit in allocated array */
74};
76struct t758_ReadWriteSockets {
77 struct t758_Sockets read, write;
78};
80/**
81 * Remove a file descriptor from a sockets array.
82 */
83static void t758_removeFd(struct t758_Sockets *sockets, curl_socket_t fd,
84 int mention)
85{
86 int i;
88 if(mention)
89 curl_mfprintf(stderr, "%s remove socket fd %" FMT_SOCKET_T "\n",
90 t758_tag(), fd);
92 for(i = 0; i < sockets->count; ++i) {
93 if(sockets->sockets[i] == fd) {
94 if(i < sockets->count - 1)
95 memmove(&sockets->sockets[i], &sockets->sockets[i + 1],
96 sizeof(curl_socket_t) * (sockets->count - (i + 1)));
97 --sockets->count;
98 }
99 }
100}
102/**
103 * Add a file descriptor to a sockets array.
104 * Return 0 on success, 1 on error.
105 */
106static int t758_addFd(struct t758_Sockets *sockets, curl_socket_t fd,
107 const char *what)
108{
109 /**
110 * To ensure we only have each file descriptor once, we remove it then add
111 * it again.
112 */
113 curl_mfprintf(stderr, "%s add socket fd %" FMT_SOCKET_T " for %s\n",
114 t758_tag(), fd, what);
115 t758_removeFd(sockets, fd, 0);
116 /*
117 * Allocate array storage when required.
118 */
119 if(!sockets->sockets) {
120 sockets->sockets = curlx_malloc(sizeof(curl_socket_t) * 20U);
121 if(!sockets->sockets)
122 return 1;
123 sockets->max_count = 20;
124 }
125 else if(sockets->count + 1 > sockets->max_count) {
126 curl_socket_t *ptr = curlx_realloc(sockets->sockets,
127 sizeof(curl_socket_t) *
128 (sockets->max_count + 20));
129 if(!ptr)
130 /* cleanup in test_cleanup */
131 return 1;
132 sockets->sockets = ptr;
133 sockets->max_count += 20;
134 }
135 /*
136 * Add file descriptor to array.
137 */
138 sockets->sockets[sockets->count] = fd;
139 ++sockets->count;
140 return 0;
141}
143/**
144 * Callback invoked by curl to poll reading / writing of a socket.
145 */
146static int t758_curlSocketCallback(CURL *curl, curl_socket_t s, int action,
147 void *userp, void *socketp)
148{
149 struct t758_ReadWriteSockets *sockets = userp;
150 CURLcode result;
152 (void)curl;
153 (void)socketp;
155 t758_ctx.socket_calls++;
156 t758_msg("-> CURLMOPT_SOCKETFUNCTION");
157 if(t758_ctx.socket_calls == t758_ctx.max_socket_calls) {
158 t758_msg("<- CURLMOPT_SOCKETFUNCTION returns error");
159 return -1;
160 }
162 /* Pause is forbidden in this callback. This also returns
163 CURLE_BAD_FUNCTION_ARGUMENT before the connection has been setup. */
164 result = curl_easy_pause(curl, CURLPAUSE_ALL);
165 if(!result) {
166 t758_msg("<- curl_easy_pause should return error!");
167 return -1;
168 }
170 if(action == CURL_POLL_IN || action == CURL_POLL_INOUT)
171 if(t758_addFd(&sockets->read, s, "read"))
172 return -1; /* bail out */
174 if(action == CURL_POLL_OUT || action == CURL_POLL_INOUT)
175 if(t758_addFd(&sockets->write, s, "write"))
176 return -1;
178 if(action == CURL_POLL_REMOVE) {
179 t758_removeFd(&sockets->read, s, 1);
180 t758_removeFd(&sockets->write, s, 0);
181 }
183 return 0;
184}
186/**
187 * Callback invoked by curl to set a timeout.
188 */
189static int t758_curlTimerCallback(CURLM *multi, long timeout_ms, void *userp)
190{
191 struct curltime *timeout = userp;
193 (void)multi;
194 t758_ctx.timer_calls++;
195 t758_msg("-> CURLMOPT_TIMERFUNCTION");
196 if(t758_ctx.timer_calls == t758_ctx.max_timer_calls) {
197 t758_msg("<- CURLMOPT_TIMERFUNCTION returns error");
198 return -1;
199 }
200 if(timeout_ms != -1) {
201 *timeout = curlx_now();
202 timeout->tv_usec += (int)timeout_ms * 1000;
203 }
204 else {
205 timeout->tv_sec = -1;
206 }
207 return 0;
208}
210static int t758_cert_verify_callback(X509_STORE_CTX *ctx, void *arg)
211{
212 SSL *ssl;
213 (void)arg;
214 ssl = (SSL *)X509_STORE_CTX_get_ex_data(
215 ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
216 t758_ctx.number_of_cert_verify_callbacks++;
217 if(!t758_ctx.fake_async_cert_verification_pending) {
218 t758_ctx.fake_async_cert_verification_pending = 1;
219 t758_msg(" initial t758_cert_verify_callback");
220 return SSL_set_retry_verify(ssl);
221 }
222 else if(t758_ctx.fake_async_cert_verification_finished) {
223 t758_msg(" final t758_cert_verify_callback");
224 return 1; /* success */
225 }
226 else {
227 t758_msg(" pending t758_cert_verify_callback");
228 return SSL_set_retry_verify(ssl);
229 }
230}
232static CURLcode t758_set_ssl_ctx_callback(CURL *curl, void *ssl_ctx,
233 void *clientp)
234{
235 SSL_CTX *ctx = (SSL_CTX *)ssl_ctx;
236 (void)curl;
237 SSL_CTX_set_cert_verify_callback(ctx, t758_cert_verify_callback, clientp);
238 return CURLE_OK;
239}
241/**
242 * Check for curl completion.
243 */
244static int t758_checkForCompletion(CURLM *multi, int *success)
245{
246 int result = 0;
247 *success = 0;
248 while(1) {
249 int numMessages;
250 CURLMsg *message = curl_multi_info_read(multi, &numMessages);
251 if(!message)
252 break;
253 if(message->msg == CURLMSG_DONE) {
254 result = 1;
255 if(message->data.result == CURLE_OK)
256 *success = 1;
257 else
258 *success = 0;
259 }
260 else {
261 curl_mfprintf(stderr, "%s got an unexpected message from curl: %d\n",
262 t758_tag(), (int)message->msg);
263 result = 1;
264 *success = 0;
265 }
266 }
267 return result;
268}
270static ssize_t t758_getMicroSecondTimeout(struct curltime *timeout)
271{
272 struct curltime now;
273 ssize_t result;
274 now = curlx_now();
275 result = (ssize_t)(((timeout->tv_sec - now.tv_sec) * 1000000) +
276 timeout->tv_usec - now.tv_usec);
277 if(result < 0)
278 result = 0;
280 return result;
281}
283/**
284 * Update a fd_set with all of the sockets in use.
285 */
286static void t758_updateFdSet(struct t758_Sockets *sockets, fd_set *fdset,
287 curl_socket_t *maxFd)
288{
289 int i;
290 for(i = 0; i < sockets->count; ++i) {
291 FD_SET(sockets->sockets[i], fdset);
292 if(*maxFd < sockets->sockets[i] + 1) {
293 *maxFd = sockets->sockets[i] + 1;
294 }
295 }
296}
298static CURLMcode t758_saction(CURLM *multi, curl_socket_t s,
299 int evBitmask, const char *info)
300{
301 int numhandles = 0;
302 CURLMcode mresult = curl_multi_socket_action(multi, s, evBitmask,
303 &numhandles);
304 if(mresult != CURLM_OK) {
305 curl_mfprintf(stderr, "%s curl error on %s (%d) %s\n",
306 t758_tag(), info, mresult, curl_multi_strerror(mresult));
307 }
308 return mresult;
309}
311/**
312 * Invoke curl when a file descriptor is set.
313 */
314static CURLMcode t758_checkFdSet(CURLM *multi, struct t758_Sockets *sockets,
315 fd_set *fdset, int evBitmask,
316 const char *name)
317{
318 int i;
319 CURLMcode mresult = CURLM_OK;
320 for(i = 0; i < sockets->count; ++i) {
321 if(FD_ISSET(sockets->sockets[i], fdset)) {
322 mresult = t758_saction(multi, sockets->sockets[i], evBitmask, name);
323 if(mresult)
324 break;
325 }
326 }
327 return mresult;
328}
330static CURLcode t758_one(const char *URL, int timer_fail_at,
331 int socket_fail_at)
332{
333 CURLcode result = CURLE_OK;
334 CURL *curl = NULL;
335 CURLM *multi = NULL;
336 struct t758_ReadWriteSockets sockets = { { NULL, 0, 0 }, { NULL, 0, 0 } };
337 int success = 0;
338 struct curltime timeout = { 0 };
339 timeout.tv_sec = (time_t)-1;
341 /* set the limits */
342 memset(&t758_ctx, 0, sizeof(t758_ctx));
343 t758_ctx.max_timer_calls = timer_fail_at;
344 t758_ctx.max_socket_calls = socket_fail_at;
346 t758_msg("start");
347 start_test_timing();
349 if(curl_global_sslset(CURLSSLBACKEND_OPENSSL, NULL, NULL) != CURLSSLSET_OK) {
350 t758_msg("could not set OpenSSL as backend");
351 result = CURLE_FAILED_INIT;
352 return result;
353 }
355 res_global_init(CURL_GLOBAL_ALL);
356 if(result != CURLE_OK)
357 return result;
359 curl_global_trace("all");
361 easy_init(curl);
362 debug_config.nohex = TRUE;
363 debug_config.tracetime = TRUE;
364 easy_setopt(curl, CURLOPT_DEBUGDATA, &debug_config);
365 easy_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
366 easy_setopt(curl, CURLOPT_VERBOSE, 1L);
368 /* specify target */
369 easy_setopt(curl, CURLOPT_URL, URL);
371 /* go verbose */
372 easy_setopt(curl, CURLOPT_VERBOSE, 1L);
374 easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, t758_set_ssl_ctx_callback);
376 multi_init(multi);
378 multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, t758_curlSocketCallback);
379 multi_setopt(multi, CURLMOPT_SOCKETDATA, &sockets);
381 multi_setopt(multi, CURLMOPT_TIMERFUNCTION, t758_curlTimerCallback);
382 multi_setopt(multi, CURLMOPT_TIMERDATA, &timeout);
384 multi_add_handle(multi, curl);
386 if(t758_saction(multi, CURL_SOCKET_TIMEOUT, 0, "timeout")) {
387 result = TEST_ERR_MAJOR_BAD;
388 goto test_cleanup;
389 }
390 while(!t758_checkForCompletion(multi, &success)) {
391 fd_set readSet, writeSet;
392 curl_socket_t maxFd = 0;
393 struct timeval tv = { 0 };
394 tv.tv_sec = 10;
396 if(t758_ctx.fake_async_cert_verification_pending &&
397 !t758_ctx.fake_async_cert_verification_finished) {
398 /* the wakeup socket is monitored */
399 if((sockets.read.count > 1) || sockets.write.count) {
400 t758_msg("during verification there should be no sockets scheduled");
401 result = TEST_ERR_MAJOR_BAD;
402 goto test_cleanup;
403 }
404 if(t758_ctx.number_of_cert_verify_callbacks != 1) {
405 t758_msg("expecting exactly one cert verify callback here");
406 result = TEST_ERR_MAJOR_BAD;
407 goto test_cleanup;
408 }
409 t758_ctx.fake_async_cert_verification_finished = 1;
410 if(t758_saction(multi, CURL_SOCKET_TIMEOUT, 0, "timeout")) {
411 t758_msg("spurious retry cert action");
412 result = TEST_ERR_MAJOR_BAD;
413 goto test_cleanup;
414 }
415 curl_easy_pause(curl, CURLPAUSE_CONT);
416 if(t758_saction(multi, CURL_SOCKET_TIMEOUT, 0, "timeout")) {
417 t758_msg("unblocking transfer after cert verification finished");
418 result = TEST_ERR_MAJOR_BAD;
419 goto test_cleanup;
420 }
421 if(t758_ctx.number_of_cert_verify_callbacks != 2) {
422 t758_msg("this should have triggered the callback again, right?");
423 result = TEST_ERR_MAJOR_BAD;
424 goto test_cleanup;
425 }
426 t758_msg("TEST: all fine?");
427 }
428 FD_ZERO(&readSet);
429 FD_ZERO(&writeSet);
430 t758_updateFdSet(&sockets.read, &readSet, &maxFd);
431 t758_updateFdSet(&sockets.write, &writeSet, &maxFd);
433 if(timeout.tv_sec != (time_t)-1) {
434 int usTimeout = curlx_sztosi(t758_getMicroSecondTimeout(&timeout));
435 tv.tv_sec = usTimeout / 1000000;
436 tv.tv_usec = usTimeout % 1000000;
437 }
438 else if(maxFd <= 0) {
439 tv.tv_sec = 0;
440 tv.tv_usec = 100000;
441 }
443 select_test((int)maxFd, &readSet, &writeSet, NULL, &tv);
445 /* Check the sockets for reading / writing */
446 if(t758_checkFdSet(multi, &sockets.read, &readSet, CURL_CSELECT_IN,
447 "read")) {
448 result = TEST_ERR_MAJOR_BAD;
449 goto test_cleanup;
450 }
451 if(t758_checkFdSet(multi, &sockets.write, &writeSet, CURL_CSELECT_OUT,
452 "write")) {
453 result = TEST_ERR_MAJOR_BAD;
454 goto test_cleanup;
455 }
457 if(timeout.tv_sec != (time_t)-1 &&
458 t758_getMicroSecondTimeout(&timeout) == 0 &&
459 /* curl's timer has elapsed. */
460 t758_saction(multi, CURL_SOCKET_TIMEOUT, 0, "timeout")) {
461 result = TEST_ERR_BAD_TIMEOUT;
462 goto test_cleanup;
463 }
465 abort_on_test_timeout();
466 }
467 if(success && t758_ctx.number_of_cert_verify_callbacks != 2) {
468 t758_msg("unexpected invocations of cert verify callback");
469 result = TEST_ERR_MAJOR_BAD;
470 goto test_cleanup;
471 }
473 if(!success) {
474 t758_msg("Error getting file.");
475 result = TEST_ERR_MAJOR_BAD;
476 }
478test_cleanup:
480 /* proper cleanup sequence */
481 t758_msg("cleanup");
482 curl_multi_remove_handle(multi, curl);
483 curl_easy_cleanup(curl);
484 curl_multi_cleanup(multi);
485 curl_global_cleanup();
487 /* free local memory */
488 curlx_free(sockets.read.sockets);
489 curlx_free(sockets.write.sockets);
490 t758_msg("done");
492 return result;
493}
495static CURLcode test_lib758(const char *URL)
496{
497 CURLcode result;
498 /* rerun the same transfer multiple times and make it fail in different
499 callback calls */
500 result = t758_one(URL, 0, 0); /* no callback fails */
501 if(result)
502 curl_mfprintf(stderr, "%s FAILED: %d\n", t758_tag(), (int)result);
504 return result;
505}
507#else /* T578_ENABLED */
508static CURLcode test_lib758(const char *URL)
509{
510 (void)URL;
511 return CURLE_OK;
512}
513#endif
Diagnostics4 git calls · 300 ms
CallStatusSizeTime0300 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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 · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 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 · hit2 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=2 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
fetch2004.6 KiB295 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=295 git.response.bytes=4702 git.response.cache=MISS git.response.duration_ms=295 git.fetch.wants=379cc873faa3bd7f447f1894b7855fc2f2d87066 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=4702 git.pkt.payload.max=4681 git.sideband.band1.bytes=4680 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
- a5e31b7bfce38682
- Request
- 738d9e46…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format