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