lib526.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. */
  10. /*
  11. * This code sets up multiple easy handles that transfer a single file from
  12. * the same URL, in a serial manner after each other. Due to the connection
  13. * sharing within the multi handle all transfers are performed on the same
  14. * persistent connection.
  15. *
  16. * This source code is used for lib526, lib527 and lib532 with only #ifdefs
  17. * controlling the small differences.
  18. *
  19. * - lib526 closes all easy handles after
  20. * they all have transfered the file over the single connection
  21. * - lib527 closes each easy handle after each single transfer.
  22. * - lib532 uses only a single easy handle that is removed, reset and then
  23. * re-added for each transfer
  24. *
  25. * Test case 526, 527 and 532 use FTP, while test 528 uses the lib526 tool but
  26. * with HTTP.
  27. */
  28. #include "test.h"
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #include "testutil.h"
  33. #define MAIN_LOOP_HANG_TIMEOUT 90 * 1000
  34. #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
  35. #define NUM_HANDLES 4
  36. int test(char *URL)
  37. {
  38. int res = 0;
  39. CURL *curl[NUM_HANDLES];
  40. int running;
  41. char done=FALSE;
  42. CURLM *m;
  43. int current=0;
  44. int i, j;
  45. struct timeval ml_start;
  46. struct timeval mp_start;
  47. char ml_timedout = FALSE;
  48. char mp_timedout = FALSE;
  49. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  50. fprintf(stderr, "curl_global_init() failed\n");
  51. return TEST_ERR_MAJOR_BAD;
  52. }
  53. /* get NUM_HANDLES easy handles */
  54. for(i=0; i < NUM_HANDLES; i++) {
  55. curl[i] = curl_easy_init();
  56. if(!curl[i]) {
  57. fprintf(stderr, "curl_easy_init() failed "
  58. "on handle #%d\n", i);
  59. for (j=i-1; j >= 0; j--) {
  60. curl_easy_cleanup(curl[j]);
  61. }
  62. curl_global_cleanup();
  63. return TEST_ERR_MAJOR_BAD + i;
  64. }
  65. curl_easy_setopt(curl[i], CURLOPT_URL, URL);
  66. /* go verbose */
  67. curl_easy_setopt(curl[i], CURLOPT_VERBOSE, 1L);
  68. }
  69. if ((m = curl_multi_init()) == NULL) {
  70. fprintf(stderr, "curl_multi_init() failed\n");
  71. for(i=0; i < NUM_HANDLES; i++) {
  72. curl_easy_cleanup(curl[i]);
  73. }
  74. curl_global_cleanup();
  75. return TEST_ERR_MAJOR_BAD;
  76. }
  77. if ((res = (int)curl_multi_add_handle(m, curl[current])) != CURLM_OK) {
  78. fprintf(stderr, "curl_multi_add_handle() failed, "
  79. "with code %d\n", res);
  80. curl_multi_cleanup(m);
  81. for(i=0; i < NUM_HANDLES; i++) {
  82. curl_easy_cleanup(curl[i]);
  83. }
  84. curl_global_cleanup();
  85. return TEST_ERR_MAJOR_BAD;
  86. }
  87. ml_timedout = FALSE;
  88. ml_start = tutil_tvnow();
  89. fprintf(stderr, "Start at URL 0\n");
  90. while (!done) {
  91. fd_set rd, wr, exc;
  92. int max_fd;
  93. struct timeval interval;
  94. interval.tv_sec = 1;
  95. interval.tv_usec = 0;
  96. if (tutil_tvdiff(tutil_tvnow(), ml_start) >
  97. MAIN_LOOP_HANG_TIMEOUT) {
  98. ml_timedout = TRUE;
  99. break;
  100. }
  101. mp_timedout = FALSE;
  102. mp_start = tutil_tvnow();
  103. while (res == CURLM_CALL_MULTI_PERFORM) {
  104. res = (int)curl_multi_perform(m, &running);
  105. if (tutil_tvdiff(tutil_tvnow(), mp_start) >
  106. MULTI_PERFORM_HANG_TIMEOUT) {
  107. mp_timedout = TRUE;
  108. break;
  109. }
  110. if (running <= 0) {
  111. #ifdef LIB527
  112. /* NOTE: this code does not remove the handle from the multi handle
  113. here, which would be the nice, sane and documented way of working.
  114. This however tests that the API survives this abuse gracefully. */
  115. curl_easy_cleanup(curl[current]);
  116. #endif
  117. if(++current < NUM_HANDLES) {
  118. fprintf(stderr, "Advancing to URL %d\n", current);
  119. #ifdef LIB532
  120. /* first remove the only handle we use */
  121. curl_multi_remove_handle(m, curl[0]);
  122. /* make us re-use the same handle all the time, and try resetting
  123. the handle first too */
  124. curl_easy_reset(curl[0]);
  125. curl_easy_setopt(curl[0], CURLOPT_URL, URL);
  126. curl_easy_setopt(curl[0], CURLOPT_VERBOSE, 1L);
  127. /* re-add it */
  128. res = (int)curl_multi_add_handle(m, curl[0]);
  129. #else
  130. res = (int)curl_multi_add_handle(m, curl[current]);
  131. #endif
  132. if(res) {
  133. fprintf(stderr, "add handle failed: %d.\n", res);
  134. res = 243;
  135. break;
  136. }
  137. }
  138. else
  139. done = TRUE; /* bail out */
  140. break;
  141. }
  142. }
  143. if (mp_timedout || done)
  144. break;
  145. if (res != CURLM_OK) {
  146. fprintf(stderr, "not okay???\n");
  147. break;
  148. }
  149. FD_ZERO(&rd);
  150. FD_ZERO(&wr);
  151. FD_ZERO(&exc);
  152. max_fd = 0;
  153. if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) {
  154. fprintf(stderr, "unexpected failured of fdset.\n");
  155. res = 189;
  156. break;
  157. }
  158. if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
  159. fprintf(stderr, "bad select??\n");
  160. res = 195;
  161. break;
  162. }
  163. res = CURLM_CALL_MULTI_PERFORM;
  164. }
  165. if (ml_timedout || mp_timedout) {
  166. if (ml_timedout) fprintf(stderr, "ml_timedout\n");
  167. if (mp_timedout) fprintf(stderr, "mp_timedout\n");
  168. fprintf(stderr, "ABORTING TEST, since it seems "
  169. "that it would have run forever.\n");
  170. res = TEST_ERR_RUNS_FOREVER;
  171. }
  172. #ifndef LIB527
  173. /* get NUM_HANDLES easy handles */
  174. for(i=0; i < NUM_HANDLES; i++) {
  175. #ifdef LIB526
  176. curl_multi_remove_handle(m, curl[i]);
  177. #endif
  178. curl_easy_cleanup(curl[i]);
  179. }
  180. #endif
  181. curl_multi_cleanup(m);
  182. curl_global_cleanup();
  183. return res;
  184. }