lib1515.c 3.9 KB

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