lib525.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include <fcntl.h>
  26. #include "testutil.h"
  27. #include "warnless.h"
  28. #include "memdebug.h"
  29. #define TEST_HANG_TIMEOUT 60 * 1000
  30. int test(char *URL)
  31. {
  32. int res = 0;
  33. CURL *curl = NULL;
  34. FILE *hd_src = NULL;
  35. int hd;
  36. struct_stat file_info;
  37. CURLM *m = NULL;
  38. int running;
  39. start_test_timing();
  40. if(!libtest_arg2) {
  41. #ifdef LIB529
  42. /* test 529 */
  43. fprintf(stderr, "Usage: lib529 [url] [uploadfile]\n");
  44. #else
  45. /* test 525 */
  46. fprintf(stderr, "Usage: lib525 [url] [uploadfile]\n");
  47. #endif
  48. return TEST_ERR_USAGE;
  49. }
  50. hd_src = fopen(libtest_arg2, "rb");
  51. if(!hd_src) {
  52. fprintf(stderr, "fopen failed with error: %d (%s)\n",
  53. errno, strerror(errno));
  54. fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
  55. return TEST_ERR_FOPEN;
  56. }
  57. /* get the file size of the local file */
  58. hd = fstat(fileno(hd_src), &file_info);
  59. if(hd == -1) {
  60. /* can't open file, bail out */
  61. fprintf(stderr, "fstat() failed with error: %d (%s)\n",
  62. errno, strerror(errno));
  63. fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
  64. fclose(hd_src);
  65. return TEST_ERR_FSTAT;
  66. }
  67. res_global_init(CURL_GLOBAL_ALL);
  68. if(res) {
  69. fclose(hd_src);
  70. return res;
  71. }
  72. easy_init(curl);
  73. /* enable uploading */
  74. easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  75. /* specify target */
  76. easy_setopt(curl, CURLOPT_URL, URL);
  77. /* go verbose */
  78. easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  79. /* use active FTP */
  80. easy_setopt(curl, CURLOPT_FTPPORT, "-");
  81. /* now specify which file to upload */
  82. easy_setopt(curl, CURLOPT_READDATA, hd_src);
  83. /* NOTE: if you want this code to work on Windows with libcurl as a DLL, you
  84. MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to
  85. do so will give you a crash since a DLL may not use the variable's memory
  86. when passed in to it from an app like this. */
  87. /* Set the size of the file to upload (optional). If you give a *_LARGE
  88. option you MUST make sure that the type of the passed-in argument is a
  89. curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
  90. make sure that to pass in a type 'long' argument. */
  91. easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
  92. multi_init(m);
  93. multi_add_handle(m, curl);
  94. for(;;) {
  95. struct timeval interval;
  96. fd_set rd, wr, exc;
  97. int maxfd = -99;
  98. interval.tv_sec = 1;
  99. interval.tv_usec = 0;
  100. multi_perform(m, &running);
  101. abort_on_test_timeout();
  102. if(!running)
  103. break; /* done */
  104. FD_ZERO(&rd);
  105. FD_ZERO(&wr);
  106. FD_ZERO(&exc);
  107. multi_fdset(m, &rd, &wr, &exc, &maxfd);
  108. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  109. select_test(maxfd + 1, &rd, &wr, &exc, &interval);
  110. abort_on_test_timeout();
  111. }
  112. test_cleanup:
  113. #ifdef LIB529
  114. /* test 529 */
  115. /* proper cleanup sequence - type PA */
  116. curl_multi_remove_handle(m, curl);
  117. curl_multi_cleanup(m);
  118. curl_easy_cleanup(curl);
  119. curl_global_cleanup();
  120. #else
  121. /* test 525 */
  122. /* proper cleanup sequence - type PB */
  123. curl_multi_remove_handle(m, curl);
  124. curl_easy_cleanup(curl);
  125. curl_multi_cleanup(m);
  126. curl_global_cleanup();
  127. #endif
  128. /* close the local file */
  129. fclose(hd_src);
  130. return res;
  131. }