xref: /curl/tests/libtest/lib597.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 #include "test.h"
25 
26 #include <limits.h>
27 
28 #include "testutil.h"
29 #include "warnless.h"
30 #include "memdebug.h"
31 
32 #define TEST_HANG_TIMEOUT 60 * 1000
33 
34 /*
35  * Test case for below scenario:
36  *   - Connect to an FTP server using CONNECT_ONLY option
37  *
38  * The test case originated for verifying CONNECT_ONLY option shall not
39  * block after protocol connect is done, but it returns the message
40  * with function curl_multi_info_read().
41  */
42 
test(char * URL)43 CURLcode test(char *URL)
44 {
45   CURL *easy = NULL;
46   CURLM *multi = NULL;
47   CURLcode res = CURLE_OK;
48   int running;
49   int msgs_left;
50   CURLMsg *msg;
51 
52   start_test_timing();
53 
54   global_init(CURL_GLOBAL_ALL);
55 
56   easy_init(easy);
57 
58   multi_init(multi);
59 
60   /* go verbose */
61   easy_setopt(easy, CURLOPT_VERBOSE, 1L);
62 
63   /* specify target */
64   easy_setopt(easy, CURLOPT_URL, URL);
65 
66   easy_setopt(easy, CURLOPT_CONNECT_ONLY, 1L);
67 
68   multi_add_handle(multi, easy);
69 
70   for(;;) {
71     struct timeval interval;
72     fd_set fdread;
73     fd_set fdwrite;
74     fd_set fdexcep;
75     long timeout = -99;
76     int maxfd = -99;
77 
78     multi_perform(multi, &running);
79 
80     abort_on_test_timeout();
81 
82     if(!running)
83       break; /* done */
84 
85     FD_ZERO(&fdread);
86     FD_ZERO(&fdwrite);
87     FD_ZERO(&fdexcep);
88 
89     multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
90 
91     /* At this point, maxfd is guaranteed to be greater or equal than -1. */
92 
93     multi_timeout(multi, &timeout);
94 
95     /* At this point, timeout is guaranteed to be greater or equal than
96        -1. */
97 
98     if(timeout != -1L) {
99       int itimeout;
100 #if LONG_MAX > INT_MAX
101       itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
102 #else
103       itimeout = (int)timeout;
104 #endif
105       interval.tv_sec = itimeout/1000;
106       interval.tv_usec = (itimeout%1000)*1000;
107     }
108     else {
109       interval.tv_sec = TEST_HANG_TIMEOUT/1000 - 1;
110       interval.tv_usec = 0;
111     }
112 
113     select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
114 
115     abort_on_test_timeout();
116   }
117 
118   msg = curl_multi_info_read(multi, &msgs_left);
119   if(msg)
120     res = msg->data.result;
121 
122   multi_remove_handle(multi, easy);
123 
124 test_cleanup:
125 
126   /* undocumented cleanup sequence - type UA */
127 
128   curl_multi_cleanup(multi);
129   curl_easy_cleanup(easy);
130   curl_global_cleanup();
131 
132   return res;
133 }
134