xref: /curl/tests/libtest/lib1592.c (revision 25cbc2f7)
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 /*
25  * See https://github.com/curl/curl/issues/3371
26  *
27  * This test case checks whether curl_multi_remove_handle() cancels
28  * asynchronous DNS resolvers without blocking where possible.  Obviously, it
29  * only tests whichever resolver cURL is actually built with.
30  */
31 
32 /* We're willing to wait a very generous two seconds for the removal.  This is
33    as low as we can go while still easily supporting SIGALRM timing for the
34    non-threaded blocking resolver.  It doesn't matter that much because when
35    the test passes, we never wait this long. We set it much higher to avoid
36    issues when running on overloaded CI machines. */
37 #define TEST_HANG_TIMEOUT 60 * 1000
38 
39 #include "test.h"
40 #include "testutil.h"
41 
42 #include <sys/stat.h>
43 
test(char * URL)44 CURLcode test(char *URL)
45 {
46   int stillRunning;
47   CURLM *multiHandle = NULL;
48   CURL *curl = NULL;
49   CURLcode res = CURLE_OK;
50   CURLMcode mres;
51   int timeout;
52 
53   global_init(CURL_GLOBAL_ALL);
54 
55   multi_init(multiHandle);
56 
57   easy_init(curl);
58 
59   easy_setopt(curl, CURLOPT_VERBOSE, 1L);
60   easy_setopt(curl, CURLOPT_URL, URL);
61 
62   /* Set a DNS server that hopefully will not respond when using c-ares. */
63   if(curl_easy_setopt(curl, CURLOPT_DNS_SERVERS, "0.0.0.0") == CURLE_OK)
64     /* Since we could set the DNS server, presume we are working with a
65        resolver that can be cancelled (i.e. c-ares).  Thus,
66        curl_multi_remove_handle() should not block even when the resolver
67        request is outstanding.  So, set a request timeout _longer_ than the
68        test hang timeout so we will fail if the handle removal call incorrectly
69        blocks. */
70     timeout = TEST_HANG_TIMEOUT * 2;
71   else {
72     /* If we can't set the DNS server, presume that we are configured to use a
73        resolver that can't be cancelled (i.e. the threaded resolver or the
74        non-threaded blocking resolver).  So, we just test that the
75        curl_multi_remove_handle() call does finish well within our test
76        timeout.
77 
78        But, it is very unlikely that the resolver request will take any time at
79        all because we haven't been able to configure the resolver to use an
80        non-responsive DNS server.  At least we exercise the flow.
81        */
82     fprintf(stderr,
83             "CURLOPT_DNS_SERVERS not supported; "
84             "assuming curl_multi_remove_handle() will block\n");
85     timeout = TEST_HANG_TIMEOUT / 2;
86   }
87 
88   /* Setting a timeout on the request should ensure that even if we have to
89      wait for the resolver during curl_multi_remove_handle(), it won't take
90      longer than this, because the resolver request inherits its timeout from
91      this. */
92   easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout);
93 
94   multi_add_handle(multiHandle, curl);
95 
96   /* This should move the handle from INIT => CONNECT => WAITRESOLVE. */
97   fprintf(stderr, "curl_multi_perform()...\n");
98   multi_perform(multiHandle, &stillRunning);
99   fprintf(stderr, "curl_multi_perform() succeeded\n");
100 
101   /* Start measuring how long it takes to remove the handle. */
102   fprintf(stderr, "curl_multi_remove_handle()...\n");
103   start_test_timing();
104   mres = curl_multi_remove_handle(multiHandle, curl);
105   if(mres) {
106     fprintf(stderr, "curl_multi_remove_handle() failed, with code %d\n", mres);
107     res = TEST_ERR_MULTI;
108     goto test_cleanup;
109   }
110   fprintf(stderr, "curl_multi_remove_handle() succeeded\n");
111 
112   /* Fail the test if it took too long to remove.  This happens after the fact,
113      and says "it seems that it would have run forever", which isn't true, but
114      it's close enough, and simple to do. */
115   abort_on_test_timeout();
116 
117 test_cleanup:
118   curl_easy_cleanup(curl);
119   curl_multi_cleanup(multiHandle);
120   curl_global_cleanup();
121 
122   return res;
123 }
124