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