mk-lib1521.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. # SPDX-License-Identifier: curl
  23. #
  24. ###########################################################################
  25. # Usage:
  26. # perl mk-lib1521.pl < ../../include/curl/curl.h > lib1521.c
  27. # minimum and maximum long signed values
  28. my $minlong = "LONG_MIN";
  29. my $maxlong = "LONG_MAX";
  30. # maximum curl_off_t
  31. my $maxofft = "CURL_OFF_T_MAX";
  32. my $line = "";
  33. my $incomment = 0;
  34. # Options allowed to return CURLE_BAD_FUNCTION_ARGUMENT if given a string they
  35. # do not recognize as valid
  36. my @bad_function_argument = (
  37. 'CURLOPT_DNS_LOCAL_IP4',
  38. 'CURLOPT_DNS_LOCAL_IP6',
  39. 'CURLOPT_DNS_SERVERS',
  40. 'CURLOPT_PROXY_TLSAUTH_TYPE',
  41. 'CURLOPT_SSLENGINE',
  42. 'CURLOPT_TLSAUTH_TYPE',
  43. );
  44. # Options allowed to return CURLE_UNSUPPORTED_PROTOCOL if given a string they
  45. # do not recognize as valid
  46. my @unsupported_protocol = (
  47. 'CURLOPT_PROTOCOLS_STR',
  48. 'CURLOPT_REDIR_PROTOCOLS_STR',
  49. );
  50. # Options allowed to return CURLE_SSL_ENGINE_NOTFOUND if given a string they
  51. # do not recognize as valid
  52. my @ssl_engine_notfound = (
  53. 'CURLOPT_SSLENGINE',
  54. );
  55. # Options allowed to return CURLE_UNSUPPORTED_PROTOCOL if given a bad
  56. # numerical input they do not recognize as valid
  57. my @unsupported_protocol_num = (
  58. 'CURLOPT_HTTP_VERSION',
  59. );
  60. # Options allowed to return CURLE_NOT_BUILT_IN if given a bad
  61. # numerical input they do not recognize as valid
  62. my @not_built_in_num = (
  63. 'CURLOPT_HTTPAUTH',
  64. 'CURLOPT_PROXYAUTH',
  65. 'CURLOPT_SOCKS5_AUTH',
  66. );
  67. #
  68. # Generate a set of string checks
  69. #
  70. my $allowedstringerrors = <<MOO
  71. switch(code) {
  72. case CURLE_BAD_FUNCTION_ARGUMENT:
  73. MOO
  74. ;
  75. for my $o (@bad_function_argument) {
  76. $allowedstringerrors .= <<MOO
  77. if(!strcmp("$o", name))
  78. return;
  79. MOO
  80. ;
  81. }
  82. $allowedstringerrors .= <<MOO
  83. break;
  84. MOO
  85. ;
  86. $allowedstringerrors .= <<MOO
  87. case CURLE_UNSUPPORTED_PROTOCOL:
  88. MOO
  89. ;
  90. for my $o (@unsupported_protocol) {
  91. $allowedstringerrors .= <<MOO
  92. if(!strcmp("$o", name))
  93. return;
  94. MOO
  95. ;
  96. }
  97. $allowedstringerrors .= <<MOO
  98. break;
  99. MOO
  100. ;
  101. $allowedstringerrors .= <<MOO
  102. case CURLE_SSL_ENGINE_NOTFOUND:
  103. MOO
  104. ;
  105. for my $o (@ssl_engine_notfound) {
  106. $allowedstringerrors .= <<MOO
  107. if(!strcmp("$o", name))
  108. return;
  109. MOO
  110. ;
  111. }
  112. $allowedstringerrors .= <<MOO
  113. break;
  114. default:
  115. break;
  116. }
  117. MOO
  118. ;
  119. #
  120. # Generate a set of string checks
  121. #
  122. my $allowednumerrors = <<MOO
  123. switch(code) {
  124. case CURLE_UNSUPPORTED_PROTOCOL:
  125. MOO
  126. ;
  127. for my $o (@unsupported_protocol_num) {
  128. $allowednumerrors .= <<MOO
  129. if(!strcmp("$o", name))
  130. return;
  131. MOO
  132. ;
  133. }
  134. $allowednumerrors .= <<MOO
  135. break;
  136. case CURLE_NOT_BUILT_IN:
  137. MOO
  138. ;
  139. for my $o (@not_built_in_num) {
  140. $allowednumerrors .= <<MOO
  141. if(!strcmp("$o", name))
  142. return;
  143. MOO
  144. ;
  145. }
  146. $allowednumerrors .= <<MOO
  147. break;
  148. default:
  149. break;
  150. }
  151. MOO
  152. ;
  153. print <<HEADER
  154. /***************************************************************************
  155. * _ _ ____ _
  156. * Project ___| | | | _ \\| |
  157. * / __| | | | |_) | |
  158. * | (__| |_| | _ <| |___
  159. * \\___|\\___/|_| \\_\\_____|
  160. *
  161. * Copyright (C) Daniel Stenberg, <daniel\@haxx.se>, et al.
  162. *
  163. * This software is licensed as described in the file COPYING, which
  164. * you should have received as part of this distribution. The terms
  165. * are also available at https://curl.se/docs/copyright.html.
  166. *
  167. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  168. * copies of the Software, and permit persons to whom the Software is
  169. * furnished to do so, under the terms of the COPYING file.
  170. *
  171. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  172. * KIND, either express or implied.
  173. *
  174. * SPDX-License-Identifier: curl
  175. *
  176. ***************************************************************************/
  177. #include "test.h"
  178. #include "memdebug.h"
  179. #include <limits.h>
  180. /* This source code is generated by mk-lib1521.pl ! */
  181. struct testdata {
  182. char *blaha;
  183. };
  184. #define LO $minlong
  185. #define HI $maxlong
  186. #define OFF_LO (curl_off_t) LO
  187. #define OFF_HI (curl_off_t) $maxofft
  188. #define OFF_NO (curl_off_t) 0
  189. static size_t writecb(char *buffer, size_t size, size_t nitems,
  190. void *outstream)
  191. {
  192. (void)buffer;
  193. (void)size;
  194. (void)nitems;
  195. (void)outstream;
  196. return 0;
  197. }
  198. static size_t readcb(char *buffer,
  199. size_t size,
  200. size_t nitems,
  201. void *instream)
  202. {
  203. (void)buffer;
  204. (void)size;
  205. (void)nitems;
  206. (void)instream;
  207. return 0;
  208. }
  209. static void errlongzero(const char *name, CURLcode code, int lineno)
  210. {
  211. printf("%s set to 0 returned %d, \\"%s\\" on line %d\\n",
  212. name, code, curl_easy_strerror(code), lineno);
  213. }
  214. static void errlong(const char *name, CURLcode code, int lineno)
  215. {
  216. $allowednumerrors
  217. printf("%s set to non-zero returned %d, \\"%s\\" on line %d\\n",
  218. name, code, curl_easy_strerror(code), lineno);
  219. }
  220. static void errstring(const char *name, CURLcode code, int lineno)
  221. {
  222. /* allow this set of options to return CURLE_BAD_FUNCTION_ARGUMENT
  223. when given a strange string input */
  224. $allowedstringerrors
  225. printf("%s set to a string returned %d, \\"%s\\" on line %d\\n",
  226. name, code, curl_easy_strerror(code), lineno);
  227. }
  228. static void err(const char *name, CURLcode val, int lineno)
  229. {
  230. printf("%s returned %d, \\"%s\\" on line %d\\n",
  231. name, val, curl_easy_strerror(val), lineno);
  232. }
  233. static void errnull(const char *name, CURLcode val, int lineno)
  234. {
  235. printf("%s set to NULL returned %d, \\"%s\\" on line %d\\n",
  236. name, val, curl_easy_strerror(val), lineno);
  237. }
  238. static void geterr(const char *name, CURLcode val, int lineno)
  239. {
  240. printf("CURLINFO_%s returned %d, \\"%s\\" on line %d\\n",
  241. name, val, curl_easy_strerror(val), lineno);
  242. }
  243. static curl_progress_callback progresscb;
  244. static curl_write_callback headercb;
  245. static curl_debug_callback debugcb;
  246. static curl_trailer_callback trailercb;
  247. static curl_ssl_ctx_callback ssl_ctx_cb;
  248. static curl_ioctl_callback ioctlcb;
  249. static curl_sockopt_callback sockoptcb;
  250. static curl_opensocket_callback opensocketcb;
  251. static curl_seek_callback seekcb;
  252. static curl_sshkeycallback ssh_keycb;
  253. static curl_sshhostkeycallback ssh_hostkeycb;
  254. static curl_chunk_bgn_callback chunk_bgn_cb;
  255. static curl_chunk_end_callback chunk_end_cb;
  256. static curl_fnmatch_callback fnmatch_cb;
  257. static curl_closesocket_callback closesocketcb;
  258. static curl_xferinfo_callback xferinfocb;
  259. static curl_hstsread_callback hstsreadcb;
  260. static curl_hstswrite_callback hstswritecb;
  261. static curl_resolver_start_callback resolver_start_cb;
  262. static curl_prereq_callback prereqcb;
  263. /* long options that are okay to return
  264. CURLE_BAD_FUNCTION_ARGUMENT */
  265. static bool bad_long(CURLcode res, int check)
  266. {
  267. if(res != CURLE_BAD_FUNCTION_ARGUMENT)
  268. return 0; /* not okay */
  269. if(check < CURLOPTTYPE_OBJECTPOINT) {
  270. /* LONG */
  271. return 1;
  272. }
  273. else if((check >= CURLOPTTYPE_OFF_T) &&
  274. (check < CURLOPTTYPE_BLOB)) {
  275. /* OFF_T */
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. /* macro to check the first setopt of an option which then is allowed to get a
  281. non-existing function return code back */
  282. #define present(x) ((x != CURLE_NOT_BUILT_IN) && (x != CURLE_UNKNOWN_OPTION))
  283. CURLcode test(char *URL)
  284. {
  285. CURL *curl = NULL;
  286. CURL *dep = NULL;
  287. CURLSH *share = NULL;
  288. char errorbuffer[CURL_ERROR_SIZE];
  289. void *conv_from_network_cb = NULL;
  290. void *conv_to_network_cb = NULL;
  291. void *conv_from_utf8_cb = NULL;
  292. void *interleavecb = NULL;
  293. char *stringpointerextra = (char *)"moooo";
  294. struct curl_slist *slist = NULL;
  295. struct curl_httppost *httppost = NULL;
  296. curl_mime *mimepost = NULL;
  297. FILE *stream = stderr;
  298. struct testdata object;
  299. char *charp;
  300. long val;
  301. curl_off_t oval;
  302. double dval;
  303. curl_socket_t sockfd;
  304. struct curl_certinfo *certinfo;
  305. struct curl_tlssessioninfo *tlssession;
  306. struct curl_blob blob = { (void *)"silly", 5, 0};
  307. CURLcode res = CURLE_OK;
  308. (void)URL; /* not used */
  309. global_init(CURL_GLOBAL_ALL);
  310. easy_init(dep);
  311. easy_init(curl);
  312. share = curl_share_init();
  313. if(!share) {
  314. res = CURLE_OUT_OF_MEMORY;
  315. goto test_cleanup;
  316. }
  317. CURL_IGNORE_DEPRECATION(
  318. HEADER
  319. ;
  320. while(<STDIN>) {
  321. s/^\s*(.*?)\s*$/$1/; # Trim.
  322. # Remove multi-line comment trail.
  323. if($incomment) {
  324. if($_ !~ /.*?\*\/\s*(.*)$/) {
  325. next;
  326. }
  327. $_ = $1;
  328. $incomment = 0;
  329. }
  330. if($line ne "") {
  331. # Unfold line.
  332. $_ = "$line $1";
  333. $line = "";
  334. }
  335. # Remove comments.
  336. while($_ =~ /^(.*?)\/\*.*?\*\/(.*)$/) {
  337. $_ = "$1 $2";
  338. }
  339. s/^\s*(.*?)\s*$/$1/; # Trim again.
  340. if($_ =~ /^(.*)\/\*/) {
  341. $_ = $1;
  342. $incomment = 1;
  343. }
  344. # Ignore preprocessor directives and blank lines.
  345. if($_ =~ /^(?:#|$)/) {
  346. next;
  347. }
  348. # Handle lines that may be continued as if they were folded.
  349. if($_ !~ /[;,{}]$/) {
  350. # Folded line.
  351. $line = $_;
  352. next;
  353. }
  354. if($_ =~ / CURL_DEPRECATED\(/) {
  355. # Drop deprecation info.
  356. if($_ !~ /^(.*?) CURL_DEPRECATED\(.*?"\)(.*)$/) {
  357. # Needs unfolding.
  358. $line = $_;
  359. next;
  360. }
  361. $_ = $1 . $2;
  362. }
  363. if($_ =~ /^CURLOPT(?:DEPRECATED)?\(/ && $_ !~ /\),$/) {
  364. # Multi-line CURLOPTs need unfolding.
  365. $line = $_;
  366. next;
  367. }
  368. if($_ =~ /^CURLOPT(?:DEPRECATED)?\(([^ ]*), ([^ ]*), (\d*)[,)]/) {
  369. my ($name, $type, $val)=($1, $2, $3);
  370. my $w=" ";
  371. my $w2="$w$w";
  372. my $w3="$w$w$w";
  373. my $opt = $name;
  374. $opt =~ s/^CURLOPT_//;
  375. my $exists = "${w}{\n";
  376. # the first check for an option
  377. my $fpref = "${exists}${w2}CURLcode first =\n${w3}curl_easy_setopt(curl, $name,";
  378. my $ifpresent = "${w2}if(present(first)) {\n";
  379. my $pref = "${w3}res = curl_easy_setopt(curl, $name,";
  380. my $i = ' ' x (length($w) + 25);
  381. my $fcheck = <<MOO
  382. if(first && present(first)) /* first setopt check only */
  383. err("$name", first, __LINE__);
  384. MOO
  385. ;
  386. my $fstringcheck = <<MOO
  387. if(first && present(first)) /* first setopt check only */
  388. errstring("$name", first, __LINE__);
  389. MOO
  390. ;
  391. my $check = <<MOO
  392. if(res)
  393. err("$name", res, __LINE__);
  394. MOO
  395. ;
  396. my $flongcheckzero = <<MOO
  397. if(first && present(first) && !bad_long(res,
  398. $name))
  399. errlongzero("$name", first, __LINE__);
  400. MOO
  401. ;
  402. my $longcheck = <<MOO
  403. if(res && !bad_long(res, $name))
  404. errlong("$name", res, __LINE__);
  405. MOO
  406. ;
  407. my $nullcheck = <<MOO
  408. if(res)
  409. errnull(\"$name\", res, __LINE__);
  410. MOO
  411. ;
  412. print "\n /****** Verify $name ******/\n";
  413. if($type eq "CURLOPTTYPE_STRINGPOINT") {
  414. print "${fpref} \"string\");\n$fstringcheck";
  415. print "$ifpresent";
  416. print "${pref} NULL);\n$nullcheck";
  417. }
  418. elsif(($type eq "CURLOPTTYPE_LONG") ||
  419. ($type eq "CURLOPTTYPE_VALUES")) {
  420. print "${fpref} 0L);\n$flongcheckzero";
  421. print "$ifpresent";
  422. print "${pref} 22L);\n$longcheck";
  423. print "${pref} LO);\n$longcheck";
  424. print "${pref} HI);\n$longcheck";
  425. }
  426. elsif($type eq "CURLOPTTYPE_OFF_T") {
  427. print "${fpref} OFF_NO);\n$flongcheckzero";
  428. print "$ifpresent";
  429. my $lvl = " " x 29;
  430. print "${pref}\n${lvl}(curl_off_t)22);\n$longcheck";
  431. print "${pref} OFF_HI);\n$longcheck";
  432. print "${pref} OFF_LO);\n$longcheck";
  433. }
  434. elsif(($type eq "CURLOPTTYPE_OBJECTPOINT") ||
  435. ($type eq "CURLOPTTYPE_CBPOINT")) {
  436. if($name =~ /DEPENDS/) {
  437. print "${fpref} dep);\n$fcheck";
  438. }
  439. elsif($name =~ "SHARE") {
  440. print "${fpref} share);\n$fcheck";
  441. }
  442. elsif($name eq "CURLOPT_ERRORBUFFER") {
  443. print "${fpref} errorbuffer);\n$fcheck";
  444. }
  445. elsif(($name eq "CURLOPT_POSTFIELDS") ||
  446. ($name eq "CURLOPT_COPYPOSTFIELDS")) {
  447. # set size to zero to avoid it being "illegal"
  448. print " (void)curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);\n";
  449. print "${fpref} stringpointerextra);\n$fcheck";
  450. }
  451. elsif($name eq "CURLOPT_HTTPPOST") {
  452. print "${fpref} httppost);\n$fcheck";
  453. }
  454. elsif($name eq "CURLOPT_MIMEPOST") {
  455. print "${fpref} mimepost);\n$fcheck";
  456. }
  457. elsif($name eq "CURLOPT_STDERR") {
  458. print "${fpref} stream);\n$fcheck";
  459. }
  460. else {
  461. print "${fpref} &object);\n$fcheck";
  462. }
  463. print "$ifpresent";
  464. print "${pref} NULL);\n$nullcheck";
  465. }
  466. elsif($type eq "CURLOPTTYPE_SLISTPOINT") {
  467. print "${fpref} slist);\n$fcheck";
  468. print "$ifpresent";
  469. print "${pref} NULL);\n$nullcheck";
  470. }
  471. elsif($type eq "CURLOPTTYPE_FUNCTIONPOINT") {
  472. if($name =~ /([^ ]*)FUNCTION/) {
  473. my $l=lc($1);
  474. $l =~ s/^curlopt_//;
  475. print "${fpref}\n$i${l}cb);\n$fcheck";
  476. }
  477. else {
  478. print "${fpref} &func);\n$fcheck";
  479. }
  480. print "$ifpresent";
  481. print "${pref} NULL);\n$nullcheck";
  482. }
  483. elsif($type eq "CURLOPTTYPE_BLOB") {
  484. print "${fpref} &blob);\n$check";
  485. print "$ifpresent";
  486. print "${pref} NULL);\n$nullcheck";
  487. }
  488. else {
  489. print STDERR "\nUnknown type: $type\n";
  490. exit 22; # exit to make this noticed!
  491. }
  492. print <<MOO
  493. } /* end of secondary checks */
  494. } /* end of single setopt */
  495. MOO
  496. ;
  497. }
  498. elsif($_ =~ /^CURLINFO_NONE/) {
  499. $infomode = 1;
  500. }
  501. elsif($infomode &&
  502. ($_ =~ /^CURLINFO_([^ ]*) *= *CURLINFO_([^ ]*)/)) {
  503. my ($info, $type)=($1, $2);
  504. my $c = " res = curl_easy_getinfo(curl, CURLINFO_$info,";
  505. my $check = " if(res)\n geterr(\"$info\", res, __LINE__);\n";
  506. if($type eq "STRING") {
  507. print "$c &charp);\n$check";
  508. }
  509. elsif($type eq "LONG") {
  510. print "$c &val);\n$check";
  511. }
  512. elsif($type eq "OFF_T") {
  513. print "$c &oval);\n$check";
  514. }
  515. elsif($type eq "DOUBLE") {
  516. print "$c &dval);\n$check";
  517. }
  518. elsif($type eq "SLIST") {
  519. print "$c &slist);\n$check";
  520. print " if(slist)\n curl_slist_free_all(slist);\n";
  521. }
  522. elsif($type eq "SOCKET") {
  523. print "$c &sockfd);\n$check";
  524. }
  525. elsif($type eq "PTR") {
  526. if($info eq "CERTINFO") {
  527. print "$c &certinfo);\n$check";
  528. }
  529. elsif(($info eq "TLS_SESSION") ||
  530. ($info eq "TLS_SSL_PTR")) {
  531. print "$c &tlssession);\n$check";
  532. }
  533. else {
  534. print STDERR "$info/$type is unsupported\n";
  535. }
  536. }
  537. else {
  538. print STDERR "$type is unsupported\n";
  539. }
  540. }
  541. }
  542. print <<FOOTER
  543. )
  544. curl_easy_setopt(curl, (CURLoption)1, 0);
  545. res = CURLE_OK;
  546. test_cleanup:
  547. curl_easy_cleanup(curl);
  548. curl_easy_cleanup(dep);
  549. curl_share_cleanup(share);
  550. curl_global_cleanup();
  551. if(!res)
  552. puts("ok");
  553. return res;
  554. }
  555. FOOTER
  556. ;