lib525.c 4.3 KB

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