lib1515.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * Check for bugs #1303 and #1327: libcurl should never remove DNS entries
  26. * created via CURLOPT_RESOLVE, neither after DNS_CACHE_TIMEOUT elapses
  27. * (test1515) nor a dead connection is detected (test1616).
  28. */
  29. #include "test.h"
  30. #include "testutil.h"
  31. #include "warnless.h"
  32. #include "memdebug.h"
  33. #define TEST_HANG_TIMEOUT 60 * 1000
  34. #define DNS_TIMEOUT 1
  35. static int debug_callback(CURL *curl, curl_infotype info, char *msg,
  36. size_t len, void *ptr)
  37. {
  38. (void)curl;
  39. (void)ptr;
  40. if(info == CURLINFO_TEXT)
  41. fprintf(stderr, "debug: %.*s", (int) len, msg);
  42. return 0;
  43. }
  44. static CURLcode do_one_request(CURLM *m, char *URL, char *resolve)
  45. {
  46. CURL *curls;
  47. struct curl_slist *resolve_list = NULL;
  48. int still_running;
  49. CURLcode res = CURLE_OK;
  50. CURLMsg *msg;
  51. int msgs_left;
  52. resolve_list = curl_slist_append(resolve_list, resolve);
  53. easy_init(curls);
  54. easy_setopt(curls, CURLOPT_URL, URL);
  55. easy_setopt(curls, CURLOPT_RESOLVE, resolve_list);
  56. easy_setopt(curls, CURLOPT_DEBUGFUNCTION, debug_callback);
  57. easy_setopt(curls, CURLOPT_VERBOSE, 1);
  58. easy_setopt(curls, CURLOPT_DNS_CACHE_TIMEOUT, DNS_TIMEOUT);
  59. multi_add_handle(m, curls);
  60. multi_perform(m, &still_running);
  61. abort_on_test_timeout();
  62. while(still_running) {
  63. struct timeval timeout;
  64. fd_set fdread, fdwrite, fdexcep;
  65. int maxfd = -99;
  66. FD_ZERO(&fdread);
  67. FD_ZERO(&fdwrite);
  68. FD_ZERO(&fdexcep);
  69. timeout.tv_sec = 1;
  70. timeout.tv_usec = 0;
  71. multi_fdset(m, &fdread, &fdwrite, &fdexcep, &maxfd);
  72. select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  73. abort_on_test_timeout();
  74. multi_perform(m, &still_running);
  75. abort_on_test_timeout();
  76. }
  77. do {
  78. msg = curl_multi_info_read(m, &msgs_left);
  79. if(msg && msg->msg == CURLMSG_DONE && msg->easy_handle == curls) {
  80. res = msg->data.result;
  81. break;
  82. }
  83. } while(msg);
  84. test_cleanup:
  85. curl_multi_remove_handle(m, curls);
  86. curl_easy_cleanup(curls);
  87. curl_slist_free_all(resolve_list);
  88. return res;
  89. }
  90. CURLcode test(char *URL)
  91. {
  92. CURLM *multi = NULL;
  93. CURLcode res = CURLE_OK;
  94. char *address = libtest_arg2;
  95. char *port = libtest_arg3;
  96. char *path = URL;
  97. char dns_entry[256];
  98. int i;
  99. int count = 2;
  100. msnprintf(dns_entry, sizeof(dns_entry), "testserver.example.com:%s:%s",
  101. port, address);
  102. start_test_timing();
  103. global_init(CURL_GLOBAL_ALL);
  104. multi_init(multi);
  105. for(i = 1; i <= count; i++) {
  106. char target_url[256];
  107. msnprintf(target_url, sizeof(target_url),
  108. "http://testserver.example.com:%s/%s%04d", port, path, i);
  109. /* second request must succeed like the first one */
  110. res = do_one_request(multi, target_url, dns_entry);
  111. if(res != CURLE_OK) {
  112. goto test_cleanup;
  113. }
  114. if(i < count)
  115. sleep(DNS_TIMEOUT + 1);
  116. }
  117. test_cleanup:
  118. curl_multi_cleanup(multi);
  119. curl_global_cleanup();
  120. return res;
  121. }