Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/libtest/first.h
2649 views
1
#ifndef HEADER_LIBTEST_FIRST_H
2
#define HEADER_LIBTEST_FIRST_H
3
/***************************************************************************
4
* _ _ ____ _
5
* Project ___| | | | _ \| |
6
* / __| | | | |_) | |
7
* | (__| |_| | _ <| |___
8
* \___|\___/|_| \_\_____|
9
*
10
* Copyright (C) Daniel Stenberg, <[email protected]>, et al.
11
*
12
* This software is licensed as described in the file COPYING, which
13
* you should have received as part of this distribution. The terms
14
* are also available at https://curl.se/docs/copyright.html.
15
*
16
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
17
* copies of the Software, and permit persons to whom the Software is
18
* furnished to do so, under the terms of the COPYING file.
19
*
20
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21
* KIND, either express or implied.
22
*
23
* SPDX-License-Identifier: curl
24
*
25
***************************************************************************/
26
#define CURL_NO_OLDIES
27
#define CURL_DISABLE_DEPRECATION
28
29
/* Now include the curl_setup.h file from libcurl's private libdir (the source
30
version, but that might include "curl_config.h" from the build dir so we
31
need both of them in the include path), so that we get good in-depth
32
knowledge about the system we're building this on */
33
#include "curl_setup.h"
34
35
#include <curl/curl.h>
36
37
typedef CURLcode (*entry_func_t)(const char *);
38
39
struct entry_s {
40
const char *name;
41
entry_func_t ptr;
42
};
43
44
extern const struct entry_s s_entries[];
45
46
extern int unitfail; /* for unittests */
47
48
#include <curlx/curlx.h>
49
50
#ifdef HAVE_SYS_SELECT_H
51
/* since so many tests use select(), we can just as well include it here */
52
#include <sys/select.h>
53
#endif
54
55
/* GCC <4.6 does not support '#pragma GCC diagnostic push' and
56
does not support 'pragma GCC diagnostic' inside functions. */
57
#if (defined(__GNUC__) && \
58
((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))))
59
#define CURL_GNUC_DIAG
60
#endif
61
62
#define test_setopt(A,B,C) \
63
if((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) \
64
goto test_cleanup
65
66
#define test_multi_setopt(A,B,C) \
67
if((res = curl_multi_setopt((A), (B), (C))) != CURLE_OK) \
68
goto test_cleanup
69
70
extern const char *libtest_arg2; /* set by first.c to the argv[2] or NULL */
71
extern const char *libtest_arg3; /* set by first.c to the argv[3] or NULL */
72
extern const char *libtest_arg4; /* set by first.c to the argv[4] or NULL */
73
74
/* argc and argv as passed in to the main() function */
75
extern int test_argc;
76
extern const char **test_argv;
77
extern int testnum;
78
extern struct curltime tv_test_start; /* for test timing */
79
80
extern int coptind;
81
extern const char *coptarg;
82
int cgetopt(int argc, const char * const argv[], const char *optstring);
83
84
extern int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc,
85
struct timeval *tv);
86
87
extern char *hexdump(const unsigned char *buffer, size_t len);
88
89
#ifndef CURL_DISABLE_WEBSOCKETS
90
CURLcode ws_send_ping(CURL *curl, const char *send_payload);
91
CURLcode ws_recv_pong(CURL *curl, const char *expected_payload);
92
/* just close the connection */
93
void ws_close(CURL *curl);
94
#endif
95
96
/*
97
** TEST_ERR_* values must within the CURLcode range to not cause compiler
98
** errors.
99
100
** For portability reasons TEST_ERR_* values should be less than 127.
101
*/
102
103
#define TEST_ERR_MAJOR_BAD CURLE_OBSOLETE20
104
#define TEST_ERR_RUNS_FOREVER CURLE_OBSOLETE24
105
#define TEST_ERR_EASY_INIT CURLE_OBSOLETE29
106
#define TEST_ERR_MULTI CURLE_OBSOLETE32
107
#define TEST_ERR_NUM_HANDLES CURLE_OBSOLETE34
108
#define TEST_ERR_SELECT CURLE_OBSOLETE40
109
#define TEST_ERR_SUCCESS CURLE_OBSOLETE41
110
#define TEST_ERR_FAILURE CURLE_OBSOLETE44
111
#define TEST_ERR_USAGE CURLE_OBSOLETE46
112
#define TEST_ERR_FOPEN CURLE_OBSOLETE50
113
#define TEST_ERR_FSTAT CURLE_OBSOLETE51
114
#define TEST_ERR_BAD_TIMEOUT CURLE_OBSOLETE57
115
116
/*
117
** Macros for test source code readability/maintainability.
118
**
119
** All of the following macros require that an int data type 'res' variable
120
** exists in scope where macro is used, and that it has been initialized to
121
** zero before the macro is used.
122
**
123
** exe_* and chk_* macros are helper macros not intended to be used from
124
** outside of this header file. Arguments 'Y' and 'Z' of these represent
125
** source code file and line number, while Arguments 'A', 'B', etc, are
126
** the arguments used to actually call a libcurl function.
127
**
128
** All easy_* and multi_* macros call a libcurl function and evaluate if
129
** the function has succeeded or failed. When the function succeeds 'res'
130
** variable is not set nor cleared and program continues normal flow. On
131
** the other hand if function fails 'res' variable is set and a jump to
132
** label 'test_cleanup' is performed.
133
**
134
** Every easy_* and multi_* macros have a res_easy_* and res_multi_* macro
135
** counterpart that operates in the same way with the exception that no
136
** jump takes place in case of failure. res_easy_* and res_multi_* macros
137
** should be immediately followed by checking if 'res' variable has been
138
** set.
139
**
140
** 'res' variable when set will hold a CURLcode, CURLMcode, or any of the
141
** TEST_ERR_* values defined above. It is advisable to return this value
142
** as test result.
143
*/
144
145
/* ---------------------------------------------------------------- */
146
147
#define exe_easy_init(A,Y,Z) do { \
148
if(((A) = curl_easy_init()) == NULL) { \
149
curl_mfprintf(stderr, "%s:%d curl_easy_init() failed\n", (Y), (Z)); \
150
res = TEST_ERR_EASY_INIT; \
151
} \
152
} while(0)
153
154
#define res_easy_init(A) \
155
exe_easy_init((A), (__FILE__), (__LINE__))
156
157
#define chk_easy_init(A,Y,Z) do { \
158
exe_easy_init((A), (Y), (Z)); \
159
if(res) \
160
goto test_cleanup; \
161
} while(0)
162
163
#define easy_init(A) \
164
chk_easy_init((A), (__FILE__), (__LINE__))
165
166
/* ---------------------------------------------------------------- */
167
168
#define exe_multi_init(A,Y,Z) do { \
169
if(((A) = curl_multi_init()) == NULL) { \
170
curl_mfprintf(stderr, "%s:%d curl_multi_init() failed\n", (Y), (Z)); \
171
res = TEST_ERR_MULTI; \
172
} \
173
} while(0)
174
175
#define res_multi_init(A) \
176
exe_multi_init((A), (__FILE__), (__LINE__))
177
178
#define chk_multi_init(A,Y,Z) do { \
179
exe_multi_init((A), (Y), (Z)); \
180
if(res) \
181
goto test_cleanup; \
182
} while(0)
183
184
#define multi_init(A) \
185
chk_multi_init((A), (__FILE__), (__LINE__))
186
187
/* ---------------------------------------------------------------- */
188
189
#define exe_easy_setopt(A,B,C,Y,Z) do { \
190
CURLcode ec; \
191
if((ec = curl_easy_setopt((A), (B), (C))) != CURLE_OK) { \
192
curl_mfprintf(stderr, "%s:%d curl_easy_setopt() failed, " \
193
"with code %d (%s)\n", \
194
(Y), (Z), ec, curl_easy_strerror(ec)); \
195
res = ec; \
196
} \
197
} while(0)
198
199
#define res_easy_setopt(A, B, C) \
200
exe_easy_setopt((A), (B), (C), (__FILE__), (__LINE__))
201
202
#define chk_easy_setopt(A, B, C, Y, Z) do { \
203
exe_easy_setopt((A), (B), (C), (Y), (Z)); \
204
if(res) \
205
goto test_cleanup; \
206
} while(0)
207
208
#define easy_setopt(A, B, C) \
209
chk_easy_setopt((A), (B), (C), (__FILE__), (__LINE__))
210
211
/* ---------------------------------------------------------------- */
212
213
#define exe_multi_setopt(A, B, C, Y, Z) do { \
214
CURLMcode ec; \
215
if((ec = curl_multi_setopt((A), (B), (C))) != CURLM_OK) { \
216
curl_mfprintf(stderr, "%s:%d curl_multi_setopt() failed, " \
217
"with code %d (%s)\n", \
218
(Y), (Z), ec, curl_multi_strerror(ec)); \
219
res = TEST_ERR_MULTI; \
220
} \
221
} while(0)
222
223
#define res_multi_setopt(A,B,C) \
224
exe_multi_setopt((A), (B), (C), (__FILE__), (__LINE__))
225
226
#define chk_multi_setopt(A,B,C,Y,Z) do { \
227
exe_multi_setopt((A), (B), (C), (Y), (Z)); \
228
if(res) \
229
goto test_cleanup; \
230
} while(0)
231
232
#define multi_setopt(A,B,C) \
233
chk_multi_setopt((A), (B), (C), (__FILE__), (__LINE__))
234
235
/* ---------------------------------------------------------------- */
236
237
#define exe_multi_add_handle(A,B,Y,Z) do { \
238
CURLMcode ec; \
239
if((ec = curl_multi_add_handle((A), (B))) != CURLM_OK) { \
240
curl_mfprintf(stderr, "%s:%d curl_multi_add_handle() failed, " \
241
"with code %d (%s)\n", \
242
(Y), (Z), ec, curl_multi_strerror(ec)); \
243
res = TEST_ERR_MULTI; \
244
} \
245
} while(0)
246
247
#define res_multi_add_handle(A, B) \
248
exe_multi_add_handle((A), (B), (__FILE__), (__LINE__))
249
250
#define chk_multi_add_handle(A, B, Y, Z) do { \
251
exe_multi_add_handle((A), (B), (Y), (Z)); \
252
if(res) \
253
goto test_cleanup; \
254
} while(0)
255
256
#define multi_add_handle(A, B) \
257
chk_multi_add_handle((A), (B), (__FILE__), (__LINE__))
258
259
/* ---------------------------------------------------------------- */
260
261
#define exe_multi_remove_handle(A,B,Y,Z) do { \
262
CURLMcode ec; \
263
if((ec = curl_multi_remove_handle((A), (B))) != CURLM_OK) { \
264
curl_mfprintf(stderr, "%s:%d curl_multi_remove_handle() failed, " \
265
"with code %d (%s)\n", \
266
(Y), (Z), ec, curl_multi_strerror(ec)); \
267
res = TEST_ERR_MULTI; \
268
} \
269
} while(0)
270
271
#define res_multi_remove_handle(A, B) \
272
exe_multi_remove_handle((A), (B), (__FILE__), (__LINE__))
273
274
#define chk_multi_remove_handle(A, B, Y, Z) do { \
275
exe_multi_remove_handle((A), (B), (Y), (Z)); \
276
if(res) \
277
goto test_cleanup; \
278
} while(0)
279
280
281
#define multi_remove_handle(A, B) \
282
chk_multi_remove_handle((A), (B), (__FILE__), (__LINE__))
283
284
/* ---------------------------------------------------------------- */
285
286
#define exe_multi_perform(A,B,Y,Z) do { \
287
CURLMcode ec; \
288
if((ec = curl_multi_perform((A), (B))) != CURLM_OK) { \
289
curl_mfprintf(stderr, "%s:%d curl_multi_perform() failed, " \
290
"with code %d (%s)\n", \
291
(Y), (Z), ec, curl_multi_strerror(ec)); \
292
res = TEST_ERR_MULTI; \
293
} \
294
else if(*((B)) < 0) { \
295
curl_mfprintf(stderr, "%s:%d curl_multi_perform() succeeded, " \
296
"but returned invalid running_handles value (%d)\n", \
297
(Y), (Z), (int)*((B))); \
298
res = TEST_ERR_NUM_HANDLES; \
299
} \
300
} while(0)
301
302
#define res_multi_perform(A, B) \
303
exe_multi_perform((A), (B), (__FILE__), (__LINE__))
304
305
#define chk_multi_perform(A, B, Y, Z) do { \
306
exe_multi_perform((A), (B), (Y), (Z)); \
307
if(res) \
308
goto test_cleanup; \
309
} while(0)
310
311
#define multi_perform(A,B) \
312
chk_multi_perform((A), (B), (__FILE__), (__LINE__))
313
314
/* ---------------------------------------------------------------- */
315
316
#define exe_multi_fdset(A, B, C, D, E, Y, Z) do { \
317
CURLMcode ec; \
318
if((ec = curl_multi_fdset((A), (B), (C), (D), (E))) != CURLM_OK) { \
319
curl_mfprintf(stderr, "%s:%d curl_multi_fdset() failed, " \
320
"with code %d (%s)\n", \
321
(Y), (Z), ec, curl_multi_strerror(ec)); \
322
res = TEST_ERR_MULTI; \
323
} \
324
else if(*((E)) < -1) { \
325
curl_mfprintf(stderr, "%s:%d curl_multi_fdset() succeeded, " \
326
"but returned invalid max_fd value (%d)\n", \
327
(Y), (Z), (int)*((E))); \
328
res = TEST_ERR_NUM_HANDLES; \
329
} \
330
} while(0)
331
332
#define res_multi_fdset(A, B, C, D, E) \
333
exe_multi_fdset((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
334
335
#define chk_multi_fdset(A, B, C, D, E, Y, Z) do { \
336
exe_multi_fdset((A), (B), (C), (D), (E), (Y), (Z)); \
337
if(res) \
338
goto test_cleanup; \
339
} while(0)
340
341
#define multi_fdset(A, B, C, D, E) \
342
chk_multi_fdset((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
343
344
/* ---------------------------------------------------------------- */
345
346
#define exe_multi_timeout(A,B,Y,Z) do { \
347
CURLMcode ec; \
348
if((ec = curl_multi_timeout((A), (B))) != CURLM_OK) { \
349
curl_mfprintf(stderr, "%s:%d curl_multi_timeout() failed, " \
350
"with code %d (%s)\n", \
351
(Y), (Z), ec, curl_multi_strerror(ec)); \
352
res = TEST_ERR_BAD_TIMEOUT; \
353
} \
354
else if(*((B)) < -1L) { \
355
curl_mfprintf(stderr, "%s:%d curl_multi_timeout() succeeded, " \
356
"but returned invalid timeout value (%ld)\n", \
357
(Y), (Z), (long)*((B))); \
358
res = TEST_ERR_BAD_TIMEOUT; \
359
} \
360
} while(0)
361
362
#define res_multi_timeout(A, B) \
363
exe_multi_timeout((A), (B), (__FILE__), (__LINE__))
364
365
#define chk_multi_timeout(A, B, Y, Z) do { \
366
exe_multi_timeout((A), (B), (Y), (Z)); \
367
if(res) \
368
goto test_cleanup; \
369
} while(0)
370
371
#define multi_timeout(A, B) \
372
chk_multi_timeout((A), (B), (__FILE__), (__LINE__))
373
374
/* ---------------------------------------------------------------- */
375
376
#define exe_multi_poll(A,B,C,D,E,Y,Z) do { \
377
CURLMcode ec; \
378
if((ec = curl_multi_poll((A), (B), (C), (D), (E))) != CURLM_OK) { \
379
curl_mfprintf(stderr, "%s:%d curl_multi_poll() failed, " \
380
"with code %d (%s)\n", \
381
(Y), (Z), ec, curl_multi_strerror(ec)); \
382
res = TEST_ERR_MULTI; \
383
} \
384
else if(*((E)) < 0) { \
385
curl_mfprintf(stderr, "%s:%d curl_multi_poll() succeeded, " \
386
"but returned invalid numfds value (%d)\n", \
387
(Y), (Z), (int)*((E))); \
388
res = TEST_ERR_NUM_HANDLES; \
389
} \
390
} while(0)
391
392
#define res_multi_poll(A, B, C, D, E) \
393
exe_multi_poll((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
394
395
#define chk_multi_poll(A, B, C, D, E, Y, Z) do { \
396
exe_multi_poll((A), (B), (C), (D), (E), (Y), (Z)); \
397
if(res) \
398
goto test_cleanup; \
399
} while(0)
400
401
#define multi_poll(A, B, C, D, E) \
402
chk_multi_poll((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
403
404
/* ---------------------------------------------------------------- */
405
406
#define exe_multi_wakeup(A,Y,Z) do { \
407
CURLMcode ec; \
408
if((ec = curl_multi_wakeup((A))) != CURLM_OK) { \
409
curl_mfprintf(stderr, "%s:%d curl_multi_wakeup() failed, " \
410
"with code %d (%s)\n", \
411
(Y), (Z), ec, curl_multi_strerror(ec)); \
412
res = TEST_ERR_MULTI; \
413
} \
414
} while(0)
415
416
#define res_multi_wakeup(A) \
417
exe_multi_wakeup((A), (__FILE__), (__LINE__))
418
419
#define chk_multi_wakeup(A, Y, Z) do { \
420
exe_multi_wakeup((A), (Y), (Z)); \
421
if(res) \
422
goto test_cleanup; \
423
} while(0)
424
425
#define multi_wakeup(A) \
426
chk_multi_wakeup((A), (__FILE__), (__LINE__))
427
428
/* ---------------------------------------------------------------- */
429
430
#define exe_select_test(A, B, C, D, E, Y, Z) do { \
431
int ec; \
432
if(select_wrapper((A), (B), (C), (D), (E)) == -1) { \
433
char ecbuf[STRERROR_LEN]; \
434
ec = SOCKERRNO; \
435
curl_mfprintf(stderr, "%s:%d select() failed, with " \
436
"errno %d (%s)\n", \
437
(Y), (Z), \
438
ec, curlx_strerror(ec, ecbuf, sizeof(ecbuf))); \
439
res = TEST_ERR_SELECT; \
440
} \
441
} while(0)
442
443
#define res_select_test(A, B, C, D, E) \
444
exe_select_test((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
445
446
#define chk_select_test(A, B, C, D, E, Y, Z) do { \
447
exe_select_test((A), (B), (C), (D), (E), (Y), (Z)); \
448
if(res) \
449
goto test_cleanup; \
450
} while(0)
451
452
#define select_test(A, B, C, D, E) \
453
chk_select_test((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
454
455
/* ---------------------------------------------------------------- */
456
457
#define start_test_timing() do { \
458
tv_test_start = curlx_now(); \
459
} while(0)
460
461
#define TEST_HANG_TIMEOUT 60 * 1000 /* global default */
462
463
#define exe_test_timedout(T,Y,Z) do { \
464
timediff_t timediff = curlx_timediff(curlx_now(), tv_test_start); \
465
if(timediff > (T)) { \
466
curl_mfprintf(stderr, "%s:%d ABORTING TEST, since it seems " \
467
"that it would have run forever (%ld ms > %ld ms)\n", \
468
(Y), (Z), (long)timediff, (long)(TEST_HANG_TIMEOUT)); \
469
res = TEST_ERR_RUNS_FOREVER; \
470
} \
471
} while(0)
472
473
#define res_test_timedout() \
474
exe_test_timedout(TEST_HANG_TIMEOUT, (__FILE__), (__LINE__))
475
476
#define res_test_timedout_custom(T) \
477
exe_test_timedout((T), (__FILE__), (__LINE__))
478
479
#define chk_test_timedout(T, Y, Z) do { \
480
exe_test_timedout(T, Y, Z); \
481
if(res) \
482
goto test_cleanup; \
483
} while(0)
484
485
#define abort_on_test_timeout() \
486
chk_test_timedout(TEST_HANG_TIMEOUT, (__FILE__), (__LINE__))
487
488
#define abort_on_test_timeout_custom(T) \
489
chk_test_timedout((T), (__FILE__), (__LINE__))
490
491
/* ---------------------------------------------------------------- */
492
493
#define exe_global_init(A,Y,Z) do { \
494
CURLcode ec; \
495
if((ec = curl_global_init((A))) != CURLE_OK) { \
496
curl_mfprintf(stderr, "%s:%d curl_global_init() failed, " \
497
"with code %d (%s)\n", \
498
(Y), (Z), ec, curl_easy_strerror(ec)); \
499
res = ec; \
500
} \
501
} while(0)
502
503
#define res_global_init(A) \
504
exe_global_init((A), (__FILE__), (__LINE__))
505
506
#define chk_global_init(A, Y, Z) do { \
507
exe_global_init((A), (Y), (Z)); \
508
if(res) \
509
return res; \
510
} while(0)
511
512
/* global_init() is different than other macros. In case of
513
failure it 'return's instead of going to 'test_cleanup'. */
514
515
#define global_init(A) \
516
chk_global_init((A), (__FILE__), (__LINE__))
517
518
#define NO_SUPPORT_BUILT_IN \
519
{ \
520
(void)URL; \
521
curl_mfprintf(stderr, "Missing support\n"); \
522
return CURLE_UNSUPPORTED_PROTOCOL; \
523
}
524
525
#define NUM_HANDLES 4 /* global default */
526
527
#endif /* HEADER_LIBTEST_FIRST_H */
528
529