lib1515.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "testtrace.h"
  31. #include "testutil.h"
  32. #include "warnless.h"
  33. #include "memdebug.h"
  34. #define TEST_HANG_TIMEOUT 60 * 1000
  35. #define DNS_TIMEOUT 1
  36. static CURLcode do_one_request(CURLM *m, char *URL, char *resolve)
  37. {
  38. CURL *curls;
  39. struct curl_slist *resolve_list = NULL;
  40. int still_running;
  41. CURLcode res = CURLE_OK;
  42. CURLMsg *msg;
  43. int msgs_left;
  44. resolve_list = curl_slist_append(resolve_list, resolve);
  45. easy_init(curls);
  46. easy_setopt(curls, CURLOPT_URL, URL);
  47. easy_setopt(curls, CURLOPT_RESOLVE, resolve_list);
  48. easy_setopt(curls, CURLOPT_DNS_CACHE_TIMEOUT, DNS_TIMEOUT);
  49. libtest_debug_config.nohex = 1;
  50. libtest_debug_config.tracetime = 1;
  51. easy_setopt(curls, CURLOPT_DEBUGDATA, &libtest_debug_config);
  52. easy_setopt(curls, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
  53. easy_setopt(curls, CURLOPT_VERBOSE, 1L);
  54. multi_add_handle(m, curls);
  55. multi_perform(m, &still_running);
  56. abort_on_test_timeout();
  57. while(still_running) {
  58. struct timeval timeout;
  59. fd_set fdread, fdwrite, fdexcep;
  60. int maxfd = -99;
  61. FD_ZERO(&fdread);
  62. FD_ZERO(&fdwrite);
  63. FD_ZERO(&fdexcep);
  64. timeout.tv_sec = 1;
  65. timeout.tv_usec = 0;
  66. multi_fdset(m, &fdread, &fdwrite, &fdexcep, &maxfd);
  67. select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  68. abort_on_test_timeout();
  69. multi_perform(m, &still_running);
  70. abort_on_test_timeout();
  71. }
  72. do {
  73. msg = curl_multi_info_read(m, &msgs_left);
  74. if(msg && msg->msg == CURLMSG_DONE && msg->easy_handle == curls) {
  75. res = msg->data.result;
  76. break;
  77. }
  78. } while(msg);
  79. test_cleanup:
  80. curl_multi_remove_handle(m, curls);
  81. curl_easy_cleanup(curls);
  82. curl_slist_free_all(resolve_list);
  83. return res;
  84. }
  85. CURLcode test(char *URL)
  86. {
  87. CURLM *multi = NULL;
  88. CURLcode res = CURLE_OK;
  89. char *address = libtest_arg2;
  90. char *port = libtest_arg3;
  91. char *path = URL;
  92. char dns_entry[256];
  93. int i;
  94. int count = 2;
  95. msnprintf(dns_entry, sizeof(dns_entry), "testserver.example.com:%s:%s",
  96. port, address);
  97. start_test_timing();
  98. global_init(CURL_GLOBAL_ALL);
  99. curl_global_trace("all");
  100. multi_init(multi);
  101. for(i = 1; i <= count; i++) {
  102. char target_url[256];
  103. msnprintf(target_url, sizeof(target_url),
  104. "http://testserver.example.com:%s/%s%04d", port, path, i);
  105. /* second request must succeed like the first one */
  106. res = do_one_request(multi, target_url, dns_entry);
  107. if(res != CURLE_OK) {
  108. fprintf(stderr, "request %s failed with %d\n", target_url, res);
  109. goto test_cleanup;
  110. }
  111. if(i < count)
  112. sleep(DNS_TIMEOUT + 1);
  113. }
  114. test_cleanup:
  115. curl_multi_cleanup(multi);
  116. curl_global_cleanup();
  117. return res;
  118. }