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 /*
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, size_t len, void *ptr)
43 {
44   (void)curl;
45   (void)ptr;
46 
47   if(info == CURLINFO_TEXT)
48     fprintf(stderr, "debug: %.*s", (int) len, msg);
49 
50   return 0;
51 }
52 
do_one_request(CURLM * m,char * URL,char * resolve)53 static int do_one_request(CURLM *m, char *URL, char *resolve)
54 {
55   CURL *curls;
56   struct curl_slist *resolve_list = NULL;
57   int still_running;
58   int res = 0;
59   CURLMsg *msg;
60   int msgs_left;
61 
62   resolve_list = curl_slist_append(resolve_list, resolve);
63 
64   easy_init(curls);
65 
66   easy_setopt(curls, CURLOPT_URL, URL);
67   easy_setopt(curls, CURLOPT_RESOLVE, resolve_list);
68   easy_setopt(curls, CURLOPT_DEBUGFUNCTION, debug_callback);
69   easy_setopt(curls, CURLOPT_VERBOSE, 1);
70   easy_setopt(curls, CURLOPT_DNS_CACHE_TIMEOUT, DNS_TIMEOUT);
71 
72   multi_add_handle(m, curls);
73   multi_perform(m, &still_running);
74 
75   abort_on_test_timeout();
76 
77   while(still_running) {
78     struct timeval timeout;
79     fd_set fdread, fdwrite, fdexcep;
80     int maxfd = -99;
81 
82     FD_ZERO(&fdread);
83     FD_ZERO(&fdwrite);
84     FD_ZERO(&fdexcep);
85     timeout.tv_sec = 1;
86     timeout.tv_usec = 0;
87 
88     multi_fdset(m, &fdread, &fdwrite, &fdexcep, &maxfd);
89     select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
90 
91     abort_on_test_timeout();
92     multi_perform(m, &still_running);
93 
94     abort_on_test_timeout();
95   }
96 
97   while((msg = curl_multi_info_read(m, &msgs_left))) {
98     if(msg->msg == CURLMSG_DONE && msg->easy_handle == curls) {
99       res = msg->data.result;
100       break;
101     }
102   }
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   snprintf(dns_entry, sizeof(dns_entry), "testserver.example.com:%s:%s", port, address);
125 
126   start_test_timing();
127 
128   global_init(CURL_GLOBAL_ALL);
129   multi_init(multi);
130 
131   for(i = 1; i <= count; i++) {
132     char target_url[256];
133     snprintf(target_url, sizeof(target_url), "http://testserver.example.com:%s%s%04d", port, path, i);
134 
135     /* second request must succeed like the first one */
136     if((res = do_one_request(multi, target_url, dns_entry)))
137       goto test_cleanup;
138 
139     if(i < count)
140       sleep(DNS_TIMEOUT + 1);
141   }
142 
143 test_cleanup:
144 
145   curl_multi_cleanup(multi);
146 
147   return (int) res;
148 }
149