lib1515.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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 
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 
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 
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 
93  multi_perform(m, &still_running);
94 
96  }
97 
98  do {
99  msg = curl_multi_info_read(m, &msgs_left);
100  if(msg && msg->msg == CURLMSG_DONE && msg->easy_handle == curls) {
101  res = msg->data.result;
102  break;
103  }
104  } while(msg);
105 
106 test_cleanup:
107 
108  curl_multi_remove_handle(m, curls);
109  curl_easy_cleanup(curls);
110  curl_slist_free_all(resolve_list);
111 
112  return res;
113 }
114 
115 int test(char *URL)
116 {
117  CURLM *multi = NULL;
118  int res = 0;
119  char *address = libtest_arg2;
120  char *port = libtest_arg3;
121  char *path = URL;
122  char dns_entry[256];
123  int i;
124  int count = 2;
125 
126  snprintf(dns_entry, sizeof(dns_entry), "testserver.example.com:%s:%s",
127  port, address);
128 
130 
132  multi_init(multi);
133 
134  for(i = 1; i <= count; i++) {
135  char target_url[256];
136  snprintf(target_url, sizeof(target_url),
137  "http://testserver.example.com:%s/%s%04d", port, path, i);
138 
139  /* second request must succeed like the first one */
140  res = do_one_request(multi, target_url, dns_entry);
141  if(res)
142  goto test_cleanup;
143 
144  if(i < count)
145  sleep(DNS_TIMEOUT + 1);
146  }
147 
148 test_cleanup:
149 
150  curl_multi_cleanup(multi);
152 
153  return (int) res;
154 }
static int do_one_request(CURLM *m, char *URL, char *resolve)
Definition: lib1515.c:54
#define select_test(A, B, C, D, E)
Definition: test.h:378
#define global_init(A)
Definition: test.h:431
#define easy_setopt(A, B, C)
Definition: test.h:190
CURL * easy_handle
Definition: multi.h:95
#define multi_add_handle(A, B)
Definition: test.h:238
UNITTEST_START char * ptr
Definition: unit1330.c:38
Definition: multi.h:93
static int res
int test(char *URL)
Definition: lib1515.c:115
unsigned int i
Definition: unit1303.c:79
#define abort_on_test_timeout()
Definition: test.h:404
size_t len
Definition: curl_sasl.c:55
char * libtest_arg2
Definition: first.c:75
static int debug_callback(CURL *curl, curl_infotype info, char *msg, size_t len, void *ptr)
Definition: lib1515.c:42
CURL_EXTERN struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: slist.c:89
#define easy_init(A)
Definition: test.h:145
CURLMSG msg
Definition: multi.h:94
#define multi_fdset(A, B, C, D, E)
Definition: test.h:323
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
static unsigned short port
Definition: sockfilt.c:137
#define multi_perform(A, B)
Definition: test.h:293
CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle, CURL *curl_handle)
#define multi_init(A)
Definition: test.h:166
char * libtest_arg3
Definition: first.c:76
void CURLM
Definition: multi.h:58
void CURL
Definition: curl.h:102
CURL_EXTERN CURLMsg * curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
union CURLMsg::@6 data
#define fprintf
Definition: curl_printf.h:41
#define snprintf
Definition: curl_printf.h:42
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
curl_infotype
Definition: curl.h:429
CURLcode result
Definition: multi.h:98
static CURL * curl
Definition: sessioninfo.c:35
CURL_EXTERN void curl_slist_free_all(struct curl_slist *)
Definition: slist.c:129
#define DNS_TIMEOUT
Definition: lib1515.c:36
#define start_test_timing()
Definition: test.h:383
CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle)
const char * path
Definition: util.c:192


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15