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
42#define _MPRINTF_REPLACE
43#include <curl/mprintf.h>
44
45static int debug_callback(CURL *curl, curl_infotype info, char *msg, 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
56static 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  while((msg = curl_multi_info_read(m, &msgs_left))) {
101    if(msg->msg == CURLMSG_DONE && msg->easy_handle == curls) {
102      res = msg->data.result;
103      break;
104    }
105  }
106
107test_cleanup:
108
109  curl_multi_remove_handle(m, curls);
110  curl_easy_cleanup(curls);
111  curl_slist_free_all(resolve_list);
112
113  return res;
114}
115
116int test(char *URL)
117{
118  CURLM* multi = NULL;
119  int res = 0;
120  char *address = libtest_arg2;
121  char *port = libtest_arg3;
122  char *path = URL;
123  char dns_entry[256];
124  int i;
125  int count = 2;
126
127  snprintf(dns_entry, sizeof(dns_entry), "testserver.example.com:%s:%s", port, address);
128
129  start_test_timing();
130
131  global_init(CURL_GLOBAL_ALL);
132  multi_init(multi);
133
134  for(i = 1; i <= count; i++) {
135    char target_url[256];
136    snprintf(target_url, sizeof(target_url), "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
146test_cleanup:
147
148  curl_multi_cleanup(multi);
149
150  return (int) res;
151}
152