xref: /curl/tests/libtest/lib1515.c (revision e1f93bef)
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 "testtrace.h"
33 #include "testutil.h"
34 #include "warnless.h"
35 #include "memdebug.h"
36 
37 #define TEST_HANG_TIMEOUT 60 * 1000
38 
39 #define DNS_TIMEOUT 1
40 
do_one_request(CURLM * m,char * URL,char * resolve)41 static CURLcode do_one_request(CURLM *m, char *URL, char *resolve)
42 {
43   CURL *curls;
44   struct curl_slist *resolve_list = NULL;
45   int still_running;
46   CURLcode res = CURLE_OK;
47   CURLMsg *msg;
48   int msgs_left;
49 
50   resolve_list = curl_slist_append(resolve_list, resolve);
51 
52   easy_init(curls);
53 
54   easy_setopt(curls, CURLOPT_URL, URL);
55   easy_setopt(curls, CURLOPT_RESOLVE, resolve_list);
56   easy_setopt(curls, CURLOPT_DNS_CACHE_TIMEOUT, DNS_TIMEOUT);
57 
58   libtest_debug_config.nohex = 1;
59   libtest_debug_config.tracetime = 1;
60   easy_setopt(curls, CURLOPT_DEBUGDATA, &libtest_debug_config);
61   easy_setopt(curls, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
62   easy_setopt(curls, CURLOPT_VERBOSE, 1L);
63 
64   multi_add_handle(m, curls);
65   multi_perform(m, &still_running);
66 
67   abort_on_test_timeout();
68 
69   while(still_running) {
70     struct timeval timeout;
71     fd_set fdread, fdwrite, fdexcep;
72     int maxfd = -99;
73 
74     FD_ZERO(&fdread);
75     FD_ZERO(&fdwrite);
76     FD_ZERO(&fdexcep);
77     timeout.tv_sec = 1;
78     timeout.tv_usec = 0;
79 
80     multi_fdset(m, &fdread, &fdwrite, &fdexcep, &maxfd);
81     select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
82 
83     abort_on_test_timeout();
84     multi_perform(m, &still_running);
85 
86     abort_on_test_timeout();
87   }
88 
89   do {
90     msg = curl_multi_info_read(m, &msgs_left);
91     if(msg && msg->msg == CURLMSG_DONE && msg->easy_handle == curls) {
92       res = msg->data.result;
93       break;
94     }
95   } while(msg);
96 
97 test_cleanup:
98 
99   curl_multi_remove_handle(m, curls);
100   curl_easy_cleanup(curls);
101   curl_slist_free_all(resolve_list);
102 
103   return res;
104 }
105 
test(char * URL)106 CURLcode test(char *URL)
107 {
108   CURLM *multi = NULL;
109   CURLcode res = CURLE_OK;
110   char *address = libtest_arg2;
111   char *port = libtest_arg3;
112   char *path = URL;
113   char dns_entry[256];
114   int i;
115   int count = 2;
116 
117   msnprintf(dns_entry, sizeof(dns_entry), "testserver.example.com:%s:%s",
118             port, address);
119 
120   start_test_timing();
121 
122   global_init(CURL_GLOBAL_ALL);
123   curl_global_trace("all");
124   multi_init(multi);
125 
126   for(i = 1; i <= count; i++) {
127     char target_url[256];
128     msnprintf(target_url, sizeof(target_url),
129               "http://testserver.example.com:%s/%s%04d", port, path, i);
130 
131     /* second request must succeed like the first one */
132     res = do_one_request(multi, target_url, dns_entry);
133     if(res != CURLE_OK) {
134       fprintf(stderr, "request %s failed with %d\n", target_url, res);
135       goto test_cleanup;
136     }
137 
138     if(i < count)
139       sleep(DNS_TIMEOUT + 1);
140   }
141 
142 test_cleanup:
143 
144   curl_multi_cleanup(multi);
145   curl_global_cleanup();
146 
147   return res;
148 }
149