2
0

CurlTests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 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. # else
  218. # ifdef HAVE_WINSOCK_H
  219. # include <winsock.h>
  220. # endif
  221. # endif
  222. #endif
  223. int
  224. main ()
  225. {
  226. /* ioctlsocket source code */
  227. int socket;
  228. unsigned long flags = ioctlsocket(socket, FIONBIO, &flags);
  229. ;
  230. return 0;
  231. }
  232. #endif
  233. #ifdef HAVE_IOCTLSOCKET_CAMEL
  234. /* includes start */
  235. #ifdef HAVE_WINDOWS_H
  236. # ifndef WIN32_LEAN_AND_MEAN
  237. # define WIN32_LEAN_AND_MEAN
  238. # endif
  239. # include <windows.h>
  240. # ifdef HAVE_WINSOCK2_H
  241. # include <winsock2.h>
  242. # else
  243. # ifdef HAVE_WINSOCK_H
  244. # include <winsock.h>
  245. # endif
  246. # endif
  247. #endif
  248. int
  249. main ()
  250. {
  251. /* IoctlSocket source code */
  252. if(0 != IoctlSocket(0, 0, 0))
  253. return 1;
  254. ;
  255. return 0;
  256. }
  257. #endif
  258. #ifdef HAVE_IOCTLSOCKET_CAMEL_FIONBIO
  259. /* includes start */
  260. #ifdef HAVE_WINDOWS_H
  261. # ifndef WIN32_LEAN_AND_MEAN
  262. # define WIN32_LEAN_AND_MEAN
  263. # endif
  264. # include <windows.h>
  265. # ifdef HAVE_WINSOCK2_H
  266. # include <winsock2.h>
  267. # else
  268. # ifdef HAVE_WINSOCK_H
  269. # include <winsock.h>
  270. # endif
  271. # endif
  272. #endif
  273. int
  274. main ()
  275. {
  276. /* IoctlSocket source code */
  277. long flags = 0;
  278. if(0 != IoctlSocket(0, FIONBIO, &flags))
  279. return 1;
  280. ;
  281. return 0;
  282. }
  283. #endif
  284. #ifdef HAVE_IOCTLSOCKET_FIONBIO
  285. /* includes start */
  286. #ifdef HAVE_WINDOWS_H
  287. # ifndef WIN32_LEAN_AND_MEAN
  288. # define WIN32_LEAN_AND_MEAN
  289. # endif
  290. # include <windows.h>
  291. # ifdef HAVE_WINSOCK2_H
  292. # include <winsock2.h>
  293. # else
  294. # ifdef HAVE_WINSOCK_H
  295. # include <winsock.h>
  296. # endif
  297. # endif
  298. #endif
  299. int
  300. main ()
  301. {
  302. int flags = 0;
  303. if(0 != ioctlsocket(0, FIONBIO, &flags))
  304. return 1;
  305. ;
  306. return 0;
  307. }
  308. #endif
  309. #ifdef HAVE_IOCTL_FIONBIO
  310. /* headers for FIONBIO test */
  311. /* includes start */
  312. #ifdef HAVE_SYS_TYPES_H
  313. # include <sys/types.h>
  314. #endif
  315. #ifdef HAVE_UNISTD_H
  316. # include <unistd.h>
  317. #endif
  318. #ifdef HAVE_SYS_SOCKET_H
  319. # include <sys/socket.h>
  320. #endif
  321. #ifdef HAVE_SYS_IOCTL_H
  322. # include <sys/ioctl.h>
  323. #endif
  324. #ifdef HAVE_STROPTS_H
  325. # include <stropts.h>
  326. #endif
  327. int
  328. main ()
  329. {
  330. int flags = 0;
  331. if(0 != ioctl(0, FIONBIO, &flags))
  332. return 1;
  333. ;
  334. return 0;
  335. }
  336. #endif
  337. #ifdef HAVE_IOCTL_SIOCGIFADDR
  338. /* headers for FIONBIO test */
  339. /* includes start */
  340. #ifdef HAVE_SYS_TYPES_H
  341. # include <sys/types.h>
  342. #endif
  343. #ifdef HAVE_UNISTD_H
  344. # include <unistd.h>
  345. #endif
  346. #ifdef HAVE_SYS_SOCKET_H
  347. # include <sys/socket.h>
  348. #endif
  349. #ifdef HAVE_SYS_IOCTL_H
  350. # include <sys/ioctl.h>
  351. #endif
  352. #ifdef HAVE_STROPTS_H
  353. # include <stropts.h>
  354. #endif
  355. #include <net/if.h>
  356. int
  357. main ()
  358. {
  359. struct ifreq ifr;
  360. if(0 != ioctl(0, SIOCGIFADDR, &ifr))
  361. return 1;
  362. ;
  363. return 0;
  364. }
  365. #endif
  366. #ifdef HAVE_SETSOCKOPT_SO_NONBLOCK
  367. /* includes start */
  368. #ifdef HAVE_WINDOWS_H
  369. # ifndef WIN32_LEAN_AND_MEAN
  370. # define WIN32_LEAN_AND_MEAN
  371. # endif
  372. # include <windows.h>
  373. # ifdef HAVE_WINSOCK2_H
  374. # include <winsock2.h>
  375. # else
  376. # ifdef HAVE_WINSOCK_H
  377. # include <winsock.h>
  378. # endif
  379. # endif
  380. #endif
  381. /* includes start */
  382. #ifdef HAVE_SYS_TYPES_H
  383. # include <sys/types.h>
  384. #endif
  385. #ifdef HAVE_SYS_SOCKET_H
  386. # include <sys/socket.h>
  387. #endif
  388. /* includes end */
  389. int
  390. main ()
  391. {
  392. if(0 != setsockopt(0, SOL_SOCKET, SO_NONBLOCK, 0, 0))
  393. return 1;
  394. ;
  395. return 0;
  396. }
  397. #endif
  398. #ifdef HAVE_GLIBC_STRERROR_R
  399. #include <string.h>
  400. #include <errno.h>
  401. void check(char c) {}
  402. int
  403. main () {
  404. char buffer[1024];
  405. /* This will not compile if strerror_r does not return a char* */
  406. check(strerror_r(EACCES, buffer, sizeof(buffer))[0]);
  407. return 0;
  408. }
  409. #endif
  410. #ifdef HAVE_POSIX_STRERROR_R
  411. #include <string.h>
  412. #include <errno.h>
  413. /* float, because a pointer can't be implicitly cast to float */
  414. void check(float f) {}
  415. int
  416. main () {
  417. char buffer[1024];
  418. /* This will not compile if strerror_r does not return an int */
  419. check(strerror_r(EACCES, buffer, sizeof(buffer)));
  420. return 0;
  421. }
  422. #endif
  423. #ifdef HAVE_FSETXATTR_6
  424. #include <sys/xattr.h> /* header from libc, not from libattr */
  425. int
  426. main() {
  427. fsetxattr(0, 0, 0, 0, 0, 0);
  428. return 0;
  429. }
  430. #endif
  431. #ifdef HAVE_FSETXATTR_5
  432. #include <sys/xattr.h> /* header from libc, not from libattr */
  433. int
  434. main() {
  435. fsetxattr(0, 0, 0, 0, 0);
  436. return 0;
  437. }
  438. #endif
  439. #ifdef HAVE_CLOCK_GETTIME_MONOTONIC
  440. #include <time.h>
  441. int
  442. main() {
  443. struct timespec ts = {0, 0};
  444. clock_gettime(CLOCK_MONOTONIC, &ts);
  445. return 0;
  446. }
  447. #endif
  448. #ifdef HAVE_BUILTIN_AVAILABLE
  449. int
  450. main() {
  451. if(__builtin_available(macOS 10.12, *)) {}
  452. return 0;
  453. }
  454. #endif
  455. #ifdef HAVE_VARIADIC_MACROS_C99
  456. #define c99_vmacro3(first, ...) fun3(first, __VA_ARGS__)
  457. #define c99_vmacro2(first, ...) fun2(first, __VA_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 = c99_vmacro3(1, 2, 3);
  469. int res2 = c99_vmacro2(1, 2);
  470. (void)res3;
  471. (void)res2;
  472. return 0;
  473. }
  474. #endif
  475. #ifdef HAVE_VARIADIC_MACROS_GCC
  476. #define gcc_vmacro3(first, args...) fun3(first, args)
  477. #define gcc_vmacro2(first, args...) fun2(first, args)
  478. int fun3(int arg1, int arg2, int arg3);
  479. int fun2(int arg1, int arg2);
  480. int fun3(int arg1, int arg2, int arg3) {
  481. return arg1 + arg2 + arg3;
  482. }
  483. int fun2(int arg1, int arg2) {
  484. return arg1 + arg2;
  485. }
  486. int
  487. main() {
  488. int res3 = gcc_vmacro3(1, 2, 3);
  489. int res2 = gcc_vmacro2(1, 2);
  490. (void)res3;
  491. (void)res2;
  492. return 0;
  493. }
  494. #endif