lib530.c 9.7 KB

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