CurlTests.c 11 KB

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