lib591.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include "test.h"
  25. /* lib591 is used for test cases 591, 592, 593 and 594 */
  26. #include <limits.h>
  27. #include <fcntl.h>
  28. #include "testutil.h"
  29. #include "warnless.h"
  30. #include "memdebug.h"
  31. #define TEST_HANG_TIMEOUT 60 * 1000
  32. int test(char *URL)
  33. {
  34. CURL *easy = NULL;
  35. CURLM *multi = NULL;
  36. int res = 0;
  37. int running;
  38. int msgs_left;
  39. CURLMsg *msg;
  40. FILE *upload = NULL;
  41. start_test_timing();
  42. upload = fopen(libtest_arg3, "rb");
  43. if(!upload) {
  44. fprintf(stderr, "fopen() failed with error: %d (%s)\n",
  45. errno, strerror(errno));
  46. fprintf(stderr, "Error opening file: (%s)\n", libtest_arg3);
  47. return TEST_ERR_FOPEN;
  48. }
  49. res_global_init(CURL_GLOBAL_ALL);
  50. if(res) {
  51. fclose(upload);
  52. return res;
  53. }
  54. easy_init(easy);
  55. /* go verbose */
  56. easy_setopt(easy, CURLOPT_VERBOSE, 1L);
  57. /* specify target */
  58. easy_setopt(easy, CURLOPT_URL, URL);
  59. /* enable uploading */
  60. easy_setopt(easy, CURLOPT_UPLOAD, 1L);
  61. /* data pointer for the file read function */
  62. easy_setopt(easy, CURLOPT_READDATA, upload);
  63. /* use active mode FTP */
  64. easy_setopt(easy, CURLOPT_FTPPORT, "-");
  65. /* server connection timeout */
  66. easy_setopt(easy, CURLOPT_ACCEPTTIMEOUT_MS,
  67. strtol(libtest_arg2, NULL, 10)*1000);
  68. multi_init(multi);
  69. multi_add_handle(multi, easy);
  70. for(;;) {
  71. struct timeval interval;
  72. fd_set fdread;
  73. fd_set fdwrite;
  74. fd_set fdexcep;
  75. long timeout = -99;
  76. int maxfd = -99;
  77. multi_perform(multi, &running);
  78. abort_on_test_timeout();
  79. if(!running)
  80. break; /* done */
  81. FD_ZERO(&fdread);
  82. FD_ZERO(&fdwrite);
  83. FD_ZERO(&fdexcep);
  84. multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
  85. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  86. multi_timeout(multi, &timeout);
  87. /* At this point, timeout is guaranteed to be greater or equal than -1. */
  88. if(timeout != -1L) {
  89. int itimeout;
  90. #if LONG_MAX > INT_MAX
  91. itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
  92. #else
  93. itimeout = (int)timeout;
  94. #endif
  95. interval.tv_sec = itimeout/1000;
  96. interval.tv_usec = (itimeout%1000)*1000;
  97. }
  98. else {
  99. interval.tv_sec = 0;
  100. interval.tv_usec = 100000L; /* 100 ms */
  101. }
  102. select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
  103. abort_on_test_timeout();
  104. }
  105. msg = curl_multi_info_read(multi, &msgs_left);
  106. if(msg)
  107. res = msg->data.result;
  108. test_cleanup:
  109. /* undocumented cleanup sequence - type UA */
  110. curl_multi_cleanup(multi);
  111. curl_easy_cleanup(easy);
  112. curl_global_cleanup();
  113. /* close the local file */
  114. fclose(upload);
  115. return res;
  116. }