• 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 #include "curlcheck.h"
25 
26 #ifdef HAVE_NETINET_IN_H
27 #include <netinet/in.h>
28 #endif
29 #ifdef HAVE_NETINET_IN6_H
30 #include <netinet/in6.h>
31 #endif
32 #ifdef HAVE_NETDB_H
33 #include <netdb.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
37 #endif
38 #ifdef __VMS
39 #include <in.h>
40 #include <inet.h>
41 #endif
42 
43 #ifdef HAVE_SETJMP_H
44 #include <setjmp.h>
45 #endif
46 #ifdef HAVE_SIGNAL_H
47 #include <signal.h>
48 #endif
49 
50 #include "urldata.h"
51 #include "connect.h"
52 #include "cfilters.h"
53 #include "curl_log.h"
54 
55 /* copied from hostip.c to switch using SIGALARM for timeouts.
56  * SIGALARM has only seconds resolution, so our tests will not work
57  * here. */
58 #if defined(CURLRES_SYNCH) && \
59     defined(HAVE_ALARM) && defined(SIGALRM) && defined(HAVE_SIGSETJMP)
60 #define USE_ALARM_TIMEOUT
61 #endif
62 
63 
64 static CURL *easy;
65 
unit_setup(void)66 static CURLcode unit_setup(void)
67 {
68   CURLcode res = CURLE_OK;
69 
70   global_init(CURL_GLOBAL_ALL);
71   easy = curl_easy_init();
72   if(!easy) {
73     curl_global_cleanup();
74     return CURLE_OUT_OF_MEMORY;
75   }
76   curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
77   return res;
78 }
79 
unit_stop(void)80 static void unit_stop(void)
81 {
82   curl_easy_cleanup(easy);
83   curl_global_cleanup();
84 }
85 
86 #ifdef DEBUGBUILD
87 
88 struct test_case {
89   int id;
90   const char *url;
91   const char *resolve_info;
92   unsigned char ip_version;
93   timediff_t connect_timeout_ms;
94   timediff_t he_timeout_ms;
95   timediff_t cf4_fail_delay_ms;
96   timediff_t cf6_fail_delay_ms;
97 
98   int exp_cf4_creations;
99   int exp_cf6_creations;
100   timediff_t min_duration_ms;
101   timediff_t max_duration_ms;
102   CURLcode exp_result;
103   const char *pref_family;
104 };
105 
106 struct ai_family_stats {
107   const char *family;
108   int creations;
109   timediff_t first_created;
110   timediff_t last_created;
111 };
112 
113 struct test_result {
114   CURLcode result;
115   struct curltime started;
116   struct curltime ended;
117   struct ai_family_stats cf4;
118   struct ai_family_stats cf6;
119 };
120 
121 static struct test_case *current_tc;
122 static struct test_result *current_tr;
123 
124 struct cf_test_ctx {
125   int ai_family;
126   int transport;
127   char id[16];
128   struct curltime started;
129   timediff_t fail_delay_ms;
130   struct ai_family_stats *stats;
131 };
132 
cf_test_destroy(struct Curl_cfilter * cf,struct Curl_easy * data)133 static void cf_test_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
134 {
135   struct cf_test_ctx *ctx = cf->ctx;
136 
137   infof(data, "%04dms: cf[%s] destroyed",
138        (int)Curl_timediff(Curl_now(), current_tr->started), ctx->id);
139   free(ctx);
140   cf->ctx = NULL;
141 }
142 
cf_test_connect(struct Curl_cfilter * cf,struct Curl_easy * data,bool blocking,bool * done)143 static CURLcode cf_test_connect(struct Curl_cfilter *cf,
144                                 struct Curl_easy *data,
145                                 bool blocking, bool *done)
146 {
147   struct cf_test_ctx *ctx = cf->ctx;
148   struct curltime now;
149 
150   (void)data;
151   (void)blocking;
152   *done = FALSE;
153   now = Curl_now();
154   if(Curl_timediff(now, ctx->started) >= ctx->fail_delay_ms) {
155     infof(data, "%04dms: cf[%s] fail delay reached",
156          (int)Curl_timediff(Curl_now(), current_tr->started), ctx->id);
157     return CURLE_COULDNT_CONNECT;
158   }
159   infof(data, "%04dms: cf[%s] continuing",
160        (int)Curl_timediff(Curl_now(), current_tr->started), ctx->id);
161   return CURLE_OK;
162 }
163 
164 static struct Curl_cftype cft_test = {
165   "TEST",
166   CF_TYPE_IP_CONNECT,
167   CURL_LOG_DEFAULT,
168   cf_test_destroy,
169   cf_test_connect,
170   Curl_cf_def_close,
171   Curl_cf_def_get_host,
172   Curl_cf_def_get_select_socks,
173   Curl_cf_def_data_pending,
174   Curl_cf_def_send,
175   Curl_cf_def_recv,
176   Curl_cf_def_cntrl,
177   Curl_cf_def_conn_is_alive,
178   Curl_cf_def_conn_keep_alive,
179   Curl_cf_def_query,
180 };
181 
cf_test_create(struct Curl_cfilter ** pcf,struct Curl_easy * data,struct connectdata * conn,const struct Curl_addrinfo * ai,int transport)182 static CURLcode cf_test_create(struct Curl_cfilter **pcf,
183                                struct Curl_easy *data,
184                                struct connectdata *conn,
185                                const struct Curl_addrinfo *ai,
186                                int transport)
187 {
188   struct cf_test_ctx *ctx = NULL;
189   struct Curl_cfilter *cf = NULL;
190   timediff_t created_at;
191   CURLcode result;
192 
193   (void)data;
194   (void)conn;
195   ctx = calloc(sizeof(*ctx), 1);
196   if(!ctx) {
197     result = CURLE_OUT_OF_MEMORY;
198     goto out;
199   }
200   ctx->ai_family = ai->ai_family;
201   ctx->transport = transport;
202   ctx->started = Curl_now();
203 #ifdef ENABLE_IPV6
204   if(ctx->ai_family == AF_INET6) {
205     ctx->stats = &current_tr->cf6;
206     ctx->fail_delay_ms = current_tc->cf6_fail_delay_ms;
207     curl_msprintf(ctx->id, "v6-%d", ctx->stats->creations);
208     ctx->stats->creations++;
209   }
210   else
211 #endif
212   {
213     ctx->stats = &current_tr->cf4;
214     ctx->fail_delay_ms = current_tc->cf4_fail_delay_ms;
215     curl_msprintf(ctx->id, "v4-%d", ctx->stats->creations);
216     ctx->stats->creations++;
217   }
218 
219   created_at = Curl_timediff(ctx->started, current_tr->started);
220   if(ctx->stats->creations == 1)
221     ctx->stats->first_created = created_at;
222   ctx->stats->last_created = created_at;
223   infof(data, "%04dms: cf[%s] created", (int)created_at, ctx->id);
224 
225   result = Curl_cf_create(&cf, &cft_test, ctx);
226 
227 out:
228   *pcf = (!result)? cf : NULL;
229   if(result) {
230     free(cf);
231     free(ctx);
232   }
233   return result;
234 }
235 
check_result(struct test_case * tc,struct test_result * tr)236 static void check_result(struct test_case *tc,
237                          struct test_result *tr)
238 {
239   char msg[256];
240   timediff_t duration_ms = 0;
241 
242   if(tr->result != tc->exp_result
243     && CURLE_OPERATION_TIMEDOUT != tr->result) {
244     /* on CI we encounter the TIMEOUT result, since images get less CPU
245      * and events are not as sharply timed. */
246     curl_msprintf(msg, "%d: expected result %d but got %d",
247                   tc->id, tc->exp_result, tr->result);
248     fail(msg);
249   }
250   if(tr->cf4.creations != tc->exp_cf4_creations) {
251     curl_msprintf(msg, "%d: expected %d ipv4 creations, but got %d",
252                   tc->id, tc->exp_cf4_creations, tr->cf4.creations);
253     fail(msg);
254   }
255   if(tr->cf6.creations != tc->exp_cf6_creations) {
256     curl_msprintf(msg, "%d: expected %d ipv6 creations, but got %d",
257                   tc->id, tc->exp_cf6_creations, tr->cf6.creations);
258     fail(msg);
259   }
260 
261   (void)duration_ms;
262 #ifndef USE_ALARM_TIMEOUT
263   duration_ms = Curl_timediff(tr->ended, tr->started);
264   if(duration_ms < tc->min_duration_ms) {
265     curl_msprintf(msg, "%d: expected min duration of %dms, but took %dms",
266                   tc->id, (int)tc->min_duration_ms, (int)duration_ms);
267     fail(msg);
268   }
269   if(duration_ms > tc->max_duration_ms) {
270     curl_msprintf(msg, "%d: expected max duration of %dms, but took %dms",
271                   tc->id, (int)tc->max_duration_ms, (int)duration_ms);
272     fail(msg);
273   }
274 #endif
275   if(tr->cf6.creations && tr->cf4.creations && tc->pref_family) {
276     /* did ipv4 and ipv6 both, expect the preferred family to start right arway
277      * with the other being delayed by the happy_eyeball_timeout */
278     struct ai_family_stats *stats1 = !strcmp(tc->pref_family, "v6")?
279                                      &tr->cf6 : &tr->cf4;
280     struct ai_family_stats *stats2 = !strcmp(tc->pref_family, "v6")?
281                                      &tr->cf4 : &tr->cf6;
282 
283     if(stats1->first_created > 100) {
284       curl_msprintf(msg, "%d: expected ip%s to start right away, instead "
285                     "first attempt made after %dms",
286                     tc->id, stats1->family, (int)stats1->first_created);
287       fail(msg);
288     }
289 #ifndef USE_ALARM_TIMEOUT
290     if(stats2->first_created < tc->he_timeout_ms) {
291 #else
292     if(stats2->first_created < 1000) {
293 #endif
294       curl_msprintf(msg, "%d: expected ip%s to start delayed after %dms, "
295                     "instead first attempt made after %dms",
296                     tc->id, stats2->family, (int)tc->he_timeout_ms,
297                     (int)stats2->first_created);
298       fail(msg);
299     }
300   }
301 }
302 
303 static void test_connect(struct test_case *tc)
304 {
305   struct test_result tr;
306   struct curl_slist *list = NULL;
307 
308   Curl_debug_set_transport_provider(TRNSPRT_TCP, cf_test_create);
309   current_tc = tc;
310   current_tr = &tr;
311 
312   list = curl_slist_append(NULL, tc->resolve_info);
313   fail_unless(list, "error allocating resolve list entry");
314   curl_easy_setopt(easy, CURLOPT_RESOLVE, list);
315   curl_easy_setopt(easy, CURLOPT_IPRESOLVE, (long)tc->ip_version);
316 #ifdef USE_ALARM_TIMEOUT
317   curl_easy_setopt(easy, CURLOPT_CONNECTTIMEOUT_MS, 2000L);
318   curl_easy_setopt(easy, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS,
319                    (tc->he_timeout_ms > tc->connect_timeout_ms)?
320                    3000L : 1000L);
321 #else
322   curl_easy_setopt(easy, CURLOPT_CONNECTTIMEOUT_MS,
323                    (long)tc->connect_timeout_ms);
324   curl_easy_setopt(easy, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS,
325                    (long)tc->he_timeout_ms);
326 #endif
327 
328   curl_easy_setopt(easy, CURLOPT_URL, tc->url);
329   memset(&tr, 0, sizeof(tr));
330   tr.cf6.family = "v6";
331   tr.cf4.family = "v4";
332 
333   tr.started = Curl_now();
334   tr.result = curl_easy_perform(easy);
335   tr.ended = Curl_now();
336 
337   curl_easy_setopt(easy, CURLOPT_RESOLVE, NULL);
338   curl_slist_free_all(list);
339   list = NULL;
340   current_tc = NULL;
341   current_tr = NULL;
342 
343   check_result(tc, &tr);
344 }
345 
346 #endif /* DEBUGBUILD */
347 
348 /*
349  * How these test cases work:
350  * - replace the creation of the TCP socket filter with out test filter
351  * - test filter does nothing and reports failure after configured delay
352  * - we feed addresses into the resolve cache to simulate different cases
353  * - we monitor how many instances of ipv4/v6 attempts are made and when
354  * - for mixed families, we expect HAPPY_EYEBALLS_TIMEOUT to trigger
355  *
356  * Max Duration checks needs to be conservative since CI jobs are not
357  * as sharp.
358  */
359 #define TURL "http://test.com:123"
360 
361 #define R_FAIL      CURLE_COULDNT_CONNECT
362 
363 static struct test_case TEST_CASES[] = {
364   /* TIMEOUT_MS,        FAIL_MS      CREATED    DURATION     Result, HE_PREF */
365   /* CNCT   HE          v4    v6     v4 v6      MIN   MAX */
366   { 1, TURL, "test.com:123:192.0.2.1", CURL_IPRESOLVE_WHATEVER,
367      250,  150,        200,  200,    1,  0,      200,  500,  R_FAIL, NULL },
368   /* 1 ipv4, fails after ~200ms, reports COULDNT_CONNECT   */
369   { 2, TURL, "test.com:123:192.0.2.1,192.0.2.2", CURL_IPRESOLVE_WHATEVER,
370      500,  150,        200,  200,    2,  0,      400,  800,  R_FAIL, NULL },
371   /* 2 ipv4, fails after ~400ms, reports COULDNT_CONNECT   */
372 #ifdef ENABLE_IPV6
373   { 3, TURL, "test.com:123:::1", CURL_IPRESOLVE_WHATEVER,
374      250,  150,        200,  200,    0,  1,      200,  500,  R_FAIL, NULL },
375   /* 1 ipv6, fails after ~200ms, reports COULDNT_CONNECT   */
376   { 4, TURL, "test.com:123:::1,::2", CURL_IPRESOLVE_WHATEVER,
377      500,  150,        200,  200,    0,  2,      400,  800,  R_FAIL, NULL },
378   /* 2 ipv6, fails after ~400ms, reports COULDNT_CONNECT   */
379 
380   { 5, TURL, "test.com:123:192.0.2.1,::1", CURL_IPRESOLVE_WHATEVER,
381      500,  150,        200, 200,     1,  1,      350,  800,  R_FAIL, "v4" },
382   /* mixed ip4+6, v4 starts, v6 kicks in on HE, fails after ~350ms */
383   { 6, TURL, "test.com:123:::1,192.0.2.1", CURL_IPRESOLVE_WHATEVER,
384      500,  150,        200, 200,     1,  1,      350,  800,  R_FAIL, "v6" },
385   /* mixed ip6+4, v6 starts, v4 kicks in on HE, fails after ~350ms */
386   { 7, TURL, "test.com:123:::1,192.0.2.1,::2,::3", CURL_IPRESOLVE_WHATEVER,
387      500,  600,        200, 200,     0,  3,      350,  800,  R_FAIL, "v6" },
388   /* mixed ip6+4, v6 starts, v4 never starts due to high HE, TIMEOUT */
389   { 8, TURL, "test.com:123:192.0.2.1,::1", CURL_IPRESOLVE_V4,
390      400,  150,        500, 500,     1,  0,      400,  600,  R_FAIL, NULL },
391   /* mixed ip4+6, but only use v4, check it uses full connect timeout,
392      although another address of the 'wrong' family is availbale */
393   { 9, TURL, "test.com:123:::1,192.0.2.1", CURL_IPRESOLVE_V6,
394      400,  150,        500, 500,     0,  1,      400,  600,  R_FAIL, NULL },
395   /* mixed ip4+6, but only use v6, check it uses full connect timeout,
396      although another address of the 'wrong' family is availbale */
397 #endif
398 };
399 
400 UNITTEST_START
401 
402 #if defined(DEBUGBUILD)
403   size_t i;
404 
405   for(i = 0; i < sizeof(TEST_CASES)/sizeof(TEST_CASES[0]); ++i) {
406     test_connect(&TEST_CASES[i]);
407   }
408 #else
409   (void)TEST_CASES;
410   (void)test_connect;
411 #endif
412 
413 UNITTEST_STOP
414