lib525.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. */
  10. #include "test.h"
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include "testutil.h"
  15. #define MAIN_LOOP_HANG_TIMEOUT 90 * 1000
  16. #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
  17. int test(char *URL)
  18. {
  19. int res = 0;
  20. CURL *curl;
  21. FILE *hd_src ;
  22. int hd ;
  23. int error;
  24. struct_stat file_info;
  25. int running;
  26. char done=FALSE;
  27. CURLM *m;
  28. struct timeval ml_start;
  29. struct timeval mp_start;
  30. char ml_timedout = FALSE;
  31. char mp_timedout = FALSE;
  32. if (!libtest_arg2) {
  33. fprintf(stderr, "Usage: lib525 [url] [uploadfile]\n");
  34. return -1;
  35. }
  36. /* get the file size of the local file */
  37. hd = open(libtest_arg2, O_RDONLY) ;
  38. fstat(hd, &file_info);
  39. close(hd) ;
  40. /* get a FILE * of the same file, could also be made with
  41. fdopen() from the previous descriptor, but hey this is just
  42. an example! */
  43. hd_src = fopen(libtest_arg2, "rb");
  44. if(NULL == hd_src) {
  45. error = ERRNO;
  46. fprintf(stderr, "fopen() failed with error: %d %s\n",
  47. error, strerror(error));
  48. fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
  49. return TEST_ERR_MAJOR_BAD;
  50. }
  51. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  52. fprintf(stderr, "curl_global_init() failed\n");
  53. fclose(hd_src);
  54. return TEST_ERR_MAJOR_BAD;
  55. }
  56. if ((curl = curl_easy_init()) == NULL) {
  57. fprintf(stderr, "curl_easy_init() failed\n");
  58. fclose(hd_src);
  59. curl_global_cleanup();
  60. return TEST_ERR_MAJOR_BAD;
  61. }
  62. /* enable uploading */
  63. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  64. /* specify target */
  65. curl_easy_setopt(curl,CURLOPT_URL, URL);
  66. /* go verbose */
  67. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  68. /* use active FTP */
  69. curl_easy_setopt(curl, CURLOPT_FTPPORT, "-");
  70. /* now specify which file to upload */
  71. curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
  72. /* NOTE: if you want this code to work on Windows with libcurl as a DLL, you
  73. MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to
  74. do so will give you a crash since a DLL may not use the variable's memory
  75. when passed in to it from an app like this. */
  76. /* Set the size of the file to upload (optional). If you give a *_LARGE
  77. option you MUST make sure that the type of the passed-in argument is a
  78. curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
  79. make sure that to pass in a type 'long' argument. */
  80. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  81. (curl_off_t)file_info.st_size);
  82. if ((m = curl_multi_init()) == NULL) {
  83. fprintf(stderr, "curl_multi_init() failed\n");
  84. curl_easy_cleanup(curl);
  85. curl_global_cleanup();
  86. fclose(hd_src);
  87. return TEST_ERR_MAJOR_BAD;
  88. }
  89. if ((res = (int)curl_multi_add_handle(m, curl)) != CURLM_OK) {
  90. fprintf(stderr, "curl_multi_add_handle() failed, "
  91. "with code %d\n", res);
  92. curl_multi_cleanup(m);
  93. curl_easy_cleanup(curl);
  94. curl_global_cleanup();
  95. fclose(hd_src);
  96. return TEST_ERR_MAJOR_BAD;
  97. }
  98. ml_timedout = FALSE;
  99. ml_start = tutil_tvnow();
  100. while (!done) {
  101. fd_set rd, wr, exc;
  102. int max_fd;
  103. struct timeval interval;
  104. interval.tv_sec = 1;
  105. interval.tv_usec = 0;
  106. if (tutil_tvdiff(tutil_tvnow(), ml_start) >
  107. MAIN_LOOP_HANG_TIMEOUT) {
  108. ml_timedout = TRUE;
  109. break;
  110. }
  111. mp_timedout = FALSE;
  112. mp_start = tutil_tvnow();
  113. while (res == CURLM_CALL_MULTI_PERFORM) {
  114. res = (int)curl_multi_perform(m, &running);
  115. if (tutil_tvdiff(tutil_tvnow(), mp_start) >
  116. MULTI_PERFORM_HANG_TIMEOUT) {
  117. mp_timedout = TRUE;
  118. break;
  119. }
  120. if (running <= 0) {
  121. done = TRUE;
  122. break;
  123. }
  124. }
  125. if (mp_timedout || done)
  126. break;
  127. if (res != CURLM_OK) {
  128. fprintf(stderr, "not okay???\n");
  129. break;
  130. }
  131. FD_ZERO(&rd);
  132. FD_ZERO(&wr);
  133. FD_ZERO(&exc);
  134. max_fd = 0;
  135. if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) {
  136. fprintf(stderr, "unexpected failured of fdset.\n");
  137. res = 189;
  138. break;
  139. }
  140. if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
  141. fprintf(stderr, "bad select??\n");
  142. res = 195;
  143. break;
  144. }
  145. res = CURLM_CALL_MULTI_PERFORM;
  146. }
  147. if (ml_timedout || mp_timedout) {
  148. if (ml_timedout) fprintf(stderr, "ml_timedout\n");
  149. if (mp_timedout) fprintf(stderr, "mp_timedout\n");
  150. fprintf(stderr, "ABORTING TEST, since it seems "
  151. "that it would have run forever.\n");
  152. res = TEST_ERR_RUNS_FOREVER;
  153. }
  154. #ifdef LIB529
  155. /* test 529 */
  156. curl_multi_remove_handle(m, curl);
  157. curl_multi_cleanup(m);
  158. curl_easy_cleanup(curl);
  159. #else
  160. /* test 525 */
  161. curl_multi_remove_handle(m, curl);
  162. curl_easy_cleanup(curl);
  163. curl_multi_cleanup(m);
  164. #endif
  165. fclose(hd_src); /* close the local file */
  166. curl_global_cleanup();
  167. return res;
  168. }