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/* lib591 is used for test cases 591, 592, 593 and 594 */
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#include <limits.h>
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#include <fcntl.h>
3113498266Sopenharmony_ci
3213498266Sopenharmony_ci#include "testutil.h"
3313498266Sopenharmony_ci#include "warnless.h"
3413498266Sopenharmony_ci#include "memdebug.h"
3513498266Sopenharmony_ci
3613498266Sopenharmony_ci#define TEST_HANG_TIMEOUT 60 * 1000
3713498266Sopenharmony_ci
3813498266Sopenharmony_ciint test(char *URL)
3913498266Sopenharmony_ci{
4013498266Sopenharmony_ci  CURL *easy = NULL;
4113498266Sopenharmony_ci  CURLM *multi = NULL;
4213498266Sopenharmony_ci  int res = 0;
4313498266Sopenharmony_ci  int running;
4413498266Sopenharmony_ci  int msgs_left;
4513498266Sopenharmony_ci  CURLMsg *msg;
4613498266Sopenharmony_ci  FILE *upload = NULL;
4713498266Sopenharmony_ci
4813498266Sopenharmony_ci  start_test_timing();
4913498266Sopenharmony_ci
5013498266Sopenharmony_ci  upload = fopen(libtest_arg3, "rb");
5113498266Sopenharmony_ci  if(!upload) {
5213498266Sopenharmony_ci    fprintf(stderr, "fopen() failed with error: %d (%s)\n",
5313498266Sopenharmony_ci            errno, strerror(errno));
5413498266Sopenharmony_ci    fprintf(stderr, "Error opening file: (%s)\n", libtest_arg3);
5513498266Sopenharmony_ci    return TEST_ERR_FOPEN;
5613498266Sopenharmony_ci  }
5713498266Sopenharmony_ci
5813498266Sopenharmony_ci  res_global_init(CURL_GLOBAL_ALL);
5913498266Sopenharmony_ci  if(res) {
6013498266Sopenharmony_ci    fclose(upload);
6113498266Sopenharmony_ci    return res;
6213498266Sopenharmony_ci  }
6313498266Sopenharmony_ci
6413498266Sopenharmony_ci  easy_init(easy);
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci  /* go verbose */
6713498266Sopenharmony_ci  easy_setopt(easy, CURLOPT_VERBOSE, 1L);
6813498266Sopenharmony_ci
6913498266Sopenharmony_ci  /* specify target */
7013498266Sopenharmony_ci  easy_setopt(easy, CURLOPT_URL, URL);
7113498266Sopenharmony_ci
7213498266Sopenharmony_ci  /* enable uploading */
7313498266Sopenharmony_ci  easy_setopt(easy, CURLOPT_UPLOAD, 1L);
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci  /* data pointer for the file read function */
7613498266Sopenharmony_ci  easy_setopt(easy, CURLOPT_READDATA, upload);
7713498266Sopenharmony_ci
7813498266Sopenharmony_ci  /* use active mode FTP */
7913498266Sopenharmony_ci  easy_setopt(easy, CURLOPT_FTPPORT, "-");
8013498266Sopenharmony_ci
8113498266Sopenharmony_ci  /* server connection timeout */
8213498266Sopenharmony_ci  easy_setopt(easy, CURLOPT_ACCEPTTIMEOUT_MS,
8313498266Sopenharmony_ci              strtol(libtest_arg2, NULL, 10)*1000);
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci  multi_init(multi);
8613498266Sopenharmony_ci
8713498266Sopenharmony_ci  multi_add_handle(multi, easy);
8813498266Sopenharmony_ci
8913498266Sopenharmony_ci  for(;;) {
9013498266Sopenharmony_ci    struct timeval interval;
9113498266Sopenharmony_ci    fd_set fdread;
9213498266Sopenharmony_ci    fd_set fdwrite;
9313498266Sopenharmony_ci    fd_set fdexcep;
9413498266Sopenharmony_ci    long timeout = -99;
9513498266Sopenharmony_ci    int maxfd = -99;
9613498266Sopenharmony_ci
9713498266Sopenharmony_ci    multi_perform(multi, &running);
9813498266Sopenharmony_ci
9913498266Sopenharmony_ci    abort_on_test_timeout();
10013498266Sopenharmony_ci
10113498266Sopenharmony_ci    if(!running)
10213498266Sopenharmony_ci      break; /* done */
10313498266Sopenharmony_ci
10413498266Sopenharmony_ci    FD_ZERO(&fdread);
10513498266Sopenharmony_ci    FD_ZERO(&fdwrite);
10613498266Sopenharmony_ci    FD_ZERO(&fdexcep);
10713498266Sopenharmony_ci
10813498266Sopenharmony_ci    multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
10913498266Sopenharmony_ci
11013498266Sopenharmony_ci    /* At this point, maxfd is guaranteed to be greater or equal than -1. */
11113498266Sopenharmony_ci
11213498266Sopenharmony_ci    multi_timeout(multi, &timeout);
11313498266Sopenharmony_ci
11413498266Sopenharmony_ci    /* At this point, timeout is guaranteed to be greater or equal than -1. */
11513498266Sopenharmony_ci
11613498266Sopenharmony_ci    if(timeout != -1L) {
11713498266Sopenharmony_ci      int itimeout;
11813498266Sopenharmony_ci#if LONG_MAX > INT_MAX
11913498266Sopenharmony_ci      itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
12013498266Sopenharmony_ci#else
12113498266Sopenharmony_ci      itimeout = (int)timeout;
12213498266Sopenharmony_ci#endif
12313498266Sopenharmony_ci      interval.tv_sec = itimeout/1000;
12413498266Sopenharmony_ci      interval.tv_usec = (itimeout%1000)*1000;
12513498266Sopenharmony_ci    }
12613498266Sopenharmony_ci    else {
12713498266Sopenharmony_ci      interval.tv_sec = 0;
12813498266Sopenharmony_ci      interval.tv_usec = 100000L; /* 100 ms */
12913498266Sopenharmony_ci    }
13013498266Sopenharmony_ci
13113498266Sopenharmony_ci    select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
13213498266Sopenharmony_ci
13313498266Sopenharmony_ci    abort_on_test_timeout();
13413498266Sopenharmony_ci  }
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci  msg = curl_multi_info_read(multi, &msgs_left);
13713498266Sopenharmony_ci  if(msg)
13813498266Sopenharmony_ci    res = msg->data.result;
13913498266Sopenharmony_ci
14013498266Sopenharmony_citest_cleanup:
14113498266Sopenharmony_ci
14213498266Sopenharmony_ci  /* undocumented cleanup sequence - type UA */
14313498266Sopenharmony_ci
14413498266Sopenharmony_ci  curl_multi_cleanup(multi);
14513498266Sopenharmony_ci  curl_easy_cleanup(easy);
14613498266Sopenharmony_ci  curl_global_cleanup();
14713498266Sopenharmony_ci
14813498266Sopenharmony_ci  /* close the local file */
14913498266Sopenharmony_ci  fclose(upload);
15013498266Sopenharmony_ci
15113498266Sopenharmony_ci  return res;
15213498266Sopenharmony_ci}
153