Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/libtest/first.c
2654 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
#ifdef HAVE_LOCALE_H
27
#include <locale.h> /* for setlocale() */
28
#endif
29
30
#include "memdebug.h"
31
32
int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc,
33
struct timeval *tv)
34
{
35
if(nfds < 0) {
36
SET_SOCKERRNO(SOCKEINVAL);
37
return -1;
38
}
39
#ifdef USE_WINSOCK
40
/*
41
* Winsock select() requires that at least one of the three fd_set
42
* pointers is not NULL and points to a non-empty fdset. IOW Winsock
43
* select() can not be used to sleep without a single fd_set.
44
*/
45
if(!nfds) {
46
Sleep((DWORD)curlx_tvtoms(tv));
47
return 0;
48
}
49
#endif
50
return select(nfds, rd, wr, exc, tv);
51
}
52
53
const char *libtest_arg2 = NULL;
54
const char *libtest_arg3 = NULL;
55
const char *libtest_arg4 = NULL;
56
int test_argc;
57
const char **test_argv;
58
int testnum;
59
60
struct curltime tv_test_start; /* for test timing */
61
62
int unitfail; /* for unittests */
63
64
int coptind;
65
const char *coptarg;
66
67
int cgetopt(int argc, const char * const argv[], const char *optstring)
68
{
69
static int optpos = 1;
70
int coptopt;
71
const char *arg;
72
73
if(coptind == 0) { /* Reset? */
74
coptind = !!argc;
75
optpos = 1;
76
}
77
78
arg = argv[coptind];
79
if(arg && strcmp(arg, "--") == 0) {
80
coptind++;
81
return -1;
82
}
83
else if(!arg || arg[0] != '-') {
84
return -1;
85
}
86
else {
87
const char *opt = strchr(optstring, arg[optpos]);
88
coptopt = arg[optpos];
89
if(!opt) {
90
if(!arg[++optpos]) {
91
coptind++;
92
optpos = 1;
93
}
94
return '?';
95
}
96
else if(opt[1] == ':') {
97
if(arg[optpos + 1]) {
98
coptarg = arg + optpos + 1;
99
coptind++;
100
optpos = 1;
101
return coptopt;
102
}
103
else if(argv[coptind + 1]) {
104
coptarg = argv[coptind + 1];
105
coptind += 2;
106
optpos = 1;
107
return coptopt;
108
}
109
else {
110
if(!arg[++optpos]) {
111
coptind++;
112
optpos = 1;
113
}
114
return *optstring == ':' ? ':' : '?';
115
}
116
}
117
else {
118
if(!arg[++optpos]) {
119
coptind++;
120
optpos = 1;
121
}
122
return coptopt;
123
}
124
}
125
}
126
127
#ifdef CURLDEBUG
128
static void memory_tracking_init(void)
129
{
130
char *env;
131
/* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
132
env = getenv("CURL_MEMDEBUG");
133
if(env) {
134
/* use the value as file name */
135
curl_dbg_memdebug(env);
136
}
137
/* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
138
env = getenv("CURL_MEMLIMIT");
139
if(env) {
140
long num = atol(env);
141
if(num > 0)
142
curl_dbg_memlimit(num);
143
}
144
}
145
#else
146
# define memory_tracking_init() Curl_nop_stmt
147
#endif
148
149
/* returns a hexdump in a static memory area */
150
char *hexdump(const unsigned char *buf, size_t len)
151
{
152
static char dump[200 * 3 + 1];
153
char *p = dump;
154
size_t i;
155
if(len > 200)
156
return NULL;
157
for(i = 0; i < len; i++, p += 3)
158
curl_msnprintf(p, 4, "%02x ", buf[i]);
159
return dump;
160
}
161
162
#ifndef CURL_DISABLE_WEBSOCKETS
163
CURLcode ws_send_ping(CURL *curl, const char *send_payload)
164
{
165
size_t sent;
166
CURLcode result = curl_ws_send(curl, send_payload, strlen(send_payload),
167
&sent, 0, CURLWS_PING);
168
curl_mfprintf(stderr, "ws: curl_ws_send returned %u, sent %zu\n",
169
result, sent);
170
return result;
171
}
172
173
CURLcode ws_recv_pong(CURL *curl, const char *expected_payload)
174
{
175
size_t rlen;
176
const struct curl_ws_frame *meta;
177
char buffer[256];
178
CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
179
if(result) {
180
curl_mfprintf(stderr, "ws: curl_ws_recv returned %u, received %zu\n",
181
result, rlen);
182
return result;
183
}
184
185
if(!(meta->flags & CURLWS_PONG)) {
186
curl_mfprintf(stderr, "recv_pong: wrong frame, got %zu bytes rflags %x\n",
187
rlen, meta->flags);
188
return CURLE_RECV_ERROR;
189
}
190
191
curl_mfprintf(stderr, "ws: got PONG back\n");
192
if(rlen == strlen(expected_payload) &&
193
!memcmp(expected_payload, buffer, rlen)) {
194
curl_mfprintf(stderr, "ws: got the same payload back\n");
195
return CURLE_OK; /* lib2304 returned 'result' here. Intentional? */
196
}
197
curl_mfprintf(stderr, "ws: did NOT get the same payload back\n");
198
return CURLE_RECV_ERROR;
199
}
200
201
/* just close the connection */
202
void ws_close(CURL *curl)
203
{
204
size_t sent;
205
CURLcode result = curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
206
curl_mfprintf(stderr, "ws: curl_ws_send returned %u, sent %zu\n",
207
result, sent);
208
}
209
#endif /* CURL_DISABLE_WEBSOCKETS */
210
211
212
int main(int argc, const char **argv)
213
{
214
const char *URL = "";
215
CURLcode result;
216
entry_func_t entry_func;
217
const char *entry_name;
218
char *env;
219
size_t tmp;
220
221
CURLX_SET_BINMODE(stdout);
222
223
memory_tracking_init();
224
#ifdef _WIN32
225
curlx_now_init();
226
#endif
227
228
/*
229
* Setup proper locale from environment. This is needed to enable locale-
230
* specific behavior by the C library in order to test for undesired side
231
* effects that could cause in libcurl.
232
*/
233
#ifdef HAVE_SETLOCALE
234
setlocale(LC_ALL, "");
235
#endif
236
237
test_argc = argc - 1;
238
test_argv = argv + 1;
239
240
if(argc < 2) {
241
curl_mfprintf(stderr, "Pass testname "
242
"(and URL as argument for numbered tests) please\n");
243
return 1;
244
}
245
246
entry_name = argv[1];
247
entry_func = NULL;
248
for(tmp = 0; s_entries[tmp].ptr; ++tmp) {
249
if(strcmp(entry_name, s_entries[tmp].name) == 0) {
250
entry_func = s_entries[tmp].ptr;
251
break;
252
}
253
}
254
255
if(!entry_func) {
256
curl_mfprintf(stderr, "Test '%s' not found.\n", entry_name);
257
return 1;
258
}
259
260
if(argc > 2) {
261
URL = argv[2];
262
curl_mfprintf(stderr, "URL: %s\n", URL);
263
}
264
265
if(argc > 3)
266
libtest_arg2 = argv[3];
267
268
if(argc > 4)
269
libtest_arg3 = argv[4];
270
271
if(argc > 5)
272
libtest_arg4 = argv[5];
273
274
env = getenv("CURL_TESTNUM");
275
if(env)
276
testnum = atoi(env);
277
else
278
testnum = 0;
279
280
result = entry_func(URL);
281
curl_mfprintf(stderr, "Test ended with result %d\n", result);
282
283
#ifdef _WIN32
284
/* flush buffers of all streams regardless of mode */
285
_flushall();
286
#endif
287
288
/* Regular program status codes are limited to 0..127 and 126 and 127 have
289
* special meanings by the shell, so limit a normal return code to 125 */
290
return (int)result <= 125 ? (int)result : 125;
291
}
292
293