unit2600.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #ifdef DEBUGBUILD
  67. struct test_case {
  68. int id;
  69. const char *url;
  70. const char *resolve_info;
  71. unsigned char ip_version;
  72. timediff_t connect_timeout_ms;
  73. timediff_t he_timeout_ms;
  74. timediff_t cf4_fail_delay_ms;
  75. timediff_t cf6_fail_delay_ms;
  76. int exp_cf4_creations;
  77. int exp_cf6_creations;
  78. timediff_t min_duration_ms;
  79. timediff_t max_duration_ms;
  80. CURLcode exp_result;
  81. const char *pref_family;
  82. };
  83. struct ai_family_stats {
  84. const char *family;
  85. int creations;
  86. timediff_t first_created;
  87. timediff_t last_created;
  88. };
  89. struct test_result {
  90. CURLcode result;
  91. struct curltime started;
  92. struct curltime ended;
  93. struct ai_family_stats cf4;
  94. struct ai_family_stats cf6;
  95. };
  96. static struct test_case *current_tc;
  97. static struct test_result *current_tr;
  98. struct cf_test_ctx {
  99. int ai_family;
  100. int transport;
  101. char id[16];
  102. struct curltime started;
  103. timediff_t fail_delay_ms;
  104. struct ai_family_stats *stats;
  105. };
  106. static void cf_test_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  107. {
  108. struct cf_test_ctx *ctx = cf->ctx;
  109. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  110. infof(data, "%04dms: cf[%s] destroyed",
  111. (int)Curl_timediff(Curl_now(), current_tr->started), ctx->id);
  112. #else
  113. (void)data;
  114. #endif
  115. free(ctx);
  116. cf->ctx = NULL;
  117. }
  118. static CURLcode cf_test_connect(struct Curl_cfilter *cf,
  119. struct Curl_easy *data,
  120. bool blocking, bool *done)
  121. {
  122. struct cf_test_ctx *ctx = cf->ctx;
  123. timediff_t duration_ms;
  124. (void)data;
  125. (void)blocking;
  126. *done = FALSE;
  127. duration_ms = Curl_timediff(Curl_now(), ctx->started);
  128. if(duration_ms >= ctx->fail_delay_ms) {
  129. infof(data, "%04dms: cf[%s] fail delay reached",
  130. (int)duration_ms, ctx->id);
  131. return CURLE_COULDNT_CONNECT;
  132. }
  133. if(duration_ms)
  134. infof(data, "%04dms: cf[%s] continuing", (int)duration_ms, ctx->id);
  135. Curl_expire(data, ctx->fail_delay_ms - duration_ms, EXPIRE_RUN_NOW);
  136. return CURLE_OK;
  137. }
  138. static struct Curl_cftype cft_test = {
  139. "TEST",
  140. CF_TYPE_IP_CONNECT,
  141. CURL_LOG_LVL_NONE,
  142. cf_test_destroy,
  143. cf_test_connect,
  144. Curl_cf_def_close,
  145. Curl_cf_def_get_host,
  146. Curl_cf_def_get_select_socks,
  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(sizeof(*ctx), 1);
  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 ENABLE_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. #endif /* DEBUGBUILD */
  295. /*
  296. * How these test cases work:
  297. * - replace the creation of the TCP socket filter with our test filter
  298. * - test filter does nothing and reports failure after configured delay
  299. * - we feed addresses into the resolve cache to simulate different cases
  300. * - we monitor how many instances of ipv4/v6 attempts are made and when
  301. * - for mixed families, we expect HAPPY_EYEBALLS_TIMEOUT to trigger
  302. *
  303. * Max Duration checks needs to be conservative since CI jobs are not
  304. * as sharp.
  305. */
  306. #define TURL "http://test.com:123"
  307. #define R_FAIL CURLE_COULDNT_CONNECT
  308. /* timeout values accounting for low cpu resources in CI */
  309. #define TC_TMOT 90000 /* 90 sec max test duration */
  310. #define CNCT_TMOT 60000 /* 60sec connect timeout */
  311. static struct test_case TEST_CASES[] = {
  312. /* TIMEOUT_MS, FAIL_MS CREATED DURATION Result, HE_PREF */
  313. /* CNCT HE v4 v6 v4 v6 MIN MAX */
  314. { 1, TURL, "test.com:123:192.0.2.1", CURL_IPRESOLVE_WHATEVER,
  315. CNCT_TMOT, 150, 200, 200, 1, 0, 200, TC_TMOT, R_FAIL, NULL },
  316. /* 1 ipv4, fails after ~200ms, reports COULDNT_CONNECT */
  317. { 2, TURL, "test.com:123:192.0.2.1,192.0.2.2", CURL_IPRESOLVE_WHATEVER,
  318. CNCT_TMOT, 150, 200, 200, 2, 0, 400, TC_TMOT, R_FAIL, NULL },
  319. /* 2 ipv4, fails after ~400ms, reports COULDNT_CONNECT */
  320. #ifdef ENABLE_IPV6
  321. { 3, TURL, "test.com:123:::1", CURL_IPRESOLVE_WHATEVER,
  322. CNCT_TMOT, 150, 200, 200, 0, 1, 200, TC_TMOT, R_FAIL, NULL },
  323. /* 1 ipv6, fails after ~200ms, reports COULDNT_CONNECT */
  324. { 4, TURL, "test.com:123:::1,::2", CURL_IPRESOLVE_WHATEVER,
  325. CNCT_TMOT, 150, 200, 200, 0, 2, 400, TC_TMOT, R_FAIL, NULL },
  326. /* 2 ipv6, fails after ~400ms, reports COULDNT_CONNECT */
  327. { 5, TURL, "test.com:123:192.0.2.1,::1", CURL_IPRESOLVE_WHATEVER,
  328. CNCT_TMOT, 150, 200, 200, 1, 1, 350, TC_TMOT, R_FAIL, "v4" },
  329. /* mixed ip4+6, v4 starts, v6 kicks in on HE, fails after ~350ms */
  330. { 6, TURL, "test.com:123:::1,192.0.2.1", CURL_IPRESOLVE_WHATEVER,
  331. CNCT_TMOT, 150, 200, 200, 1, 1, 350, TC_TMOT, R_FAIL, "v6" },
  332. /* mixed ip6+4, v6 starts, v4 never starts due to high HE, TIMEOUT */
  333. { 7, TURL, "test.com:123:192.0.2.1,::1", CURL_IPRESOLVE_V4,
  334. CNCT_TMOT, 150, 500, 500, 1, 0, 400, TC_TMOT, R_FAIL, NULL },
  335. /* mixed ip4+6, but only use v4, check it uses full connect timeout,
  336. although another address of the 'wrong' family is available */
  337. { 8, TURL, "test.com:123:::1,192.0.2.1", CURL_IPRESOLVE_V6,
  338. CNCT_TMOT, 150, 500, 500, 0, 1, 400, TC_TMOT, R_FAIL, NULL },
  339. /* mixed ip4+6, but only use v6, check it uses full connect timeout,
  340. although another address of the 'wrong' family is available */
  341. #endif
  342. };
  343. UNITTEST_START
  344. #if defined(DEBUGBUILD)
  345. size_t i;
  346. for(i = 0; i < sizeof(TEST_CASES)/sizeof(TEST_CASES[0]); ++i) {
  347. test_connect(&TEST_CASES[i]);
  348. }
  349. #else
  350. (void)TEST_CASES;
  351. (void)test_connect;
  352. #endif
  353. UNITTEST_STOP