1 #***************************************************************************
3 # Project ___| | | | _ \| |
5 # | (__| |_| | _ <| |___
6 # \___|\___/|_| \_\_____|
8 # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 # KIND, either express or implied.
21 # SPDX-License-Identifier: curl
23 ###########################################################################
31 use base qw(Exporter);
49 my @xml; # test data file contents
50 my $xmlfile; # test data file name
55 # Normalize the part function arguments for proper caching. This includes the
56 # file name in the arguments since that is an implied parameter that affects the
57 # return value. Any error messages will only be displayed the first time, but
58 # those are disabled by default anyway, so should never been seen outside
62 return join("\t", @_);
67 # remove everything not hex
68 $s =~ s/[^A-Fa-f0-9]//g;
70 $s =~ s/([a-fA-F0-9][a-fA-F0-9])/chr(hex($1))/eg;
77 if(($_ =~ /^ *\<testcase ([^>]*)/)) {
79 while($attr =~ s/ *([^=]*)= *(\"([^\"]*)\"|([^\> ]*))//) {
80 my ($var, $cont)=($1, $2);
81 $cont =~ s/^\"(.*)\"$/$1/;
90 # if $part is undefined (ie only one argument) then
91 # return the attributes of the section
93 my ($section, $part)=@_;
98 # print "Section: $section, part: $part\n";
101 # print "$inside: $_";
102 if(!$inside && ($_ =~ /^ *\<$section/)) {
105 if((1 ==$inside) && ( ($_ =~ /^ *\<$part ([^>]*)/) ||
111 while($attr =~ s/ *([^=]*)= *(\"([^\"]*)\"|([^\> ]*))//) {
112 my ($var, $cont)=($1, $2);
113 $cont =~ s/^\"(.*)\"$/$1/;
118 # detect end of section when part wasn't found
119 elsif((1 ==$inside) && ($_ =~ /^ *\<\/$section\>/)) {
122 elsif((2 ==$inside) && ($_ =~ /^ *\<\/$part/)) {
128 memoize('getpartattr', NORMALIZER => 'normalize_part'); # cache each result
131 my ($section, $part)=@_;
141 if(!$inside && ($_ =~ /^ *\<$section/)) {
144 elsif(($inside >= 1) && ($_ =~ /^ *\<$part[ \>]/)) {
148 elsif($_ =~ /$part [^>]*base64=/) {
149 # attempt to detect our base64 encoded part
152 elsif($_ =~ /$part [^>]*hex=/) {
153 # attempt to detect a hex-encoded part
158 elsif(($inside >= 2) && ($_ =~ /^ *\<\/$part[ \>]/)) {
164 elsif(($inside >= 1) && ($_ =~ /^ *\<\/$section/)) {
166 print STDERR "$xmlfile:$line:1: error: missing </$part> tag before </$section>\n";
167 @this = ("format error in $xmlfile");
169 if($trace && @this) {
170 print STDERR "*** getpart.pm: $section/$part returned data!\n";
172 if($warning && !@this) {
173 print STDERR "*** getpart.pm: $section/$part returned empty!\n";
176 # decode the whole array before returning it!
178 my $decoded = decode_base64($_);
183 # decode the whole array before returning it!
185 my $decoded = decode_hex($_);
191 elsif($inside >= 2) {
195 if($trace && @this) {
196 # section/part has data but end of section not detected,
197 # end of file implies end of section.
198 print STDERR "*** getpart.pm: $section/$part returned data!\n";
200 if($warning && !@this) {
201 # section/part does not exist or has no data without an end of
202 # section; end of file implies end of section.
203 print STDERR "*** getpart.pm: $section/$part returned empty!\n";
207 memoize('getpart', NORMALIZER => 'normalize_part'); # cache each result
210 my ($section, $part)=@_;
215 if(!$inside && ($_ =~ /^ *\<$section/)) {
218 elsif((1 == $inside) && ($_ =~ /^ *\<$part[ \>]/)) {
221 elsif((1 == $inside) && ($_ =~ /^ *\<\/$section/)) {
222 return 0; # does not exist
225 return 0; # does not exist
227 # The code currently never calls this more than once per part per file, so
228 # caching a result that will never be used again just slows things down.
229 # memoize('partexists', NORMALIZER => 'normalize_part'); # cache each result
234 if(defined $xmlfile && $file eq $xmlfile) {
235 # This test is already loaded
242 if(open(my $xmlh, "<", "$file")) {
243 binmode $xmlh; # for crapage systems, use binary
252 print STDERR "file $file wouldn't open!\n";
261 # Return entire document as list of lines
266 # write the test to the given file
270 if(open(my $xmlh, ">", "$file")) {
271 binmode $xmlh; # for crapage systems, use binary
280 print STDERR "file $file wouldn't open!\n";
288 # Strip off all lines that match the specified pattern and return
293 my ($pattern, $arrayref) = @_;
298 if($_ !~ /$pattern/) {
306 # pass array *REFERENCES* !
309 my ($firstref, $secondref)=@_;
311 my $first = join("", @$firstref);
312 my $second = join("", @$secondref);
314 # we cannot compare arrays index per index since with the base64 chunks,
315 # they may not be "evenly" distributed
317 # NOTE: this no longer strips off carriage returns from the arrays. Is that
318 # really necessary? It ruins the testing of newlines. I believe it was once
319 # added to enable tests on win32.
321 if($first ne $second) {
329 # Write a given array to the specified file
332 my ($filename, $arrayref)=@_;
334 open(my $temp, ">", "$filename") || die "Failure writing file";
335 binmode($temp,":raw"); # cygwin fix by Kevin Roth
339 close($temp) || die "Failure writing file";
343 # Load a specified file and return it as an array
349 if (open(my $temp, "<", "$filename")) {