• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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 
debug_callback(CURL * curl,curl_infotype info,char * msg,size_t len,void * ptr)40 static int debug_callback(CURL *curl, curl_infotype info, char *msg,
41                           size_t len, void *ptr)
42 {
43   (void)curl;
44   (void)ptr;
45 
46   if(info == CURLINFO_TEXT)
47     fprintf(stderr, "debug: %.*s", (int) len, msg);
48 
49   return 0;
50 }
51 
do_one_request(CURLM * m,char * URL,char * resolve)52 static int do_one_request(CURLM *m, char *URL, char *resolve)
53 {
54   CURL *curls;
55   struct curl_slist *resolve_list = NULL;
56   int still_running;
57   int res = 0;
58   CURLMsg *msg;
59   int msgs_left;
60 
61   resolve_list = curl_slist_append(resolve_list, resolve);
62 
63   easy_init(curls);
64 
65   easy_setopt(curls, CURLOPT_URL, URL);
66   easy_setopt(curls, CURLOPT_RESOLVE, resolve_list);
67   easy_setopt(curls, CURLOPT_DEBUGFUNCTION, debug_callback);
68   easy_setopt(curls, CURLOPT_VERBOSE, 1);
69   easy_setopt(curls, CURLOPT_DNS_CACHE_TIMEOUT, DNS_TIMEOUT);
70 
71   multi_add_handle(m, curls);
72   multi_perform(m, &still_running);
73 
74   abort_on_test_timeout();
75 
76   while(still_running) {
77     struct timeval timeout;
78     fd_set fdread, fdwrite, fdexcep;
79     int maxfd = -99;
80 
81     FD_ZERO(&fdread);
82     FD_ZERO(&fdwrite);
83     FD_ZERO(&fdexcep);
84     timeout.tv_sec = 1;
85     timeout.tv_usec = 0;
86 
87     multi_fdset(m, &fdread, &fdwrite, &fdexcep, &maxfd);
88     select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
89 
90     abort_on_test_timeout();
91     multi_perform(m, &still_running);
92 
93     abort_on_test_timeout();
94   }
95 
96   do {
97     msg = curl_multi_info_read(m, &msgs_left);
98     if(msg && msg->msg == CURLMSG_DONE && msg->easy_handle == curls) {
99       res = msg->data.result;
100       break;
101     }
102   } while(msg);
103 
104 test_cleanup:
105 
106   curl_multi_remove_handle(m, curls);
107   curl_easy_cleanup(curls);
108   curl_slist_free_all(resolve_list);
109 
110   return res;
111 }
112 
test(char * URL)113 int test(char *URL)
114 {
115   CURLM *multi = NULL;
116   int res = 0;
117   char *address = libtest_arg2;
118   char *port = libtest_arg3;
119   char *path = URL;
120   char dns_entry[256];
121   int i;
122   int count = 2;
123 
124   msnprintf(dns_entry, sizeof(dns_entry), "testserver.example.com:%s:%s",
125             port, address);
126 
127   start_test_timing();
128 
129   global_init(CURL_GLOBAL_ALL);
130   multi_init(multi);
131 
132   for(i = 1; i <= count; i++) {
133     char target_url[256];
134     msnprintf(target_url, sizeof(target_url),
135               "http://testserver.example.com:%s/%s%04d", port, path, i);
136 
137     /* second request must succeed like the first one */
138     res = do_one_request(multi, target_url, dns_entry);
139     if(res)
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   curl_global_cleanup();
150 
151   return (int) res;
152 }
153