unit1606.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #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. return CURLE_OUT_OF_MEMORY;
  33. return res;
  34. }
  35. static void unit_stop(void)
  36. {
  37. curl_easy_cleanup(easy);
  38. curl_global_cleanup();
  39. }
  40. static int runawhile(long time_limit,
  41. long speed_limit,
  42. curl_off_t speed,
  43. int dec)
  44. {
  45. int counter = 1;
  46. struct curltime now = {1, 0};
  47. CURLcode result;
  48. int finaltime;
  49. curl_easy_setopt(easy, CURLOPT_LOW_SPEED_LIMIT, speed_limit);
  50. curl_easy_setopt(easy, CURLOPT_LOW_SPEED_TIME, time_limit);
  51. Curl_speedinit(easy);
  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. finaltime = (int)(now.tv_sec - 1);
  63. return finaltime;
  64. }
  65. UNITTEST_START
  66. fail_unless(runawhile(41, 41, 40, 0) == 41,
  67. "wrong low speed timeout");
  68. fail_unless(runawhile(21, 21, 20, 0) == 21,
  69. "wrong low speed timeout");
  70. fail_unless(runawhile(60, 60, 40, 0) == 60,
  71. "wrong log speed timeout");
  72. fail_unless(runawhile(50, 50, 40, 0) == 50,
  73. "wrong log speed timeout");
  74. fail_unless(runawhile(40, 40, 40, 0) == 99,
  75. "should not time out");
  76. fail_unless(runawhile(10, 50, 100, 2) == 36,
  77. "bad timeout");
  78. UNITTEST_STOP