Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/libtest/lib1541.c
2649 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) 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 "first.h"
25
26
#include "memdebug.h"
27
28
struct t1541_transfer_status {
29
CURL *curl;
30
int hd_count;
31
int bd_count;
32
CURLcode result;
33
};
34
35
#define KN(a) a, #a
36
37
static void t1541_geterr(const char *name, CURLcode val, int lineno)
38
{
39
curl_mprintf("CURLINFO_%s returned %d, \"%s\" on line %d\n",
40
name, val, curl_easy_strerror(val), lineno);
41
}
42
43
static void report_time(const char *key, const char *where, curl_off_t time,
44
bool ok)
45
{
46
if(ok)
47
curl_mprintf("%s on %s is OK\n", key, where);
48
else
49
curl_mprintf("%s on %s is WRONG: %" CURL_FORMAT_CURL_OFF_T "\n",
50
key, where, time);
51
}
52
53
static void check_time(CURL *curl, int key, const char *name,
54
const char *where)
55
{
56
curl_off_t tval;
57
CURLcode res = curl_easy_getinfo(curl, (CURLINFO)key, &tval);
58
if(res) {
59
t1541_geterr(name, res, __LINE__);
60
}
61
else
62
report_time(name, where, tval, tval > 0);
63
}
64
65
static void check_time0(CURL *curl, int key, const char *name,
66
const char *where)
67
{
68
curl_off_t tval;
69
CURLcode res = curl_easy_getinfo(curl, (CURLINFO)key, &tval);
70
if(res) {
71
t1541_geterr(name, res, __LINE__);
72
}
73
else
74
report_time(name, where, tval, !tval);
75
}
76
77
static size_t t1541_header_callback(char *ptr, size_t size, size_t nmemb,
78
void *userp)
79
{
80
struct t1541_transfer_status *st = (struct t1541_transfer_status *)userp;
81
size_t len = size * nmemb;
82
83
(void)ptr;
84
if(!st->hd_count++) {
85
/* first header, check some CURLINFO value to be reported. See #13125 */
86
check_time(st->curl, KN(CURLINFO_CONNECT_TIME_T), "1st header");
87
check_time(st->curl, KN(CURLINFO_PRETRANSFER_TIME_T), "1st header");
88
check_time(st->curl, KN(CURLINFO_STARTTRANSFER_TIME_T), "1st header");
89
/* continuously updated */
90
check_time(st->curl, KN(CURLINFO_TOTAL_TIME_T), "1st header");
91
/* no SSL, must be 0 */
92
check_time0(st->curl, KN(CURLINFO_APPCONNECT_TIME_T), "1st header");
93
/* download not really started */
94
check_time0(st->curl, KN(CURLINFO_SPEED_DOWNLOAD_T), "1st header");
95
}
96
(void)fwrite(ptr, size, nmemb, stdout);
97
return len;
98
}
99
100
static size_t t1541_write_cb(char *ptr, size_t size, size_t nmemb, void *userp)
101
{
102
struct t1541_transfer_status *st = (struct t1541_transfer_status *)userp;
103
104
(void)ptr;
105
(void)st;
106
fwrite(ptr, size, nmemb, stdout);
107
return size * nmemb;
108
}
109
110
static CURLcode test_lib1541(const char *URL)
111
{
112
CURL *curl = NULL;
113
CURLcode res = CURLE_OK;
114
struct t1541_transfer_status st;
115
116
start_test_timing();
117
118
memset(&st, 0, sizeof(st));
119
120
global_init(CURL_GLOBAL_ALL);
121
122
easy_init(curl);
123
st.curl = curl; /* to allow callbacks access */
124
125
easy_setopt(curl, CURLOPT_URL, URL);
126
easy_setopt(curl, CURLOPT_WRITEFUNCTION, t1541_write_cb);
127
easy_setopt(curl, CURLOPT_WRITEDATA, &st);
128
easy_setopt(curl, CURLOPT_HEADERFUNCTION, t1541_header_callback);
129
easy_setopt(curl, CURLOPT_HEADERDATA, &st);
130
131
easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
132
133
res = curl_easy_perform(curl);
134
135
check_time(curl, KN(CURLINFO_CONNECT_TIME_T), "done");
136
check_time(curl, KN(CURLINFO_PRETRANSFER_TIME_T), "done");
137
check_time(curl, KN(CURLINFO_POSTTRANSFER_TIME_T), "done");
138
check_time(curl, KN(CURLINFO_STARTTRANSFER_TIME_T), "done");
139
/* no SSL, must be 0 */
140
check_time0(curl, KN(CURLINFO_APPCONNECT_TIME_T), "done");
141
check_time(curl, KN(CURLINFO_SPEED_DOWNLOAD_T), "done");
142
check_time(curl, KN(CURLINFO_TOTAL_TIME_T), "done");
143
144
test_cleanup:
145
146
curl_easy_cleanup(curl);
147
curl_global_cleanup();
148
149
return res; /* return the final return code */
150
}
151
152