113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include "test.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci#include <limits.h>
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#include "testutil.h"
2913498266Sopenharmony_ci#include "warnless.h"
3013498266Sopenharmony_ci#include "memdebug.h"
3113498266Sopenharmony_ci
3213498266Sopenharmony_ci#define TEST_HANG_TIMEOUT 60 * 1000
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci/*
3513498266Sopenharmony_ci * Test case for below scenario:
3613498266Sopenharmony_ci *   - Connect to an FTP server using CONNECT_ONLY option
3713498266Sopenharmony_ci *
3813498266Sopenharmony_ci * The test case originated for verifying CONNECT_ONLY option shall not
3913498266Sopenharmony_ci * block after protocol connect is done, but it returns the message
4013498266Sopenharmony_ci * with function curl_multi_info_read().
4113498266Sopenharmony_ci */
4213498266Sopenharmony_ci
4313498266Sopenharmony_ciint test(char *URL)
4413498266Sopenharmony_ci{
4513498266Sopenharmony_ci  CURL *easy = NULL;
4613498266Sopenharmony_ci  CURLM *multi = NULL;
4713498266Sopenharmony_ci  int res = 0;
4813498266Sopenharmony_ci  int running;
4913498266Sopenharmony_ci  int msgs_left;
5013498266Sopenharmony_ci  CURLMsg *msg;
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci  start_test_timing();
5313498266Sopenharmony_ci
5413498266Sopenharmony_ci  global_init(CURL_GLOBAL_ALL);
5513498266Sopenharmony_ci
5613498266Sopenharmony_ci  easy_init(easy);
5713498266Sopenharmony_ci
5813498266Sopenharmony_ci  multi_init(multi);
5913498266Sopenharmony_ci
6013498266Sopenharmony_ci  /* go verbose */
6113498266Sopenharmony_ci  easy_setopt(easy, CURLOPT_VERBOSE, 1L);
6213498266Sopenharmony_ci
6313498266Sopenharmony_ci  /* specify target */
6413498266Sopenharmony_ci  easy_setopt(easy, CURLOPT_URL, URL);
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci  easy_setopt(easy, CURLOPT_CONNECT_ONLY, 1L);
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci  multi_add_handle(multi, easy);
6913498266Sopenharmony_ci
7013498266Sopenharmony_ci  for(;;) {
7113498266Sopenharmony_ci    struct timeval interval;
7213498266Sopenharmony_ci    fd_set fdread;
7313498266Sopenharmony_ci    fd_set fdwrite;
7413498266Sopenharmony_ci    fd_set fdexcep;
7513498266Sopenharmony_ci    long timeout = -99;
7613498266Sopenharmony_ci    int maxfd = -99;
7713498266Sopenharmony_ci
7813498266Sopenharmony_ci    multi_perform(multi, &running);
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci    abort_on_test_timeout();
8113498266Sopenharmony_ci
8213498266Sopenharmony_ci    if(!running)
8313498266Sopenharmony_ci      break; /* done */
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci    FD_ZERO(&fdread);
8613498266Sopenharmony_ci    FD_ZERO(&fdwrite);
8713498266Sopenharmony_ci    FD_ZERO(&fdexcep);
8813498266Sopenharmony_ci
8913498266Sopenharmony_ci    multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
9013498266Sopenharmony_ci
9113498266Sopenharmony_ci    /* At this point, maxfd is guaranteed to be greater or equal than -1. */
9213498266Sopenharmony_ci
9313498266Sopenharmony_ci    multi_timeout(multi, &timeout);
9413498266Sopenharmony_ci
9513498266Sopenharmony_ci    /* At this point, timeout is guaranteed to be greater or equal than
9613498266Sopenharmony_ci       -1. */
9713498266Sopenharmony_ci
9813498266Sopenharmony_ci    if(timeout != -1L) {
9913498266Sopenharmony_ci      int itimeout;
10013498266Sopenharmony_ci#if LONG_MAX > INT_MAX
10113498266Sopenharmony_ci      itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
10213498266Sopenharmony_ci#else
10313498266Sopenharmony_ci      itimeout = (int)timeout;
10413498266Sopenharmony_ci#endif
10513498266Sopenharmony_ci      interval.tv_sec = itimeout/1000;
10613498266Sopenharmony_ci      interval.tv_usec = (itimeout%1000)*1000;
10713498266Sopenharmony_ci    }
10813498266Sopenharmony_ci    else {
10913498266Sopenharmony_ci      interval.tv_sec = TEST_HANG_TIMEOUT/1000 - 1;
11013498266Sopenharmony_ci      interval.tv_usec = 0;
11113498266Sopenharmony_ci    }
11213498266Sopenharmony_ci
11313498266Sopenharmony_ci    select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
11413498266Sopenharmony_ci
11513498266Sopenharmony_ci    abort_on_test_timeout();
11613498266Sopenharmony_ci  }
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci  msg = curl_multi_info_read(multi, &msgs_left);
11913498266Sopenharmony_ci  if(msg)
12013498266Sopenharmony_ci    res = msg->data.result;
12113498266Sopenharmony_ci
12213498266Sopenharmony_ci  multi_remove_handle(multi, easy);
12313498266Sopenharmony_ci
12413498266Sopenharmony_citest_cleanup:
12513498266Sopenharmony_ci
12613498266Sopenharmony_ci  /* undocumented cleanup sequence - type UA */
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci  curl_multi_cleanup(multi);
12913498266Sopenharmony_ci  curl_easy_cleanup(easy);
13013498266Sopenharmony_ci  curl_global_cleanup();
13113498266Sopenharmony_ci
13213498266Sopenharmony_ci  return res;
13313498266Sopenharmony_ci}
134