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