2
0

lib526.c 4.9 KB

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