lib1515.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 http://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. #define _MPRINTF_REPLACE
  37. #include <curl/mprintf.h>
  38. static int debug_callback(CURL *curl, curl_infotype info, char *msg, size_t len, void *ptr)
  39. {
  40. (void)curl;
  41. (void)ptr;
  42. if(info == CURLINFO_TEXT)
  43. fprintf(stderr, "debug: %.*s", (int) len, msg);
  44. return 0;
  45. }
  46. static int do_one_request(CURLM *m, char *URL, char *resolve)
  47. {
  48. CURL *curls;
  49. struct curl_slist *resolve_list = NULL;
  50. int still_running;
  51. int res = 0;
  52. CURLMsg *msg;
  53. int msgs_left;
  54. resolve_list = curl_slist_append(resolve_list, resolve);
  55. easy_init(curls);
  56. easy_setopt(curls, CURLOPT_URL, URL);
  57. easy_setopt(curls, CURLOPT_RESOLVE, resolve_list);
  58. easy_setopt(curls, CURLOPT_DEBUGFUNCTION, debug_callback);
  59. easy_setopt(curls, CURLOPT_VERBOSE, 1);
  60. easy_setopt(curls, CURLOPT_DNS_CACHE_TIMEOUT, DNS_TIMEOUT);
  61. multi_add_handle(m, curls);
  62. multi_perform(m, &still_running);
  63. abort_on_test_timeout();
  64. while(still_running) {
  65. struct timeval timeout;
  66. fd_set fdread, fdwrite, fdexcep;
  67. int maxfd = -99;
  68. FD_ZERO(&fdread);
  69. FD_ZERO(&fdwrite);
  70. FD_ZERO(&fdexcep);
  71. timeout.tv_sec = 1;
  72. timeout.tv_usec = 0;
  73. multi_fdset(m, &fdread, &fdwrite, &fdexcep, &maxfd);
  74. select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  75. abort_on_test_timeout();
  76. multi_perform(m, &still_running);
  77. abort_on_test_timeout();
  78. }
  79. while((msg = curl_multi_info_read(m, &msgs_left))) {
  80. if(msg->msg == CURLMSG_DONE && msg->easy_handle == curls) {
  81. res = msg->data.result;
  82. break;
  83. }
  84. }
  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. snprintf(dns_entry, sizeof(dns_entry), "testserver.example.com:%s:%s", 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), "http://testserver.example.com:%s%s%04d", port, path, i);
  108. /* second request must succeed like the first one */
  109. if((res = do_one_request(multi, target_url, dns_entry)))
  110. goto test_cleanup;
  111. if(i < count)
  112. sleep(DNS_TIMEOUT + 1);
  113. }
  114. test_cleanup:
  115. curl_multi_cleanup(multi);
  116. return (int) res;
  117. }