lib526.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  22. /*
  23. * This code sets up multiple easy handles that transfer a single file from
  24. * the same URL, in a serial manner after each other. Due to the connection
  25. * sharing within the multi handle all transfers are performed on the same
  26. * persistent connection.
  27. *
  28. * This source code is used for lib526, lib527 and lib532 with only #ifdefs
  29. * controlling the small differences.
  30. *
  31. * - lib526 closes all easy handles after
  32. * they all have transferred the file over the single connection
  33. * - lib527 closes each easy handle after each single transfer.
  34. * - lib532 uses only a single easy handle that is removed, reset and then
  35. * re-added for each transfer
  36. *
  37. * Test case 526, 527 and 532 use FTP, while test 528 uses the lib526 tool but
  38. * with HTTP.
  39. */
  40. #include "test.h"
  41. #include <fcntl.h>
  42. #include "testutil.h"
  43. #include "warnless.h"
  44. #include "memdebug.h"
  45. #define TEST_HANG_TIMEOUT 60 * 1000
  46. #define NUM_HANDLES 4
  47. int test(char *URL)
  48. {
  49. int res = 0;
  50. CURL *curl[NUM_HANDLES];
  51. int running;
  52. CURLM *m = NULL;
  53. int current = 0;
  54. int i;
  55. for(i = 0; i < NUM_HANDLES; i++)
  56. curl[i] = NULL;
  57. start_test_timing();
  58. global_init(CURL_GLOBAL_ALL);
  59. /* get NUM_HANDLES easy handles */
  60. for(i = 0; i < NUM_HANDLES; i++) {
  61. easy_init(curl[i]);
  62. /* specify target */
  63. easy_setopt(curl[i], CURLOPT_URL, URL);
  64. /* go verbose */
  65. easy_setopt(curl[i], CURLOPT_VERBOSE, 1L);
  66. }
  67. multi_init(m);
  68. multi_add_handle(m, curl[current]);
  69. fprintf(stderr, "Start at URL 0\n");
  70. for(;;) {
  71. struct timeval interval;
  72. fd_set rd, wr, exc;
  73. int maxfd = -99;
  74. interval.tv_sec = 1;
  75. interval.tv_usec = 0;
  76. multi_perform(m, &running);
  77. abort_on_test_timeout();
  78. if(!running) {
  79. #ifdef LIB527
  80. /* NOTE: this code does not remove the handle from the multi handle
  81. here, which would be the nice, sane and documented way of working.
  82. This however tests that the API survives this abuse gracefully. */
  83. curl_easy_cleanup(curl[current]);
  84. curl[current] = NULL;
  85. #endif
  86. if(++current < NUM_HANDLES) {
  87. fprintf(stderr, "Advancing to URL %d\n", current);
  88. #ifdef LIB532
  89. /* first remove the only handle we use */
  90. curl_multi_remove_handle(m, curl[0]);
  91. /* make us re-use the same handle all the time, and try resetting
  92. the handle first too */
  93. curl_easy_reset(curl[0]);
  94. easy_setopt(curl[0], CURLOPT_URL, URL);
  95. /* go verbose */
  96. easy_setopt(curl[0], CURLOPT_VERBOSE, 1L);
  97. /* re-add it */
  98. multi_add_handle(m, curl[0]);
  99. #else
  100. multi_add_handle(m, curl[current]);
  101. #endif
  102. }
  103. else {
  104. break; /* done */
  105. }
  106. }
  107. FD_ZERO(&rd);
  108. FD_ZERO(&wr);
  109. FD_ZERO(&exc);
  110. multi_fdset(m, &rd, &wr, &exc, &maxfd);
  111. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  112. select_test(maxfd + 1, &rd, &wr, &exc, &interval);
  113. abort_on_test_timeout();
  114. }
  115. test_cleanup:
  116. #if defined(LIB526)
  117. /* test 526 and 528 */
  118. /* proper cleanup sequence - type PB */
  119. for(i = 0; i < NUM_HANDLES; i++) {
  120. curl_multi_remove_handle(m, curl[i]);
  121. curl_easy_cleanup(curl[i]);
  122. }
  123. curl_multi_cleanup(m);
  124. curl_global_cleanup();
  125. #elif defined(LIB527)
  126. /* test 527 */
  127. /* Upon non-failure test flow the easy's have already been cleanup'ed. In
  128. case there is a failure we arrive here with easy's that have not been
  129. cleanup'ed yet, in this case we have to cleanup them or otherwise these
  130. will be leaked, let's use undocumented cleanup sequence - type UB */
  131. if(res)
  132. for(i = 0; i < NUM_HANDLES; i++)
  133. curl_easy_cleanup(curl[i]);
  134. curl_multi_cleanup(m);
  135. curl_global_cleanup();
  136. #elif defined(LIB532)
  137. /* test 532 */
  138. /* undocumented cleanup sequence - type UB */
  139. for(i = 0; i < NUM_HANDLES; i++)
  140. curl_easy_cleanup(curl[i]);
  141. curl_multi_cleanup(m);
  142. curl_global_cleanup();
  143. #endif
  144. return res;
  145. }