Imported Upstream version 7.59.0
[platform/upstream/curl.git] / tests / libtest / lib597.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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 #include "test.h"
23
24 #include <limits.h>
25
26 #include "testutil.h"
27 #include "warnless.h"
28 #include "memdebug.h"
29
30 #define TEST_HANG_TIMEOUT 5 * 1000
31
32 /*
33  * Test case for below scenario:
34  *   - Connect to an FTP server using CONNECT_ONLY option
35  *   - transfer some files with re-using the connection (omitted in test case)
36  *   - Disconnect from FTP server with sending QUIT command
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
43 enum {
44   CONNECT_ONLY_PHASE = 0,
45   QUIT_PHASE,
46   LAST_PHASE
47 };
48
49 int test(char *URL)
50 {
51   CURL *easy = NULL;
52   CURLM *multi = NULL;
53   int res = 0;
54   int running;
55   int msgs_left;
56   int phase;
57   CURLMsg *msg;
58
59   start_test_timing();
60
61   global_init(CURL_GLOBAL_ALL);
62
63   easy_init(easy);
64
65   multi_init(multi);
66
67   for(phase = CONNECT_ONLY_PHASE; phase < LAST_PHASE; ++phase) {
68     /* go verbose */
69     easy_setopt(easy, CURLOPT_VERBOSE, 1L);
70
71     /* specify target */
72     easy_setopt(easy, CURLOPT_URL, URL);
73
74     /* enable 'CONNECT_ONLY' option when in connect phase */
75     if(phase == CONNECT_ONLY_PHASE)
76       easy_setopt(easy, CURLOPT_CONNECT_ONLY, 1L);
77
78     /* enable 'NOBODY' option to send 'QUIT' command in quit phase */
79     if(phase == QUIT_PHASE) {
80       easy_setopt(easy, CURLOPT_CONNECT_ONLY, 0L);
81       easy_setopt(easy, CURLOPT_NOBODY, 1L);
82       easy_setopt(easy, CURLOPT_FORBID_REUSE, 1L);
83     }
84
85     multi_add_handle(multi, easy);
86
87     for(;;) {
88       struct timeval interval;
89       fd_set fdread;
90       fd_set fdwrite;
91       fd_set fdexcep;
92       long timeout = -99;
93       int maxfd = -99;
94
95       multi_perform(multi, &running);
96
97       abort_on_test_timeout();
98
99       if(!running)
100         break; /* done */
101
102       FD_ZERO(&fdread);
103       FD_ZERO(&fdwrite);
104       FD_ZERO(&fdexcep);
105
106       multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
107
108       /* At this point, maxfd is guaranteed to be greater or equal than -1. */
109
110       multi_timeout(multi, &timeout);
111
112       /* At this point, timeout is guaranteed to be greater or equal than
113          -1. */
114
115       if(timeout != -1L) {
116         int itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
117         interval.tv_sec = itimeout/1000;
118         interval.tv_usec = (itimeout%1000)*1000;
119       }
120       else {
121         interval.tv_sec = TEST_HANG_TIMEOUT/1000 + 1;
122         interval.tv_usec = 0;
123       }
124
125       select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
126
127       abort_on_test_timeout();
128     }
129
130     msg = curl_multi_info_read(multi, &msgs_left);
131     if(msg)
132       res = msg->data.result;
133
134     multi_remove_handle(multi, easy);
135   }
136
137 test_cleanup:
138
139   /* undocumented cleanup sequence - type UA */
140
141   curl_multi_cleanup(multi);
142   curl_easy_cleanup(easy);
143   curl_global_cleanup();
144
145   return res;
146 }