CurlTests.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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 HAVE_INET_NTOA_R_DECL
  203. #include <arpa/inet.h>
  204. typedef void (*func_type)();
  205. int main()
  206. {
  207. #ifndef inet_ntoa_r
  208. func_type func;
  209. func = (func_type)inet_ntoa_r;
  210. (void)func;
  211. #endif
  212. return 0;
  213. }
  214. #endif
  215. #ifdef HAVE_INET_NTOA_R_DECL_REENTRANT
  216. #define _REENTRANT
  217. #include <arpa/inet.h>
  218. typedef void (*func_type)();
  219. int main()
  220. {
  221. #ifndef inet_ntoa_r
  222. func_type func;
  223. func = (func_type)&inet_ntoa_r;
  224. (void)func;
  225. #endif
  226. return 0;
  227. }
  228. #endif
  229. #ifdef HAVE_GETADDRINFO
  230. #include <netdb.h>
  231. #include <sys/types.h>
  232. #include <sys/socket.h>
  233. int main(void) {
  234. struct addrinfo hints, *ai;
  235. int error;
  236. memset(&hints, 0, sizeof(hints));
  237. hints.ai_family = AF_UNSPEC;
  238. hints.ai_socktype = SOCK_STREAM;
  239. #ifndef getaddrinfo
  240. (void)getaddrinfo;
  241. #endif
  242. error = getaddrinfo("127.0.0.1", "8080", &hints, &ai);
  243. if (error) {
  244. return 1;
  245. }
  246. return 0;
  247. }
  248. #endif
  249. #ifdef HAVE_FILE_OFFSET_BITS
  250. #ifdef _FILE_OFFSET_BITS
  251. #undef _FILE_OFFSET_BITS
  252. #endif
  253. #define _FILE_OFFSET_BITS 64
  254. #include <sys/types.h>
  255. /* Check that off_t can represent 2**63 - 1 correctly.
  256. We can't simply define LARGE_OFF_T to be 9223372036854775807,
  257. since some C++ compilers masquerading as C compilers
  258. incorrectly reject 9223372036854775807. */
  259. #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
  260. int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
  261. && LARGE_OFF_T % 2147483647 == 1)
  262. ? 1 : -1];
  263. int main () { ; return 0; }
  264. #endif
  265. #ifdef HAVE_IOCTLSOCKET
  266. /* includes start */
  267. #ifdef HAVE_WINDOWS_H
  268. # ifndef WIN32_LEAN_AND_MEAN
  269. # define WIN32_LEAN_AND_MEAN
  270. # endif
  271. # include <windows.h>
  272. # ifdef HAVE_WINSOCK2_H
  273. # include <winsock2.h>
  274. # else
  275. # ifdef HAVE_WINSOCK_H
  276. # include <winsock.h>
  277. # endif
  278. # endif
  279. #endif
  280. int
  281. main ()
  282. {
  283. /* ioctlsocket source code */
  284. int socket;
  285. unsigned long flags = ioctlsocket(socket, FIONBIO, &flags);
  286. ;
  287. return 0;
  288. }
  289. #endif
  290. #ifdef HAVE_IOCTLSOCKET_CAMEL
  291. /* includes start */
  292. #ifdef HAVE_WINDOWS_H
  293. # ifndef WIN32_LEAN_AND_MEAN
  294. # define WIN32_LEAN_AND_MEAN
  295. # endif
  296. # include <windows.h>
  297. # ifdef HAVE_WINSOCK2_H
  298. # include <winsock2.h>
  299. # else
  300. # ifdef HAVE_WINSOCK_H
  301. # include <winsock.h>
  302. # endif
  303. # endif
  304. #endif
  305. int
  306. main ()
  307. {
  308. /* IoctlSocket source code */
  309. if(0 != IoctlSocket(0, 0, 0))
  310. return 1;
  311. ;
  312. return 0;
  313. }
  314. #endif
  315. #ifdef HAVE_IOCTLSOCKET_CAMEL_FIONBIO
  316. /* includes start */
  317. #ifdef HAVE_WINDOWS_H
  318. # ifndef WIN32_LEAN_AND_MEAN
  319. # define WIN32_LEAN_AND_MEAN
  320. # endif
  321. # include <windows.h>
  322. # ifdef HAVE_WINSOCK2_H
  323. # include <winsock2.h>
  324. # else
  325. # ifdef HAVE_WINSOCK_H
  326. # include <winsock.h>
  327. # endif
  328. # endif
  329. #endif
  330. int
  331. main ()
  332. {
  333. /* IoctlSocket source code */
  334. long flags = 0;
  335. if(0 != ioctlsocket(0, FIONBIO, &flags))
  336. return 1;
  337. ;
  338. return 0;
  339. }
  340. #endif
  341. #ifdef HAVE_IOCTLSOCKET_FIONBIO
  342. /* includes start */
  343. #ifdef HAVE_WINDOWS_H
  344. # ifndef WIN32_LEAN_AND_MEAN
  345. # define WIN32_LEAN_AND_MEAN
  346. # endif
  347. # include <windows.h>
  348. # ifdef HAVE_WINSOCK2_H
  349. # include <winsock2.h>
  350. # else
  351. # ifdef HAVE_WINSOCK_H
  352. # include <winsock.h>
  353. # endif
  354. # endif
  355. #endif
  356. int
  357. main ()
  358. {
  359. int flags = 0;
  360. if(0 != ioctlsocket(0, FIONBIO, &flags))
  361. return 1;
  362. ;
  363. return 0;
  364. }
  365. #endif
  366. #ifdef HAVE_IOCTL_FIONBIO
  367. /* headers for FIONBIO test */
  368. /* includes start */
  369. #ifdef HAVE_SYS_TYPES_H
  370. # include <sys/types.h>
  371. #endif
  372. #ifdef HAVE_UNISTD_H
  373. # include <unistd.h>
  374. #endif
  375. #ifdef HAVE_SYS_SOCKET_H
  376. # include <sys/socket.h>
  377. #endif
  378. #ifdef HAVE_SYS_IOCTL_H
  379. # include <sys/ioctl.h>
  380. #endif
  381. #ifdef HAVE_STROPTS_H
  382. # include <stropts.h>
  383. #endif
  384. int
  385. main ()
  386. {
  387. int flags = 0;
  388. if(0 != ioctl(0, FIONBIO, &flags))
  389. return 1;
  390. ;
  391. return 0;
  392. }
  393. #endif
  394. #ifdef HAVE_IOCTL_SIOCGIFADDR
  395. /* headers for FIONBIO test */
  396. /* includes start */
  397. #ifdef HAVE_SYS_TYPES_H
  398. # include <sys/types.h>
  399. #endif
  400. #ifdef HAVE_UNISTD_H
  401. # include <unistd.h>
  402. #endif
  403. #ifdef HAVE_SYS_SOCKET_H
  404. # include <sys/socket.h>
  405. #endif
  406. #ifdef HAVE_SYS_IOCTL_H
  407. # include <sys/ioctl.h>
  408. #endif
  409. #ifdef HAVE_STROPTS_H
  410. # include <stropts.h>
  411. #endif
  412. #include <net/if.h>
  413. int
  414. main ()
  415. {
  416. struct ifreq ifr;
  417. if(0 != ioctl(0, SIOCGIFADDR, &ifr))
  418. return 1;
  419. ;
  420. return 0;
  421. }
  422. #endif
  423. #ifdef HAVE_SETSOCKOPT_SO_NONBLOCK
  424. /* includes start */
  425. #ifdef HAVE_WINDOWS_H
  426. # ifndef WIN32_LEAN_AND_MEAN
  427. # define WIN32_LEAN_AND_MEAN
  428. # endif
  429. # include <windows.h>
  430. # ifdef HAVE_WINSOCK2_H
  431. # include <winsock2.h>
  432. # else
  433. # ifdef HAVE_WINSOCK_H
  434. # include <winsock.h>
  435. # endif
  436. # endif
  437. #endif
  438. /* includes start */
  439. #ifdef HAVE_SYS_TYPES_H
  440. # include <sys/types.h>
  441. #endif
  442. #ifdef HAVE_SYS_SOCKET_H
  443. # include <sys/socket.h>
  444. #endif
  445. /* includes end */
  446. int
  447. main ()
  448. {
  449. if(0 != setsockopt(0, SOL_SOCKET, SO_NONBLOCK, 0, 0))
  450. return 1;
  451. ;
  452. return 0;
  453. }
  454. #endif
  455. #ifdef HAVE_GLIBC_STRERROR_R
  456. #include <string.h>
  457. #include <errno.h>
  458. void check(char c) {}
  459. int
  460. main () {
  461. char buffer[1024];
  462. /* This will not compile if strerror_r does not return a char* */
  463. check(strerror_r(EACCES, buffer, sizeof(buffer))[0]);
  464. return 0;
  465. }
  466. #endif
  467. #ifdef HAVE_POSIX_STRERROR_R
  468. #include <string.h>
  469. #include <errno.h>
  470. /* float, because a pointer can't be implicitly cast to float */
  471. void check(float f) {}
  472. int
  473. main () {
  474. char buffer[1024];
  475. /* This will not compile if strerror_r does not return an int */
  476. check(strerror_r(EACCES, buffer, sizeof(buffer)));
  477. return 0;
  478. }
  479. #endif
  480. #ifdef HAVE_FSETXATTR_6
  481. #include <sys/xattr.h> /* header from libc, not from libattr */
  482. int
  483. main() {
  484. fsetxattr(0, 0, 0, 0, 0, 0);
  485. return 0;
  486. }
  487. #endif
  488. #ifdef HAVE_FSETXATTR_5
  489. #include <sys/xattr.h> /* header from libc, not from libattr */
  490. int
  491. main() {
  492. fsetxattr(0, 0, 0, 0, 0);
  493. return 0;
  494. }
  495. #endif
  496. #ifdef HAVE_CLOCK_GETTIME_MONOTONIC
  497. #include <time.h>
  498. int
  499. main() {
  500. struct timespec ts = {0, 0};
  501. clock_gettime(CLOCK_MONOTONIC, &ts);
  502. return 0;
  503. }
  504. #endif
  505. #ifdef HAVE_BUILTIN_AVAILABLE
  506. int
  507. main() {
  508. if(__builtin_available(macOS 10.12, *)) {}
  509. return 0;
  510. }
  511. #endif
  512. #ifdef HAVE_VARIADIC_MACROS_C99
  513. #define c99_vmacro3(first, ...) fun3(first, __VA_ARGS__)
  514. #define c99_vmacro2(first, ...) fun2(first, __VA_ARGS__)
  515. int fun3(int arg1, int arg2, int arg3);
  516. int fun2(int arg1, int arg2);
  517. int fun3(int arg1, int arg2, int arg3) {
  518. return arg1 + arg2 + arg3;
  519. }
  520. int fun2(int arg1, int arg2) {
  521. return arg1 + arg2;
  522. }
  523. int
  524. main() {
  525. int res3 = c99_vmacro3(1, 2, 3);
  526. int res2 = c99_vmacro2(1, 2);
  527. (void)res3;
  528. (void)res2;
  529. return 0;
  530. }
  531. #endif
  532. #ifdef HAVE_VARIADIC_MACROS_GCC
  533. #define gcc_vmacro3(first, args...) fun3(first, args)
  534. #define gcc_vmacro2(first, args...) fun2(first, args)
  535. int fun3(int arg1, int arg2, int arg3);
  536. int fun2(int arg1, int arg2);
  537. int fun3(int arg1, int arg2, int arg3) {
  538. return arg1 + arg2 + arg3;
  539. }
  540. int fun2(int arg1, int arg2) {
  541. return arg1 + arg2;
  542. }
  543. int
  544. main() {
  545. int res3 = gcc_vmacro3(1, 2, 3);
  546. int res2 = gcc_vmacro2(1, 2);
  547. (void)res3;
  548. (void)res2;
  549. return 0;
  550. }
  551. #endif