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