lib530.c 9.9 KB

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