xref: /curl/tests/libtest/lib530.c (revision 25cbc2f7)
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  ***************************************************************************/
24 
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  */
30 
31 #include "test.h"
32 
33 #include <fcntl.h>
34 
35 #include "testutil.h"
36 #include "warnless.h"
37 #include "memdebug.h"
38 
39 #define TEST_HANG_TIMEOUT 60 * 1000
40 
41 struct Sockets
42 {
43   curl_socket_t *sockets;
44   int count;      /* number of sockets actually stored in array */
45   int max_count;  /* max number of sockets that fit in allocated array */
46 };
47 
48 struct ReadWriteSockets
49 {
50   struct Sockets read, write;
51 };
52 
53 /**
54  * Remove a file descriptor from a sockets array.
55  */
removeFd(struct Sockets * sockets,curl_socket_t fd,int mention)56 static void removeFd(struct Sockets *sockets, curl_socket_t fd, int mention)
57 {
58   int i;
59 
60   if(mention)
61     fprintf(stderr, "Remove socket fd %d\n", (int) fd);
62 
63   for(i = 0; i < sockets->count; ++i) {
64     if(sockets->sockets[i] == fd) {
65       if(i < sockets->count - 1)
66         memmove(&sockets->sockets[i], &sockets->sockets[i + 1],
67               sizeof(curl_socket_t) * (sockets->count - (i + 1)));
68       --sockets->count;
69     }
70   }
71 }
72 
73 /**
74  * Add a file descriptor to a sockets array.
75  * Return 0 on success, 1 on error.
76  */
addFd(struct Sockets * sockets,curl_socket_t fd,const char * what)77 static int addFd(struct Sockets *sockets, curl_socket_t fd, const char *what)
78 {
79   /**
80    * To ensure we only have each file descriptor once, we remove it then add
81    * it again.
82    */
83   fprintf(stderr, "Add socket fd %d for %s\n", (int) fd, what);
84   removeFd(sockets, fd, 0);
85   /*
86    * Allocate array storage when required.
87    */
88   if(!sockets->sockets) {
89     sockets->sockets = malloc(sizeof(curl_socket_t) * 20U);
90     if(!sockets->sockets)
91       return 1;
92     sockets->max_count = 20;
93   }
94   else if(sockets->count + 1 > sockets->max_count) {
95     curl_socket_t *ptr = realloc(sockets->sockets, sizeof(curl_socket_t) *
96                                  (sockets->max_count + 20));
97     if(!ptr)
98       /* cleanup in test_cleanup */
99       return 1;
100     sockets->sockets = ptr;
101     sockets->max_count += 20;
102   }
103   /*
104    * Add file descriptor to array.
105    */
106   sockets->sockets[sockets->count] = fd;
107   ++sockets->count;
108   return 0;
109 }
110 
111 static int max_socket_calls;
112 static int socket_calls = 0;
113 
114 /**
115  * Callback invoked by curl to poll reading / writing of a socket.
116  */
curlSocketCallback(CURL * easy,curl_socket_t s,int action,void * userp,void * socketp)117 static int curlSocketCallback(CURL *easy, curl_socket_t s, int action,
118                               void *userp, void *socketp)
119 {
120   struct ReadWriteSockets *sockets = userp;
121 
122   (void)easy; /* unused */
123   (void)socketp; /* unused */
124 
125   fprintf(stderr, "CURLMOPT_SOCKETFUNCTION called: %u\n", socket_calls++);
126   if(socket_calls == max_socket_calls) {
127     fprintf(stderr, "curlSocketCallback returns error\n");
128     return -1;
129   }
130 
131   if(action == CURL_POLL_IN || action == CURL_POLL_INOUT)
132     if(addFd(&sockets->read, s, "read"))
133       return -1; /* bail out */
134 
135   if(action == CURL_POLL_OUT || action == CURL_POLL_INOUT)
136     if(addFd(&sockets->write, s, "write"))
137       return -1;
138 
139   if(action == CURL_POLL_REMOVE) {
140     removeFd(&sockets->read, s, 1);
141     removeFd(&sockets->write, s, 0);
142   }
143 
144   return 0;
145 }
146 
147 static int max_timer_calls;
148 static int timer_calls = 0;
149 
150 /**
151  * Callback invoked by curl to set a timeout.
152  */
curlTimerCallback(CURLM * multi,long timeout_ms,void * userp)153 static int curlTimerCallback(CURLM *multi, long timeout_ms, void *userp)
154 {
155   struct timeval *timeout = userp;
156 
157   (void)multi; /* unused */
158   fprintf(stderr, "CURLMOPT_TIMERFUNCTION called: %u\n", timer_calls++);
159   if(timer_calls == max_timer_calls) {
160     fprintf(stderr, "curlTimerCallback returns error\n");
161     return -1;
162   }
163   if(timeout_ms != -1) {
164     *timeout = tutil_tvnow();
165     timeout->tv_usec += (int)timeout_ms * 1000;
166   }
167   else {
168     timeout->tv_sec = -1;
169   }
170   return 0;
171 }
172 
173 /**
174  * Check for curl completion.
175  */
checkForCompletion(CURLM * curl,int * success)176 static int checkForCompletion(CURLM *curl, int *success)
177 {
178   int result = 0;
179   *success = 0;
180   while(1) {
181     int numMessages;
182     CURLMsg *message = curl_multi_info_read(curl, &numMessages);
183     if(!message)
184       break;
185     if(message->msg == CURLMSG_DONE) {
186       result = 1;
187       if(message->data.result == CURLE_OK)
188         *success = 1;
189       else
190         *success = 0;
191     }
192     else {
193       fprintf(stderr, "Got an unexpected message from curl: %i\n",
194               message->msg);
195       result = 1;
196       *success = 0;
197     }
198   }
199   return result;
200 }
201 
getMicroSecondTimeout(struct timeval * timeout)202 static int getMicroSecondTimeout(struct timeval *timeout)
203 {
204   struct timeval now;
205   ssize_t result;
206   now = tutil_tvnow();
207   result = (ssize_t)((timeout->tv_sec - now.tv_sec) * 1000000 +
208     timeout->tv_usec - now.tv_usec);
209   if(result < 0)
210     result = 0;
211 
212   return curlx_sztosi(result);
213 }
214 
215 /**
216  * Update a fd_set with all of the sockets in use.
217  */
updateFdSet(struct Sockets * sockets,fd_set * fdset,curl_socket_t * maxFd)218 static void updateFdSet(struct Sockets *sockets, fd_set* fdset,
219                         curl_socket_t *maxFd)
220 {
221   int i;
222   for(i = 0; i < sockets->count; ++i) {
223     FD_SET(sockets->sockets[i], fdset);
224     if(*maxFd < sockets->sockets[i] + 1) {
225       *maxFd = sockets->sockets[i] + 1;
226     }
227   }
228 }
229 
socket_action(CURLM * curl,curl_socket_t s,int evBitmask,const char * info)230 static int socket_action(CURLM *curl, curl_socket_t s, int evBitmask,
231                          const char *info)
232 {
233   int numhandles = 0;
234   CURLMcode result = curl_multi_socket_action(curl, s, evBitmask, &numhandles);
235   if(result != CURLM_OK) {
236     fprintf(stderr, "Curl error on %s: %i (%s)\n",
237             info, result, curl_multi_strerror(result));
238   }
239   return (int)result;
240 }
241 
242 /**
243  * Invoke curl when a file descriptor is set.
244  */
checkFdSet(CURLM * curl,struct Sockets * sockets,fd_set * fdset,int evBitmask,const char * name)245 static int checkFdSet(CURLM *curl,
246                       struct Sockets *sockets, fd_set *fdset,
247                       int evBitmask, const char *name)
248 {
249   int i;
250   int result = 0;
251   for(i = 0; i < sockets->count; ++i) {
252     if(FD_ISSET(sockets->sockets[i], fdset)) {
253       result = socket_action(curl, sockets->sockets[i], evBitmask, name);
254       if(result)
255         break;
256     }
257   }
258   return result;
259 }
260 
testone(char * URL,int timercb,int socketcb)261 static CURLcode testone(char *URL, int timercb, int socketcb)
262 {
263   CURLcode res = CURLE_OK;
264   CURL *curl = NULL;  CURLM *m = NULL;
265   struct ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
266   struct timeval timeout = {-1, 0};
267   int success = 0;
268 
269   /* set the limits */
270   max_timer_calls = timercb;
271   max_socket_calls = socketcb;
272   timer_calls = 0; /* reset the globals */
273   socket_calls = 0;
274 
275   fprintf(stderr, "start test: %d %d\n", timercb, socketcb);
276   start_test_timing();
277 
278   res_global_init(CURL_GLOBAL_ALL);
279   if(res != CURLE_OK)
280     return res;
281 
282   easy_init(curl);
283 
284   /* specify target */
285   easy_setopt(curl, CURLOPT_URL, URL);
286 
287   /* go verbose */
288   easy_setopt(curl, CURLOPT_VERBOSE, 1L);
289 
290   multi_init(m);
291 
292   multi_setopt(m, CURLMOPT_SOCKETFUNCTION, curlSocketCallback);
293   multi_setopt(m, CURLMOPT_SOCKETDATA, &sockets);
294 
295   multi_setopt(m, CURLMOPT_TIMERFUNCTION, curlTimerCallback);
296   multi_setopt(m, CURLMOPT_TIMERDATA, &timeout);
297 
298   multi_add_handle(m, curl);
299 
300   if(socket_action(m, CURL_SOCKET_TIMEOUT, 0, "timeout")) {
301     res = TEST_ERR_MAJOR_BAD;
302     goto test_cleanup;
303   }
304 
305   while(!checkForCompletion(m, &success)) {
306     fd_set readSet, writeSet;
307     curl_socket_t maxFd = 0;
308     struct timeval tv = {10, 0};
309 
310     FD_ZERO(&readSet);
311     FD_ZERO(&writeSet);
312     updateFdSet(&sockets.read, &readSet, &maxFd);
313     updateFdSet(&sockets.write, &writeSet, &maxFd);
314 
315     if(timeout.tv_sec != -1) {
316       int usTimeout = getMicroSecondTimeout(&timeout);
317       tv.tv_sec = usTimeout / 1000000;
318       tv.tv_usec = usTimeout % 1000000;
319     }
320     else if(maxFd <= 0) {
321       tv.tv_sec = 0;
322       tv.tv_usec = 100000;
323     }
324 
325     assert(maxFd);
326     select_test((int)maxFd, &readSet, &writeSet, NULL, &tv);
327 
328     /* Check the sockets for reading / writing */
329     if(checkFdSet(m, &sockets.read, &readSet, CURL_CSELECT_IN, "read")) {
330       res = TEST_ERR_MAJOR_BAD;
331       goto test_cleanup;
332     }
333     if(checkFdSet(m, &sockets.write, &writeSet, CURL_CSELECT_OUT, "write")) {
334       res = TEST_ERR_MAJOR_BAD;
335       goto test_cleanup;
336     }
337 
338     if(timeout.tv_sec != -1 && getMicroSecondTimeout(&timeout) == 0) {
339       /* Curl's timer has elapsed. */
340       if(socket_action(m, CURL_SOCKET_TIMEOUT, 0, "timeout")) {
341         res = TEST_ERR_BAD_TIMEOUT;
342         goto test_cleanup;
343       }
344     }
345 
346     abort_on_test_timeout();
347   }
348 
349   if(!success) {
350     fprintf(stderr, "Error getting file.\n");
351     res = TEST_ERR_MAJOR_BAD;
352   }
353 
354 test_cleanup:
355 
356   /* proper cleanup sequence */
357   fprintf(stderr, "cleanup: %d %d\n", timercb, socketcb);
358   curl_multi_remove_handle(m, curl);
359   curl_easy_cleanup(curl);
360   curl_multi_cleanup(m);
361   curl_global_cleanup();
362 
363   /* free local memory */
364   free(sockets.read.sockets);
365   free(sockets.write.sockets);
366   return res;
367 }
368 
test(char * URL)369 CURLcode test(char *URL)
370 {
371   CURLcode rc;
372   /* rerun the same transfer multiple times and make it fail in different
373      callback calls */
374   rc = testone(URL, 0, 0);
375   if(rc)
376     fprintf(stderr, "test 0/0 failed: %d\n", rc);
377 
378   rc = testone(URL, 1, 0);
379   if(!rc)
380     fprintf(stderr, "test 1/0 failed: %d\n", rc);
381 
382   rc = testone(URL, 2, 0);
383   if(!rc)
384     fprintf(stderr, "test 2/0 failed: %d\n", rc);
385 
386   rc = testone(URL, 0, 1);
387   if(!rc)
388     fprintf(stderr, "test 0/1 failed: %d\n", rc);
389 
390   rc = testone(URL, 0, 2);
391   if(!rc)
392     fprintf(stderr, "test 0/2 failed: %d\n", rc);
393 
394   return CURLE_OK;
395 }
396