CurlTests.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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_FILE_OFFSET_BITS
  175. #ifdef _FILE_OFFSET_BITS
  176. #undef _FILE_OFFSET_BITS
  177. #endif
  178. #define _FILE_OFFSET_BITS 64
  179. #include <sys/types.h>
  180. /* Check that off_t can represent 2**63 - 1 correctly.
  181. We can't simply define LARGE_OFF_T to be 9223372036854775807,
  182. since some C++ compilers masquerading as C compilers
  183. incorrectly reject 9223372036854775807. */
  184. #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
  185. int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
  186. && LARGE_OFF_T % 2147483647 == 1)
  187. ? 1 : -1];
  188. int main () { ; return 0; }
  189. #endif
  190. #ifdef HAVE_IOCTLSOCKET
  191. /* includes start */
  192. #ifdef HAVE_WINDOWS_H
  193. # ifndef WIN32_LEAN_AND_MEAN
  194. # define WIN32_LEAN_AND_MEAN
  195. # endif
  196. # include <windows.h>
  197. # ifdef HAVE_WINSOCK2_H
  198. # include <winsock2.h>
  199. # endif
  200. #endif
  201. int
  202. main ()
  203. {
  204. /* ioctlsocket source code */
  205. int socket;
  206. unsigned long flags = ioctlsocket(socket, FIONBIO, &flags);
  207. ;
  208. return 0;
  209. }
  210. #endif
  211. #ifdef HAVE_IOCTLSOCKET_CAMEL
  212. /* includes start */
  213. #ifdef HAVE_WINDOWS_H
  214. # ifndef WIN32_LEAN_AND_MEAN
  215. # define WIN32_LEAN_AND_MEAN
  216. # endif
  217. # include <windows.h>
  218. # ifdef HAVE_WINSOCK2_H
  219. # include <winsock2.h>
  220. # endif
  221. #endif
  222. int
  223. main ()
  224. {
  225. /* IoctlSocket source code */
  226. if(0 != IoctlSocket(0, 0, 0))
  227. return 1;
  228. ;
  229. return 0;
  230. }
  231. #endif
  232. #ifdef HAVE_IOCTLSOCKET_CAMEL_FIONBIO
  233. /* includes start */
  234. #ifdef HAVE_WINDOWS_H
  235. # ifndef WIN32_LEAN_AND_MEAN
  236. # define WIN32_LEAN_AND_MEAN
  237. # endif
  238. # include <windows.h>
  239. # ifdef HAVE_WINSOCK2_H
  240. # include <winsock2.h>
  241. # endif
  242. #endif
  243. int
  244. main ()
  245. {
  246. /* IoctlSocket source code */
  247. long flags = 0;
  248. if(0 != IoctlSocket(0, FIONBIO, &flags))
  249. return 1;
  250. ;
  251. return 0;
  252. }
  253. #endif
  254. #ifdef HAVE_IOCTLSOCKET_FIONBIO
  255. /* includes start */
  256. #ifdef HAVE_WINDOWS_H
  257. # ifndef WIN32_LEAN_AND_MEAN
  258. # define WIN32_LEAN_AND_MEAN
  259. # endif
  260. # include <windows.h>
  261. # ifdef HAVE_WINSOCK2_H
  262. # include <winsock2.h>
  263. # endif
  264. #endif
  265. int
  266. main ()
  267. {
  268. int flags = 0;
  269. if(0 != ioctlsocket(0, FIONBIO, &flags))
  270. return 1;
  271. ;
  272. return 0;
  273. }
  274. #endif
  275. #ifdef HAVE_IOCTL_FIONBIO
  276. /* headers for FIONBIO test */
  277. /* includes start */
  278. #ifdef HAVE_SYS_TYPES_H
  279. # include <sys/types.h>
  280. #endif
  281. #ifdef HAVE_UNISTD_H
  282. # include <unistd.h>
  283. #endif
  284. #ifdef HAVE_SYS_SOCKET_H
  285. # include <sys/socket.h>
  286. #endif
  287. #ifdef HAVE_SYS_IOCTL_H
  288. # include <sys/ioctl.h>
  289. #endif
  290. #ifdef HAVE_STROPTS_H
  291. # include <stropts.h>
  292. #endif
  293. int
  294. main ()
  295. {
  296. int flags = 0;
  297. if(0 != ioctl(0, FIONBIO, &flags))
  298. return 1;
  299. ;
  300. return 0;
  301. }
  302. #endif
  303. #ifdef HAVE_IOCTL_SIOCGIFADDR
  304. /* headers for FIONBIO test */
  305. /* includes start */
  306. #ifdef HAVE_SYS_TYPES_H
  307. # include <sys/types.h>
  308. #endif
  309. #ifdef HAVE_UNISTD_H
  310. # include <unistd.h>
  311. #endif
  312. #ifdef HAVE_SYS_SOCKET_H
  313. # include <sys/socket.h>
  314. #endif
  315. #ifdef HAVE_SYS_IOCTL_H
  316. # include <sys/ioctl.h>
  317. #endif
  318. #ifdef HAVE_STROPTS_H
  319. # include <stropts.h>
  320. #endif
  321. #include <net/if.h>
  322. int
  323. main ()
  324. {
  325. struct ifreq ifr;
  326. if(0 != ioctl(0, SIOCGIFADDR, &ifr))
  327. return 1;
  328. ;
  329. return 0;
  330. }
  331. #endif
  332. #ifdef HAVE_SETSOCKOPT_SO_NONBLOCK
  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. # endif
  342. #endif
  343. /* includes start */
  344. #ifdef HAVE_SYS_TYPES_H
  345. # include <sys/types.h>
  346. #endif
  347. #ifdef HAVE_SYS_SOCKET_H
  348. # include <sys/socket.h>
  349. #endif
  350. /* includes end */
  351. int
  352. main ()
  353. {
  354. if(0 != setsockopt(0, SOL_SOCKET, SO_NONBLOCK, 0, 0))
  355. return 1;
  356. ;
  357. return 0;
  358. }
  359. #endif
  360. #ifdef HAVE_GLIBC_STRERROR_R
  361. #include <string.h>
  362. #include <errno.h>
  363. void check(char c) {}
  364. int
  365. main () {
  366. char buffer[1024];
  367. /* This will not compile if strerror_r does not return a char* */
  368. check(strerror_r(EACCES, buffer, sizeof(buffer))[0]);
  369. return 0;
  370. }
  371. #endif
  372. #ifdef HAVE_POSIX_STRERROR_R
  373. #include <string.h>
  374. #include <errno.h>
  375. /* float, because a pointer can't be implicitly cast to float */
  376. void check(float f) {}
  377. int
  378. main () {
  379. char buffer[1024];
  380. /* This will not compile if strerror_r does not return an int */
  381. check(strerror_r(EACCES, buffer, sizeof(buffer)));
  382. return 0;
  383. }
  384. #endif
  385. #ifdef HAVE_FSETXATTR_6
  386. #include <sys/xattr.h> /* header from libc, not from libattr */
  387. int
  388. main() {
  389. fsetxattr(0, 0, 0, 0, 0, 0);
  390. return 0;
  391. }
  392. #endif
  393. #ifdef HAVE_FSETXATTR_5
  394. #include <sys/xattr.h> /* header from libc, not from libattr */
  395. int
  396. main() {
  397. fsetxattr(0, 0, 0, 0, 0);
  398. return 0;
  399. }
  400. #endif
  401. #ifdef HAVE_CLOCK_GETTIME_MONOTONIC
  402. #include <time.h>
  403. int
  404. main() {
  405. struct timespec ts = {0, 0};
  406. clock_gettime(CLOCK_MONOTONIC, &ts);
  407. return 0;
  408. }
  409. #endif
  410. #ifdef HAVE_BUILTIN_AVAILABLE
  411. int
  412. main() {
  413. if(__builtin_available(macOS 10.12, *)) {}
  414. return 0;
  415. }
  416. #endif
  417. #ifdef HAVE_VARIADIC_MACROS_C99
  418. #define c99_vmacro3(first, ...) fun3(first, __VA_ARGS__)
  419. #define c99_vmacro2(first, ...) fun2(first, __VA_ARGS__)
  420. int fun3(int arg1, int arg2, int arg3);
  421. int fun2(int arg1, int arg2);
  422. int fun3(int arg1, int arg2, int arg3) {
  423. return arg1 + arg2 + arg3;
  424. }
  425. int fun2(int arg1, int arg2) {
  426. return arg1 + arg2;
  427. }
  428. int
  429. main() {
  430. int res3 = c99_vmacro3(1, 2, 3);
  431. int res2 = c99_vmacro2(1, 2);
  432. (void)res3;
  433. (void)res2;
  434. return 0;
  435. }
  436. #endif
  437. #ifdef HAVE_VARIADIC_MACROS_GCC
  438. #define gcc_vmacro3(first, args...) fun3(first, args)
  439. #define gcc_vmacro2(first, args...) fun2(first, 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 = gcc_vmacro3(1, 2, 3);
  451. int res2 = gcc_vmacro2(1, 2);
  452. (void)res3;
  453. (void)res2;
  454. return 0;
  455. }
  456. #endif
  457. #ifdef HAVE_ATOMIC
  458. /* includes start */
  459. #ifdef HAVE_SYS_TYPES_H
  460. # include <sys/types.h>
  461. #endif
  462. #ifdef HAVE_UNISTD_H
  463. # include <unistd.h>
  464. #endif
  465. #ifdef HAVE_STDATOMIC_H
  466. # include <stdatomic.h>
  467. #endif
  468. /* includes end */
  469. int
  470. main() {
  471. _Atomic int i = 1;
  472. i = 0; // Force an atomic-write operation.
  473. return i;
  474. }
  475. #endif
  476. #ifdef HAVE_WIN32_WINNT
  477. /* includes start */
  478. #ifdef WIN32
  479. # include "../lib/setup-win32.h"
  480. #endif
  481. /* includes end */
  482. #define enquote(x) #x
  483. #define expand(x) enquote(x)
  484. #pragma message("_WIN32_WINNT=" expand(_WIN32_WINNT))
  485. int
  486. main() {
  487. return 0;
  488. }
  489. #endif