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