unit1606.c 2.6 KB

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