testutil.pm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  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.
  13. #
  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.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # SPDX-License-Identifier: curl
  22. #
  23. ###########################################################################
  24. # This module contains miscellaneous functions needed in several parts of
  25. # the test suite.
  26. package testutil;
  27. use strict;
  28. use warnings;
  29. BEGIN {
  30. use base qw(Exporter);
  31. our @EXPORT = qw(
  32. runclient
  33. runclientoutput
  34. setlogfunc
  35. shell_quote
  36. subbase64
  37. subnewlines
  38. subsha256base64file
  39. substrippemfile
  40. );
  41. our @EXPORT_OK = qw(
  42. clearlogs
  43. logmsg
  44. );
  45. }
  46. use Digest::SHA qw(sha256);
  47. use MIME::Base64;
  48. use globalconfig qw(
  49. $torture
  50. $verbose
  51. );
  52. my $logfunc; # optional reference to function for logging
  53. my @logmessages; # array holding logged messages
  54. #######################################################################
  55. # Log an informational message
  56. # If a log callback function was set in setlogfunc, it is called. If not,
  57. # then the log message is buffered until retrieved by clearlogs.
  58. #
  59. # logmsg must only be called by one of the runner_* entry points and functions
  60. # called by them, or else logs risk being lost, since those are the only
  61. # functions that know about and will return buffered logs.
  62. sub logmsg {
  63. if(!scalar(@_)) {
  64. return;
  65. }
  66. if(defined $logfunc) {
  67. &$logfunc(@_);
  68. return;
  69. }
  70. push @logmessages, @_;
  71. }
  72. #######################################################################
  73. # Set the function to use for logging
  74. sub setlogfunc {
  75. ($logfunc)=@_;
  76. }
  77. #######################################################################
  78. # Clear the buffered log messages after returning them
  79. sub clearlogs {
  80. my $loglines = join('', @logmessages);
  81. undef @logmessages;
  82. return $loglines;
  83. }
  84. #######################################################################
  85. sub includefile {
  86. my ($f) = @_;
  87. open(F, "<$f");
  88. my @a = <F>;
  89. close(F);
  90. return join("", @a);
  91. }
  92. sub subbase64 {
  93. my ($thing) = @_;
  94. # cut out the base64 piece
  95. while($$thing =~ s/%b64\[(.*?)\]b64%/%%B64%%/i) {
  96. my $d = $1;
  97. # encode %NN characters
  98. $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  99. my $enc = encode_base64($d, "");
  100. # put the result into there
  101. $$thing =~ s/%%B64%%/$enc/;
  102. }
  103. # hex decode
  104. while($$thing =~ s/%hex\[(.*?)\]hex%/%%HEX%%/i) {
  105. # decode %NN characters
  106. my $d = $1;
  107. $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  108. $$thing =~ s/%%HEX%%/$d/;
  109. }
  110. # repeat
  111. while($$thing =~ s/%repeat\[(\d+) x (.*?)\]%/%%REPEAT%%/i) {
  112. # decode %NN characters
  113. my ($d, $n) = ($2, $1);
  114. $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  115. $n =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  116. my $all = $d x $n;
  117. $$thing =~ s/%%REPEAT%%/$all/;
  118. }
  119. # include a file
  120. $$thing =~ s/%include ([^%]*)%[\n\r]+/includefile($1)/ge;
  121. }
  122. my $prevupdate; # module scope so it remembers the last value
  123. sub subnewlines {
  124. my ($force, $thing) = @_;
  125. if($force) {
  126. # enforce CRLF newline
  127. $$thing =~ s/\x0d*\x0a/\x0d\x0a/;
  128. return;
  129. }
  130. # When curl is built with Hyper, it gets all response headers delivered as
  131. # name/value pairs and curl "invents" the newlines when it saves the
  132. # headers. Therefore, curl will always save headers with CRLF newlines
  133. # when built to use Hyper. By making sure we deliver all tests using CRLF
  134. # as well, all test comparisons will survive without knowing about this
  135. # little quirk.
  136. if(($$thing =~ /^HTTP\/(1.1|1.0|2|3) [1-5][^\x0d]*\z/) ||
  137. ($$thing =~ /^(GET|POST|PUT|DELETE) \S+ HTTP\/\d+(\.\d+)?/) ||
  138. (($$thing =~ /^[a-z0-9_-]+: [^\x0d]*\z/i) &&
  139. # skip curl error messages
  140. ($$thing !~ /^curl: \(\d+\) /))) {
  141. # enforce CRLF newline
  142. $$thing =~ s/\x0d*\x0a/\x0d\x0a/;
  143. $prevupdate = 1;
  144. }
  145. else {
  146. if(($$thing =~ /^\n\z/) && $prevupdate) {
  147. # if there's a blank link after a line we update, we hope it is
  148. # the empty line following headers
  149. $$thing =~ s/\x0a/\x0d\x0a/;
  150. }
  151. $prevupdate = 0;
  152. }
  153. }
  154. #######################################################################
  155. # Run the application under test and return its return code
  156. #
  157. sub runclient {
  158. my ($cmd)=@_;
  159. my $ret = system($cmd);
  160. print "CMD ($ret): $cmd\n" if($verbose && !$torture);
  161. return $ret;
  162. # This is one way to test curl on a remote machine
  163. # my $out = system("ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'");
  164. # sleep 2; # time to allow the NFS server to be updated
  165. # return $out;
  166. }
  167. #######################################################################
  168. # Run the application under test and return its stdout
  169. #
  170. sub runclientoutput {
  171. my ($cmd)=@_;
  172. return `$cmd 2>/dev/null`;
  173. # This is one way to test curl on a remote machine
  174. # my @out = `ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'`;
  175. # sleep 2; # time to allow the NFS server to be updated
  176. # return @out;
  177. }
  178. #######################################################################
  179. # Quote an argument for passing safely to a Bourne shell
  180. # This does the same thing as String::ShellQuote but doesn't need a package.
  181. #
  182. sub shell_quote {
  183. my ($s)=@_;
  184. if($s !~ m/^[-+=.,_\/:a-zA-Z0-9]+$/) {
  185. # string contains a "dangerous" character--quote it
  186. $s =~ s/'/'"'"'/g;
  187. $s = "'" . $s . "'";
  188. }
  189. return $s;
  190. }
  191. sub get_sha256_base64 {
  192. my ($file_path) = @_;
  193. return encode_base64(sha256(do { local $/; open my $fh, '<:raw', $file_path or die $!; <$fh> }), "");
  194. }
  195. sub subsha256base64file {
  196. my ($thing) = @_;
  197. # SHA-256 base64
  198. while ($$thing =~ s/%sha256b64file\[(.*?)\]sha256b64file%/%%SHA256B64FILE%%/i) {
  199. my $file_path = $1;
  200. $file_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  201. my $hash_b64 = get_sha256_base64($file_path);
  202. $$thing =~ s/%%SHA256B64FILE%%/$hash_b64/;
  203. }
  204. }
  205. sub get_file_content {
  206. my ($file_path) = @_;
  207. my $content = do { local $/; open my $fh, '<', $file_path or die $!; <$fh> };
  208. $content =~ s/(^|-----END .*?-----[\r\n]?)(.*?)(-----BEGIN .*?-----|$)/$1$3/gs;
  209. $content =~ s/\r\n/\n/g;
  210. chomp($content);
  211. return $content;
  212. }
  213. sub substrippemfile {
  214. my ($thing) = @_;
  215. # File content substitution
  216. while ($$thing =~ s/%strippemfile\[(.*?)\]strippemfile%/%%FILE%%/i) {
  217. my $file_path = $1;
  218. $file_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  219. my $file_content = get_file_content($file_path);
  220. $$thing =~ s/%%FILE%%/$file_content/;
  221. }
  222. }
  223. 1;