| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 CURLcode unit_setup(void) | 
|  | 28 | { | 
|  | 29 | return CURLE_OK; | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | static void unit_stop(void) | 
|  | 33 | { | 
|  | 34 |  | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | static int runawhile(struct Curl_easy *easy, | 
|  | 38 | long time_limit, | 
|  | 39 | long speed_limit, | 
|  | 40 | curl_off_t speed, | 
|  | 41 | int dec) | 
|  | 42 | { | 
|  | 43 | int counter = 1; | 
|  | 44 | struct timeval now = {1, 0}; | 
|  | 45 | CURLcode result; | 
|  | 46 | int finaltime; | 
|  | 47 |  | 
|  | 48 | curl_easy_setopt(easy, CURLOPT_LOW_SPEED_LIMIT, speed_limit); | 
|  | 49 | curl_easy_setopt(easy, CURLOPT_LOW_SPEED_TIME, time_limit); | 
|  | 50 | Curl_speedinit(easy); | 
|  | 51 |  | 
|  | 52 | do { | 
|  | 53 | /* fake the current transfer speed */ | 
|  | 54 | easy->progress.current_speed = speed; | 
|  | 55 | result = Curl_speedcheck(easy, now); | 
|  | 56 | if(result) | 
|  | 57 | break; | 
|  | 58 | /* step the time */ | 
|  | 59 | now.tv_sec = ++counter; | 
|  | 60 | speed -= dec; | 
|  | 61 | } while(counter < 100); | 
|  | 62 |  | 
|  | 63 | finaltime = (int)(now.tv_sec - 1); | 
|  | 64 |  | 
|  | 65 | return finaltime; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | UNITTEST_START | 
|  | 69 | { | 
|  | 70 | struct Curl_easy *easy = curl_easy_init(); | 
|  | 71 | abort_unless(easy, "out of memory"); | 
|  | 72 |  | 
|  | 73 | fail_unless(runawhile(easy, 41, 41, 40, 0) == 41, | 
|  | 74 | "wrong low speed timeout"); | 
|  | 75 | fail_unless(runawhile(easy, 21, 21, 20, 0) == 21, | 
|  | 76 | "wrong low speed timeout"); | 
|  | 77 | fail_unless(runawhile(easy, 60, 60, 40, 0) == 60, | 
|  | 78 | "wrong log speed timeout"); | 
|  | 79 | fail_unless(runawhile(easy, 50, 50, 40, 0) == 50, | 
|  | 80 | "wrong log speed timeout"); | 
|  | 81 | fail_unless(runawhile(easy, 40, 40, 40, 0) == 99, | 
|  | 82 | "should not time out"); | 
|  | 83 | fail_unless(runawhile(easy, 10, 50, 100, 2) == 36, | 
|  | 84 | "bad timeout"); | 
|  | 85 |  | 
|  | 86 | curl_easy_cleanup(easy); | 
|  | 87 |  | 
|  | 88 | return 0; | 
|  | 89 | } | 
|  | 90 | UNITTEST_STOP |