lib1523.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. /* test case and code based on https://github.com/curl/curl/issues/3927 */
  26. #include "testutil.h"
  27. #include "warnless.h"
  28. #include "memdebug.h"
  29. static int dload_progress_cb(void *a, curl_off_t b, curl_off_t c,
  30. curl_off_t d, curl_off_t e)
  31. {
  32. (void)a;
  33. (void)b;
  34. (void)c;
  35. (void)d;
  36. (void)e;
  37. return 0;
  38. }
  39. static size_t write_cb(char *d, size_t n, size_t l, void *p)
  40. {
  41. /* take care of the data here, ignored in this example */
  42. (void)d;
  43. (void)p;
  44. return n*l;
  45. }
  46. static CURLcode run(CURL *hnd, long limit, long time)
  47. {
  48. curl_easy_setopt(hnd, CURLOPT_LOW_SPEED_LIMIT, limit);
  49. curl_easy_setopt(hnd, CURLOPT_LOW_SPEED_TIME, time);
  50. return curl_easy_perform(hnd);
  51. }
  52. int test(char *URL)
  53. {
  54. CURLcode ret;
  55. CURL *hnd;
  56. char buffer[CURL_ERROR_SIZE];
  57. curl_global_init(CURL_GLOBAL_ALL);
  58. hnd = curl_easy_init();
  59. curl_easy_setopt(hnd, CURLOPT_URL, URL);
  60. curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, write_cb);
  61. curl_easy_setopt(hnd, CURLOPT_ERRORBUFFER, buffer);
  62. curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
  63. curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, dload_progress_cb);
  64. printf("Start: %d\n", time(NULL));
  65. ret = run(hnd, 1, 2);
  66. if(ret)
  67. fprintf(stderr, "error %d: %s\n", ret, buffer);
  68. ret = run(hnd, 12000, 1);
  69. if(ret != CURLE_OPERATION_TIMEDOUT)
  70. fprintf(stderr, "error %d: %s\n", ret, buffer);
  71. else
  72. ret = CURLE_OK;
  73. printf("End: %d\n", time(NULL));
  74. curl_easy_cleanup(hnd);
  75. curl_global_cleanup();
  76. return (int)ret;
  77. }