1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 /* !checksrc! disable ASSIGNWITHINCONDITION all */
25 /* Now include the curl_setup.h file from libcurl's private libdir (the source
26 version, but that might include "curl_config.h" from the build dir so we
27 need both of them in the include path), so that we get good in-depth
28 knowledge about the system we're building this on */
30 #define CURL_NO_OLDIES
32 #include "curl_setup.h"
34 #include <curl/curl.h>
36 #ifdef HAVE_SYS_SELECT_H
37 /* since so many tests use select(), we can just as well include it here */
38 #include <sys/select.h>
45 #include "curl_printf.h"
47 #define test_setopt(A,B,C) \
48 if((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) goto test_cleanup
50 #define test_multi_setopt(A,B,C) \
51 if((res = curl_multi_setopt((A), (B), (C))) != CURLE_OK) goto test_cleanup
53 extern char *libtest_arg2; /* set by first.c to the argv[2] or NULL */
54 extern char *libtest_arg3; /* set by first.c to the argv[3] or NULL */
56 /* argc and argv as passed in to the main() function */
58 extern char **test_argv;
60 extern struct timeval tv_test_start; /* for test timing */
62 extern int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc,
65 extern void wait_ms(int ms); /* wait this many milliseconds */
67 extern int test(char *URL); /* the actual test function provided by each
68 individual libXXX.c file */
70 extern char *hexdump(unsigned char *buffer, size_t len);
77 ** TEST_ERR_* values must be greater than CURL_LAST CURLcode in order
78 ** to avoid confusion with any CURLcode or CURLMcode. These TEST_ERR_*
79 ** codes are returned to signal test specific situations and should
80 ** not get mixed with CURLcode or CURLMcode values.
82 ** For portability reasons TEST_ERR_* values should be less than 127.
85 #define TEST_ERR_MAJOR_BAD 126
86 #define TEST_ERR_RUNS_FOREVER 125
87 #define TEST_ERR_EASY_INIT 124
88 #define TEST_ERR_MULTI_INIT 123
89 #define TEST_ERR_NUM_HANDLES 122
90 #define TEST_ERR_SELECT 121
91 #define TEST_ERR_SUCCESS 120
92 #define TEST_ERR_FAILURE 119
93 #define TEST_ERR_USAGE 118
94 #define TEST_ERR_FOPEN 117
95 #define TEST_ERR_FSTAT 116
96 #define TEST_ERR_BAD_TIMEOUT 115
99 ** Macros for test source code readability/maintainability.
101 ** All of the following macros require that an int data type 'res' variable
102 ** exists in scope where macro is used, and that it has been initialized to
103 ** zero before the macro is used.
105 ** exe_* and chk_* macros are helper macros not intended to be used from
106 ** outside of this header file. Arguments 'Y' and 'Z' of these represent
107 ** source code file and line number, while Arguments 'A', 'B', etc, are
108 ** the arguments used to actually call a libcurl function.
110 ** All easy_* and multi_* macros call a libcurl function and evaluate if
111 ** the function has succeeded or failed. When the function succeeds 'res'
112 ** variable is not set nor cleared and program continues normal flow. On
113 ** the other hand if function fails 'res' variable is set and a jump to
114 ** label 'test_cleanup' is performed.
116 ** Every easy_* and multi_* macros have a res_easy_* and res_multi_* macro
117 ** counterpart that operates in the same way with the exception that no
118 ** jump takes place in case of failure. res_easy_* and res_multi_* macros
119 ** should be immediately followed by checking if 'res' variable has been
122 ** 'res' variable when set will hold a CURLcode, CURLMcode, or any of the
123 ** TEST_ERR_* values defined above. It is advisable to return this value
127 /* ---------------------------------------------------------------- */
129 #define exe_easy_init(A,Y,Z) do { \
130 if(((A) = curl_easy_init()) == NULL) { \
131 fprintf(stderr, "%s:%d curl_easy_init() failed\n", (Y), (Z)); \
132 res = TEST_ERR_EASY_INIT; \
136 #define res_easy_init(A) \
137 exe_easy_init((A), (__FILE__), (__LINE__))
139 #define chk_easy_init(A,Y,Z) do { \
140 exe_easy_init((A), (Y), (Z)); \
145 #define easy_init(A) \
146 chk_easy_init((A), (__FILE__), (__LINE__))
148 /* ---------------------------------------------------------------- */
150 #define exe_multi_init(A,Y,Z) do { \
151 if(((A) = curl_multi_init()) == NULL) { \
152 fprintf(stderr, "%s:%d curl_multi_init() failed\n", (Y), (Z)); \
153 res = TEST_ERR_MULTI_INIT; \
157 #define res_multi_init(A) \
158 exe_multi_init((A), (__FILE__), (__LINE__))
160 #define chk_multi_init(A,Y,Z) do { \
161 exe_multi_init((A), (Y), (Z)); \
166 #define multi_init(A) \
167 chk_multi_init((A), (__FILE__), (__LINE__))
169 /* ---------------------------------------------------------------- */
171 #define exe_easy_setopt(A,B,C,Y,Z) do { \
173 if((ec = curl_easy_setopt((A), (B), (C))) != CURLE_OK) { \
174 fprintf(stderr, "%s:%d curl_easy_setopt() failed, " \
175 "with code %d (%s)\n", \
176 (Y), (Z), (int)ec, curl_easy_strerror(ec)); \
181 #define res_easy_setopt(A, B, C) \
182 exe_easy_setopt((A), (B), (C), (__FILE__), (__LINE__))
184 #define chk_easy_setopt(A, B, C, Y, Z) do { \
185 exe_easy_setopt((A), (B), (C), (Y), (Z)); \
190 #define easy_setopt(A, B, C) \
191 chk_easy_setopt((A), (B), (C), (__FILE__), (__LINE__))
193 /* ---------------------------------------------------------------- */
195 #define exe_multi_setopt(A, B, C, Y, Z) do { \
197 if((ec = curl_multi_setopt((A), (B), (C))) != CURLM_OK) { \
198 fprintf(stderr, "%s:%d curl_multi_setopt() failed, " \
199 "with code %d (%s)\n", \
200 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
205 #define res_multi_setopt(A,B,C) \
206 exe_multi_setopt((A), (B), (C), (__FILE__), (__LINE__))
208 #define chk_multi_setopt(A,B,C,Y,Z) do { \
209 exe_multi_setopt((A), (B), (C), (Y), (Z)); \
214 #define multi_setopt(A,B,C) \
215 chk_multi_setopt((A), (B), (C), (__FILE__), (__LINE__))
217 /* ---------------------------------------------------------------- */
219 #define exe_multi_add_handle(A,B,Y,Z) do { \
221 if((ec = curl_multi_add_handle((A), (B))) != CURLM_OK) { \
222 fprintf(stderr, "%s:%d curl_multi_add_handle() failed, " \
223 "with code %d (%s)\n", \
224 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
229 #define res_multi_add_handle(A, B) \
230 exe_multi_add_handle((A), (B), (__FILE__), (__LINE__))
232 #define chk_multi_add_handle(A, B, Y, Z) do { \
233 exe_multi_add_handle((A), (B), (Y), (Z)); \
238 #define multi_add_handle(A, B) \
239 chk_multi_add_handle((A), (B), (__FILE__), (__LINE__))
241 /* ---------------------------------------------------------------- */
243 #define exe_multi_remove_handle(A,B,Y,Z) do { \
245 if((ec = curl_multi_remove_handle((A), (B))) != CURLM_OK) { \
246 fprintf(stderr, "%s:%d curl_multi_remove_handle() failed, " \
247 "with code %d (%s)\n", \
248 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
253 #define res_multi_remove_handle(A, B) \
254 exe_multi_remove_handle((A), (B), (__FILE__), (__LINE__))
256 #define chk_multi_remove_handle(A, B, Y, Z) do { \
257 exe_multi_remove_handle((A), (B), (Y), (Z)); \
263 #define multi_remove_handle(A, B) \
264 chk_multi_remove_handle((A), (B), (__FILE__), (__LINE__))
266 /* ---------------------------------------------------------------- */
268 #define exe_multi_perform(A,B,Y,Z) do { \
270 if((ec = curl_multi_perform((A), (B))) != CURLM_OK) { \
271 fprintf(stderr, "%s:%d curl_multi_perform() failed, " \
272 "with code %d (%s)\n", \
273 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
276 else if(*((B)) < 0) { \
277 fprintf(stderr, "%s:%d curl_multi_perform() succeeded, " \
278 "but returned invalid running_handles value (%d)\n", \
279 (Y), (Z), (int)*((B))); \
280 res = TEST_ERR_NUM_HANDLES; \
284 #define res_multi_perform(A, B) \
285 exe_multi_perform((A), (B), (__FILE__), (__LINE__))
287 #define chk_multi_perform(A, B, Y, Z) do { \
288 exe_multi_perform((A), (B), (Y), (Z)); \
293 #define multi_perform(A,B) \
294 chk_multi_perform((A), (B), (__FILE__), (__LINE__))
296 /* ---------------------------------------------------------------- */
298 #define exe_multi_fdset(A, B, C, D, E, Y, Z) do { \
300 if((ec = curl_multi_fdset((A), (B), (C), (D), (E))) != CURLM_OK) { \
301 fprintf(stderr, "%s:%d curl_multi_fdset() failed, " \
302 "with code %d (%s)\n", \
303 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
306 else if(*((E)) < -1) { \
307 fprintf(stderr, "%s:%d curl_multi_fdset() succeeded, " \
308 "but returned invalid max_fd value (%d)\n", \
309 (Y), (Z), (int)*((E))); \
310 res = TEST_ERR_NUM_HANDLES; \
314 #define res_multi_fdset(A, B, C, D, E) \
315 exe_multi_fdset((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
317 #define chk_multi_fdset(A, B, C, D, E, Y, Z) do { \
318 exe_multi_fdset((A), (B), (C), (D), (E), (Y), (Z)); \
323 #define multi_fdset(A, B, C, D, E) \
324 chk_multi_fdset((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
326 /* ---------------------------------------------------------------- */
328 #define exe_multi_timeout(A,B,Y,Z) do { \
330 if((ec = curl_multi_timeout((A), (B))) != CURLM_OK) { \
331 fprintf(stderr, "%s:%d curl_multi_timeout() failed, " \
332 "with code %d (%s)\n", \
333 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
336 else if(*((B)) < -1L) { \
337 fprintf(stderr, "%s:%d curl_multi_timeout() succeeded, " \
338 "but returned invalid timeout value (%ld)\n", \
339 (Y), (Z), (long)*((B))); \
340 res = TEST_ERR_BAD_TIMEOUT; \
344 #define res_multi_timeout(A, B) \
345 exe_multi_timeout((A), (B), (__FILE__), (__LINE__))
347 #define chk_multi_timeout(A, B, Y, Z) do { \
348 exe_multi_timeout((A), (B), (Y), (Z)); \
353 #define multi_timeout(A, B) \
354 chk_multi_timeout((A), (B), (__FILE__), (__LINE__))
356 /* ---------------------------------------------------------------- */
358 #define exe_select_test(A, B, C, D, E, Y, Z) do { \
360 if(select_wrapper((A), (B), (C), (D), (E)) == -1) { \
362 fprintf(stderr, "%s:%d select() failed, with " \
364 (Y), (Z), ec, strerror(ec)); \
365 res = TEST_ERR_SELECT; \
369 #define res_select_test(A, B, C, D, E) \
370 exe_select_test((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
372 #define chk_select_test(A, B, C, D, E, Y, Z) do { \
373 exe_select_test((A), (B), (C), (D), (E), (Y), (Z)); \
378 #define select_test(A, B, C, D, E) \
379 chk_select_test((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
381 /* ---------------------------------------------------------------- */
383 #define start_test_timing() do { \
384 tv_test_start = tutil_tvnow(); \
387 #define exe_test_timedout(Y,Z) do { \
388 if(tutil_tvdiff(tutil_tvnow(), tv_test_start) > TEST_HANG_TIMEOUT) { \
389 fprintf(stderr, "%s:%d ABORTING TEST, since it seems " \
390 "that it would have run forever.\n", (Y), (Z)); \
391 res = TEST_ERR_RUNS_FOREVER; \
395 #define res_test_timedout() \
396 exe_test_timedout((__FILE__), (__LINE__))
398 #define chk_test_timedout(Y, Z) do { \
399 exe_test_timedout(Y, Z); \
404 #define abort_on_test_timeout() \
405 chk_test_timedout((__FILE__), (__LINE__))
407 /* ---------------------------------------------------------------- */
409 #define exe_global_init(A,Y,Z) do { \
411 if((ec = curl_global_init((A))) != CURLE_OK) { \
412 fprintf(stderr, "%s:%d curl_global_init() failed, " \
413 "with code %d (%s)\n", \
414 (Y), (Z), (int)ec, curl_easy_strerror(ec)); \
419 #define res_global_init(A) \
420 exe_global_init((A), (__FILE__), (__LINE__))
422 #define chk_global_init(A, Y, Z) do { \
423 exe_global_init((A), (Y), (Z)); \
428 /* global_init() is different than other macros. In case of
429 failure it 'return's instead of going to 'test_cleanup'. */
431 #define global_init(A) \
432 chk_global_init((A), (__FILE__), (__LINE__))
434 /* ---------------------------------------------------------------- */