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