convsrctest.pl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 1998 - 2022, 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. #=======================================================================
  26. # Read a test definition which exercises curl's --libcurl option.
  27. # Generate either compilable source code for a new test tool,
  28. # or a new test definition which runs the tool and expects the
  29. # same output.
  30. # This should verify that the --libcurl code really does perform
  31. # the same actions as the original curl invocation.
  32. #-----------------------------------------------------------------------
  33. # The output of curl's --libcurl option differs in several ways from
  34. # the code needed to integrate with the test tool environment:
  35. # - #include "test.h"
  36. # - no call of curl_global_init & curl_global_cleanup
  37. # - main() function vs. test() function
  38. # - no checking of curl_easy_setopt calls vs. test_setopt wrapper
  39. # - handling of stdout
  40. # - variable names ret & hnd vs. res & curl
  41. # - URL as literal string vs. passed as argument
  42. #=======================================================================
  43. use strict;
  44. require "getpart.pm";
  45. # Boilerplate code for test tool
  46. my $head =
  47. '#include "test.h"
  48. #include "memdebug.h"
  49. int test(char *URL)
  50. {
  51. CURLcode res;
  52. CURL *curl;
  53. ';
  54. # Other declarations from --libcurl come here
  55. # e.g. curl_slist
  56. my $init =
  57. '
  58. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  59. fprintf(stderr, "curl_global_init() failed\n");
  60. return TEST_ERR_MAJOR_BAD;
  61. }
  62. if ((curl = curl_easy_init()) == NULL) {
  63. fprintf(stderr, "curl_easy_init() failed\n");
  64. curl_global_cleanup();
  65. return TEST_ERR_MAJOR_BAD;
  66. }
  67. ';
  68. # Option setting, perform and cleanup come here
  69. my $exit =
  70. ' curl_global_cleanup();
  71. return (int)res;
  72. }
  73. ';
  74. my $myname = leaf($0);
  75. sub usage {die "Usage: $myname -c|-test=num testfile\n";}
  76. sub main {
  77. @ARGV == 2
  78. or usage;
  79. my($opt,$testfile) = @ARGV;
  80. if(loadtest($testfile)) {
  81. die "$myname: $testfile doesn't look like a test case\n";
  82. }
  83. my $comment = sprintf("DO NOT EDIT - generated from %s by %s",
  84. leaf($testfile), $myname);
  85. if($opt eq '-c') {
  86. generate_c($comment);
  87. }
  88. elsif(my($num) = $opt =~ /^-test=(\d+)$/) {
  89. generate_test($comment, $num);
  90. }
  91. else {
  92. usage;
  93. }
  94. }
  95. sub generate_c {
  96. my($comment) = @_;
  97. # Fetch the generated code, which is the output file checked by
  98. # the old test.
  99. my @libcurl = getpart("verify", "file")
  100. or die "$myname: no <verify><file> section found\n";
  101. # Mangle the code into a suitable form for a test tool.
  102. # We want to extract the important parts (declarations,
  103. # URL, setopt calls, cleanup code) from the --libcurl
  104. # boilerplate and insert them into a new boilerplate.
  105. my(@decl,@code);
  106. # First URL passed in as argument, others as global
  107. my @urlvars = ('URL', 'libtest_arg2', 'libtest_arg3');
  108. my($seen_main,$seen_setopt,$seen_return);
  109. foreach (@libcurl) {
  110. # Check state changes first (even though it
  111. # duplicates some matches) so that the other tests
  112. # are in a logical order).
  113. if(/^int main/) {
  114. $seen_main = 1;
  115. }
  116. if($seen_main and /curl_easy_setopt/) {
  117. # Don't match 'curl_easy_setopt' in comment!
  118. $seen_setopt = 1;
  119. }
  120. if(/^\s*return/) {
  121. $seen_return = 1;
  122. }
  123. # Now filter the code according to purpose
  124. if(! $seen_main) {
  125. next;
  126. }
  127. elsif(! $seen_setopt) {
  128. if(/^\s*(int main|\{|CURLcode |CURL |hnd = curl_easy_init)/) {
  129. # Initialization handled by boilerplate
  130. next;
  131. }
  132. else {
  133. push @decl, $_;
  134. }
  135. }
  136. elsif(! $seen_return) {
  137. if(/CURLOPT_URL/) {
  138. # URL is passed in as argument or by global
  139. my $var = shift @urlvars;
  140. s/\"[^\"]*\"/$var/;
  141. }
  142. s/\bhnd\b/curl/;
  143. # Convert to macro wrapper
  144. s/curl_easy_setopt/test_setopt/;
  145. if(/curl_easy_perform/) {
  146. s/\bret\b/res/;
  147. push @code, $_;
  148. push @code, "test_cleanup:\n";
  149. }
  150. else {
  151. push @code, $_;
  152. }
  153. }
  154. }
  155. print ("/* $comment */\n",
  156. $head,
  157. @decl,
  158. $init,
  159. @code,
  160. $exit);
  161. }
  162. # Read the original test data file and transform it
  163. # - add a "DO NOT EDIT comment"
  164. # - replace CURLOPT_URL string with URL variable
  165. # - remove <verify><file> section (was the --libcurl output)
  166. # - insert a <client><tool> section with our new C program name
  167. # - replace <client><command> section with the URL
  168. sub generate_test {
  169. my($comment,$newnumber) = @_;
  170. my @libcurl = getpart("verify", "file")
  171. or die "$myname: no <verify><file> section found\n";
  172. # Scan the --libcurl code to find the URL used.
  173. my $url;
  174. foreach (@libcurl) {
  175. if(my($u) = /CURLOPT_URL, \"([^\"]*)\"/) {
  176. $url = $u;
  177. }
  178. }
  179. die "$myname: CURLOPT_URL not found\n"
  180. unless defined $url;
  181. # Traverse the pseudo-XML transforming as required
  182. my @new;
  183. my(@path,$path,$skip);
  184. foreach (getall()) {
  185. if(my($end) = /\s*<(\/?)testcase>/) {
  186. push @new, $_;
  187. push @new, "# $comment\n"
  188. unless $end;
  189. }
  190. elsif(my($tag) = /^\s*<(\w+)/) {
  191. push @path, $tag;
  192. $path = join '/', @path;
  193. if($path eq 'verify/file') {
  194. $skip = 1;
  195. }
  196. push @new, $_
  197. unless $skip;
  198. if($path eq 'client') {
  199. push @new, ("<tool>\n",
  200. "lib$newnumber\n",
  201. "</tool>\n");
  202. }
  203. elsif($path eq 'client/command') {
  204. push @new, sh_quote($url)."\n";
  205. }
  206. }
  207. elsif(my($etag) = /^\s*<\/(\w+)/) {
  208. my $tag = pop @path;
  209. die "$myname: mismatched </$etag>\n"
  210. unless $tag eq $etag;
  211. push @new, $_
  212. unless $skip;
  213. $skip --
  214. if $path eq 'verify/file';
  215. $path = join '/', @path;
  216. }
  217. else {
  218. if($path eq 'client/command') {
  219. # Replaced above
  220. }
  221. else {
  222. push @new, $_
  223. unless $skip;
  224. }
  225. }
  226. }
  227. print @new;
  228. }
  229. sub leaf {
  230. # Works for POSIX filenames
  231. (my $path = shift) =~ s!.*/!!;
  232. return $path;
  233. }
  234. sub sh_quote {
  235. my $word = shift;
  236. $word =~ s/[\$\"\'\\]/\\$&/g;
  237. return '"' . $word . '"';
  238. }
  239. main;