quickjs-tart

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

unit1606.c (2899B)


      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 "speedcheck.h"
     27 #include "urldata.h"
     28 
     29 static CURLcode t1606_setup(struct Curl_easy **easy)
     30 {
     31   CURLcode res = CURLE_OK;
     32 
     33   global_init(CURL_GLOBAL_ALL);
     34   *easy = curl_easy_init();
     35   if(!*easy) {
     36     curl_global_cleanup();
     37     return CURLE_OUT_OF_MEMORY;
     38   }
     39   return res;
     40 }
     41 
     42 static void t1606_stop(struct Curl_easy *easy)
     43 {
     44   curl_easy_cleanup(easy);
     45   curl_global_cleanup();
     46 }
     47 
     48 static int runawhile(struct Curl_easy *easy,
     49                      long time_limit,
     50                      long speed_limit,
     51                      curl_off_t speed,
     52                      int dec)
     53 {
     54   int counter = 1;
     55   struct curltime now = {1, 0};
     56   CURLcode result;
     57   int finaltime;
     58 
     59   curl_easy_setopt(easy, CURLOPT_LOW_SPEED_LIMIT, speed_limit);
     60   curl_easy_setopt(easy, CURLOPT_LOW_SPEED_TIME, time_limit);
     61   Curl_speedinit(easy);
     62 
     63   do {
     64     /* fake the current transfer speed */
     65     easy->progress.current_speed = speed;
     66     result = Curl_speedcheck(easy, now);
     67     if(result)
     68       break;
     69     /* step the time */
     70     now.tv_sec = ++counter;
     71     speed -= dec;
     72   } while(counter < 100);
     73 
     74   finaltime = (int)(now.tv_sec - 1);
     75 
     76   return finaltime;
     77 }
     78 
     79 static CURLcode test_unit1606(char *arg)
     80 {
     81   struct Curl_easy *easy;
     82 
     83   UNITTEST_BEGIN(t1606_setup(&easy))
     84 
     85   fail_unless(runawhile(easy, 41, 41, 40, 0) == 41,
     86               "wrong low speed timeout");
     87   fail_unless(runawhile(easy, 21, 21, 20, 0) == 21,
     88               "wrong low speed timeout");
     89   fail_unless(runawhile(easy, 60, 60, 40, 0) == 60,
     90               "wrong log speed timeout");
     91   fail_unless(runawhile(easy, 50, 50, 40, 0) == 50,
     92               "wrong log speed timeout");
     93   fail_unless(runawhile(easy, 40, 40, 40, 0) == 99,
     94               "should not time out");
     95   fail_unless(runawhile(easy, 10, 50, 100, 2) == 36,
     96               "bad timeout");
     97 
     98   UNITTEST_END(t1606_stop(easy))
     99 }