test.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, 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 http://curl.haxx.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. ***************************************************************************/
  22. /* Now include the curl_setup.h file from libcurl's private libdir (the source
  23. version, but that might include "curl_config.h" from the build dir so we
  24. need both of them in the include path), so that we get good in-depth
  25. knowledge about the system we're building this on */
  26. #define CURL_NO_OLDIES
  27. #include "curl_setup.h"
  28. #include <curl/curl.h>
  29. #ifdef HAVE_SYS_SELECT_H
  30. /* since so many tests use select(), we can just as well include it here */
  31. #include <sys/select.h>
  32. #endif
  33. #ifdef TPF
  34. # include "select.h"
  35. #endif
  36. #define test_setopt(A,B,C) \
  37. if((res = curl_easy_setopt((A),(B),(C))) != CURLE_OK) goto test_cleanup
  38. #define test_multi_setopt(A,B,C) \
  39. if((res = curl_multi_setopt((A),(B),(C))) != CURLE_OK) goto test_cleanup
  40. extern char *libtest_arg2; /* set by first.c to the argv[2] or NULL */
  41. extern char *libtest_arg3; /* set by first.c to the argv[3] or NULL */
  42. /* argc and argv as passed in to the main() function */
  43. extern int test_argc;
  44. extern char **test_argv;
  45. extern struct timeval tv_test_start; /* for test timing */
  46. extern int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc,
  47. struct timeval *tv);
  48. extern void wait_ms(int ms); /* wait this many milliseconds */
  49. extern int test(char *URL); /* the actual test function provided by each
  50. individual libXXX.c file */
  51. #ifdef UNITTESTS
  52. extern int unitfail;
  53. #endif
  54. /*
  55. ** TEST_ERR_* values must be greater than CURL_LAST CURLcode in order
  56. ** to avoid confusion with any CURLcode or CURLMcode. These TEST_ERR_*
  57. ** codes are returned to signal test specific situations and should
  58. ** not get mixed with CURLcode or CURLMcode values.
  59. **
  60. ** For portability reasons TEST_ERR_* values should be less than 127.
  61. */
  62. #define TEST_ERR_MAJOR_BAD 126
  63. #define TEST_ERR_RUNS_FOREVER 125
  64. #define TEST_ERR_EASY_INIT 124
  65. #define TEST_ERR_MULTI_INIT 123
  66. #define TEST_ERR_NUM_HANDLES 122
  67. #define TEST_ERR_SELECT 121
  68. #define TEST_ERR_SUCCESS 120
  69. #define TEST_ERR_FAILURE 119
  70. #define TEST_ERR_USAGE 118
  71. #define TEST_ERR_FOPEN 117
  72. #define TEST_ERR_FSTAT 116
  73. #define TEST_ERR_BAD_TIMEOUT 115
  74. /*
  75. ** Macros for test source code readability/maintainability.
  76. **
  77. ** All of the following macros require that an int data type 'res' variable
  78. ** exists in scope where macro is used, and that it has been initialized to
  79. ** zero before the macro is used.
  80. **
  81. ** exe_* and chk_* macros are helper macros not intended to be used from
  82. ** outside of this header file. Arguments 'Y' and 'Z' of these represent
  83. ** source code file and line number, while Arguments 'A', 'B', etc, are
  84. ** the arguments used to actually call a libcurl function.
  85. **
  86. ** All easy_* and multi_* macros call a libcurl function and evaluate if
  87. ** the function has succeeded or failed. When the function succeeds 'res'
  88. ** variable is not set nor cleared and program continues normal flow. On
  89. ** the other hand if function fails 'res' variable is set and a jump to
  90. ** label 'test_cleanup' is performed.
  91. **
  92. ** Every easy_* and multi_* macros have a res_easy_* and res_multi_* macro
  93. ** counterpart that operates in tha same way with the exception that no
  94. ** jump takes place in case of failure. res_easy_* and res_multi_* macros
  95. ** should be immediately followed by checking if 'res' variable has been
  96. ** set.
  97. **
  98. ** 'res' variable when set will hold a CURLcode, CURLMcode, or any of the
  99. ** TEST_ERR_* values defined above. It is advisable to return this value
  100. ** as test result.
  101. */
  102. /* ---------------------------------------------------------------- */
  103. #define exe_easy_init(A,Y,Z) do { \
  104. if(((A) = curl_easy_init()) == NULL) { \
  105. fprintf(stderr, "%s:%d curl_easy_init() failed\n", (Y), (Z)); \
  106. res = TEST_ERR_EASY_INIT; \
  107. } \
  108. } WHILE_FALSE
  109. #define res_easy_init(A) \
  110. exe_easy_init((A),(__FILE__),(__LINE__))
  111. #define chk_easy_init(A,Y,Z) do { \
  112. exe_easy_init((A),(Y),(Z)); \
  113. if(res) \
  114. goto test_cleanup; \
  115. } WHILE_FALSE
  116. #define easy_init(A) \
  117. chk_easy_init((A),(__FILE__),(__LINE__))
  118. /* ---------------------------------------------------------------- */
  119. #define exe_multi_init(A,Y,Z) do { \
  120. if(((A) = curl_multi_init()) == NULL) { \
  121. fprintf(stderr, "%s:%d curl_multi_init() failed\n", (Y), (Z)); \
  122. res = TEST_ERR_MULTI_INIT; \
  123. } \
  124. } WHILE_FALSE
  125. #define res_multi_init(A) \
  126. exe_multi_init((A),(__FILE__),(__LINE__))
  127. #define chk_multi_init(A,Y,Z) do { \
  128. exe_multi_init((A),(Y),(Z)); \
  129. if(res) \
  130. goto test_cleanup; \
  131. } WHILE_FALSE
  132. #define multi_init(A) \
  133. chk_multi_init((A),(__FILE__),(__LINE__))
  134. /* ---------------------------------------------------------------- */
  135. #define exe_easy_setopt(A,B,C,Y,Z) do { \
  136. CURLcode ec; \
  137. if((ec = curl_easy_setopt((A),(B),(C))) != CURLE_OK) { \
  138. fprintf(stderr, "%s:%d curl_easy_setopt() failed, " \
  139. "with code %d (%s)\n", \
  140. (Y), (Z), (int)ec, curl_easy_strerror(ec)); \
  141. res = (int)ec; \
  142. } \
  143. } WHILE_FALSE
  144. #define res_easy_setopt(A,B,C) \
  145. exe_easy_setopt((A),(B),(C),(__FILE__),(__LINE__))
  146. #define chk_easy_setopt(A,B,C,Y,Z) do { \
  147. exe_easy_setopt((A),(B),(C),(Y),(Z)); \
  148. if(res) \
  149. goto test_cleanup; \
  150. } WHILE_FALSE
  151. #define easy_setopt(A,B,C) \
  152. chk_easy_setopt((A),(B),(C),(__FILE__),(__LINE__))
  153. /* ---------------------------------------------------------------- */
  154. #define exe_multi_setopt(A,B,C,Y,Z) do { \
  155. CURLMcode ec; \
  156. if((ec = curl_multi_setopt((A),(B),(C))) != CURLM_OK) { \
  157. fprintf(stderr, "%s:%d curl_multi_setopt() failed, " \
  158. "with code %d (%s)\n", \
  159. (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
  160. res = (int)ec; \
  161. } \
  162. } WHILE_FALSE
  163. #define res_multi_setopt(A,B,C) \
  164. exe_multi_setopt((A),(B),(C),(__FILE__),(__LINE__))
  165. #define chk_multi_setopt(A,B,C,Y,Z) do { \
  166. exe_multi_setopt((A),(B),(C),(Y),(Z)); \
  167. if(res) \
  168. goto test_cleanup; \
  169. } WHILE_FALSE
  170. #define multi_setopt(A,B,C) \
  171. chk_multi_setopt((A),(B),(C),(__FILE__),(__LINE__))
  172. /* ---------------------------------------------------------------- */
  173. #define exe_multi_add_handle(A,B,Y,Z) do { \
  174. CURLMcode ec; \
  175. if((ec = curl_multi_add_handle((A),(B))) != CURLM_OK) { \
  176. fprintf(stderr, "%s:%d curl_multi_add_handle() failed, " \
  177. "with code %d (%s)\n", \
  178. (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
  179. res = (int)ec; \
  180. } \
  181. } WHILE_FALSE
  182. #define res_multi_add_handle(A,B) \
  183. exe_multi_add_handle((A),(B),(__FILE__),(__LINE__))
  184. #define chk_multi_add_handle(A,B,Y,Z) do { \
  185. exe_multi_add_handle((A),(B),(Y),(Z)); \
  186. if(res) \
  187. goto test_cleanup; \
  188. } WHILE_FALSE
  189. #define multi_add_handle(A,B) \
  190. chk_multi_add_handle((A),(B),(__FILE__),(__LINE__))
  191. /* ---------------------------------------------------------------- */
  192. #define exe_multi_remove_handle(A,B,Y,Z) do { \
  193. CURLMcode ec; \
  194. if((ec = curl_multi_remove_handle((A),(B))) != CURLM_OK) { \
  195. fprintf(stderr, "%s:%d curl_multi_remove_handle() failed, " \
  196. "with code %d (%s)\n", \
  197. (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
  198. res = (int)ec; \
  199. } \
  200. } WHILE_FALSE
  201. #define res_multi_remove_handle(A,B) \
  202. exe_multi_remove_handle((A),(B),(__FILE__),(__LINE__))
  203. #define chk_multi_remove_handle(A,B,Y,Z) do { \
  204. exe_multi_remove_handle((A),(B),(Y),(Z)); \
  205. if(res) \
  206. goto test_cleanup; \
  207. } WHILE_FALSE
  208. #define multi_remove_handle(A,B) \
  209. chk_multi_remove_handle((A),(B),(__FILE__),(__LINE__))
  210. /* ---------------------------------------------------------------- */
  211. #define exe_multi_perform(A,B,Y,Z) do { \
  212. CURLMcode ec; \
  213. if((ec = curl_multi_perform((A),(B))) != CURLM_OK) { \
  214. fprintf(stderr, "%s:%d curl_multi_perform() failed, " \
  215. "with code %d (%s)\n", \
  216. (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
  217. res = (int)ec; \
  218. } \
  219. else if(*((B)) < 0) { \
  220. fprintf(stderr, "%s:%d curl_multi_perform() succeeded, " \
  221. "but returned invalid running_handles value (%d)\n", \
  222. (Y), (Z), (int)*((B))); \
  223. res = TEST_ERR_NUM_HANDLES; \
  224. } \
  225. } WHILE_FALSE
  226. #define res_multi_perform(A,B) \
  227. exe_multi_perform((A),(B),(__FILE__),(__LINE__))
  228. #define chk_multi_perform(A,B,Y,Z) do { \
  229. exe_multi_perform((A),(B),(Y),(Z)); \
  230. if(res) \
  231. goto test_cleanup; \
  232. } WHILE_FALSE
  233. #define multi_perform(A,B) \
  234. chk_multi_perform((A),(B),(__FILE__),(__LINE__))
  235. /* ---------------------------------------------------------------- */
  236. #define exe_multi_fdset(A,B,C,D,E,Y,Z) do { \
  237. CURLMcode ec; \
  238. if((ec = curl_multi_fdset((A),(B),(C),(D),(E))) != CURLM_OK) { \
  239. fprintf(stderr, "%s:%d curl_multi_fdset() failed, " \
  240. "with code %d (%s)\n", \
  241. (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
  242. res = (int)ec; \
  243. } \
  244. else if(*((E)) < -1) { \
  245. fprintf(stderr, "%s:%d curl_multi_fdset() succeeded, " \
  246. "but returned invalid max_fd value (%d)\n", \
  247. (Y), (Z), (int)*((E))); \
  248. res = TEST_ERR_NUM_HANDLES; \
  249. } \
  250. } WHILE_FALSE
  251. #define res_multi_fdset(A,B,C,D,E) \
  252. exe_multi_fdset((A),(B),(C),(D),(E),(__FILE__),(__LINE__))
  253. #define chk_multi_fdset(A,B,C,D,E,Y,Z) do { \
  254. exe_multi_fdset((A),(B),(C),(D),(E),(Y),(Z)); \
  255. if(res) \
  256. goto test_cleanup; \
  257. } WHILE_FALSE
  258. #define multi_fdset(A,B,C,D,E) \
  259. chk_multi_fdset((A),(B),(C),(D),(E),(__FILE__),(__LINE__))
  260. /* ---------------------------------------------------------------- */
  261. #define exe_multi_timeout(A,B,Y,Z) do { \
  262. CURLMcode ec; \
  263. if((ec = curl_multi_timeout((A),(B))) != CURLM_OK) { \
  264. fprintf(stderr, "%s:%d curl_multi_timeout() failed, " \
  265. "with code %d (%s)\n", \
  266. (Y), (Z), (int)ec, curl_multi_strerror(ec)); \
  267. res = (int)ec; \
  268. } \
  269. else if(*((B)) < -1L) { \
  270. fprintf(stderr, "%s:%d curl_multi_timeout() succeeded, " \
  271. "but returned invalid timeout value (%ld)\n", \
  272. (Y), (Z), (long)*((B))); \
  273. res = TEST_ERR_BAD_TIMEOUT; \
  274. } \
  275. } WHILE_FALSE
  276. #define res_multi_timeout(A,B) \
  277. exe_multi_timeout((A),(B),(__FILE__),(__LINE__))
  278. #define chk_multi_timeout(A,B,Y,Z) do { \
  279. exe_multi_timeout((A),(B),(Y),(Z)); \
  280. if(res) \
  281. goto test_cleanup; \
  282. } WHILE_FALSE
  283. #define multi_timeout(A,B) \
  284. chk_multi_timeout((A),(B),(__FILE__),(__LINE__))
  285. /* ---------------------------------------------------------------- */
  286. #define exe_select_test(A,B,C,D,E,Y,Z) do { \
  287. int ec; \
  288. if(select_wrapper((A),(B),(C),(D),(E)) == -1 ) { \
  289. ec = SOCKERRNO; \
  290. fprintf(stderr, "%s:%d select() failed, with " \
  291. "errno %d (%s)\n", \
  292. (Y), (Z), ec, strerror(ec)); \
  293. res = TEST_ERR_SELECT; \
  294. } \
  295. } WHILE_FALSE
  296. #define res_select_test(A,B,C,D,E) \
  297. exe_select_test((A),(B),(C),(D),(E),(__FILE__),(__LINE__))
  298. #define chk_select_test(A,B,C,D,E,Y,Z) do { \
  299. exe_select_test((A),(B),(C),(D),(E),(Y),(Z)); \
  300. if(res) \
  301. goto test_cleanup; \
  302. } WHILE_FALSE
  303. #define select_test(A,B,C,D,E) \
  304. chk_select_test((A),(B),(C),(D),(E),(__FILE__),(__LINE__))
  305. /* ---------------------------------------------------------------- */
  306. #define start_test_timing() do { \
  307. tv_test_start = tutil_tvnow(); \
  308. } WHILE_FALSE
  309. #define exe_test_timedout(Y,Z) do { \
  310. if(tutil_tvdiff(tutil_tvnow(), tv_test_start) > TEST_HANG_TIMEOUT) { \
  311. fprintf(stderr, "%s:%d ABORTING TEST, since it seems " \
  312. "that it would have run forever.\n", (Y), (Z)); \
  313. res = TEST_ERR_RUNS_FOREVER; \
  314. } \
  315. } WHILE_FALSE
  316. #define res_test_timedout() \
  317. exe_test_timedout((__FILE__),(__LINE__))
  318. #define chk_test_timedout(Y,Z) do { \
  319. exe_test_timedout(Y,Z); \
  320. if(res) \
  321. goto test_cleanup; \
  322. } WHILE_FALSE
  323. #define abort_on_test_timeout() \
  324. chk_test_timedout((__FILE__),(__LINE__))
  325. /* ---------------------------------------------------------------- */
  326. #define exe_global_init(A,Y,Z) do { \
  327. CURLcode ec; \
  328. if((ec = curl_global_init((A))) != CURLE_OK) { \
  329. fprintf(stderr, "%s:%d curl_global_init() failed, " \
  330. "with code %d (%s)\n", \
  331. (Y), (Z), (int)ec, curl_easy_strerror(ec)); \
  332. res = (int)ec; \
  333. } \
  334. } WHILE_FALSE
  335. #define res_global_init(A) \
  336. exe_global_init((A),(__FILE__),(__LINE__))
  337. #define chk_global_init(A,Y,Z) do { \
  338. exe_global_init((A),(Y),(Z)); \
  339. if(res) \
  340. return res; \
  341. } WHILE_FALSE
  342. /* global_init() is different than other macros. In case of
  343. failure it 'return's instead of going to 'test_cleanup'. */
  344. #define global_init(A) \
  345. chk_global_init((A),(__FILE__),(__LINE__))
  346. /* ---------------------------------------------------------------- */