1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, 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 ***************************************************************************/
22 #include "curlcheck.h"
23
24 #include "speedcheck.h"
25 #include "urldata.h"
26
27 static CURL *easy;
28
unit_setup(void)29 static CURLcode unit_setup(void)
30 {
31 int 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
unit_stop(void)42 static void unit_stop(void)
43 {
44 curl_easy_cleanup(easy);
45 curl_global_cleanup();
46 }
47
runawhile(long time_limit,long speed_limit,curl_off_t speed,int dec)48 static int runawhile(long time_limit,
49 long speed_limit,
50 curl_off_t speed,
51 int dec)
52 {
53 int counter = 1;
54 struct curltime now = {1, 0};
55 CURLcode result;
56 int finaltime;
57
58 curl_easy_setopt(easy, CURLOPT_LOW_SPEED_LIMIT, speed_limit);
59 curl_easy_setopt(easy, CURLOPT_LOW_SPEED_TIME, time_limit);
60 Curl_speedinit(easy);
61
62 do {
63 /* fake the current transfer speed */
64 easy->progress.current_speed = speed;
65 result = Curl_speedcheck(easy, now);
66 if(result)
67 break;
68 /* step the time */
69 now.tv_sec = ++counter;
70 speed -= dec;
71 } while(counter < 100);
72
73 finaltime = (int)(now.tv_sec - 1);
74
75 return finaltime;
76 }
77
78 UNITTEST_START
79 fail_unless(runawhile(41, 41, 40, 0) == 41,
80 "wrong low speed timeout");
81 fail_unless(runawhile(21, 21, 20, 0) == 21,
82 "wrong low speed timeout");
83 fail_unless(runawhile(60, 60, 40, 0) == 60,
84 "wrong log speed timeout");
85 fail_unless(runawhile(50, 50, 40, 0) == 50,
86 "wrong log speed timeout");
87 fail_unless(runawhile(40, 40, 40, 0) == 99,
88 "should not time out");
89 fail_unless(runawhile(10, 50, 100, 2) == 36,
90 "bad timeout");
91 UNITTEST_STOP
92