unit2600.c 12 KB

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