Imported Upstream version 7.59.0
[platform/upstream/curl.git] / tests / unit / unit1606.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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.haxx.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
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     return CURLE_OUT_OF_MEMORY;
37   return res;
38 }
39
40 static void unit_stop(void)
41 {
42   curl_easy_cleanup(easy);
43   curl_global_cleanup();
44 }
45
46 static int runawhile(long time_limit,
47                      long speed_limit,
48                      curl_off_t speed,
49                      int dec)
50 {
51   int counter = 1;
52   struct curltime now = {1, 0};
53   CURLcode result;
54   int finaltime;
55
56   curl_easy_setopt(easy, CURLOPT_LOW_SPEED_LIMIT, speed_limit);
57   curl_easy_setopt(easy, CURLOPT_LOW_SPEED_TIME, time_limit);
58   Curl_speedinit(easy);
59
60   do {
61     /* fake the current transfer speed */
62     easy->progress.current_speed = speed;
63     result = Curl_speedcheck(easy, now);
64     if(result)
65       break;
66     /* step the time */
67     now.tv_sec = ++counter;
68     speed -= dec;
69   } while(counter < 100);
70
71   finaltime = (int)(now.tv_sec - 1);
72
73   return finaltime;
74 }
75
76 UNITTEST_START
77   fail_unless(runawhile(41, 41, 40, 0) == 41,
78               "wrong low speed timeout");
79   fail_unless(runawhile(21, 21, 20, 0) == 21,
80               "wrong low speed timeout");
81   fail_unless(runawhile(60, 60, 40, 0) == 60,
82               "wrong log speed timeout");
83   fail_unless(runawhile(50, 50, 40, 0) == 50,
84               "wrong log speed timeout");
85   fail_unless(runawhile(40, 40, 40, 0) == 99,
86               "should not time out");
87   fail_unless(runawhile(10, 50, 100, 2) == 36,
88               "bad timeout");
89 UNITTEST_STOP