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 /* Now include the curl_setup.h file from libcurl's private libdir (the source
24 version, but that might include "curl_config.h" from the build dir so we
25 need both of them in the include path), so that we get good in-depth
26 knowledge about the system we're building this on */
28 #define CURL_NO_OLDIES
30 #include "curl_setup.h"
32 #include <curl/curl.h>
34 #ifdef HAVE_SYS_SELECT_H
35 /* since so many tests use select(), we can just as well include it here */
36 #include <sys/select.h>
43 #include "curl_printf.h"
45 #define test_setopt(A,B,C) \
46 if((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) goto test_cleanup
48 #define test_multi_setopt(A,B,C) \
49 if((res = curl_multi_setopt((A), (B), (C))) != CURLE_OK) goto test_cleanup
51 extern char *libtest_arg2; /* set by first.c to the argv[2] or NULL */
52 extern char *libtest_arg3; /* set by first.c to the argv[3] or NULL */
54 /* argc and argv as passed in to the main() function */
56 extern char **test_argv;
58 extern struct timeval tv_test_start; /* for test timing */
60 extern int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc,
63 extern void wait_ms(int ms); /* wait this many milliseconds */
65 extern int test(char *URL); /* the actual test function provided by each
66 individual libXXX.c file */
68 extern char *hexdump(unsigned char *buffer, size_t len);
75 ** TEST_ERR_* values must be greater than CURL_LAST CURLcode in order
76 ** to avoid confusion with any CURLcode or CURLMcode. These TEST_ERR_*
77 ** codes are returned to signal test specific situations and should
78 ** not get mixed with CURLcode or CURLMcode values.
80 ** For portability reasons TEST_ERR_* values should be less than 127.
83 #define TEST_ERR_MAJOR_BAD 126
84 #define TEST_ERR_RUNS_FOREVER 125
85 #define TEST_ERR_EASY_INIT 124
86 #define TEST_ERR_MULTI_INIT 123
87 #define TEST_ERR_NUM_HANDLES 122
88 #define TEST_ERR_SELECT 121
89 #define TEST_ERR_SUCCESS 120
90 #define TEST_ERR_FAILURE 119
91 #define TEST_ERR_USAGE 118
92 #define TEST_ERR_FOPEN 117
93 #define TEST_ERR_FSTAT 116
94 #define TEST_ERR_BAD_TIMEOUT 115
97 ** Macros for test source code readability/maintainability.
99 ** All of the following macros require that an int data type 'res' variable
100 ** exists in scope where macro is used, and that it has been initialized to
101 ** zero before the macro is used.
103 ** exe_* and chk_* macros are helper macros not intended to be used from
104 ** outside of this header file. Arguments 'Y' and 'Z' of these represent
105 ** source code file and line number, while Arguments 'A', 'B', etc, are
106 ** the arguments used to actually call a libcurl function.
108 ** All easy_* and multi_* macros call a libcurl function and evaluate if
109 ** the function has succeeded or failed. When the function succeeds 'res'
110 ** variable is not set nor cleared and program continues normal flow. On
111 ** the other hand if function fails 'res' variable is set and a jump to
112 ** label 'test_cleanup' is performed.
114 ** Every easy_* and multi_* macros have a res_easy_* and res_multi_* macro
115 ** counterpart that operates in the same way with the exception that no
116 ** jump takes place in case of failure. res_easy_* and res_multi_* macros
117 ** should be immediately followed by checking if 'res' variable has been
120 ** 'res' variable when set will hold a CURLcode, CURLMcode, or any of the
121 ** TEST_ERR_* values defined above. It is advisable to return this value
125 /* ---------------------------------------------------------------- */
127 #define exe_easy_init(A,Y,Z) do { \
128 if(((A) = curl_easy_init()) == NULL) { \
129 fprintf(stderr, "%s:%d curl_easy_init() failed\n", (Y), (Z)); \
130 res = TEST_ERR_EASY_INIT; \
134 #define res_easy_init(A) \
135 exe_easy_init((A), (__FILE__), (__LINE__))
137 #define chk_easy_init(A,Y,Z) do { \
138 exe_easy_init((A), (Y), (Z)); \
143 #define easy_init(A) \
144 chk_easy_init((A), (__FILE__), (__LINE__))
146 /* ---------------------------------------------------------------- */
148 #define exe_multi_init(A,Y,Z) do { \
149 if(((A) = curl_multi_init()) == NULL) { \
150 fprintf(stderr, "%s:%d curl_multi_init() failed\n", (Y), (Z)); \
151 res = TEST_ERR_MULTI_INIT; \
155 #define res_multi_init(A) \
156 exe_multi_init((A), (__FILE__), (__LINE__))
158 #define chk_multi_init(A,Y,Z) do { \
159 exe_multi_init((A), (Y), (Z)); \
164 #define multi_init(A) \
165 chk_multi_init((A), (__FILE__), (__LINE__))
167 /* ---------------------------------------------------------------- */
169 #define exe_easy_setopt(A,B,C,Y,Z) do { \
171 if((ec = curl_easy_setopt((A), (B), (C))) != CURLE_OK) { \
172 fprintf(stderr, "%s:%d curl_easy_setopt() failed, " \
173 "with code %d (%s)\n", \
174 (Y), (Z), (int)ec, curl_easy_strerror(ec)); \
179 #define res_easy_setopt(A, B, C) \
180 exe_easy_setopt((A), (B), (C), (__FILE__), (__LINE__))
182 #define chk_easy_setopt(A, B, C, Y, Z) do { \
183 exe_easy_setopt((A), (B), (C), (Y), (Z)); \
188 #define easy_setopt(A, B, C) \
189 chk_easy_setopt((A), (B), (C), (__FILE__), (__LINE__))
191 /* ---------------------------------------------------------------- */
193 #define exe_multi_setopt(A, B, C, Y, Z) do { \
195 if((ec = curl_multi_setopt((A), (B), (C))) != CURLM_OK) { \
196 fprintf(stderr, "%s:%d curl_multi_setopt() failed, " \
197 "with code %d (%s)\n", \
198 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
203 #define res_multi_setopt(A,B,C) \
204 exe_multi_setopt((A), (B), (C), (__FILE__), (__LINE__))
206 #define chk_multi_setopt(A,B,C,Y,Z) do { \
207 exe_multi_setopt((A), (B), (C), (Y), (Z)); \
212 #define multi_setopt(A,B,C) \
213 chk_multi_setopt((A), (B), (C), (__FILE__), (__LINE__))
215 /* ---------------------------------------------------------------- */
217 #define exe_multi_add_handle(A,B,Y,Z) do { \
219 if((ec = curl_multi_add_handle((A), (B))) != CURLM_OK) { \
220 fprintf(stderr, "%s:%d curl_multi_add_handle() failed, " \
221 "with code %d (%s)\n", \
222 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
227 #define res_multi_add_handle(A, B) \
228 exe_multi_add_handle((A), (B), (__FILE__), (__LINE__))
230 #define chk_multi_add_handle(A, B, Y, Z) do { \
231 exe_multi_add_handle((A), (B), (Y), (Z)); \
236 #define multi_add_handle(A, B) \
237 chk_multi_add_handle((A), (B), (__FILE__), (__LINE__))
239 /* ---------------------------------------------------------------- */
241 #define exe_multi_remove_handle(A,B,Y,Z) do { \
243 if((ec = curl_multi_remove_handle((A), (B))) != CURLM_OK) { \
244 fprintf(stderr, "%s:%d curl_multi_remove_handle() failed, " \
245 "with code %d (%s)\n", \
246 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
251 #define res_multi_remove_handle(A, B) \
252 exe_multi_remove_handle((A), (B), (__FILE__), (__LINE__))
254 #define chk_multi_remove_handle(A, B, Y, Z) do { \
255 exe_multi_remove_handle((A), (B), (Y), (Z)); \
261 #define multi_remove_handle(A, B) \
262 chk_multi_remove_handle((A), (B), (__FILE__), (__LINE__))
264 /* ---------------------------------------------------------------- */
266 #define exe_multi_perform(A,B,Y,Z) do { \
268 if((ec = curl_multi_perform((A), (B))) != CURLM_OK) { \
269 fprintf(stderr, "%s:%d curl_multi_perform() failed, " \
270 "with code %d (%s)\n", \
271 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
274 else if(*((B)) < 0) { \
275 fprintf(stderr, "%s:%d curl_multi_perform() succeeded, " \
276 "but returned invalid running_handles value (%d)\n", \
277 (Y), (Z), (int)*((B))); \
278 res = TEST_ERR_NUM_HANDLES; \
282 #define res_multi_perform(A, B) \
283 exe_multi_perform((A), (B), (__FILE__), (__LINE__))
285 #define chk_multi_perform(A, B, Y, Z) do { \
286 exe_multi_perform((A), (B), (Y), (Z)); \
291 #define multi_perform(A,B) \
292 chk_multi_perform((A), (B), (__FILE__), (__LINE__))
294 /* ---------------------------------------------------------------- */
296 #define exe_multi_fdset(A, B, C, D, E, Y, Z) do { \
298 if((ec = curl_multi_fdset((A), (B), (C), (D), (E))) != CURLM_OK) { \
299 fprintf(stderr, "%s:%d curl_multi_fdset() failed, " \
300 "with code %d (%s)\n", \
301 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
304 else if(*((E)) < -1) { \
305 fprintf(stderr, "%s:%d curl_multi_fdset() succeeded, " \
306 "but returned invalid max_fd value (%d)\n", \
307 (Y), (Z), (int)*((E))); \
308 res = TEST_ERR_NUM_HANDLES; \
312 #define res_multi_fdset(A, B, C, D, E) \
313 exe_multi_fdset((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
315 #define chk_multi_fdset(A, B, C, D, E, Y, Z) do { \
316 exe_multi_fdset((A), (B), (C), (D), (E), (Y), (Z)); \
321 #define multi_fdset(A, B, C, D, E) \
322 chk_multi_fdset((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
324 /* ---------------------------------------------------------------- */
326 #define exe_multi_timeout(A,B,Y,Z) do { \
328 if((ec = curl_multi_timeout((A), (B))) != CURLM_OK) { \
329 fprintf(stderr, "%s:%d curl_multi_timeout() failed, " \
330 "with code %d (%s)\n", \
331 (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
334 else if(*((B)) < -1L) { \
335 fprintf(stderr, "%s:%d curl_multi_timeout() succeeded, " \
336 "but returned invalid timeout value (%ld)\n", \
337 (Y), (Z), (long)*((B))); \
338 res = TEST_ERR_BAD_TIMEOUT; \
342 #define res_multi_timeout(A, B) \
343 exe_multi_timeout((A), (B), (__FILE__), (__LINE__))
345 #define chk_multi_timeout(A, B, Y, Z) do { \
346 exe_multi_timeout((A), (B), (Y), (Z)); \
351 #define multi_timeout(A, B) \
352 chk_multi_timeout((A), (B), (__FILE__), (__LINE__))
354 /* ---------------------------------------------------------------- */
356 #define exe_select_test(A, B, C, D, E, Y, Z) do { \
358 if(select_wrapper((A), (B), (C), (D), (E)) == -1) { \
360 fprintf(stderr, "%s:%d select() failed, with " \
362 (Y), (Z), ec, strerror(ec)); \
363 res = TEST_ERR_SELECT; \
367 #define res_select_test(A, B, C, D, E) \
368 exe_select_test((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
370 #define chk_select_test(A, B, C, D, E, Y, Z) do { \
371 exe_select_test((A), (B), (C), (D), (E), (Y), (Z)); \
376 #define select_test(A, B, C, D, E) \
377 chk_select_test((A), (B), (C), (D), (E), (__FILE__), (__LINE__))
379 /* ---------------------------------------------------------------- */
381 #define start_test_timing() do { \
382 tv_test_start = tutil_tvnow(); \
385 #define exe_test_timedout(Y,Z) do { \
386 if(tutil_tvdiff(tutil_tvnow(), tv_test_start) > TEST_HANG_TIMEOUT) { \
387 fprintf(stderr, "%s:%d ABORTING TEST, since it seems " \
388 "that it would have run forever.\n", (Y), (Z)); \
389 res = TEST_ERR_RUNS_FOREVER; \
393 #define res_test_timedout() \
394 exe_test_timedout((__FILE__), (__LINE__))
396 #define chk_test_timedout(Y, Z) do { \
397 exe_test_timedout(Y, Z); \
402 #define abort_on_test_timeout() \
403 chk_test_timedout((__FILE__), (__LINE__))
405 /* ---------------------------------------------------------------- */
407 #define exe_global_init(A,Y,Z) do { \
409 if((ec = curl_global_init((A))) != CURLE_OK) { \
410 fprintf(stderr, "%s:%d curl_global_init() failed, " \
411 "with code %d (%s)\n", \
412 (Y), (Z), (int)ec, curl_easy_strerror(ec)); \
417 #define res_global_init(A) \
418 exe_global_init((A), (__FILE__), (__LINE__))
420 #define chk_global_init(A, Y, Z) do { \
421 exe_global_init((A), (Y), (Z)); \
426 /* global_init() is different than other macros. In case of
427 failure it 'return's instead of going to 'test_cleanup'. */
429 #define global_init(A) \
430 chk_global_init((A), (__FILE__), (__LINE__))
432 /* ---------------------------------------------------------------- */