unit1606.c 2.6 KB

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