lib530.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. #if defined(__DJGPP__)
  196. #pragma GCC diagnostic push
  197. #pragma GCC diagnostic ignored "-Warith-conversion"
  198. #endif
  199. FD_SET(sockets->sockets[i], fdset);
  200. #if defined(__DJGPP__)
  201. #pragma GCC diagnostic pop
  202. #endif
  203. if(*maxFd < sockets->sockets[i] + 1) {
  204. *maxFd = sockets->sockets[i] + 1;
  205. }
  206. }
  207. }
  208. static int socket_action(CURLM *curl, curl_socket_t s, int evBitmask,
  209. const char *info)
  210. {
  211. int numhandles = 0;
  212. CURLMcode result = curl_multi_socket_action(curl, s, evBitmask, &numhandles);
  213. if(result != CURLM_OK) {
  214. fprintf(stderr, "Curl error on %s: %i (%s)\n",
  215. info, result, curl_multi_strerror(result));
  216. }
  217. return (int)result;
  218. }
  219. /**
  220. * Invoke curl when a file descriptor is set.
  221. */
  222. static int checkFdSet(CURLM *curl,
  223. struct Sockets *sockets, fd_set *fdset,
  224. int evBitmask, const char *name)
  225. {
  226. int i;
  227. int result = 0;
  228. for(i = 0; i < sockets->count; ++i) {
  229. if(FD_ISSET(sockets->sockets[i], fdset)) {
  230. result = socket_action(curl, sockets->sockets[i], evBitmask, name);
  231. if(result)
  232. break;
  233. }
  234. }
  235. return result;
  236. }
  237. static CURLcode testone(char *URL, int timercb, int socketcb)
  238. {
  239. CURLcode res = CURLE_OK;
  240. CURL *curl = NULL; CURLM *m = NULL;
  241. struct ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
  242. int success = 0;
  243. struct timeval timeout = {0};
  244. timeout.tv_sec = (time_t)-1;
  245. /* set the limits */
  246. max_timer_calls = timercb;
  247. max_socket_calls = socketcb;
  248. timer_calls = 0; /* reset the globals */
  249. socket_calls = 0;
  250. fprintf(stderr, "start test: %d %d\n", timercb, socketcb);
  251. start_test_timing();
  252. res_global_init(CURL_GLOBAL_ALL);
  253. if(res != CURLE_OK)
  254. return res;
  255. easy_init(curl);
  256. /* specify target */
  257. easy_setopt(curl, CURLOPT_URL, URL);
  258. /* go verbose */
  259. easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  260. multi_init(m);
  261. multi_setopt(m, CURLMOPT_SOCKETFUNCTION, curlSocketCallback);
  262. multi_setopt(m, CURLMOPT_SOCKETDATA, &sockets);
  263. multi_setopt(m, CURLMOPT_TIMERFUNCTION, curlTimerCallback);
  264. multi_setopt(m, CURLMOPT_TIMERDATA, &timeout);
  265. multi_add_handle(m, curl);
  266. if(socket_action(m, CURL_SOCKET_TIMEOUT, 0, "timeout")) {
  267. res = TEST_ERR_MAJOR_BAD;
  268. goto test_cleanup;
  269. }
  270. while(!checkForCompletion(m, &success)) {
  271. fd_set readSet, writeSet;
  272. curl_socket_t maxFd = 0;
  273. struct timeval tv = {0};
  274. tv.tv_sec = 10;
  275. FD_ZERO(&readSet);
  276. FD_ZERO(&writeSet);
  277. updateFdSet(&sockets.read, &readSet, &maxFd);
  278. updateFdSet(&sockets.write, &writeSet, &maxFd);
  279. if(timeout.tv_sec != (time_t)-1) {
  280. int usTimeout = getMicroSecondTimeout(&timeout);
  281. tv.tv_sec = usTimeout / 1000000;
  282. tv.tv_usec = usTimeout % 1000000;
  283. }
  284. else if(maxFd <= 0) {
  285. tv.tv_sec = 0;
  286. tv.tv_usec = 100000;
  287. }
  288. assert(maxFd);
  289. select_test((int)maxFd, &readSet, &writeSet, NULL, &tv);
  290. /* Check the sockets for reading / writing */
  291. if(checkFdSet(m, &sockets.read, &readSet, CURL_CSELECT_IN, "read")) {
  292. res = TEST_ERR_MAJOR_BAD;
  293. goto test_cleanup;
  294. }
  295. if(checkFdSet(m, &sockets.write, &writeSet, CURL_CSELECT_OUT, "write")) {
  296. res = TEST_ERR_MAJOR_BAD;
  297. goto test_cleanup;
  298. }
  299. if(timeout.tv_sec != (time_t)-1 && getMicroSecondTimeout(&timeout) == 0) {
  300. /* Curl's timer has elapsed. */
  301. if(socket_action(m, CURL_SOCKET_TIMEOUT, 0, "timeout")) {
  302. res = TEST_ERR_BAD_TIMEOUT;
  303. goto test_cleanup;
  304. }
  305. }
  306. abort_on_test_timeout();
  307. }
  308. if(!success) {
  309. fprintf(stderr, "Error getting file.\n");
  310. res = TEST_ERR_MAJOR_BAD;
  311. }
  312. test_cleanup:
  313. /* proper cleanup sequence */
  314. fprintf(stderr, "cleanup: %d %d\n", timercb, socketcb);
  315. curl_multi_remove_handle(m, curl);
  316. curl_easy_cleanup(curl);
  317. curl_multi_cleanup(m);
  318. curl_global_cleanup();
  319. /* free local memory */
  320. free(sockets.read.sockets);
  321. free(sockets.write.sockets);
  322. return res;
  323. }
  324. CURLcode test(char *URL)
  325. {
  326. CURLcode rc;
  327. /* rerun the same transfer multiple times and make it fail in different
  328. callback calls */
  329. rc = testone(URL, 0, 0);
  330. if(rc)
  331. fprintf(stderr, "test 0/0 failed: %d\n", rc);
  332. rc = testone(URL, 1, 0);
  333. if(!rc)
  334. fprintf(stderr, "test 1/0 failed: %d\n", rc);
  335. rc = testone(URL, 2, 0);
  336. if(!rc)
  337. fprintf(stderr, "test 2/0 failed: %d\n", rc);
  338. rc = testone(URL, 0, 1);
  339. if(!rc)
  340. fprintf(stderr, "test 0/1 failed: %d\n", rc);
  341. rc = testone(URL, 0, 2);
  342. if(!rc)
  343. fprintf(stderr, "test 0/2 failed: %d\n", rc);
  344. return CURLE_OK;
  345. }