lib582.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. ***************************************************************************/
  22. #include "test.h"
  23. #include <fcntl.h>
  24. #include "testutil.h"
  25. #include "warnless.h"
  26. #include "memdebug.h"
  27. #define TEST_HANG_TIMEOUT 60 * 1000
  28. struct Sockets
  29. {
  30. curl_socket_t *sockets;
  31. int count; /* number of sockets actually stored in array */
  32. int max_count; /* max number of sockets that fit in allocated array */
  33. };
  34. struct ReadWriteSockets
  35. {
  36. struct Sockets read, write;
  37. };
  38. /**
  39. * Remove a file descriptor from a sockets array.
  40. */
  41. static void removeFd(struct Sockets *sockets, curl_socket_t fd, int mention)
  42. {
  43. int i;
  44. if(mention)
  45. fprintf(stderr, "Remove socket fd %d\n", (int) fd);
  46. for(i = 0; i < sockets->count; ++i) {
  47. if(sockets->sockets[i] == fd) {
  48. if(i < sockets->count - 1)
  49. memmove(&sockets->sockets[i], &sockets->sockets[i + 1],
  50. sizeof(curl_socket_t) * (sockets->count - (i + 1)));
  51. --sockets->count;
  52. }
  53. }
  54. }
  55. /**
  56. * Add a file descriptor to a sockets array.
  57. */
  58. static void addFd(struct Sockets *sockets, curl_socket_t fd, const char *what)
  59. {
  60. /**
  61. * To ensure we only have each file descriptor once, we remove it then add
  62. * it again.
  63. */
  64. fprintf(stderr, "Add socket fd %d for %s\n", (int) fd, what);
  65. removeFd(sockets, fd, 0);
  66. /*
  67. * Allocate array storage when required.
  68. */
  69. if(!sockets->sockets) {
  70. sockets->sockets = malloc(sizeof(curl_socket_t) * 20U);
  71. if(!sockets->sockets)
  72. return;
  73. sockets->max_count = 20;
  74. }
  75. else if(sockets->count + 1 > sockets->max_count) {
  76. curl_socket_t *oldptr = sockets->sockets;
  77. sockets->sockets = realloc(oldptr, sizeof(curl_socket_t) *
  78. (sockets->max_count + 20));
  79. if(!sockets->sockets) {
  80. /* cleanup in test_cleanup */
  81. sockets->sockets = oldptr;
  82. return;
  83. }
  84. sockets->max_count += 20;
  85. }
  86. /*
  87. * Add file descriptor to array.
  88. */
  89. sockets->sockets[sockets->count] = fd;
  90. ++sockets->count;
  91. }
  92. /**
  93. * Callback invoked by curl to poll reading / writing of a socket.
  94. */
  95. static int curlSocketCallback(CURL *easy, curl_socket_t s, int action,
  96. void *userp, void *socketp)
  97. {
  98. struct ReadWriteSockets *sockets = userp;
  99. (void)easy; /* unused */
  100. (void)socketp; /* unused */
  101. if(action == CURL_POLL_IN || action == CURL_POLL_INOUT)
  102. addFd(&sockets->read, s, "read");
  103. if(action == CURL_POLL_OUT || action == CURL_POLL_INOUT)
  104. addFd(&sockets->write, s, "write");
  105. if(action == CURL_POLL_REMOVE) {
  106. removeFd(&sockets->read, s, 1);
  107. removeFd(&sockets->write, s, 0);
  108. }
  109. return 0;
  110. }
  111. /**
  112. * Callback invoked by curl to set a timeout.
  113. */
  114. static int curlTimerCallback(CURLM *multi, long timeout_ms, void *userp)
  115. {
  116. struct timeval *timeout = userp;
  117. (void)multi; /* unused */
  118. if(timeout_ms != -1) {
  119. *timeout = tutil_tvnow();
  120. timeout->tv_usec += timeout_ms * 1000;
  121. }
  122. else {
  123. timeout->tv_sec = -1;
  124. }
  125. return 0;
  126. }
  127. /**
  128. * Check for curl completion.
  129. */
  130. static int checkForCompletion(CURLM *curl, int *success)
  131. {
  132. int result = 0;
  133. *success = 0;
  134. while(1) {
  135. int numMessages;
  136. CURLMsg *message = curl_multi_info_read(curl, &numMessages);
  137. if(!message)
  138. break;
  139. if(message->msg == CURLMSG_DONE) {
  140. result = 1;
  141. if(message->data.result == CURLE_OK)
  142. *success = 1;
  143. else
  144. *success = 0;
  145. }
  146. else {
  147. fprintf(stderr, "Got an unexpected message from curl: %i\n",
  148. (int)message->msg);
  149. result = 1;
  150. *success = 0;
  151. }
  152. }
  153. return result;
  154. }
  155. static int getMicroSecondTimeout(struct timeval *timeout)
  156. {
  157. struct timeval now;
  158. ssize_t result;
  159. now = tutil_tvnow();
  160. result = (ssize_t)((timeout->tv_sec - now.tv_sec) * 1000000 +
  161. timeout->tv_usec - now.tv_usec);
  162. if(result < 0)
  163. result = 0;
  164. return curlx_sztosi(result);
  165. }
  166. /**
  167. * Update a fd_set with all of the sockets in use.
  168. */
  169. static void updateFdSet(struct Sockets *sockets, fd_set* fdset,
  170. curl_socket_t *maxFd)
  171. {
  172. int i;
  173. for(i = 0; i < sockets->count; ++i) {
  174. FD_SET(sockets->sockets[i], fdset);
  175. if(*maxFd < sockets->sockets[i] + 1) {
  176. *maxFd = sockets->sockets[i] + 1;
  177. }
  178. }
  179. }
  180. static void notifyCurl(CURLM *curl, curl_socket_t s, int evBitmask,
  181. const char *info)
  182. {
  183. int numhandles = 0;
  184. CURLMcode result = curl_multi_socket_action(curl, s, evBitmask, &numhandles);
  185. if(result != CURLM_OK) {
  186. fprintf(stderr, "Curl error on %s: %i (%s)\n",
  187. info, result, curl_multi_strerror(result));
  188. }
  189. }
  190. /**
  191. * Invoke curl when a file descriptor is set.
  192. */
  193. static void checkFdSet(CURLM *curl, struct Sockets *sockets, fd_set *fdset,
  194. int evBitmask, const char *name)
  195. {
  196. int i;
  197. for(i = 0; i < sockets->count; ++i) {
  198. if(FD_ISSET(sockets->sockets[i], fdset)) {
  199. notifyCurl(curl, sockets->sockets[i], evBitmask, name);
  200. }
  201. }
  202. }
  203. int test(char *URL)
  204. {
  205. int res = 0;
  206. CURL *curl = NULL;
  207. FILE *hd_src = NULL;
  208. int hd;
  209. struct_stat file_info;
  210. CURLM *m = NULL;
  211. struct ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
  212. struct timeval timeout = {-1, 0};
  213. int success = 0;
  214. start_test_timing();
  215. if(!libtest_arg3) {
  216. fprintf(stderr, "Usage: lib582 [url] [filename] [username]\n");
  217. return TEST_ERR_USAGE;
  218. }
  219. hd_src = fopen(libtest_arg2, "rb");
  220. if(!hd_src) {
  221. fprintf(stderr, "fopen() failed with error: %d (%s)\n",
  222. errno, strerror(errno));
  223. fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
  224. return TEST_ERR_FOPEN;
  225. }
  226. /* get the file size of the local file */
  227. hd = fstat(fileno(hd_src), &file_info);
  228. if(hd == -1) {
  229. /* can't open file, bail out */
  230. fprintf(stderr, "fstat() failed with error: %d (%s)\n",
  231. errno, strerror(errno));
  232. fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
  233. fclose(hd_src);
  234. return TEST_ERR_FSTAT;
  235. }
  236. fprintf(stderr, "Set to upload %d bytes\n", (int)file_info.st_size);
  237. res_global_init(CURL_GLOBAL_ALL);
  238. if(res) {
  239. fclose(hd_src);
  240. return res;
  241. }
  242. easy_init(curl);
  243. /* enable uploading */
  244. easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  245. /* specify target */
  246. easy_setopt(curl, CURLOPT_URL, URL);
  247. /* go verbose */
  248. easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  249. /* now specify which file to upload */
  250. easy_setopt(curl, CURLOPT_READDATA, hd_src);
  251. easy_setopt(curl, CURLOPT_USERPWD, libtest_arg3);
  252. easy_setopt(curl, CURLOPT_SSH_PUBLIC_KEYFILE, "curl_client_key.pub");
  253. easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE, "curl_client_key");
  254. easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  255. easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
  256. multi_init(m);
  257. multi_setopt(m, CURLMOPT_SOCKETFUNCTION, curlSocketCallback);
  258. multi_setopt(m, CURLMOPT_SOCKETDATA, &sockets);
  259. multi_setopt(m, CURLMOPT_TIMERFUNCTION, curlTimerCallback);
  260. multi_setopt(m, CURLMOPT_TIMERDATA, &timeout);
  261. multi_add_handle(m, curl);
  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. select_test((int)maxFd, &readSet, &writeSet, NULL, &tv);
  280. /* Check the sockets for reading / writing */
  281. checkFdSet(m, &sockets.read, &readSet, CURL_CSELECT_IN, "read");
  282. checkFdSet(m, &sockets.write, &writeSet, CURL_CSELECT_OUT, "write");
  283. if(timeout.tv_sec != -1 && getMicroSecondTimeout(&timeout) == 0) {
  284. /* Curl's timer has elapsed. */
  285. notifyCurl(m, CURL_SOCKET_TIMEOUT, 0, "timeout");
  286. }
  287. abort_on_test_timeout();
  288. }
  289. if(!success) {
  290. fprintf(stderr, "Error uploading file.\n");
  291. res = TEST_ERR_MAJOR_BAD;
  292. }
  293. test_cleanup:
  294. /* proper cleanup sequence - type PB */
  295. curl_multi_remove_handle(m, curl);
  296. curl_easy_cleanup(curl);
  297. curl_multi_cleanup(m);
  298. curl_global_cleanup();
  299. /* close the local file */
  300. fclose(hd_src);
  301. /* free local memory */
  302. free(sockets.read.sockets);
  303. free(sockets.write.sockets);
  304. return res;
  305. }