CurlTests.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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 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. ***************************************************************************/
  22. #ifdef TIME_WITH_SYS_TIME
  23. /* Time with sys/time test */
  24. #include <sys/types.h>
  25. #include <sys/time.h>
  26. #include <time.h>
  27. int
  28. main ()
  29. {
  30. if ((struct tm *) 0)
  31. return 0;
  32. ;
  33. return 0;
  34. }
  35. #endif
  36. #ifdef HAVE_FCNTL_O_NONBLOCK
  37. /* headers for FCNTL_O_NONBLOCK test */
  38. #include <sys/types.h>
  39. #include <unistd.h>
  40. #include <fcntl.h>
  41. /* */
  42. #if defined(sun) || defined(__sun__) || \
  43. defined(__SUNPRO_C) || defined(__SUNPRO_CC)
  44. # if defined(__SVR4) || defined(__srv4__)
  45. # define PLATFORM_SOLARIS
  46. # else
  47. # define PLATFORM_SUNOS4
  48. # endif
  49. #endif
  50. #if (defined(_AIX) || defined(__xlC__)) && !defined(_AIX41)
  51. # define PLATFORM_AIX_V3
  52. #endif
  53. /* */
  54. #if defined(PLATFORM_SUNOS4) || defined(PLATFORM_AIX_V3) || defined(__BEOS__)
  55. #error "O_NONBLOCK does not work on this platform"
  56. #endif
  57. int
  58. main ()
  59. {
  60. /* O_NONBLOCK source test */
  61. int flags = 0;
  62. if(0 != fcntl(0, F_SETFL, flags | O_NONBLOCK))
  63. return 1;
  64. return 0;
  65. }
  66. #endif
  67. /* tests for gethostbyaddr_r or gethostbyname_r */
  68. #if defined(HAVE_GETHOSTBYADDR_R_5_REENTRANT) || \
  69. defined(HAVE_GETHOSTBYADDR_R_7_REENTRANT) || \
  70. defined(HAVE_GETHOSTBYADDR_R_8_REENTRANT) || \
  71. defined(HAVE_GETHOSTBYNAME_R_3_REENTRANT) || \
  72. defined(HAVE_GETHOSTBYNAME_R_5_REENTRANT) || \
  73. defined(HAVE_GETHOSTBYNAME_R_6_REENTRANT)
  74. # define _REENTRANT
  75. /* no idea whether _REENTRANT is always set, just invent a new flag */
  76. # define TEST_GETHOSTBYFOO_REENTRANT
  77. #endif
  78. #if defined(HAVE_GETHOSTBYADDR_R_5) || \
  79. defined(HAVE_GETHOSTBYADDR_R_7) || \
  80. defined(HAVE_GETHOSTBYADDR_R_8) || \
  81. defined(HAVE_GETHOSTBYNAME_R_3) || \
  82. defined(HAVE_GETHOSTBYNAME_R_5) || \
  83. defined(HAVE_GETHOSTBYNAME_R_6) || \
  84. defined(TEST_GETHOSTBYFOO_REENTRANT)
  85. #include <sys/types.h>
  86. #include <netdb.h>
  87. int main(void)
  88. {
  89. char *address = "example.com";
  90. int length = 0;
  91. int type = 0;
  92. struct hostent h;
  93. int rc = 0;
  94. #if defined(HAVE_GETHOSTBYADDR_R_5) || \
  95. defined(HAVE_GETHOSTBYADDR_R_5_REENTRANT) || \
  96. \
  97. defined(HAVE_GETHOSTBYNAME_R_3) || \
  98. defined(HAVE_GETHOSTBYNAME_R_3_REENTRANT)
  99. struct hostent_data hdata;
  100. #elif defined(HAVE_GETHOSTBYADDR_R_7) || \
  101. defined(HAVE_GETHOSTBYADDR_R_7_REENTRANT) || \
  102. defined(HAVE_GETHOSTBYADDR_R_8) || \
  103. defined(HAVE_GETHOSTBYADDR_R_8_REENTRANT) || \
  104. \
  105. defined(HAVE_GETHOSTBYNAME_R_5) || \
  106. defined(HAVE_GETHOSTBYNAME_R_5_REENTRANT) || \
  107. defined(HAVE_GETHOSTBYNAME_R_6) || \
  108. defined(HAVE_GETHOSTBYNAME_R_6_REENTRANT)
  109. char buffer[8192];
  110. int h_errnop;
  111. struct hostent *hp;
  112. #endif
  113. #ifndef gethostbyaddr_r
  114. (void)gethostbyaddr_r;
  115. #endif
  116. #if defined(HAVE_GETHOSTBYADDR_R_5) || \
  117. defined(HAVE_GETHOSTBYADDR_R_5_REENTRANT)
  118. rc = gethostbyaddr_r(address, length, type, &h, &hdata);
  119. (void)rc;
  120. #elif defined(HAVE_GETHOSTBYADDR_R_7) || \
  121. defined(HAVE_GETHOSTBYADDR_R_7_REENTRANT)
  122. hp = gethostbyaddr_r(address, length, type, &h, buffer, 8192, &h_errnop);
  123. (void)hp;
  124. #elif defined(HAVE_GETHOSTBYADDR_R_8) || \
  125. defined(HAVE_GETHOSTBYADDR_R_8_REENTRANT)
  126. rc = gethostbyaddr_r(address, length, type, &h, buffer, 8192, &hp, &h_errnop);
  127. (void)rc;
  128. #endif
  129. #if defined(HAVE_GETHOSTBYNAME_R_3) || \
  130. defined(HAVE_GETHOSTBYNAME_R_3_REENTRANT)
  131. rc = gethostbyname_r(address, &h, &hdata);
  132. #elif defined(HAVE_GETHOSTBYNAME_R_5) || \
  133. defined(HAVE_GETHOSTBYNAME_R_5_REENTRANT)
  134. rc = gethostbyname_r(address, &h, buffer, 8192, &h_errnop);
  135. (void)hp; /* not used for test */
  136. #elif defined(HAVE_GETHOSTBYNAME_R_6) || \
  137. defined(HAVE_GETHOSTBYNAME_R_6_REENTRANT)
  138. rc = gethostbyname_r(address, &h, buffer, 8192, &hp, &h_errnop);
  139. #endif
  140. (void)length;
  141. (void)type;
  142. (void)rc;
  143. return 0;
  144. }
  145. #endif
  146. #ifdef HAVE_SOCKLEN_T
  147. #ifdef _WIN32
  148. #include <ws2tcpip.h>
  149. #else
  150. #include <sys/types.h>
  151. #include <sys/socket.h>
  152. #endif
  153. int
  154. main ()
  155. {
  156. if ((socklen_t *) 0)
  157. return 0;
  158. if (sizeof (socklen_t))
  159. return 0;
  160. ;
  161. return 0;
  162. }
  163. #endif
  164. #ifdef HAVE_IN_ADDR_T
  165. #include <sys/types.h>
  166. #include <sys/socket.h>
  167. #include <arpa/inet.h>
  168. int
  169. main ()
  170. {
  171. if ((in_addr_t *) 0)
  172. return 0;
  173. if (sizeof (in_addr_t))
  174. return 0;
  175. ;
  176. return 0;
  177. }
  178. #endif
  179. #ifdef HAVE_BOOL_T
  180. #ifdef HAVE_SYS_TYPES_H
  181. #include <sys/types.h>
  182. #endif
  183. #ifdef HAVE_STDBOOL_H
  184. #include <stdbool.h>
  185. #endif
  186. int
  187. main ()
  188. {
  189. if (sizeof (bool *) )
  190. return 0;
  191. ;
  192. return 0;
  193. }
  194. #endif
  195. #ifdef STDC_HEADERS
  196. #include <stdlib.h>
  197. #include <stdarg.h>
  198. #include <string.h>
  199. #include <float.h>
  200. int main() { return 0; }
  201. #endif
  202. #ifdef RETSIGTYPE_TEST
  203. #include <sys/types.h>
  204. #include <signal.h>
  205. #ifdef signal
  206. # undef signal
  207. #endif
  208. #ifdef __cplusplus
  209. extern "C" void (*signal (int, void (*)(int)))(int);
  210. #else
  211. void (*signal ()) ();
  212. #endif
  213. int
  214. main ()
  215. {
  216. return 0;
  217. }
  218. #endif
  219. #ifdef HAVE_INET_NTOA_R_DECL
  220. #include <arpa/inet.h>
  221. typedef void (*func_type)();
  222. int main()
  223. {
  224. #ifndef inet_ntoa_r
  225. func_type func;
  226. func = (func_type)inet_ntoa_r;
  227. (void)func;
  228. #endif
  229. return 0;
  230. }
  231. #endif
  232. #ifdef HAVE_INET_NTOA_R_DECL_REENTRANT
  233. #define _REENTRANT
  234. #include <arpa/inet.h>
  235. typedef void (*func_type)();
  236. int main()
  237. {
  238. #ifndef inet_ntoa_r
  239. func_type func;
  240. func = (func_type)&inet_ntoa_r;
  241. (void)func;
  242. #endif
  243. return 0;
  244. }
  245. #endif
  246. #ifdef HAVE_GETADDRINFO
  247. #include <netdb.h>
  248. #include <sys/types.h>
  249. #include <sys/socket.h>
  250. int main(void) {
  251. struct addrinfo hints, *ai;
  252. int error;
  253. memset(&hints, 0, sizeof(hints));
  254. hints.ai_family = AF_UNSPEC;
  255. hints.ai_socktype = SOCK_STREAM;
  256. #ifndef getaddrinfo
  257. (void)getaddrinfo;
  258. #endif
  259. error = getaddrinfo("127.0.0.1", "8080", &hints, &ai);
  260. if (error) {
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. #endif
  266. #ifdef HAVE_FILE_OFFSET_BITS
  267. #ifdef _FILE_OFFSET_BITS
  268. #undef _FILE_OFFSET_BITS
  269. #endif
  270. #define _FILE_OFFSET_BITS 64
  271. #include <sys/types.h>
  272. /* Check that off_t can represent 2**63 - 1 correctly.
  273. We can't simply define LARGE_OFF_T to be 9223372036854775807,
  274. since some C++ compilers masquerading as C compilers
  275. incorrectly reject 9223372036854775807. */
  276. #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
  277. int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
  278. && LARGE_OFF_T % 2147483647 == 1)
  279. ? 1 : -1];
  280. int main () { ; return 0; }
  281. #endif
  282. #ifdef HAVE_IOCTLSOCKET
  283. /* includes start */
  284. #ifdef HAVE_WINDOWS_H
  285. # ifndef WIN32_LEAN_AND_MEAN
  286. # define WIN32_LEAN_AND_MEAN
  287. # endif
  288. # include <windows.h>
  289. # ifdef HAVE_WINSOCK2_H
  290. # include <winsock2.h>
  291. # else
  292. # ifdef HAVE_WINSOCK_H
  293. # include <winsock.h>
  294. # endif
  295. # endif
  296. #endif
  297. int
  298. main ()
  299. {
  300. /* ioctlsocket source code */
  301. int socket;
  302. unsigned long flags = ioctlsocket(socket, FIONBIO, &flags);
  303. ;
  304. return 0;
  305. }
  306. #endif
  307. #ifdef HAVE_IOCTLSOCKET_CAMEL
  308. /* includes start */
  309. #ifdef HAVE_WINDOWS_H
  310. # ifndef WIN32_LEAN_AND_MEAN
  311. # define WIN32_LEAN_AND_MEAN
  312. # endif
  313. # include <windows.h>
  314. # ifdef HAVE_WINSOCK2_H
  315. # include <winsock2.h>
  316. # else
  317. # ifdef HAVE_WINSOCK_H
  318. # include <winsock.h>
  319. # endif
  320. # endif
  321. #endif
  322. int
  323. main ()
  324. {
  325. /* IoctlSocket source code */
  326. if(0 != IoctlSocket(0, 0, 0))
  327. return 1;
  328. ;
  329. return 0;
  330. }
  331. #endif
  332. #ifdef HAVE_IOCTLSOCKET_CAMEL_FIONBIO
  333. /* includes start */
  334. #ifdef HAVE_WINDOWS_H
  335. # ifndef WIN32_LEAN_AND_MEAN
  336. # define WIN32_LEAN_AND_MEAN
  337. # endif
  338. # include <windows.h>
  339. # ifdef HAVE_WINSOCK2_H
  340. # include <winsock2.h>
  341. # else
  342. # ifdef HAVE_WINSOCK_H
  343. # include <winsock.h>
  344. # endif
  345. # endif
  346. #endif
  347. int
  348. main ()
  349. {
  350. /* IoctlSocket source code */
  351. long flags = 0;
  352. if(0 != ioctlsocket(0, FIONBIO, &flags))
  353. return 1;
  354. ;
  355. return 0;
  356. }
  357. #endif
  358. #ifdef HAVE_IOCTLSOCKET_FIONBIO
  359. /* includes start */
  360. #ifdef HAVE_WINDOWS_H
  361. # ifndef WIN32_LEAN_AND_MEAN
  362. # define WIN32_LEAN_AND_MEAN
  363. # endif
  364. # include <windows.h>
  365. # ifdef HAVE_WINSOCK2_H
  366. # include <winsock2.h>
  367. # else
  368. # ifdef HAVE_WINSOCK_H
  369. # include <winsock.h>
  370. # endif
  371. # endif
  372. #endif
  373. int
  374. main ()
  375. {
  376. int flags = 0;
  377. if(0 != ioctlsocket(0, FIONBIO, &flags))
  378. return 1;
  379. ;
  380. return 0;
  381. }
  382. #endif
  383. #ifdef HAVE_IOCTL_FIONBIO
  384. /* headers for FIONBIO test */
  385. /* includes start */
  386. #ifdef HAVE_SYS_TYPES_H
  387. # include <sys/types.h>
  388. #endif
  389. #ifdef HAVE_UNISTD_H
  390. # include <unistd.h>
  391. #endif
  392. #ifdef HAVE_SYS_SOCKET_H
  393. # include <sys/socket.h>
  394. #endif
  395. #ifdef HAVE_SYS_IOCTL_H
  396. # include <sys/ioctl.h>
  397. #endif
  398. #ifdef HAVE_STROPTS_H
  399. # include <stropts.h>
  400. #endif
  401. int
  402. main ()
  403. {
  404. int flags = 0;
  405. if(0 != ioctl(0, FIONBIO, &flags))
  406. return 1;
  407. ;
  408. return 0;
  409. }
  410. #endif
  411. #ifdef HAVE_IOCTL_SIOCGIFADDR
  412. /* headers for FIONBIO test */
  413. /* includes start */
  414. #ifdef HAVE_SYS_TYPES_H
  415. # include <sys/types.h>
  416. #endif
  417. #ifdef HAVE_UNISTD_H
  418. # include <unistd.h>
  419. #endif
  420. #ifdef HAVE_SYS_SOCKET_H
  421. # include <sys/socket.h>
  422. #endif
  423. #ifdef HAVE_SYS_IOCTL_H
  424. # include <sys/ioctl.h>
  425. #endif
  426. #ifdef HAVE_STROPTS_H
  427. # include <stropts.h>
  428. #endif
  429. #include <net/if.h>
  430. int
  431. main ()
  432. {
  433. struct ifreq ifr;
  434. if(0 != ioctl(0, SIOCGIFADDR, &ifr))
  435. return 1;
  436. ;
  437. return 0;
  438. }
  439. #endif
  440. #ifdef HAVE_SETSOCKOPT_SO_NONBLOCK
  441. /* includes start */
  442. #ifdef HAVE_WINDOWS_H
  443. # ifndef WIN32_LEAN_AND_MEAN
  444. # define WIN32_LEAN_AND_MEAN
  445. # endif
  446. # include <windows.h>
  447. # ifdef HAVE_WINSOCK2_H
  448. # include <winsock2.h>
  449. # else
  450. # ifdef HAVE_WINSOCK_H
  451. # include <winsock.h>
  452. # endif
  453. # endif
  454. #endif
  455. /* includes start */
  456. #ifdef HAVE_SYS_TYPES_H
  457. # include <sys/types.h>
  458. #endif
  459. #ifdef HAVE_SYS_SOCKET_H
  460. # include <sys/socket.h>
  461. #endif
  462. /* includes end */
  463. int
  464. main ()
  465. {
  466. if(0 != setsockopt(0, SOL_SOCKET, SO_NONBLOCK, 0, 0))
  467. return 1;
  468. ;
  469. return 0;
  470. }
  471. #endif
  472. #ifdef HAVE_GLIBC_STRERROR_R
  473. #include <string.h>
  474. #include <errno.h>
  475. void check(char c) {}
  476. int
  477. main () {
  478. char buffer[1024];
  479. /* This will not compile if strerror_r does not return a char* */
  480. check(strerror_r(EACCES, buffer, sizeof(buffer))[0]);
  481. return 0;
  482. }
  483. #endif
  484. #ifdef HAVE_POSIX_STRERROR_R
  485. #include <string.h>
  486. #include <errno.h>
  487. /* float, because a pointer can't be implicitly cast to float */
  488. void check(float f) {}
  489. int
  490. main () {
  491. char buffer[1024];
  492. /* This will not compile if strerror_r does not return an int */
  493. check(strerror_r(EACCES, buffer, sizeof(buffer)));
  494. return 0;
  495. }
  496. #endif
  497. #ifdef HAVE_FSETXATTR_6
  498. #include <sys/xattr.h> /* header from libc, not from libattr */
  499. int
  500. main() {
  501. fsetxattr(0, 0, 0, 0, 0, 0);
  502. return 0;
  503. }
  504. #endif
  505. #ifdef HAVE_FSETXATTR_5
  506. #include <sys/xattr.h> /* header from libc, not from libattr */
  507. int
  508. main() {
  509. fsetxattr(0, 0, 0, 0, 0);
  510. return 0;
  511. }
  512. #endif
  513. #ifdef HAVE_CLOCK_GETTIME_MONOTONIC
  514. #include <time.h>
  515. int
  516. main() {
  517. struct timespec ts = {0, 0};
  518. clock_gettime(CLOCK_MONOTONIC, &ts);
  519. return 0;
  520. }
  521. #endif
  522. #ifdef HAVE_BUILTIN_AVAILABLE
  523. int
  524. main() {
  525. if(__builtin_available(macOS 10.12, *)) {}
  526. return 0;
  527. }
  528. #endif
  529. #ifdef HAVE_VARIADIC_MACROS_C99
  530. #define c99_vmacro3(first, ...) fun3(first, __VA_ARGS__)
  531. #define c99_vmacro2(first, ...) fun2(first, __VA_ARGS__)
  532. int fun3(int arg1, int arg2, int arg3);
  533. int fun2(int arg1, int arg2);
  534. int fun3(int arg1, int arg2, int arg3) {
  535. return arg1 + arg2 + arg3;
  536. }
  537. int fun2(int arg1, int arg2) {
  538. return arg1 + arg2;
  539. }
  540. int
  541. main() {
  542. int res3 = c99_vmacro3(1, 2, 3);
  543. int res2 = c99_vmacro2(1, 2);
  544. (void)res3;
  545. (void)res2;
  546. return 0;
  547. }
  548. #endif
  549. #ifdef HAVE_VARIADIC_MACROS_GCC
  550. #define gcc_vmacro3(first, args...) fun3(first, args)
  551. #define gcc_vmacro2(first, args...) fun2(first, args)
  552. int fun3(int arg1, int arg2, int arg3);
  553. int fun2(int arg1, int arg2);
  554. int fun3(int arg1, int arg2, int arg3) {
  555. return arg1 + arg2 + arg3;
  556. }
  557. int fun2(int arg1, int arg2) {
  558. return arg1 + arg2;
  559. }
  560. int
  561. main() {
  562. int res3 = gcc_vmacro3(1, 2, 3);
  563. int res2 = gcc_vmacro2(1, 2);
  564. (void)res3;
  565. (void)res2;
  566. return 0;
  567. }
  568. #endif