2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
19 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 # KIND, either express or implied.
22 #***************************************************************************
24 #=======================================================================
25 # Read a test definition which exercises curl's --libcurl option.
26 # Generate either compilable source code for a new test tool,
27 # or a new test definition which runs the tool and expects the
29 # This should verify that the --libcurl code really does perform
30 # the same actions as the original curl invocation.
31 #-----------------------------------------------------------------------
32 # The output of curl's --libcurl option differs in several ways from
33 # the code needed to integrate with the test tool environment:
35 # - no call of curl_global_init & curl_global_cleanup
36 # - main() function vs. test() function
37 # - no checking of curl_easy_setopt calls vs. test_setopt wrapper
38 # - handling of stdout
39 # - variable names ret & hnd vs. res & curl
40 # - URL as literal string vs. passed as argument
41 #=======================================================================
45 # Boilerplate code for test tool
55 # Other declarations from --libcurl come here
59 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
60 fprintf(stderr, "curl_global_init() failed\n");
61 return TEST_ERR_MAJOR_BAD;
64 if ((curl = curl_easy_init()) == NULL) {
65 fprintf(stderr, "curl_easy_init() failed\n");
66 curl_global_cleanup();
67 return TEST_ERR_MAJOR_BAD;
70 # Option setting, perform and cleanup come here
72 ' curl_global_cleanup();
78 my $myname = leaf($0);
79 sub usage {die "Usage: $myname -c|-test=num testfile\n";}
84 my($opt,$testfile) = @ARGV;
86 if(loadtest($testfile)) {
87 die "$myname: $testfile doesn't look like a test case\n";
90 my $comment = sprintf("DO NOT EDIT - generated from %s by %s",
91 leaf($testfile), $myname);
95 elsif(my($num) = $opt =~ /^-test=(\d+)$/) {
96 generate_test($comment, $num);
105 # Fetch the generated code, which is the output file checked by
107 my @libcurl = getpart("verify", "file")
108 or die "$myname: no <verify><file> section found\n";
110 # Mangle the code into a suitable form for a test tool.
111 # We want to extract the important parts (declarations,
112 # URL, setopt calls, cleanup code) from the --libcurl
113 # boilerplate and insert them into a new boilerplate.
115 # First URL passed in as argument, others as global
116 my @urlvars = ('URL', 'libtest_arg2', 'libtest_arg3');
117 my($seen_main,$seen_setopt,$seen_return);
119 # Check state changes first (even though it
120 # duplicates some matches) so that the other tests
121 # are in a logical order).
125 if($seen_main and /curl_easy_setopt/) {
126 # Don't match 'curl_easy_setopt' in comment!
133 # Now filter the code according to purpose
137 elsif(! $seen_setopt) {
138 if(/^\s*(int main|\{|CURLcode |CURL |hnd = curl_easy_init)/) {
139 # Initialisations handled by boilerplate
146 elsif(! $seen_return) {
148 # URL is passed in as argument or by global
149 my $var = shift @urlvars;
153 # Convert to macro wrapper
154 s/curl_easy_setopt/test_setopt/;
155 if(/curl_easy_perform/) {
158 push @code, "test_cleanup:\n";
166 print ("/* $comment */\n",
174 # Read the original test data file and transform it
175 # - add a "DO NOT EDIT comment"
176 # - replace CURLOPT_URL string with URL variable
177 # - remove <verify><file> section (was the --libcurl output)
178 # - insert a <client><tool> section with our new C program name
179 # - replace <client><command> section with the URL
181 my($comment,$newnumber) = @_;
182 my @libcurl = getpart("verify", "file")
183 or die "$myname: no <verify><file> section found\n";
184 # Scan the --libcurl code to find the URL used.
187 if(my($u) = /CURLOPT_URL, \"([^\"]*)\"/) {
191 die "$myname: CURLOPT_URL not found\n"
194 # Traverse the pseudo-XML transforming as required
196 my(@path,$path,$skip);
198 if(my($end) = /\s*<(\/?)testcase>/) {
200 push @new, "# $comment\n"
203 elsif(my($tag) = /^\s*<(\w+)/) {
205 $path = join '/', @path;
206 if($path eq 'verify/file') {
211 if($path eq 'client') {
212 push @new, ("<tool>\n",
216 elsif($path eq 'client/command') {
217 push @new, sh_quote($url)."\n";
220 elsif(my($etag) = /^\s*<\/(\w+)/) {
222 die "$myname: mismatched </$etag>\n"
223 unless $tag eq $etag;
227 if $path eq 'verify/file';
228 $path = join '/', @path;
231 if($path eq 'client/command') {
244 # Works for POSIX filenames
245 (my $path = shift) =~ s!.*/!!;
251 $word =~ s/[\$\"\'\\]/\\$&/g;
252 return '"' . $word . '"';