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