quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

unit1303.c (5675B)


      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 "unitcheck.h"
     25 
     26 #include "urldata.h"
     27 #include "connect.h"
     28 #include "memdebug.h" /* LAST include file */
     29 
     30 static CURLcode t1303_setup(struct Curl_easy **easy)
     31 {
     32   CURLcode res = CURLE_OK;
     33 
     34   global_init(CURL_GLOBAL_ALL);
     35   *easy = curl_easy_init();
     36   if(!*easy) {
     37     curl_global_cleanup();
     38     return CURLE_OUT_OF_MEMORY;
     39   }
     40   return res;
     41 }
     42 
     43 static void t1303_stop(struct Curl_easy *easy)
     44 {
     45   curl_easy_cleanup(easy);
     46   curl_global_cleanup();
     47 }
     48 
     49 /* BASE is just a define to make us fool around with decently large number so
     50    that we aren't zero-based */
     51 #define BASE 1000000
     52 
     53 /* macro to set the pretended current time */
     54 #define NOW(x,y) now.tv_sec = x; now.tv_usec = y
     55 /* macro to set the millisecond based timeouts to use */
     56 #define TIMEOUTS(x,y) easy->set.timeout = x; \
     57                       easy->set.connecttimeout = y
     58 
     59 /*
     60  * To test:
     61  *
     62  * 00/10/01/11 timeouts set
     63  * 0/1         during connect
     64  * T           various values on the timeouts
     65  * N           various values of now
     66  */
     67 
     68 static CURLcode test_unit1303(char *arg)
     69 {
     70   struct Curl_easy *easy;
     71 
     72   UNITTEST_BEGIN(t1303_setup(&easy))
     73 
     74   struct curltime now;
     75   unsigned int i;
     76 
     77   struct timetest {
     78     int now_s;
     79     int now_us;
     80     unsigned int timeout_ms;
     81     unsigned int connecttimeout_ms;
     82     bool connecting;
     83     timediff_t result;
     84     const char *comment;
     85   };
     86 
     87   const struct timetest run[] = {
     88   /* both timeouts set, not connecting */
     89   {BASE + 4, 0,      10000, 8000, FALSE, 6000, "6 seconds should be left"},
     90   {BASE + 4, 990000, 10000, 8000, FALSE, 5010, "5010 ms should be left"},
     91   {BASE + 10, 0,     10000, 8000, FALSE, -1,   "timeout is -1, expired"},
     92   {BASE + 12, 0,     10000, 8000, FALSE, -2000, "-2000, overdue 2 seconds"},
     93 
     94   /* both timeouts set, connecting */
     95   {BASE + 4, 0,      10000, 8000, TRUE, 4000, "4 seconds should be left"},
     96   {BASE + 4, 990000, 10000, 8000, TRUE, 3010, "3010 ms should be left"},
     97   {BASE + 8, 0,      10000, 8000, TRUE, -1,   "timeout is -1, expired"},
     98   {BASE + 10, 0,     10000, 8000, TRUE, -2000, "-2000, overdue 2 seconds"},
     99 
    100   /* no connect timeout set, not connecting */
    101   {BASE + 4, 0,      10000, 0, FALSE, 6000, "6 seconds should be left"},
    102   {BASE + 4, 990000, 10000, 0, FALSE, 5010, "5010 ms should be left"},
    103   {BASE + 10, 0,     10000, 0, FALSE, -1,   "timeout is -1, expired"},
    104   {BASE + 12, 0,     10000, 0, FALSE, -2000, "-2000, overdue 2 seconds"},
    105 
    106   /* no connect timeout set, connecting */
    107   {BASE + 4, 0,      10000, 0, TRUE, 6000, "6 seconds should be left"},
    108   {BASE + 4, 990000, 10000, 0, TRUE, 5010, "5010 ms should be left"},
    109   {BASE + 10, 0,     10000, 0, TRUE, -1,   "timeout is -1, expired"},
    110   {BASE + 12, 0,     10000, 0, TRUE, -2000, "-2000, overdue 2 seconds"},
    111 
    112   /* only connect timeout set, not connecting */
    113   {BASE + 4, 0,      0, 10000, FALSE, 0, "no timeout active"},
    114   {BASE + 4, 990000, 0, 10000, FALSE, 0, "no timeout active"},
    115   {BASE + 10, 0,     0, 10000, FALSE, 0, "no timeout active"},
    116   {BASE + 12, 0,     0, 10000, FALSE, 0, "no timeout active"},
    117 
    118   /* only connect timeout set, connecting */
    119   {BASE + 4, 0,      0, 10000, TRUE, 6000, "6 seconds should be left"},
    120   {BASE + 4, 990000, 0, 10000, TRUE, 5010, "5010 ms should be left"},
    121   {BASE + 10, 0,     0, 10000, TRUE, -1,   "timeout is -1, expired"},
    122   {BASE + 12, 0,     0, 10000, TRUE, -2000, "-2000, overdue 2 seconds"},
    123 
    124   /* no timeout set, not connecting */
    125   {BASE + 4, 0,      0, 0, FALSE, 0, "no timeout active"},
    126   {BASE + 4, 990000, 0, 0, FALSE, 0, "no timeout active"},
    127   {BASE + 10, 0,     0, 0, FALSE, 0, "no timeout active"},
    128   {BASE + 12, 0,     0, 0, FALSE, 0, "no timeout active"},
    129 
    130   /* no timeout set, connecting */
    131   {BASE + 4, 0,      0, 0, TRUE, 296000, "no timeout active"},
    132   {BASE + 4, 990000, 0, 0, TRUE, 295010, "no timeout active"},
    133   {BASE + 10, 0,     0, 0, TRUE, 290000, "no timeout active"},
    134   {BASE + 12, 0,     0, 0, TRUE, 288000, "no timeout active"},
    135 
    136   /* both timeouts set, connecting, connect timeout the longer one */
    137   {BASE + 4, 0,      10000, 12000, TRUE, 6000, "6 seconds should be left"},
    138 
    139   };
    140 
    141   /* this is the pretended start time of the transfer */
    142   easy->progress.t_startsingle.tv_sec = BASE;
    143   easy->progress.t_startsingle.tv_usec = 0;
    144   easy->progress.t_startop.tv_sec = BASE;
    145   easy->progress.t_startop.tv_usec = 0;
    146 
    147   for(i = 0; i < CURL_ARRAYSIZE(run); i++) {
    148     timediff_t timeout;
    149     NOW(run[i].now_s, run[i].now_us);
    150     TIMEOUTS(run[i].timeout_ms, run[i].connecttimeout_ms);
    151     timeout =  Curl_timeleft(easy, &now, run[i].connecting);
    152     if(timeout != run[i].result)
    153       fail(run[i].comment);
    154   }
    155 
    156   UNITTEST_END(t1303_stop(easy))
    157 }