CurlTests.c 10 KB

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