2
0

mk-lib1521.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. if(!$ARGV[0]) {
  154. die "missing target file name";
  155. }
  156. use File::Temp qw/ :mktemp /;
  157. my ($fh, $tempfile) = mkstemp("lib1521-XXXXX");
  158. print $fh <<HEADER
  159. /***************************************************************************
  160. * _ _ ____ _
  161. * Project ___| | | | _ \\| |
  162. * / __| | | | |_) | |
  163. * | (__| |_| | _ <| |___
  164. * \\___|\\___/|_| \\_\\_____|
  165. *
  166. * Copyright (C) Daniel Stenberg, <daniel\@haxx.se>, et al.
  167. *
  168. * This software is licensed as described in the file COPYING, which
  169. * you should have received as part of this distribution. The terms
  170. * are also available at https://curl.se/docs/copyright.html.
  171. *
  172. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  173. * copies of the Software, and permit persons to whom the Software is
  174. * furnished to do so, under the terms of the COPYING file.
  175. *
  176. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  177. * KIND, either express or implied.
  178. *
  179. * SPDX-License-Identifier: curl
  180. *
  181. ***************************************************************************/
  182. #include "test.h"
  183. #include "memdebug.h"
  184. #include <limits.h>
  185. /* This source code is generated by mk-lib1521.pl ! */
  186. struct testdata {
  187. char *blaha;
  188. };
  189. #define LO $minlong
  190. #define HI $maxlong
  191. #define OFF_LO (curl_off_t) LO
  192. #define OFF_HI (curl_off_t) $maxofft
  193. #define OFF_NO (curl_off_t) 0
  194. static size_t writecb(char *buffer, size_t size, size_t nitems,
  195. void *outstream)
  196. {
  197. (void)buffer;
  198. (void)size;
  199. (void)nitems;
  200. (void)outstream;
  201. return 0;
  202. }
  203. static size_t readcb(char *buffer,
  204. size_t size,
  205. size_t nitems,
  206. void *instream)
  207. {
  208. (void)buffer;
  209. (void)size;
  210. (void)nitems;
  211. (void)instream;
  212. return 0;
  213. }
  214. static void errlongzero(const char *name, CURLcode code, int lineno)
  215. {
  216. printf("%s set to 0 returned %d, \\"%s\\" on line %d\\n",
  217. name, code, curl_easy_strerror(code), lineno);
  218. }
  219. static void errlong(const char *name, CURLcode code, int lineno)
  220. {
  221. $allowednumerrors
  222. printf("%s set to non-zero returned %d, \\"%s\\" on line %d\\n",
  223. name, code, curl_easy_strerror(code), lineno);
  224. }
  225. static void errstring(const char *name, CURLcode code, int lineno)
  226. {
  227. /* allow this set of options to return CURLE_BAD_FUNCTION_ARGUMENT
  228. when given a strange string input */
  229. $allowedstringerrors
  230. printf("%s set to a string returned %d, \\"%s\\" on line %d\\n",
  231. name, code, curl_easy_strerror(code), lineno);
  232. }
  233. static void err(const char *name, CURLcode val, int lineno)
  234. {
  235. printf("%s returned %d, \\"%s\\" on line %d\\n",
  236. name, val, curl_easy_strerror(val), lineno);
  237. }
  238. static void errnull(const char *name, CURLcode val, int lineno)
  239. {
  240. printf("%s set to NULL returned %d, \\"%s\\" on line %d\\n",
  241. name, val, curl_easy_strerror(val), lineno);
  242. }
  243. static void geterr(const char *name, CURLcode val, int lineno)
  244. {
  245. printf("CURLINFO_%s returned %d, \\"%s\\" on line %d\\n",
  246. name, val, curl_easy_strerror(val), lineno);
  247. }
  248. static curl_progress_callback progresscb;
  249. static curl_write_callback headercb;
  250. static curl_debug_callback debugcb;
  251. static curl_trailer_callback trailercb;
  252. static curl_ssl_ctx_callback ssl_ctx_cb;
  253. static curl_ioctl_callback ioctlcb;
  254. static curl_sockopt_callback sockoptcb;
  255. static curl_opensocket_callback opensocketcb;
  256. static curl_seek_callback seekcb;
  257. static curl_sshkeycallback ssh_keycb;
  258. static curl_sshhostkeycallback ssh_hostkeycb;
  259. static curl_chunk_bgn_callback chunk_bgn_cb;
  260. static curl_chunk_end_callback chunk_end_cb;
  261. static curl_fnmatch_callback fnmatch_cb;
  262. static curl_closesocket_callback closesocketcb;
  263. static curl_xferinfo_callback xferinfocb;
  264. static curl_hstsread_callback hstsreadcb;
  265. static curl_hstswrite_callback hstswritecb;
  266. static curl_resolver_start_callback resolver_start_cb;
  267. static curl_prereq_callback prereqcb;
  268. /* long options that are okay to return
  269. CURLE_BAD_FUNCTION_ARGUMENT */
  270. static bool bad_long(CURLcode res, int check)
  271. {
  272. if(res != CURLE_BAD_FUNCTION_ARGUMENT)
  273. return 0; /* not okay */
  274. if(check < CURLOPTTYPE_OBJECTPOINT) {
  275. /* LONG */
  276. return 1;
  277. }
  278. else if((check >= CURLOPTTYPE_OFF_T) &&
  279. (check < CURLOPTTYPE_BLOB)) {
  280. /* OFF_T */
  281. return 1;
  282. }
  283. return 0;
  284. }
  285. /* macro to check the first setopt of an option which then is allowed to get a
  286. non-existing function return code back */
  287. #define present(x) ((x != CURLE_NOT_BUILT_IN) && (x != CURLE_UNKNOWN_OPTION))
  288. CURLcode test(char *URL)
  289. {
  290. CURL *curl = NULL;
  291. CURL *dep = NULL;
  292. CURLSH *share = NULL;
  293. char errorbuffer[CURL_ERROR_SIZE];
  294. void *conv_from_network_cb = NULL;
  295. void *conv_to_network_cb = NULL;
  296. void *conv_from_utf8_cb = NULL;
  297. void *interleavecb = NULL;
  298. char *stringpointerextra = (char *)"moooo";
  299. struct curl_slist *slist = NULL;
  300. struct curl_httppost *httppost = NULL;
  301. curl_mime *mimepost = NULL;
  302. FILE *stream = stderr;
  303. struct testdata object;
  304. char *charp;
  305. long val;
  306. curl_off_t oval;
  307. double dval;
  308. curl_socket_t sockfd;
  309. struct curl_certinfo *certinfo;
  310. struct curl_tlssessioninfo *tlssession;
  311. struct curl_blob blob = { (void *)"silly", 5, 0};
  312. CURLcode res = CURLE_OK;
  313. (void)URL; /* not used */
  314. global_init(CURL_GLOBAL_ALL);
  315. easy_init(dep);
  316. easy_init(curl);
  317. share = curl_share_init();
  318. if(!share) {
  319. res = CURLE_OUT_OF_MEMORY;
  320. goto test_cleanup;
  321. }
  322. CURL_IGNORE_DEPRECATION(
  323. HEADER
  324. ;
  325. while(<STDIN>) {
  326. s/^\s*(.*?)\s*$/$1/; # Trim.
  327. # Remove multi-line comment trail.
  328. if($incomment) {
  329. if($_ !~ /.*?\*\/\s*(.*)$/) {
  330. next;
  331. }
  332. $_ = $1;
  333. $incomment = 0;
  334. }
  335. if($line ne "") {
  336. # Unfold line.
  337. $_ = "$line $1";
  338. $line = "";
  339. }
  340. # Remove comments.
  341. while($_ =~ /^(.*?)\/\*.*?\*\/(.*)$/) {
  342. $_ = "$1 $2";
  343. }
  344. s/^\s*(.*?)\s*$/$1/; # Trim again.
  345. if($_ =~ /^(.*)\/\*/) {
  346. $_ = $1;
  347. $incomment = 1;
  348. }
  349. # Ignore preprocessor directives and blank lines.
  350. if($_ =~ /^(?:#|$)/) {
  351. next;
  352. }
  353. # Handle lines that may be continued as if they were folded.
  354. if($_ !~ /[;,{}]$/) {
  355. # Folded line.
  356. $line = $_;
  357. next;
  358. }
  359. if($_ =~ / CURL_DEPRECATED\(/) {
  360. # Drop deprecation info.
  361. if($_ !~ /^(.*?) CURL_DEPRECATED\(.*?"\)(.*)$/) {
  362. # Needs unfolding.
  363. $line = $_;
  364. next;
  365. }
  366. $_ = $1 . $2;
  367. }
  368. if($_ =~ /^CURLOPT(?:DEPRECATED)?\(/ && $_ !~ /\),$/) {
  369. # Multi-line CURLOPTs need unfolding.
  370. $line = $_;
  371. next;
  372. }
  373. if($_ =~ /^CURLOPT(?:DEPRECATED)?\(([^ ]*), ([^ ]*), (\d*)[,)]/) {
  374. my ($name, $type, $val)=($1, $2, $3);
  375. my $w=" ";
  376. my $w2="$w$w";
  377. my $w3="$w$w$w";
  378. my $opt = $name;
  379. $opt =~ s/^CURLOPT_//;
  380. my $exists = "${w}{\n";
  381. # the first check for an option
  382. my $fpref = "${exists}${w2}CURLcode first =\n${w3}curl_easy_setopt(curl, $name,";
  383. my $ifpresent = "${w2}if(present(first)) {\n";
  384. my $pref = "${w3}res = curl_easy_setopt(curl, $name,";
  385. my $i = ' ' x (length($w) + 25);
  386. my $fcheck = <<MOO
  387. if(first && present(first)) /* first setopt check only */
  388. err("$name", first, __LINE__);
  389. MOO
  390. ;
  391. my $fstringcheck = <<MOO
  392. if(first && present(first)) /* first setopt check only */
  393. errstring("$name", first, __LINE__);
  394. MOO
  395. ;
  396. my $check = <<MOO
  397. if(res)
  398. err("$name", res, __LINE__);
  399. MOO
  400. ;
  401. my $flongcheckzero = <<MOO
  402. if(first && present(first) && !bad_long(first,
  403. $name))
  404. errlongzero("$name", first, __LINE__);
  405. MOO
  406. ;
  407. my $longcheck = <<MOO
  408. if(res && !bad_long(res, $name))
  409. errlong("$name", res, __LINE__);
  410. MOO
  411. ;
  412. my $nullcheck = <<MOO
  413. if(res)
  414. errnull(\"$name\", res, __LINE__);
  415. MOO
  416. ;
  417. print $fh "\n /****** Verify $name ******/\n";
  418. if($type eq "CURLOPTTYPE_STRINGPOINT") {
  419. print $fh "${fpref} \"string\");\n$fstringcheck";
  420. print $fh "$ifpresent";
  421. print $fh "${pref} NULL);\n$nullcheck";
  422. }
  423. elsif(($type eq "CURLOPTTYPE_LONG") ||
  424. ($type eq "CURLOPTTYPE_VALUES")) {
  425. print $fh "${fpref} 0L);\n$flongcheckzero";
  426. print $fh "$ifpresent";
  427. print $fh "${pref} 22L);\n$longcheck";
  428. print $fh "${pref} LO);\n$longcheck";
  429. print $fh "${pref} HI);\n$longcheck";
  430. }
  431. elsif($type eq "CURLOPTTYPE_OFF_T") {
  432. print $fh "${fpref} OFF_NO);\n$flongcheckzero";
  433. print $fh "$ifpresent";
  434. my $lvl = " " x 29;
  435. print $fh "${pref}\n${lvl}(curl_off_t)22);\n$longcheck";
  436. print $fh "${pref} OFF_HI);\n$longcheck";
  437. print $fh "${pref} OFF_LO);\n$longcheck";
  438. }
  439. elsif(($type eq "CURLOPTTYPE_OBJECTPOINT") ||
  440. ($type eq "CURLOPTTYPE_CBPOINT")) {
  441. if($name =~ /DEPENDS/) {
  442. print $fh "${fpref} dep);\n$fcheck";
  443. }
  444. elsif($name =~ "SHARE") {
  445. print $fh "${fpref} share);\n$fcheck";
  446. }
  447. elsif($name eq "CURLOPT_ERRORBUFFER") {
  448. print $fh "${fpref} errorbuffer);\n$fcheck";
  449. }
  450. elsif(($name eq "CURLOPT_POSTFIELDS") ||
  451. ($name eq "CURLOPT_COPYPOSTFIELDS")) {
  452. # set size to zero to avoid it being "illegal"
  453. print $fh " (void)curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);\n";
  454. print $fh "${fpref} stringpointerextra);\n$fcheck";
  455. }
  456. elsif($name eq "CURLOPT_HTTPPOST") {
  457. print $fh "${fpref} httppost);\n$fcheck";
  458. }
  459. elsif($name eq "CURLOPT_MIMEPOST") {
  460. print $fh "${fpref} mimepost);\n$fcheck";
  461. }
  462. elsif($name eq "CURLOPT_STDERR") {
  463. print $fh "${fpref} stream);\n$fcheck";
  464. }
  465. else {
  466. print $fh "${fpref} &object);\n$fcheck";
  467. }
  468. print $fh "$ifpresent";
  469. print $fh "${pref} NULL);\n$nullcheck";
  470. }
  471. elsif($type eq "CURLOPTTYPE_SLISTPOINT") {
  472. print $fh "${fpref} slist);\n$fcheck";
  473. print $fh "$ifpresent";
  474. print $fh "${pref} NULL);\n$nullcheck";
  475. }
  476. elsif($type eq "CURLOPTTYPE_FUNCTIONPOINT") {
  477. if($name =~ /([^ ]*)FUNCTION/) {
  478. my $l=lc($1);
  479. $l =~ s/^curlopt_//;
  480. print $fh "${fpref}\n$i${l}cb);\n$fcheck";
  481. }
  482. else {
  483. print $fh "${fpref} &func);\n$fcheck";
  484. }
  485. print $fh "$ifpresent";
  486. print $fh "${pref} NULL);\n$nullcheck";
  487. }
  488. elsif($type eq "CURLOPTTYPE_BLOB") {
  489. print $fh "${fpref} &blob);\n$check";
  490. print $fh "$ifpresent";
  491. print $fh "${pref} NULL);\n$nullcheck";
  492. }
  493. else {
  494. print STDERR "\nUnknown type: $type\n";
  495. exit 22; # exit to make this noticed!
  496. }
  497. print $fh <<MOO
  498. } /* end of secondary checks */
  499. } /* end of single setopt */
  500. MOO
  501. ;
  502. }
  503. elsif($_ =~ /^CURLINFO_NONE/) {
  504. $infomode = 1;
  505. }
  506. elsif($infomode &&
  507. ($_ =~ /^CURLINFO_([^ ]*) *= *CURLINFO_([^ ]*)/)) {
  508. my ($info, $type)=($1, $2);
  509. my $c = " res = curl_easy_getinfo(curl, CURLINFO_$info,";
  510. my $check = " if(res)\n geterr(\"$info\", res, __LINE__);\n";
  511. if($type eq "STRING") {
  512. print $fh "$c &charp);\n$check";
  513. }
  514. elsif($type eq "LONG") {
  515. print $fh "$c &val);\n$check";
  516. }
  517. elsif($type eq "OFF_T") {
  518. print $fh "$c &oval);\n$check";
  519. }
  520. elsif($type eq "DOUBLE") {
  521. print $fh "$c &dval);\n$check";
  522. }
  523. elsif($type eq "SLIST") {
  524. print $fh "$c &slist);\n$check";
  525. print $fh " if(slist)\n curl_slist_free_all(slist);\n";
  526. }
  527. elsif($type eq "SOCKET") {
  528. print $fh "$c &sockfd);\n$check";
  529. }
  530. elsif($type eq "PTR") {
  531. if($info eq "CERTINFO") {
  532. print $fh "$c &certinfo);\n$check";
  533. }
  534. elsif(($info eq "TLS_SESSION") ||
  535. ($info eq "TLS_SSL_PTR")) {
  536. print $fh "$c &tlssession);\n$check";
  537. }
  538. else {
  539. print STDERR "$info/$type is unsupported\n";
  540. }
  541. }
  542. else {
  543. print STDERR "$type is unsupported\n";
  544. }
  545. }
  546. }
  547. print $fh <<FOOTER
  548. )
  549. curl_easy_setopt(curl, (CURLoption)1, 0);
  550. res = CURLE_OK;
  551. test_cleanup:
  552. curl_easy_cleanup(curl);
  553. curl_easy_cleanup(dep);
  554. curl_share_cleanup(share);
  555. curl_global_cleanup();
  556. if(!res)
  557. puts("ok");
  558. return res;
  559. }
  560. FOOTER
  561. ;
  562. close($fh);
  563. rename($tempfile, $ARGV[0]);