lib1515.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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.haxx.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(s) Sleep(s * 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. while((msg = curl_multi_info_read(m, &msgs_left))) {
  79. if(msg->msg == CURLMSG_DONE && msg->easy_handle == curls) {
  80. res = msg->data.result;
  81. break;
  82. }
  83. }
  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. int test(char *URL)
  91. {
  92. CURLM *multi = NULL;
  93. int res = 0;
  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. snprintf(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. snprintf(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)
  112. goto test_cleanup;
  113. if(i < count)
  114. sleep(DNS_TIMEOUT + 1);
  115. }
  116. test_cleanup:
  117. curl_multi_cleanup(multi);
  118. return (int) res;
  119. }