lib525.c 4.2 KB

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