xref: /third_party/curl/tests/unit/unit1303.c (revision 13498266)
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 "curlcheck.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci#include "urldata.h"
2713498266Sopenharmony_ci#include "connect.h"
2813498266Sopenharmony_ci#include "memdebug.h" /* LAST include file */
2913498266Sopenharmony_ci
3013498266Sopenharmony_cistatic struct Curl_easy *data;
3113498266Sopenharmony_ci
3213498266Sopenharmony_cistatic CURLcode unit_setup(void)
3313498266Sopenharmony_ci{
3413498266Sopenharmony_ci  CURLcode res = CURLE_OK;
3513498266Sopenharmony_ci
3613498266Sopenharmony_ci  global_init(CURL_GLOBAL_ALL);
3713498266Sopenharmony_ci  data = curl_easy_init();
3813498266Sopenharmony_ci  if(!data) {
3913498266Sopenharmony_ci    curl_global_cleanup();
4013498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
4113498266Sopenharmony_ci  }
4213498266Sopenharmony_ci  return res;
4313498266Sopenharmony_ci}
4413498266Sopenharmony_ci
4513498266Sopenharmony_cistatic void unit_stop(void)
4613498266Sopenharmony_ci{
4713498266Sopenharmony_ci  curl_easy_cleanup(data);
4813498266Sopenharmony_ci  curl_global_cleanup();
4913498266Sopenharmony_ci}
5013498266Sopenharmony_ci
5113498266Sopenharmony_ci/* BASE is just a define to make us fool around with decently large number so
5213498266Sopenharmony_ci   that we aren't zero-based */
5313498266Sopenharmony_ci#define BASE 1000000
5413498266Sopenharmony_ci
5513498266Sopenharmony_ci/* macro to set the pretended current time */
5613498266Sopenharmony_ci#define NOW(x,y) now.tv_sec = x; now.tv_usec = y
5713498266Sopenharmony_ci/* macro to set the millisecond based timeouts to use */
5813498266Sopenharmony_ci#define TIMEOUTS(x,y) data->set.timeout = x; data->set.connecttimeout = y
5913498266Sopenharmony_ci
6013498266Sopenharmony_ci/*
6113498266Sopenharmony_ci * To test:
6213498266Sopenharmony_ci *
6313498266Sopenharmony_ci * 00/10/01/11 timeouts set
6413498266Sopenharmony_ci * 0/1         during connect
6513498266Sopenharmony_ci * T           various values on the timeouts
6613498266Sopenharmony_ci * N           various values of now
6713498266Sopenharmony_ci */
6813498266Sopenharmony_ci
6913498266Sopenharmony_cistruct timetest {
7013498266Sopenharmony_ci  int now_s;
7113498266Sopenharmony_ci  int now_us;
7213498266Sopenharmony_ci  int timeout_ms;
7313498266Sopenharmony_ci  int connecttimeout_ms;
7413498266Sopenharmony_ci  bool connecting;
7513498266Sopenharmony_ci  timediff_t result;
7613498266Sopenharmony_ci  const char *comment;
7713498266Sopenharmony_ci};
7813498266Sopenharmony_ci
7913498266Sopenharmony_ciUNITTEST_START
8013498266Sopenharmony_ci{
8113498266Sopenharmony_ci  struct curltime now;
8213498266Sopenharmony_ci  unsigned int i;
8313498266Sopenharmony_ci
8413498266Sopenharmony_ci  const struct timetest run[] = {
8513498266Sopenharmony_ci  /* both timeouts set, not connecting */
8613498266Sopenharmony_ci  {BASE + 4, 0,      10000, 8000, FALSE, 6000, "6 seconds should be left"},
8713498266Sopenharmony_ci  {BASE + 4, 990000, 10000, 8000, FALSE, 5010, "5010 ms should be left"},
8813498266Sopenharmony_ci  {BASE + 10, 0,     10000, 8000, FALSE, -1,   "timeout is -1, expired"},
8913498266Sopenharmony_ci  {BASE + 12, 0,     10000, 8000, FALSE, -2000, "-2000, overdue 2 seconds"},
9013498266Sopenharmony_ci
9113498266Sopenharmony_ci  /* both timeouts set, connecting */
9213498266Sopenharmony_ci  {BASE + 4, 0,      10000, 8000, TRUE, 4000, "4 seconds should be left"},
9313498266Sopenharmony_ci  {BASE + 4, 990000, 10000, 8000, TRUE, 3010, "3010 ms should be left"},
9413498266Sopenharmony_ci  {BASE + 8, 0,      10000, 8000, TRUE, -1,   "timeout is -1, expired"},
9513498266Sopenharmony_ci  {BASE + 10, 0,     10000, 8000, TRUE, -2000, "-2000, overdue 2 seconds"},
9613498266Sopenharmony_ci
9713498266Sopenharmony_ci  /* no connect timeout set, not connecting */
9813498266Sopenharmony_ci  {BASE + 4, 0,      10000, 0, FALSE, 6000, "6 seconds should be left"},
9913498266Sopenharmony_ci  {BASE + 4, 990000, 10000, 0, FALSE, 5010, "5010 ms should be left"},
10013498266Sopenharmony_ci  {BASE + 10, 0,     10000, 0, FALSE, -1,   "timeout is -1, expired"},
10113498266Sopenharmony_ci  {BASE + 12, 0,     10000, 0, FALSE, -2000, "-2000, overdue 2 seconds"},
10213498266Sopenharmony_ci
10313498266Sopenharmony_ci  /* no connect timeout set, connecting */
10413498266Sopenharmony_ci  {BASE + 4, 0,      10000, 0, TRUE, 6000, "6 seconds should be left"},
10513498266Sopenharmony_ci  {BASE + 4, 990000, 10000, 0, TRUE, 5010, "5010 ms should be left"},
10613498266Sopenharmony_ci  {BASE + 10, 0,     10000, 0, TRUE, -1,   "timeout is -1, expired"},
10713498266Sopenharmony_ci  {BASE + 12, 0,     10000, 0, TRUE, -2000, "-2000, overdue 2 seconds"},
10813498266Sopenharmony_ci
10913498266Sopenharmony_ci  /* only connect timeout set, not connecting */
11013498266Sopenharmony_ci  {BASE + 4, 0,      0, 10000, FALSE, 0, "no timeout active"},
11113498266Sopenharmony_ci  {BASE + 4, 990000, 0, 10000, FALSE, 0, "no timeout active"},
11213498266Sopenharmony_ci  {BASE + 10, 0,     0, 10000, FALSE, 0, "no timeout active"},
11313498266Sopenharmony_ci  {BASE + 12, 0,     0, 10000, FALSE, 0, "no timeout active"},
11413498266Sopenharmony_ci
11513498266Sopenharmony_ci  /* only connect timeout set, connecting */
11613498266Sopenharmony_ci  {BASE + 4, 0,      0, 10000, TRUE, 6000, "6 seconds should be left"},
11713498266Sopenharmony_ci  {BASE + 4, 990000, 0, 10000, TRUE, 5010, "5010 ms should be left"},
11813498266Sopenharmony_ci  {BASE + 10, 0,     0, 10000, TRUE, -1,   "timeout is -1, expired"},
11913498266Sopenharmony_ci  {BASE + 12, 0,     0, 10000, TRUE, -2000, "-2000, overdue 2 seconds"},
12013498266Sopenharmony_ci
12113498266Sopenharmony_ci  /* no timeout set, not connecting */
12213498266Sopenharmony_ci  {BASE + 4, 0,      0, 0, FALSE, 0, "no timeout active"},
12313498266Sopenharmony_ci  {BASE + 4, 990000, 0, 0, FALSE, 0, "no timeout active"},
12413498266Sopenharmony_ci  {BASE + 10, 0,     0, 0, FALSE, 0, "no timeout active"},
12513498266Sopenharmony_ci  {BASE + 12, 0,     0, 0, FALSE, 0, "no timeout active"},
12613498266Sopenharmony_ci
12713498266Sopenharmony_ci  /* no timeout set, connecting */
12813498266Sopenharmony_ci  {BASE + 4, 0,      0, 0, TRUE, 296000, "no timeout active"},
12913498266Sopenharmony_ci  {BASE + 4, 990000, 0, 0, TRUE, 295010, "no timeout active"},
13013498266Sopenharmony_ci  {BASE + 10, 0,     0, 0, TRUE, 290000, "no timeout active"},
13113498266Sopenharmony_ci  {BASE + 12, 0,     0, 0, TRUE, 288000, "no timeout active"},
13213498266Sopenharmony_ci
13313498266Sopenharmony_ci  /* both timeouts set, connecting, connect timeout the longer one */
13413498266Sopenharmony_ci  {BASE + 4, 0,      10000, 12000, TRUE, 6000, "6 seconds should be left"},
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci  };
13713498266Sopenharmony_ci
13813498266Sopenharmony_ci  /* this is the pretended start time of the transfer */
13913498266Sopenharmony_ci  data->progress.t_startsingle.tv_sec = BASE;
14013498266Sopenharmony_ci  data->progress.t_startsingle.tv_usec = 0;
14113498266Sopenharmony_ci  data->progress.t_startop.tv_sec = BASE;
14213498266Sopenharmony_ci  data->progress.t_startop.tv_usec = 0;
14313498266Sopenharmony_ci
14413498266Sopenharmony_ci  for(i = 0; i < sizeof(run)/sizeof(run[0]); i++) {
14513498266Sopenharmony_ci    timediff_t timeout;
14613498266Sopenharmony_ci    NOW(run[i].now_s, run[i].now_us);
14713498266Sopenharmony_ci    TIMEOUTS(run[i].timeout_ms, run[i].connecttimeout_ms);
14813498266Sopenharmony_ci    timeout =  Curl_timeleft(data, &now, run[i].connecting);
14913498266Sopenharmony_ci    if(timeout != run[i].result)
15013498266Sopenharmony_ci      fail(run[i].comment);
15113498266Sopenharmony_ci  }
15213498266Sopenharmony_ci}
15313498266Sopenharmony_ciUNITTEST_STOP
154